|
¤³¤ì¤Çº¹¤ÎÉÿô = 7776000(3¥ö·î¤ÎÉÿô) °Ê¾å¤Ê¤é¤Ð¾Ã¤»¤Ð¤É¤¦¤Ç¤·¤ç¤¦¤«¡£
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int mdays[][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
int isleap(int year)
{
return (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
}
int dayofyear(int y, int m, int d)
{
int i;
int days; /* Æü¿ô */
days = d;
for (i = 1; i < m; i++)
days += mdays[isleap(y)][i - 1];
return (days);
}
const char *cut_ymdhms(const char *fn_pattern)
{
const char *ret = NULL;
if (fn_pattern != NULL)
ret = strrchr(fn_pattern, '-');
if (ret != NULL) ret++;
return ret;
}
char *cut_char(const char *str, int begin, int len)
{
static char loc[128];
strcpy(loc, str + begin);
loc[len] = '\0';
return loc;
}
struct tm maketm(const char *chartime)
{
struct tm tmx = {0};
int y, m, d;
tmx.tm_sec = atoi(cut_char(chartime, 10, 2));
tmx.tm_min = atoi(cut_char(chartime, 8, 2));
tmx.tm_hour = atoi(cut_char(chartime, 6, 2));
tmx.tm_mday = atoi(cut_char(chartime, 4, 2)) - 1;
tmx.tm_mon = atoi(cut_char(chartime, 2, 2));
tmx.tm_year = atoi(cut_char(chartime, 0, 2)) + 100;
y = tmx.tm_year; m = tmx.tm_mon; d = tmx.tm_mday;
if (m < 3) {
y--; m += 12;
}
tmx.tm_wday = (y + (int)(y / 4) - (int)(y / 100) + (int)(y / 400) + (int)(2.6 * m + 1.6) + d) % 7;
tmx.tm_yday = dayofyear(tmx.tm_year + 2000, tmx.tm_mon, tmx.tm_mday + 1);
return tmx;
}
int main(void)
{
const char fn_pattern1[] = "0010-030820220000";
const char fn_pattern2[] = "01-0020-030823123456";
const char *p = NULL;
time_t t1, t2;
struct tm tm1, tm2;
double diff;
p = cut_ymdhms(fn_pattern1);
if (p != NULL) puts(p);
tm1 = maketm(p);
t1 = mktime(&tm1);
p = cut_ymdhms(fn_pattern2);
if (p != NULL) puts(p);
tm2 = maketm(p);
t2 = mktime(&tm2);
diff = difftime(t2, t1);
printf("º¹¤ÎÉÿô = %f\n", diff);
return 0;
}
|