【发布时间】:2017-07-19 02:45:45
【问题描述】:
我需要将 time_t 作为字符串存储在 char 数组中。 还有一些关于将 time_t 转换为字符串的其他问题,但它们对我没有帮助。我想将 time_t 的值存储在一个字符串中,而不是将其转换为人类可读的格式。请在任何答案之前查看this question。
#include <stdio.h>
#include <time.h>
int main()
{
struct tm epoch_date;
time_t Time_Epoch;
strptime("2017 Jan 1 23:59:59", "%Y %b %d %T", &epoch_date);
Time_Epoch = timegm(&epoch_date); // Time_Epoch: 1488268396
return 0;
}
这段代码返回时间戳为 Time_Epoch。
我应该如何将该时间戳转换为字符串以提供所需的输出,如下所示:
所需的输出:Current date and time are: 1488268396
【问题讨论】:
-
printf("Current date and time are: %d\n", (int)Time_Epoch);
标签: c string timestamp type-conversion