• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Month as Integer, redux
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Month as Integer, redux


  • Subject: Month as Integer, redux
  • From: SeaSoft Systems <email@hidden>
  • Date: Sat, 7 Apr 2001 11:07:02 -0700

I recently needed a routine to return a numerical month. I was aghast when I saw Apple's solution to this problem (from the irreplaceable Applescript Guidebook):

on month_data_of(this_date_object)
set this_month to the month of this_date_object
set month_listA to {January, February, March, April, May, June, July, ,
August, September, October, November, December}
set month_listB to {"January", "February", "March", "April", "May", "June", ,
"July", "August", "September", "October", "November", "December"}
repeat with i from 1 to the number of items in month_listA
if this_month is item i of month_listA then
set the month_string to item i of month_listB
set the month_index to i
return {month_index, month_string, this_month}
end if
end repeat
end month_data_of

However, being in a hurry, I used it.

Then, across the list (I love this list) comes Emmanuel Levy's wonderful "french vanilla" version:


Shamelessly copied from <http://homepage.mac.com/dlivesay/aclafaq.html>:

Wow, what a great faq!!! :)

set theDate to the current date
copy theDate to b
set the month of b to January
set monthNum to (1 + (theDate - b + 1314864) div 2629728)

There's something clever going on there, but my math sucks. So hopefully
someone else can explain this little gem.


Having no real life to speak of, I reviewed this and can offer a slightly simpler solution (in the long tradition of incrementally improving on every offering, however brilliant and well-executed). My small modification replaces the "set monthNum..." statement above by:

set monthNum to (1 + (theDate - b) div N)

Interestingly, this works for any integer N in the range [2419200 <= N <= 2505600], so you can pick one that suits your fancy. Proof of this is left as an exercise for interested readers who also have no life. (Hint: consider the number of seconds in the shortest month).

There are interesting trivia issues here; for example, none of these schemes would work for every day of every month if January was not the longest month (i.e., 31 days). In addition, my offering would also fail if the distribution in the length of months was any greater than it actually is; Emmanuel's is more robust in that regard, and is safer if you are from a planet with a badly non-uniform distribution of month lengths.

Finally, in deference to Authur's comment:

You mean that do don't automatically recognize the number of seconds in
one twelfth of a solar year??? What planet are you from???

;-)

I must add that scripters from Mars (or other planets whose first month is not the longest) must devise their own month-to-number AS scheme to use with their abducted Macintosh's.

For my part, I will be using

set monthNum to (1 + (theDate - b) div 2500000)

because it looks cool to earthlings (and to aliens with ten fingers and toes).

Let the flames begin :-)


Richard Hartman


  • Prev by Date: Re: Globals are considered harmful (was Re: Recursion script which doesn't?)
  • Next by Date: Re: What is the quickest way to randomize a list?
  • Previous by thread: Re: Globals are considered harmful (was Re: Recursion script which doesn't?)
  • Next by thread: Re: Month as Integer, redux
  • Index(es):
    • Date
    • Thread