Converting epoch time to “real” date/time

后端 未结 6 667
情歌与酒
情歌与酒 2020-11-30 09:38

What I want to do is convert an epoch time (seconds since midnight 1/1/1970) to \"real\" time (m/d/y h:m:s)

So far, I have the following algorithm, which to me feels

6条回答
  •  醉梦人生
    2020-11-30 10:28

    The standard library provides functions for doing this. gmtime() or localtime() will convert a time_t (seconds since the epoch, i.e.- Jan 1 1970 00:00:00) into a struct tm. strftime() can then be used to convert a struct tm into a string (char*) based on the format you specify.

    see: http://www.cplusplus.com/reference/clibrary/ctime/

    Date/time calculations can get tricky. You are much better off using an existing solution rather than trying to roll your own, unless you have a really good reason.

提交回复
热议问题