NTP Time Stamp to ASCII Date
Ryan Rafferty
Posts: 13
I am looking for a way to convert an NTP Time Stamp (Number of seconds since 00:00:00 on January 1, 1900) to a useable ASCII Date. Or even a LONG variable for Month,Day,Year.
The problem that I am running into is that my most recent timestamp is 3,568,129,182 seconds since Jan 1, 1900. Spin will only accept positive integers up to 2,147,483,647 so when I evaluate the LONG (11010100101011010101010010011110), it returns a negative integer of -726838114.
Does anyone know of a better way to handle this?
Thanks in advance.
Ryan Rafferty
The problem that I am running into is that my most recent timestamp is 3,568,129,182 seconds since Jan 1, 1900. Spin will only accept positive integers up to 2,147,483,647 so when I evaluate the LONG (11010100101011010101010010011110), it returns a negative integer of -726838114.
Does anyone know of a better way to handle this?
Thanks in advance.
Ryan Rafferty
Comments
http://www.rayslogic.com/propeller/Programming/RTC/PropTime.zip
(BTW, just to be pedantic, the NTP timestamp is not a count of seconds since Jan. 1, 1900, just as the C time_t is not a count of seconds since Jan. 1, 1970; both of them ignore leap seconds. Which is convenient for some purposes, but a problem for others.)
Eric
Mine has only been vetted by me. I probably chose to ignore leap seconds...
However, I did get it figured out. I figured if I used a starting point of Jan 01, 2000 instead of 1900, my "Seconds Since" would be a much smaller number. So if I could subtract 3,115,760,000 from the current time stamp, what was left over would be the seconds since Jan 01, 2000. Since I cannot work with such large numbers, what I ended up doing was taking my LONG, and shifting it left 2 bits then shifting it back right 2 bits. This effectively subtracted 3,221,225,472. Now adding 65,465,472 back resulted in a number that was in fact 3,115,760,000 less than my original NTP Timestamp. Now I could use the remaining 346,903,710 (a smaller number that SPIN likes just fine) to calculate the time based on seconds since Jan 01, 2000.
I hope this helps somebody in the future...Sure made my head spin for a while.
Ryan Rafferty
To convert from a 32-bit number of seconds since Jan1, 1970 to a string you:
1. Call LocalTime with the 32-bit number as the argument
2. Call StrTime with string format as the argument
You'd have to do some math if you want it from 1900.
Probably have to worry about the Y2038 problem coming 70 years earlier for you:
http://en.wikipedia.org/wiki/Year_2038_problem
One last question....does anyone know how to make a thread as Solved?
Thanks,
Ryan Rafferty
SNTP Demo:
http://forums.parallax.com/showthread.php/129435-DEMO-Using-SNTP-to-sync-the-Spinneret-s-on-board-RTC?p=975401&viewfull=1#post975401
There is also a Perl Script that I wrote to test the formula out before I ported it over to a Spinneret ... The Perl code was run from a Linux (Fedora) based platform.
Perl Test Script:
http://forums.parallax.com/showthread.php/129435-DEMO-Using-SNTP-to-sync-the-Spinneret-s-on-board-RTC?p=992018&viewfull=1#post992018
http://forums.parallax.com/showthread.php/145697-SNTP-Simple-Network-Time-Protocol-Update?p=1160782#post1160782