如何将无符号值转换为struct tm?

时间:2014-09-24 12:55:53

标签: c++

我得到的绝对时间价值如下,

1413399540000
1411047780000
1411574340000

如何将它们转换为" struct tm"在c ++?

嗨Joachim,

这是来自GUI的访问列表时间范围的输入,我尝试了以下内容,

typedef unsigned long long uint64_t;

uint64_t mytime;
struct tm *tm;
time_t realtime;

mytime = 1447862580005ULL;
realtime = (time_t) (mytime / 1000000);
printf ("The current local time is: %s", ctime(&realtime));
tm = localtime(&realtime);
printf ("The current local time is: %s", asctime(tm));

tm->tm_year +=1900;
tm->tm_mon +=1;

printf("%04d-%02d-%02d %02d:%02d:%02d\n",
        tm->tm_year,tm->tm_mon, tm->tm_mday,
        tm->tm_hour, tm->tm_min, tm->tm_sec);

o / p:

The current local time is: Sat Jan 17 10:11:02 1970
The current local time is: Sat Jan 17 10:11:02 1970
1970-01-17 10:11:02

看起来很困惑。不确定如何验证转换是否具有核心价值。

1 个答案:

答案 0 :(得分:4)

1413399540000看起来像unix时代以来的毫秒数,即15.10.2014 18:59:00

auto millisec = 1413399540000;
time_t time = millisec / 1000;

tm local;
localtime_r(&time, &local); // TODO: Check the errors.

tm utc;
gmtime_r(&time, &utc); // TODO: Check the errors.