Re: Month arithmetic addendum
Re: Month arithmetic addendum
- Subject: Re: Month arithmetic addendum
- From: Martin Orpen <email@hidden>
- Date: Tue, 28 Sep 2004 22:35:24 +0100
Title: Re: Month arithmetic addendum
on 28/9/04 10:13 pm, Michelle Steiner at email@hidden wrote:
> set the_months to {January, February, March, April, May, June, July, ¬
> August, September, October, November, December}
> display dialog "Enter the number of months to add." default answer ""
> set n to (the text returned of the result) as integer
> if (October as number) + n is greater than 12 then set n to n + 1
> -- add error checking to ensure only integers are entered
> item (((October as number) + n) mod 13) of the_months
>
> It doesn't work with negative arithmetic if you go into the previous
> year, but that shouldn't be too hard to solve; I'm too lazy right now,
> though.
I spent 45 minutes on the train ride home messing about with date stuff.
This will nearly work (apart from some commented problems) but it'll give you an idea of how far I got before I got to the station:
tell application "iCal"
set t1 to start date of first event of calendar 9 whose summary is "xx01"
set t2 to start date of first event of calendar 9 whose summary is "xx02"
end tell
-- check month and year calculations
set m1 to month of t1 as integer
set m2 to month of t2 as integer
set y1 to year of t1
set y2 to year of t2
set tMonths to (12 - m1) + (((y2 - y1) - 1) * 12) + (m2)
set yCalc to tMonths div 12
set mCalc to tMonths mod 12
-- leap year correction
set nDays to {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
set lDays to {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
if y1 div 4 is 0 then
set mDays to lDays
else
set mDays to nDays
end if
-- must do exception for 11 months
-- day calculations
set d1 to word 3 of (t1 as string) as integer
set d2 to word 3 of (t2 as string) as integer
if d2 is greater than d1 then
set dayCalc to d2 - d1
else
set d2mE to (item m1 of mDays) - d1
set dayCalc to d2mE + d2
if dayCalc is equal to item m1 of mDays then
set dayCalc to 0
set mCalc to mCalc + 2
end if
set mCalc to mCalc - 1
if mCalc is greater than 11 then
set yCalc to yCalc + 1
set mCalc to mCalc mod 12
end if
end if
-- check times
set t3 to (time of t2) - (time of t1)
if t3 is less than 0 then
log "negative time function"
end if
yCalc & " " & mCalc & " " & dayCalc
Maybe we should award a prize for the “ugliest script of the month”?
:-)
--
Martin Orpen
Idea Digital Imaging Ltd -- The Image Specialists
http://www.idea-digital.com
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden