Search code examples
unixtimeepoch

convert unix epoch time to human readable time


First I have an Unix epoch time 1270787111.

Then based on this website: http://untangible.com/2009/01/covert-unix-epoch-dates-in-microsoft-excel-including-timezone-examples.html, I convert the number to be 40276.9758217592. Then I put the value into an Excel cell and convert it into "Date" format to be : 4/8/10 23:25 I do not know how this happens. How can the value 40926 be converted into 2010(year)04(month)08(day)? Is there any other programming way to convert an Unix epoch time into a human readable format time?


Solution

  • If you have the GNU coreutils date command, you can do this:

    $ date -d @1270787111
    Thu Apr  8 21:25:11 PDT 2010
    

    If you'd rather do it in C, use localtime() or gmtime() to convert the time_t value 1270787111 to a struct tm, then use asctime() to convert that to a human-readable string. ctime(t) is equivalent to asctime(localtime(t)). This assumes that your C library represents time_t values as seconds since the Unix epoch (1970-01-01 00:00:00); POSIX guarantees this, but C does not.

    strftime() takes a struct tm and converts it to a human-readable string, letting you control the format, rather than using the default "Thu Apr 8 21:25:11 2010\n" format imposed by ctime() and asctime() (yes, the default format includes a trailing newline).

    Be sure to read the documentation for these functions. They require a lot of pointer manipulation.