Convert Epoch Time string to Time

吃可爱长大的小学妹 提交于 2019-11-29 08:57:08

If only you had that value in an integer instead of a string, you could just call ctime. If only there were some way to convert a string to an integer....

time_t c;
c = strtoul( "1360440555", NULL, 0 );
ctime( &c );

You could use %s (GNU extension), to convert POSIX timestamp given as a string to the broken-down time tm:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <string.h>
#include <time.h>

int main() {
    struct tm tm;
    char buf[255];

    memset(&tm, 0, sizeof(struct tm));
    strptime("1360440555", "%s", &tm);
    strftime(buf, sizeof(buf), "%b %d %H:%M %Y", &tm);
    puts(buf); /* -> Feb 09 20:09 2013 */
    return 0;
}

Note: the local timezone is UTC (with other timezone the result is different).

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