2009-11-11

Unix time conversion

Good evening,

This is my first post in my first blog. After a 29 years in the computer business, I finally decided that blogs may be a good thing, after all. Not for reading, no, but I finally found it may be a good thing to write. So here comes my first snippet, a discovery that eluded me since the first of January, 1970. Or nearly so: the Unix time format and its conversion to and from the human calendar.

Question:
How do I convert a Unix timestamp into a human-readable date-time string?

Answer:

date -d @<timestamp>

Example:
$ date -d @1257958548
Wed Nov 11 17:55:48 CET 2009

Explanation:
The GNU version of the date command takes the -d option. An @-sign in front of a plain number specifies a Unix timestamp (i.e. the number of seconds since 1970-01-01). This works only on GNU/Linux date, all other Unix variants are helpless.


Question:
How do I convert human-readable date-time string into a Unix timestamp?

Answer:

date -d "Month Day hh:mm:ss TZ Year" +%s

Example:
$ date -d "Nov 11 17:55:48 CET 2009" +%s
1257958548

Again, you must use date from GNU/Linux or you are out of luck.

Enjoy!

No comments:

Post a Comment