Algorithm Request: Create YYYY/MM/DD from seconds since Jan 1, 1900 and vice-v
Timothy D. Swieter
Posts: 1,613
To test the Brilldea UDP W5100 driver I thought I would create a demo program that implements the Network Time Protocol (NTP). Attached is a very, very rough demo code of sending a single packet to an NTP server and getting a packet response. Note this code violates all recommended practices for the NTP specification so please use it cautiously. For instance the time sever address is hard coded (based on a query from pool.ntp.org). I will provide a more complete demo but first I have a challenge.....
The NTP protocol has timestamps. The timestamp is two 32-bit numbers. The first 32-bit number is the number of seconds since January 1, 1900. The second 32-bit number is the fractions of a second. For the moment lets only focus on the first 32-bit number. I started researching and trying to create an algorithm that goes from YYYY/MM/DD format to number of seconds and from number of seconds back to a YYYY/MM/DD format. However my head started spinning while working on it and so I need to go do something else for a while. I thought this would be an interesting code challenge for the forum to try out while I am focused on other tasks.
Anyone want to try and create the two algorithms?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com
The NTP protocol has timestamps. The timestamp is two 32-bit numbers. The first 32-bit number is the number of seconds since January 1, 1900. The second 32-bit number is the fractions of a second. For the moment lets only focus on the first 32-bit number. I started researching and trying to create an algorithm that goes from YYYY/MM/DD format to number of seconds and from number of seconds back to a YYYY/MM/DD format. However my head started spinning while working on it and so I need to go do something else for a while. I thought this would be an interesting code challenge for the forum to try out while I am focused on other tasks.
Anyone want to try and create the two algorithms?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com
Comments
I have done this in x86 asm a while ago and believe me: it's difficult. You have to take care of an awful lot of special cases. I attached the asm code for fun and inspiration, but I wouldn't want to try to write this in PASM
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
He died at the console of hunger and thirst.
Next day he was buried. Face down, nine edge first.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Leon Heller
Amateur radio callsign: G1HSM
Post Edited (Leon) : 3/21/2010 11:20:34 AM GMT
I would suggest you calculate the seconds for a leap year, and those for a non leap year, and the number up to 2000 since we are there and you will not be using less than that in your app.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
· Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
Yup, that should work. I don't know for how long an unsigned 32 bit will suffice to count the seconds since 1900. The signed 32 bit overflow on the Unix epoch is sometime 2038 IIRC, but by then we will all be doing 64 bit or more math.
FWIW there are 17 leap years and 53 non leap years from 1900 to 1969 inclusive. From 1970 to 1999 I haven't the numbers handy, sorry.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
He died at the console of hunger and thirst.
Next day he was buried. Face down, nine edge first.
1900-1999 = 100/4 -1 =24 leap and 76 non-leap because 1900 was NOT a leap year.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
· Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
Object: http://obex.parallax.com/objects/36/
If the year is divisible by 4 then it is a leap year, UNLESS the year is divisible by 100 then it is not a leap year. The catch or exception is if the year is also divisible by 400 in that it takes precedence over the year being divisible by 4 or 100. If the year is divisible by 400 it is a leap year.
en.wikipedia.org/wiki/Leap_year
What's interesting is that if you ask someone what the shortest Month in recorded history is due to the introduction of the Gregorian Calendar ... it is NOT February.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
This is in minutes since midnight on 1/1/2001. There are 36890 days from Jan 1, 1900 to Jan 1, 2001, so the offset minutes to add in order to reference that year would be 53121600. I'm not sure how to do the seconds. Of course, it is 60 seconds per minute, but occasionally the wizards in the tower add a leap second to account to keep in sync with the stars and wow and wobble of mother earth. Are leap seconds are documented somewhere? (caveat-I typed the above off a hard copy)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
Did tracy's routine work for you?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
It's Only A Stupid Question If You Have Not Googled It First!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" 16:9 LCD Composite video display, eProto for SunSPOT, PropNET, PolkaDOT-51
www.tdswieter.com
In my PropTime code posted here:
http://www.rayslogic.com/propeller/Programming/Programming.htm
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
My Prop Products:· http://www.rayslogic.com/Propeller/Products/Products.htm
... so now to correct this error, we subtract a 'leap year day' every 100 years. This creates an error that now undershoots a a day by 0.0025 which now can easily be 'fixed' every 400 years by adding a 'leap year day' to the calendar.
The resulting equation is:
365 + 1/4 − 1/100 + 1/400
...Which means:
Every 4 years add a day
Every 100 years subtract a day
Every 400 years add a day
Keep in mind though with the passing of time the value of a year becomes increasingly longer and longer... with events such as the earthquake in Chili alone, the rotation of the earth will miss it's mark at the equator by some 8 inches compared to where it should have been had there not been an earthquake at all.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
tf.nist.gov/pubs/bulletin/leapsecond.htm.
The most recent leap second was added at midnight of 31-Dec-2008 going into 2009. There was also one added in 2005. The table shows 24 leap seconds added going back to 1972, all positive to account for lengthening years. As of Feb. 25, 2010, UTC was 61 milliseconds behind UT1 (astronomical time measured by transit of stars).
The Chilean 8.8 earthquake shortened the day by a little over 1 millionth of a second, according to calculations. That is too small to measure, though, the margin of error being around 20 microseconds. The margin of error given on the above web site for the difference between UTC and UT1 is +/- 5 milliseconds.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Tracy Allen
www.emesystems.com
This project was a big help:
tuxgraphics.org/electronics/200710/avr-ntp-clock.shtml
Hopefully this helps some.
Post Edited (DynamoBen) : 3/24/2010 5:17:33 PM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" 16:9 LCD Composite video display, eProto for SunSPOT, PropNET, PolkaDOT-51
www.tdswieter.com
For the moment I am going to bookmark this and come back to the topic in the future. I need to focus on my original task of getting the WIZnet W5100 driver working and then return to certain protocol parsing. Some many great distractions though.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" 16:9 LCD Composite video display, eProto for SunSPOT, PropNET, PolkaDOT-51
www.tdswieter.com
http://en.wikipedia.org/wiki/Year_2038_problem
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
My Prop Info&Apps: ·http://www.rayslogic.com/propeller/propeller.htm
My Prop Products:· http://www.rayslogic.com/Propeller/Products/Products.htm