如何将 UNIX 时间戳 (UTC) 转换为故障时间?
How to convert UNIX time stamps (UTC) to broken-down time?
我有故障时间,然后我将其转换为 UTC 的 UNIX 时间戳,并且没有 DST,使用 _mkgmtime 而不是 mktime(应用本地时区)。这很好用。我现在想要的是将生成的 UNIX 时间戳转换回完全相同的故障时间,没有 DST/TimeZone 更改。我尝试使用 strftime() 但它会转换为本地时区。 localtime() 也是如此。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
int main() {
struct tm info;
char buffer[80];
info.tm_year = 2022 - 1900;
info.tm_mon = 5 - 1;
info.tm_mday = 19;
info.tm_hour = 15;
info.tm_min = 3;
info.tm_sec = 0;
info.tm_isdst = 0;
uint32_t time_passed_utc = _mkgmtime(&info);
printf("time_passed_utc uint32: %lu\n", time_passed_utc); // this returns correctly "1652972580"
strftime(buffer, sizeof(buffer), "time_passed_utc-> %c", &info );
printf(buffer); // this returns correctly "05/19/22 15:03:00"
printf("\n\n");
time_t rawtime = 1652972580;
struct tm ts;
char buf[80];
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&rawtime);
ts.tm_isdst = 0; // no DST setting
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
printf("%s\n", buf); // returns incorrectly "Thu 2022-05-19 18:03:00 E. Europe Standard Time" -> should have been "2022-05-19 15:03:00"
return(0);
}
我总是用这样的东西来思考这些问题table。要从第一列中的内容转换为其他列中的内容,请调用指示的函数。
time_t
struct tm (UTC)
struct tm (local)
string
custom string
time_t
-
gmtime
localtime
ctime
n/a
struct tm (UTC)
timegm *
-
n/a
asctime
strftime
struct tm (local)
mktime
n/a
-
asctime
strftime
您想将 time_t
转换为 UTC 中的 struct tm
,因此您需要的函数是 gmtime
.
我没有包括用于从 字符串转换 的行,因为这样做的函数不是标准的。也许 best-known 是 strptime
,它将自定义字符串转换回 struct tm
,使其或多或少成为 strftime
.
的倒数
(公平地说,timegm
也不是完全标准的。)
我有故障时间,然后我将其转换为 UTC 的 UNIX 时间戳,并且没有 DST,使用 _mkgmtime 而不是 mktime(应用本地时区)。这很好用。我现在想要的是将生成的 UNIX 时间戳转换回完全相同的故障时间,没有 DST/TimeZone 更改。我尝试使用 strftime() 但它会转换为本地时区。 localtime() 也是如此。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
int main() {
struct tm info;
char buffer[80];
info.tm_year = 2022 - 1900;
info.tm_mon = 5 - 1;
info.tm_mday = 19;
info.tm_hour = 15;
info.tm_min = 3;
info.tm_sec = 0;
info.tm_isdst = 0;
uint32_t time_passed_utc = _mkgmtime(&info);
printf("time_passed_utc uint32: %lu\n", time_passed_utc); // this returns correctly "1652972580"
strftime(buffer, sizeof(buffer), "time_passed_utc-> %c", &info );
printf(buffer); // this returns correctly "05/19/22 15:03:00"
printf("\n\n");
time_t rawtime = 1652972580;
struct tm ts;
char buf[80];
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&rawtime);
ts.tm_isdst = 0; // no DST setting
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
printf("%s\n", buf); // returns incorrectly "Thu 2022-05-19 18:03:00 E. Europe Standard Time" -> should have been "2022-05-19 15:03:00"
return(0);
}
我总是用这样的东西来思考这些问题table。要从第一列中的内容转换为其他列中的内容,请调用指示的函数。
time_t | struct tm (UTC) | struct tm (local) | string | custom string | |
---|---|---|---|---|---|
time_t | - | gmtime | localtime | ctime | n/a |
struct tm (UTC) | timegm * | - | n/a | asctime | strftime |
struct tm (local) | mktime | n/a | - | asctime | strftime |
您想将 time_t
转换为 UTC 中的 struct tm
,因此您需要的函数是 gmtime
.
我没有包括用于从 字符串转换 的行,因为这样做的函数不是标准的。也许 best-known 是 strptime
,它将自定义字符串转换回 struct tm
,使其或多或少成为 strftime
.
(公平地说,timegm
也不是完全标准的。)