Sunday, November 18. 2007Converting from Unix Timestamp to PostgreSQL Timestamp or DatePrinter FriendlyTrackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
This is really nice.
Is there anything simpler? For example: abstime(1195374767) .
The main problem with using abstime is that I think the abstime datatype is deprecated and that function doesn't return a timestamp but rather an abstime.
The other problem which may or may not be good is that the time is relative to UTC. So observe TIMESTAMP 'epoch' + 1195374767 * INTERVAL '1 second' gives you --- 2007-11-18 08:32:47 While abstime(1195374767) gives you an abstime output that looks like (well it would be different depending on your time zone -- I happen to be in Boston so: 2007-11-18 03:32:47-05 So abstime is more like the construct SELECT TIMESTAMP WITH Time Zone 'epoch' + 1195374767 * INTERVAL '1 second' Which yields a timestamp with timezone data type of 2007-11-18 03:32:47-05
For what it's worth unix timestamps, while not readily readable, are hardly "goofy" or not "rich". They have a number of properties that make them the timestamp of choice in many database applications just because of the sheer number of headaches they avoid. Desirable properties include:
- There's NEVER any confusion over which timezone you mean to have associated with a unix/epoch seconds time. There's no such thing and nothing extra to try to drag along or accidentally omit. - The epoch seconds value for every location on earth at a given moment is identical. This is incredibly useful as soon as you start recording data from multiple timezones, or if you ever move data around. - Calculating time deltas between timestamps is trivial. - Unix time/epoch seconds value can be "rendered" to whichever locale is desired. Timezone, DST and all the headaches that go with tracking them become display time details...the backend data will always be correct and not need updates everytime someone changes their DST rules.
Why all this '... * INTERVAL '1 second' as ...' conversion mess?
Use the integrated psql to_timestamp function: select(to_timestamp(61)); 1970-01-01 01:01:01+01 (I'm in GMT+1)
Great article! Today i found a really cool tool to convert unix time directly from the mysql phpMyAdmin. check out www.convert-unix-time.com and the bookmarklet.
i used it only today four times, love it! :-) regards, tom |
Entry's LinksQuicksearchCalendar
Categories
Blog Administration |