This question already has answers here:
Converting time_t to int
                                
                                    (3个答案)
                                
                        
                                2年前关闭。
            
                    
我需要将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

最佳答案

如果目的是将time_t的值存储在char数组中,则可以将sprintf用作:

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

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

10-12 18:25