从整数纪元时间值转换为本地时间 [英] Converting from an integer epoch time value to local time

查看:270
本文介绍了从整数纪元时间值转换为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直坚持从整数纪元时间值转换为本地时间。

I've been stuck on converting from an integer epoch time value to a local time.

我目前有将纪元后的时间存储在整数变量中,并且我需要一种将其转换为本地时间的方法。

I currently have the time since epoch stored in an integer variable, and I need a way to convert that to local time.

我尝试将其传递给本地时间,但似乎不起作用。

I have tried passing it into localtime but it doesn't seem to work.

如果我简单地打电话给

  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

直接获取原始时间,但是如果我想给localtime()一个整数,就会卡住值,而不只是当前时间。

And get the rawtime directly, but I'm stuck if I want to give localtime() an integer value instead of just the current time.

推荐答案

localtime()函数使用指向 const time_t 的指针,因此您必须先将整数纪元值转换为 time_t ,然后再调用 localtime

The localtime() function takes a pointer to const time_t so you have to first convert your integer epoch value to a time_t before calling localtime.

int epoch_time = SOME_VALUE;
struct tm * timeinfo;

/* Conversion to time_t as localtime() expects a time_t* */
time_t epoch_time_as_time_t = epoch_time;

/* Call to localtime() now operates on time_t */
timeinfo = localtime(&epoch_time_as_time_t);

这篇关于从整数纪元时间值转换为本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆