Perpetual Calendar - Part 1
Problem: time()/secs() and related functions only work for dates between 1970 and 2300 or so because of limitations in the way time is calculated: secs() stores time as number of seconds passed since 1/1/1970, so it can't handle dates before then.
You can get around this with some calculations, but most hand-coded time conversions give up on providing accurate day of the week (MTWTFSS).
Solution: The Doomsday Algorithm (http://rudy.ca/doomsday.html) is a method invented by John Conway, of the Game of Life fame, to figure out the day of the week for any date in the Gregorian calendar. I'll leave the detailed explanations to the site linked above, but it's easy enough to do in one's head after a little practice. But here's my implementation in mushcode:
&CD finds the Century Day
&MDD finds the Doomsday for the month
&DD finds the difference between the date wanted and Doomsday
&isleapyear checks for leapyear
&dayofweek returns the final value, given a date in the form Jan 15 1876
dayofweek(#11322TVn)
Type: THING Flags: VISUAL NO_COMMAND
Functions for calculating the day of the week for aritrary dates. Implements the Doomsday algorithm. For more info, see http://en.wikipedia.org/wiki/Doomsday_algorithm
Owner: Nammyung(#1764PweAC) Zone: *NOTHING* Ducats: 10
Parent: *NOTHING*
Powers:
Warnings checked:
Created: Thu May 24 15:12:49 2007
Last Modification: Thu May 24 23:56:09 2007
CD [#1764]: mod(add(mul(remainder(div(%0,100),4),5),2),7)
DAYOFWEEK [#1764]: switch(mod(add(u(dd,last(%0)),mod(sub(first(rest(%0)),u(mdd,first(%0),last(%0))),7)),7),1,Mon,2,Tue,3,Wed,4,Thu,5,Fri,6,Sat,7,Sun,0,Sun,Error)
DD [#1764]: add(div(mod(%0,100),12),mod(mod(%0,100),12),div(mod(mod(%0,100),12),4),u(cd,%0))
ISLEAPYEAR [#1764]: cand(not(mod(%0,4)),nand(not(mod(%0,100)),mod(%0,400)))
MDD [#1764]: switch(%0,Jan,if(u(isleapyear,%1),4,3),Feb,2,Mar,7,Apr,4,May,9,Jun,6,Jul,11,Aug,8,Sep,5,Oct,10,Nov,7,Dec,12,Error)
Home: Nammyung's Room(#1761RnJ)
Location: shelf(#5923Tnt)
There are clearly many ways to make this simpler and more efficient: the Doomsday algorithm was meant to make the process easy to remember for a person; moreover, I could have used timefmt() etimefmt(), etc., to make things more obvious in the code. But for now, it works.
In the next part, what to do with this?
- Nammyung's blog
- Login or register to post comments

Click 
