Hello,
Does anyone know of a good resource for algorithms based on date and time? For example, calculating the day of the week for a certain date, or changing "number of seconds past x" to an actual date?
time.c from SDCC - You can use it for the general idea.
date & time have little to do with algorithms. They are a huge collection of rules. Always, always use a well tested date & time library for whatever language you are using - *NEVER* write one yourself.
It doesn't seem that huge given that anything below seconds is in base 10, and there are only 6 (min, hour, day, week, month, year) other denominations before it becomes base 10 again. Sure, there's daylight savings and the 12 vs 24 hour thing, but I can't think of any other exceptions.
Besides, if he's using this for KnightOS, there very well may not be a library available.
The SDCC one works, but I don't know if there are native versions available in z80 (unless you just compile time.c)
What kind of time/date representation are you using, SirCmpwn? Something based on the Unix Epoch, or something else?
That clock only keeps track of seconds. You can make the clock's start at 1997, 1970, 2010, w/e. Up to you. SDCC uses 1970, tios 1997. :-\ I hope that's right...
I see, so you can set it to any number and it just counts up the seconds? That's pretty interesting, I hadn't thought of picking my own epoch.
want a stopwatch?
It is simply a 32bit long.
willrandship wrote:
It doesn't seem that huge given that anything below seconds is in base 10, and there are only 6 (min, hour, day, week, month, year) other denominations before it becomes base 10 again. Sure, there's daylight savings and the 12 vs 24 hour thing, but I can't think of any other exceptions.
Besides, if he's using this for KnightOS, there very well may not be a library available.
That is for time, you are missing the "date" part.
You are also grossly underestimating the number of edge cases involved in date & time - which is very, very bad thing to do. People like you who think "oh, it's easy" are the reason things like this: http://www.appleinsider.com/articles/11/01/01/apple_admits_new_years_alarm_bug.html this: http://blog.makezine.com/archive/2008/12/cause-of-zune-leapyear-problem-free.html and this: http://en.wikipedia.org/wiki/Year_2000_problem happen
Again, *NEVER EVER EVER* under *ANY* circumstance write your own date & time library. There already exists tested ones for every language, in platform-agnostic versions - USE THEM.
/thread
If these exist, would you please link me to one for z80? My searches are futile.
SirCmpwn wrote:
If these exist, would you please link me to one for z80? My searches are futile.
Porting an existing C library is probably your best bet there.
I can offer the date and time routines from TI-83+ version of BBC BASIC, if that would help at all. You'll need to adjust them for your own use. They operate on a DateTime structure with the following layout:
Code: DateTime.Now = allocVar(0)
Time.Second = allocVar(1)
Time.Minute = allocVar(1)
Time.Hour = allocVar(1)
Date.DayOfWeek = allocVar(1)
Date.DayOfMonth = allocVar(1)
Date.Month = allocVar(1)
Date.Year = allocVar(1)
Date.Century = allocVar(1)
All numbers in the above are stored as packed BCD.
General date and time functions can be found in DateTime.asm, including routines to convert the above to and from strings (you'll also need Utility.asm for Utilily.DecodeBCD and Utilily.EncodeBCD and String.asm for some character helper routines). There is a Z80 implementation of the Doomsday rule in there to retrieve the day of the week based on the day of the month, month and year.
As for converting the date and time to and from the value used by the real-time clock I'm afraid I cheated and used the TI-OS routines in RTC.asm. CP/M encodes dates as a day number (days since the epoch, 31/12/1977) along with the time so I don't have any relevant links for you there I'm afraid.
Thanks, ben, those should be very useful!