View Full Version : Spin converter for Unix time

Phil Pilgrim (PhiPi)
03-28-2012, 05:42 AM
In response to this thread (http://forums.parallax.com/showthread.php?138928), in order to promote acceptance of the Unix time (http://en.wikipedia.org/wiki/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 (http://www.amazon.com/Astronomical-Formulae-Calculators-Jean-Meeus/dp/0943396220/ref=sr_1_2?ie=UTF8&qid=1332911970&sr=8-2) and the code by David Prentice on AVR Freaks (http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=439726).

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.


03-28-2012, 10:11 AM
Heyyy, good job!

03-28-2012, 10:20 AM
Wonder how it compares to my "Prop Time"...

03-28-2012, 08:19 PM
Very useful! Thanks for making this available.