Doomsday

Hi All,

Here is a code for finding the Doomsday (read Wikipedia’s page for more info; this script is based on that).
Requires Satimage.osax

property anchors : {2, 0, 5, 3} -- (2000-2099 = 2; 2100-2199 = 0; 2200-2299 = 5; 2300-2399 = 3) the same every 400 years
property days : {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

set the_year to text returned of (display dialog "What year would like to find the Doomsday for?" default answer "xxxx" buttons {"Cancel", "OK"} default button "OK")
set the_anchor to (text 1 thru 2 of the_year) as integer
set the_anchor_day to (item ((the_anchor mod 4) + 1)) of anchors
set yy to (text -2 thru -1 of the_year) as integer
set a to floor (yy / 12)
set b to yy mod 12
set c to floor ((yy mod 12) / 4)
set d to (a + b + c) mod 7 as integer
set dooms to (d + the_anchor_day) mod 7

set the_day to item (dooms + 1) of day

beep 1
display dialog "Doomsday for the year " & the_year & " is " & the_day buttons {"Cancel", "OK"} default button "OK"

Hello.

It is cool. It is refreshing with something new, thanks for sharing! :slight_smile:
(You have lost an s, in the last days variable).

The link to the wikipedia article is here, since there are so much that relates to doomsday.

Hi! Thanks for the feedback! Sorry for the missing s (I first coded the dialogs and some variables in French then translated on the go. not the best way). And thanks for the link to Wiki, sure I should have given it straight.

BTW I was wondering if the day could be returned directly from its integer. This would avoid the property list. Haven’t investigated too much but I think I discovered recently that possibility yet not played with it. Does it make sense?

It is interesting, -thursday! Sadly I don’t have time to look into your interesting problem at the moment, it may be more subtle than it seems, but from the wiki page, it seemed like Conway managed without properties, but then again, Conway, is Conway. :smiley:

Hi.

That’s fun. :slight_smile:

With regard to your code, ‘days’ is actually an AppleScript constant with the value 86400 ” the number of seconds in a day. It can be set to something else in a script, but ideally it should be treated as a reserved word.

SInce all the numbers are positive, you don’t need to use Satimage’s ‘floor’. You can simply use AppleScript’s ‘div’ instead of ‘/’.

If you have text which is coercible to number, you can perform mathematical operations on it directly. The coercions are then automatic. So instead of ‘(text 1 thru 2 of the_year) as integer’, you could just have ‘the_year div 100’. Of course, you could, if you liked, explicitly coerce the_year to integer before doing the various math operations with it.

property anchors : {2, 0, 5, 3} -- (2000-2099 = 2; 2100-2199 = 0; 2200-2299 = 5; 2300-2399 = 3) the same every 400 years
property wkdays : {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}

set the_year to (text returned of (display dialog "What year would like to find the Doomsday for?" default answer "xxxx" buttons {"Cancel", "OK"} default button "OK")) as integer
set the_anchor to the_year div 100
set the_anchor_day to item (the_anchor mod 4 + 1) of anchors
set yy to the_year mod 100
set a to yy div 12
set b to yy mod 12
set c to b div 4
set d to (a + b + c) mod 7
set dooms to (d + the_anchor_day) mod 7

set the_day to item (dooms + 1) of wkdays

beep 1
display dialog "Doomsday for the year " & the_year & " is " & the_day buttons {"Cancel", "OK"} default button "OK"

Not sensibly. AppleScript’s weekdays can be coerced to integer, but not vice versa. You could add ‘dooms’ days (assuming you’re no longer using ‘days’ for something else!) to a known Sunday date and coerce the weekday of the result to text, but it’s a bit convoluted and only produces English results:

set the_day to (weekday of ((date ("1 1 2006" as text)) + dooms * days)) as text

I don’t know the mathematical significance of the anchor numbers, but the list of them can be replaced with a mathematical cheat: :slight_smile:

set the_anchor_day to (2053 div (10 ^ (3 - the_anchor mod 4))) mod 10

Addendum: Another cheat, since we’re using AppleScript anyway, would be to create a “memorable date” in the given year and simply read off the weekday! :wink:

set the_year to (text returned of (display dialog "What year would like to find the Doomsday for?" default answer "xxxx" buttons {"Cancel", "OK"} default button "OK")) as integer

tell (current date)
	set {its day, its month, its year} to {4, 4, the_year}
	set the_day to item (its weekday) of {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} -- Or: its weekday as text
end tell

beep 1
display dialog "Doomsday for the year " & the_year & " is " & the_day buttons {"Cancel", "OK"} default button "OK"

Yep! Sorry, you’re absolutely right! As I said, I translated it from French without thinking to that :confused:

I figured it not later than some minutes ago. I couldn’t undesrtand what the “floor of the quotient” was yesterday. Then I saw floor as a verb of Satimage.osax so I ran with it. Yesterday evening, helping my son with his maths homework I fell upon quotients, etc. and I understood what it was :slight_smile: Then earlier today I came accross div in a script and understood it was finally the same.

That’s where you make the difference :slight_smile: (I’m accostumed to scripting QuarkXPress so I often need to (force) coerce values :rolleyes:)

Ah! Bad luck! I thought there is a manner of taking it in reverse. :frowning:

Nice! To get it in French, I can simply add the line set my_day to word 1 of the_day (provided I erased weekday of in your line of code).

:smiley: Great! In fact I simply read the Wiki’s page and followed it to (try to) code into AS. Thanks for taking it a step further! Very appreciated!

The above looks horrific; but, like CMYS’s original script, it’s the equivalent of a human thinking: “Mod the ‘century’ figure by 4 to find out where it comes in its group of four centuries and use the equivalent digit from 2053.”

One operator fewer for a computer (taking into account the calculation of the_anchor above) would be:

set the_anchor_day to (9 - the_year div 100 mod 4 * 2) mod 7

:cool: