mktime時間の変換
最近、いくつかのlogを分析する際に時間の処理が必要になり、logの時間フォーマットは「05/Jan/2015:16:12:15」です.2つの時間の差を計算する必要があるので、数字に変換して処理すると便利です.
そこでこの文字列を解析し、対応するフィールドを抽出し、mktimeでtime_に変換します.t.その結果ubuntuでテストは問題なくandroidで計算された時間は入力された時間より3600 s少ない.
struct tmの中のtmをisdstは0に設定されています.このフィールドは北京時間にとってプログラムの中で自発的に0に設定すべきで、さもなくばシステムはそれを1に設定して、偏差をもたらす可能性があります.
また、北京時間はUTC/GMT時間より8時間早い.http://www.worldtimeserver.com/current_time_in_CN.aspx
time(NULL)はUTC時間を得ますが、localtimeに変換してUTC時間に変換すると、28800 s、つまり8時間の差があります.
次の検証コードを参照してください.
そこでこの文字列を解析し、対応するフィールドを抽出し、mktimeでtime_に変換します.t.その結果ubuntuでテストは問題なくandroidで計算された時間は入力された時間より3600 s少ない.
struct tmの中のtmをisdstは0に設定されています.このフィールドは北京時間にとってプログラムの中で自発的に0に設定すべきで、さもなくばシステムはそれを1に設定して、偏差をもたらす可能性があります.
また、北京時間はUTC/GMT時間より8時間早い.http://www.worldtimeserver.com/current_time_in_CN.aspx
time(NULL)はUTC時間を得ますが、localtimeに変換してUTC時間に変換すると、28800 s、つまり8時間の差があります.
次の検証コードを参照してください.
#include
#include
#include
#include
#include
struct tm parseTime(const char* ts)
{
// Format like "05/Jan/2015:16:12:15 +0800"
printf("timeFromString:%s
", ts);
const char *Monthes[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jly", "Aug", "Sep", "Oct", "Nov", "Dec"};
const int DAY_IDX = 0;
const int MON_IDX = 1;
const int YEAR_IDX = 2;
const int HOUR_IDX = 3;
const int MIN_IDX = 4;
const int SEC_IDX = 5;
const int intervals[6] = {3, 4, 5, 3, 3, 3};
int vals[6];
char buf[5];
for (int i=0; i<6; ++i)
{
//snprintf(buf, intervals[i], "%s", ts);
strncpy(buf, ts, intervals[i]-1);
buf[intervals[i]-1] = '\0';
ts += intervals[i];
if (i == 1)
{
for (int j=1; j<=12; ++j)
{
if (!strncmp(buf, Monthes[j-1], 3))
{
vals[i] = j;
break;
}
}
}
else
{
vals[i] = atoi(buf);
}
//printf("buf:%s, %d
", buf, vals[i]);
}
struct tm y2k;
y2k.tm_year = vals[YEAR_IDX] - 1900;
y2k.tm_mon = vals[MON_IDX] - 1 ;
y2k.tm_mday = vals[DAY_IDX];
y2k.tm_hour = vals[HOUR_IDX];
y2k.tm_min = vals[MIN_IDX];
y2k.tm_sec = vals[SEC_IDX];;
y2k.tm_isdst = 0; // important for Chinese time! no dst!
return y2k;
}
time_t localTimeFromString(const char* ts)
{
struct tm y2k = parseTime(ts);
return mktime(&y2k);
}
time_t localTimeToUTC(const char* ts)
{
time_t result;
struct tm y2k = parseTime(ts);
#if 0
result = mktime(&y2k) - timezone;
printf("timezone:%ld
", timezone);
//printf("mktime:%ld
", result);
#else
char *oldTZ = getenv("TZ");
putenv("TZ=UTC");
tzset();
result = mktime(&y2k);
//printf("mktime:%ld
", result);
printf("timezone:%ld
", timezone);
if(oldTZ == NULL)
{
putenv("TZ=");
}
else
{
char buf[256];
sprintf(buf, "TZ=%s", oldTZ);
putenv(buf);
}
tzset();
#endif
return result;
}
int formatTime(time_t t, char *buf, int maxsize)
{
const char* format = "%d/%b/%Y:%H:%M:%S
";
struct tm *tmp;
tmp = localtime(&t);
if (tmp == NULL)
{
perror("localtime");
return -1;
}
if (strftime(buf, maxsize, format, tmp) == 0) {
fprintf(stderr, "strftime returned 0");
return -1;
}
return 0;
}
int main ()
{
time_t now = time(NULL);
char buf[256] = {0};
formatTime(now, buf, 256);
printf("formatedString:%s
", buf);
time_t nt = localTimeFromString(buf);
printf("oldtime:%ld
newtime:%ld
", now, nt);
const char *s = "01/Jan/1970:00:00:00";
time_t utcT = localTimeToUTC(s);
printf("timer: %ld, utcT:%ld
", time(NULL), utcT);
utcT = localTimeToUTC(buf);
printf("now: %ld
utcT:%ld
diff:%f
", now, utcT, difftime(utcT, now));
return 0;
}