参考:
http://www.epochconverter.com/
http://www.epochconverter.com/programming/functions-c.php
工作中常常在server log中或调试时接触到从Epoch算起的时间,如1385615629这样的时间,真心看不懂。
还好,有 http://www.epochconverter.com/ 这样一个页面,可以将其转成可读格式的时间。比如,将1385615629转成:
GMT: Thu, 28 Nov 2013 05:13:49 GMT
Your time zone: 11/28/2013 1:13:49 PM GMT+8
该页面还提供各种语言的Epoch时间和可读时间格式的相互转换代码,比如C版本的
Convert from epoch to human readable date
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t now;
struct tm ts;
char buf[80];
// Get current time
time(&now);
// Format time, "ddd yyyy-mm-dd hh:mm:ss zzz"
ts = *localtime(&now);
strftime(buf, sizeof(buf), "%a %Y-%m-%d %H:%M:%S %Z", &ts);
printf("%s\n", buf);
return 0;
}
Convert from human readable date to epoch
struct tm t;
time_t t_of_day;
t.tm_year = 2011-1900;
t.tm_mon = 7; // Month, 0 - jan
t.tm_mday = 8; // Day of the month
t.tm_hour = 16;
t.tm_min = 11;
t.tm_sec = 42;
t.tm_isdst = -1; // Is DST on? 1 = yes, 0 = no, -1 = unknown
t_of_day = mktime(&t);
printf("seconds since the Epoch: %ld\n", (long) t_of_day)