sscanf()
orを使用strptime()
して、ほとんどのフィールドを抽出します。タイムゾーン文字と DST 文字は、独自にデコードする必要があります。2 桁の年しか使用していないため、範囲を定義する必要があります。以下の例では、1970 年から 2069 年を使用しています。抽出されたタイムゾーン文字を使用して、通常のタイムゾーン名を形成します。を呼び出す前mktime()
に、TZ をタイムゾーン名に設定します。次に、time_t
手に持って、現地時間に変換します。
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
extern time_t mktime_TZ(struct tm *tm, const char *tz);
extern time_t DecodeTimeString_time_t(const char *time_string);
void DecodeTimeString_Local(const char *time_string, struct tm *local) {
// Various error handling not shown
time_t t;
t = DecodeTimeString_time_t(time_string);
*local = *localtime(&t);
}
time_t DecodeTimeString_time_t(const char *time_string /* YYMMDDHHMMCY */) {
struct tm tm;
char Zone, DST;
int result = sscanf(time_string, "%2d%2d%2d%2d%2d%[CEMP]%[NY]",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &Zone, &DST);
if (result != 7) {
; // handle error
}
// Your need to decide how to handle 2 digits years
// Assume 70-99 is 1970-1999 and 0 to 69 is 2000-2069
if (tm.tm_year < 70) tm.tm_year += 2000-1900;
tm.tm_mon--; // Assume DateString used "01" for January, etc.
tm.tm_sec = 0;
tm.tm_isdst = Zone == 'Y';
const char *TZ;
switch (Zone) {
case 'P': TZ = "PST8PDT"; break; // Pacific
case 'M': TZ = "MST7MDT"; break; // mountain
case 'C': TZ = "CST6CDT"; break; // central
case 'E': TZ = "EST5EDT"; break; // eastern
}
time_t t = mktime_TZ(&tm, TZ);
return t;
}
// Form time_t from struct tm given a TZ
time_t mktime_TZ(struct tm *tm, const char *tz) {
time_t t;
const char *old_tz = getenv("TZ");
if (setenv("TZ", tz, 1 /* overwrite */)) {
return -1; // handle error
}
tzset();
t = mktime(tm);
if (old_tz) {
if (setenv("TZ", old_tz, 1 /* overwrite */)) {
return -1; // handle error
}
}
else {
if (unsetenv("TZ")) {
return -1; // handle error
}
}
tzset();
return t;
}