cod*_*box 5 c datetime parsing date utc
我有一个包含本地日期/时间的字符串,我需要将其转换为time_t值(UTC) - 我一直在尝试这个:
char* date = "2009/09/01/00";
struct tm cal = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL};
strptime(date, "%Y/%m/%d/%H", &cal);
time_t t = mktime(&cal);
Run Code Online (Sandbox Code Playgroud)
但是我得到的time_t值是如果字符串被解析为UTC而不是本地时间的话我期望的值.也许我误解了strptime应该做什么,但是在我9月1日的时区(英国),我们正在使用BST(即UTC + 1小时)所以我希望我最终得到的值比UTC提前1小时.
有没有办法将字符串解释为localtime,自动考虑在该日期生效的UTC偏移量?请注意,我需要time_t值而不是struct tm,在上面的示例中我希望time_t值对应于2009-09-01 01:00:00 GMT
您可以使用它mktime
来解释本地时区中的struct tm.执行此操作时,请小心设置tm_isdst
标记.它是夏季的0,冬季的1,以及-1来计算mktime
它.这是一些示例代码:
void main()
{
char* date = "2009/09/01/00";
struct tm cal = {};
// Read string into struct tm
strptime(date, "%Y/%m/%d/%H", &cal);
// Tell mktime to figure out the daylight saving time
cal.tm_isdst = -1;
printf("%20s: %s", "Before mktime", asctime(&cal));
// Convert struct tm to time_t
time_t t = mktime(&cal);
// Convert time_t to localtime
struct tm localcal = *localtime(&t);
printf("%20s: %s", "Local time", asctime(&localcal));
printf("%20s: %i\n", "Local DST", localcal.tm_isdst);
// Convert time_t to GMT
struct tm gmcal = *gmtime(&t);
printf("%20s: %s", "GM time", asctime(&gmcal));
printf("%20s: %i\n", "GM DST", gmcal.tm_isdst);
}
Run Code Online (Sandbox Code Playgroud)
这打印(我住在GMT + 1,现在是冬天):
Before mktime: Tue Sep 1 00:00:00 2009
Local time: Tue Sep 1 00:00:00 2009
Local DST: 1
GM time: Mon Aug 31 22:00:00 2009
GM DST: 0
Run Code Online (Sandbox Code Playgroud)
它看起来像mktime
根据当前的夏令时转换9月的日期.现在是十一月,所以它实际上是一小时.我还没有办法纠正这个问题.
我想我现在已经破解了它,感谢 Andomar - 这段代码满足了我的需要,并且无论当前的 DST 状态如何,它似乎都可以工作(我更改了 PC 上的时钟来检查这一点):
#include <time.h>
#include <assert.h>
time_t parseLocalDate(char* date){
struct tm cal = {0, 0, 0, 0, 0, 0, 0, 0, -1, 0, NULL};
strptime(date, "%Y/%m/%d/%H", &cal);
return mktime(&cal);
}
int main(int argc, char *argv[]){
// DST is effect, Local Time = GMT+1
assert(1251759600 == parseLocalDate("2009/09/01/00")); // Mon, 31 Aug 2009 23:00:00 GMT
assert(1254351600 == parseLocalDate("2009/10/01/00")); // Wed, 30 Sep 2009 23:00:00 GMT
// DST not in effect, Local Time = GMT
assert(1257033600 == parseLocalDate("2009/11/01/00")); // Sun, 01 Nov 2009 00:00:00 GMT
}
Run Code Online (Sandbox Code Playgroud)