【发布时间】:2018-09-09 09:39:03
【问题描述】:
我正在尝试将表示自 1970 年 1 月 1 日以来秒数的任意整数值转换为人类可读的日期。
这是我得到的最接近的日期,但我一直得到当前日期。如何获取不是当前日期的struct tm?
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int main() {
struct tm * timeStruct;
time_t myTime = 946684800; //s from 1970 to 2000
int timeStamp = time(&myTime); //I thought this would set the date to the values of myTime, it just sets it to now
timeStruct = localtime(&myTime);
cout << timeStamp;
cout << "\n";
cout << asctime(timeStruct); //This should read Jan 1, 2000, instead it keeps giving me the current time
cout << "\n";
system("pause");
return 0;
}
【问题讨论】:
-
那是因为
time返回当前时间。不支持您尝试使用它的方式。 -
gmtime和mktime可能对您有帮助吗? -
@Sneftel 我已经编辑了我的标题以更准确地反映问题。我担心我得到的答案会曲解我的意图。