Converting from an integer epoch time value to local time

↘锁芯ラ 提交于 2019-12-06 14:46:34

问题


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.

I can get localtime to work if I simply call

  time_t rawtime;
  struct tm * timeinfo;

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

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


回答1:


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);


来源:https://stackoverflow.com/questions/14679022/converting-from-an-integer-epoch-time-value-to-local-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!