Shop OBEX P1 Docs P2 Docs Learn Events
Spin converter for Unix time — Parallax Forums

Spin converter for Unix time

Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
edited 2012-03-28 13:19 in Propeller 1
In response to this thread, in order to promote acceptance of the Unix time standard, and in the spirit of "a bird in the hand", I've written an object that converts back and forth between Unix time and date/time data, and provides some human-readable string output as well. The math for these conversions was adapted from two sources: Jean Meeus, Astronomical Formulae for Calculators and the code by David Prentice on AVR Freaks.

Converting negative Unix time values presented some challenges, but I think I got it right, at least as far back as -2137500000 (Tue 08 Apr 1902 10:00:00). Unfortunately, I was not able to test them as thoroughly as I did the positive values, since Perl does not handle negative times at all. Anyway, I tested 10_000 randomly-generated positive values and got perfect agreement with UTC times. No attempt was made to detect overflow, which can occur when local time zones are included in computations that occur near the end of the Unix time epoch in 2038.

The attached zip contains the object, embedded in the Spin test program archive, the Perl host test program, and an HTML doc file for the unix_time.spin object, the latter being in autodoc-ready format.

If anyone can come up with a way to test the negative time values more thoroughly, please let me know. Once I know that people are happy with the code's accuracy and utility, I'll upload it to the OBEX.

Thanks,
-Phil

Comments

Sign In or Register to comment.