I need to know the current UTC time and convert it to Unix time stamp. I tried using combination of time()
, mktime()
, gmtime()
but not getting correct UTC time Following is the snippet I am using:
我需要知道当前UTC时间,并将其转换为Unix时间戳。我尝试使用时间的组合()、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. Can anyone tell me why this is happening ?
这给了我如下的输出:>时间:Sat 2015-01-10 00:14:34 CST,在当地时间里增加了5小时30分钟,而不是减去5小时30分钟。谁能告诉我为什么会发生这种情况?
3 个解决方案
#1
2
[edit] Re-thought the issue.
[编辑]反思这个问题。
OP wants a Unix time stamp. Such a time stamp is the seconds since Jan 1, 1970. This is likely nothing more than the below.
OP需要Unix时间戳。这样的时间戳是自1970年1月1日以来的几秒钟。这很可能只是下面的情况。
sprintf(buffer, "%lld", (long long) time(NULL));
OTOH, OP also seems to want year-month-day hour:minute:second. This is not a UNIX time stamp. It is simply a time stamp which the rest of this answer discusses a portable way to derive.
OTOH, OP也似乎想要每年一个月的时间:分钟:第二。这不是一个UNIX时间戳。这只是一个时间戳,剩下的答案讨论了一种可移植的派生方式。
time()
returns some real type capable of representing time. It is very often some integer type representing the numbers of seconds since Jan 1, 1970 0:00:00 UTC (or GMT).
time()返回一些能够表示时间的真正类型。它通常是一些整数类型,表示自1970年1月1日以来的秒数(或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
. Instead, it gets the locale's timezone. That result is adjusted per the is_daylight_time field ts.tm_isdst
. So the following does not make sense.
strftime()可以(@Jasen)不知道来自&ts的时区。相反,它会获取语言环境的时区。这个结果是根据is_daylight_time字段。tm_isdst进行调整的。所以下面的内容是没有意义的。
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
#2
1
The return value of time()
already is in UTC:
时间()的返回值已经在UTC中:
time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
时间()返回的时间为自纪元以来的秒数,1970-01-01 00:00 +0000 (UTC)。
It's not clear to me why you're doing further processing.
我不清楚你为什么要做进一步的处理。
#3
0
Fix Your Clock
Your time zone is IST "Asia/Calcutta" (I couldn't find anything else with a 5:30 offset from GMT)
您的时区是“Asia/Calcutta”(我在格林尼治标准时间下午5:30的时候找不到其他东西)
But it's printing as GMT as CST ths suggests that your computers clock is badly misconfigured.
但它的印刷时间是格林尼治标准时间,这表明你的电脑时钟被严重错误配置了。
The code you posted works fine here. I cannot provoke it to print CST instead of GMT.
你发布的代码在这里很好。我不能让它印刷CST而不是GMT。
#1
2
[edit] Re-thought the issue.
[编辑]反思这个问题。
OP wants a Unix time stamp. Such a time stamp is the seconds since Jan 1, 1970. This is likely nothing more than the below.
OP需要Unix时间戳。这样的时间戳是自1970年1月1日以来的几秒钟。这很可能只是下面的情况。
sprintf(buffer, "%lld", (long long) time(NULL));
OTOH, OP also seems to want year-month-day hour:minute:second. This is not a UNIX time stamp. It is simply a time stamp which the rest of this answer discusses a portable way to derive.
OTOH, OP也似乎想要每年一个月的时间:分钟:第二。这不是一个UNIX时间戳。这只是一个时间戳,剩下的答案讨论了一种可移植的派生方式。
time()
returns some real type capable of representing time. It is very often some integer type representing the numbers of seconds since Jan 1, 1970 0:00:00 UTC (or GMT).
time()返回一些能够表示时间的真正类型。它通常是一些整数类型,表示自1970年1月1日以来的秒数(或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
. Instead, it gets the locale's timezone. That result is adjusted per the is_daylight_time field ts.tm_isdst
. So the following does not make sense.
strftime()可以(@Jasen)不知道来自&ts的时区。相反,它会获取语言环境的时区。这个结果是根据is_daylight_time字段。tm_isdst进行调整的。所以下面的内容是没有意义的。
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
#2
1
The return value of time()
already is in UTC:
时间()的返回值已经在UTC中:
time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
时间()返回的时间为自纪元以来的秒数,1970-01-01 00:00 +0000 (UTC)。
It's not clear to me why you're doing further processing.
我不清楚你为什么要做进一步的处理。
#3
0
Fix Your Clock
Your time zone is IST "Asia/Calcutta" (I couldn't find anything else with a 5:30 offset from GMT)
您的时区是“Asia/Calcutta”(我在格林尼治标准时间下午5:30的时候找不到其他东西)
But it's printing as GMT as CST ths suggests that your computers clock is badly misconfigured.
但它的印刷时间是格林尼治标准时间,这表明你的电脑时钟被严重错误配置了。
The code you posted works fine here. I cannot provoke it to print CST instead of GMT.
你发布的代码在这里很好。我不能让它印刷CST而不是GMT。