gpt4 book ai didi

time_t 的 C 字符串表示

转载 作者:行者123 更新时间:2023-11-30 14:59:50 26 4
gpt4 key购买 nike

我需要将 time_t 作为字符串存储在字符数组中。还有一些关于将 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

最佳答案

如果目标是将 time_t 的值存储在字符数组中,则可以使用 sprintf 如下:

char strTime[50];
sprintf(strTime,"%d",Time_Epoch);

关于time_t 的 C 字符串表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42503517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com