简体   繁体   English

将UTC时间转换为C中的Unix时间戳

[英]Convert UTC time to unix time stamp in C

I need to know the current UTC time and convert it to Unix time stamp. 我需要知道当前的UTC时间并将其转换为Unix时间戳。 I tried using combination of time() , mktime() , gmtime() but not getting correct UTC time Following is the snippet I am using: 我尝试使用time()mktime()gmtime() ,但未获得正确的UTC时间以下是我正在使用的代码段:

time_t now;
struct tm  ts;
char buf[80];
now = time(NULL);
ts = *gmtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);

This gives me following output as --> Time :: Sat 2015-01-10 00:14:34 CST which is adding 5 Hr 30 minutes into local time instead of subtracting 5 Hr 30 min. 这给了我以下输出-> Time :: Sat 2015-01-10 00:14:34 CST,它会将本地时间增加5小时30分钟,而不是减去5小时30分钟。 Can anyone tell me why this is happening ? 谁能告诉我为什么会这样?

[edit] Re-thought the issue. [编辑]重新考虑了这个问题。

OP wants a Unix time stamp . OP需要Unix时间戳 Such a time stamp is the seconds since Jan 1, 1970. This is likely nothing more than the below. 这样的时间戳是自1970年1月1日以来的秒数。这很可能只不过是下面的时间。

sprintf(buffer, "%lld", (long long) time(NULL));

OTOH, OP also seems to want year-month-day hour:minute:second. OTOH,OP也似乎想要年月日时:分:秒。 This is not a UNIX time stamp. 这不是UNIX时间戳。 It is simply a time stamp which the rest of this answer discusses a portable way to derive. 这只是一个时间戳,此答案的其余部分讨论了一种简便的导出方法。


time() returns some real type capable of representing time. time()返回一些能够表示时间的实型。 It is very often some integer type representing the numbers of seconds since Jan 1, 1970 0:00:00 UTC (or GMT ). 它通常是某种整数类型,表示自1970年1月1日UTC (或GMT )以来的秒数。

 now = time(NULL);

gmtime() coverts time_t into a year, month, day, etc. structure for the UTC (GMT) timezone. gmtime() time_t转换为UTC(GMT)时区的年,月,日等结构。

ts = *gmtime(&now);

Now comes a coding problem: 现在出现一个编码问题:

strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);

strftime() may ( @Jasen ) not known the timezone from &ts . strftime()可能( @Jasen )不知道&ts的时区。 Instead, it gets the locale's timezone. 而是获取语言环境的时区。 That result is adjusted per the is_daylight_time field ts.tm_isdst . 该结果根据is_daylight_time字段ts.tm_isdst进行调整。 So the following does not make sense. 因此,以下内容没有任何意义。

ts = *gmtime(&now);  // UTC/GMT time
strftime(buf, sizeof(buf), "... %Z ...", &ts);  // print local standard time name

Instead use 改为使用

ts = *gmtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S UTC", &ts);

Consider ISO 8601 formats 考虑ISO 8601格式

strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", &ts);
//  2015-01-06T03:51:57Z

The return value of time() already is in UTC: time()的返回值已经在UTC中:

time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). time()返回的时间是自1970年1月1日00:00:00 +0000(UTC)以来的秒数。

It's not clear to me why you're doing further processing. 我不清楚您为什么要进行进一步处理。

Fix Your Clock 修理你的时钟

Your time zone is IST "Asia/Calcutta" (I couldn't find anything else with a 5:30 offset from GMT) 您所在的时区是IST“亚洲/ Calcutta”(与格林尼治标准时间5:30偏移的其他时间我找不到)

But it's printing as GMT as CST ths suggests that your computers clock is badly misconfigured. 但这就像CST一样显示为格林尼治标准时间(GMT),表明您的计算机时钟配置错误。

The code you posted works fine here. 您发布的代码在这里可以正常工作。 I cannot provoke it to print CST instead of GMT. 我不能挑衅它打印CST而不是GMT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM