Re: getting the date
Re: getting the date
- Subject: Re: getting the date
- From: Nigel Garvey <email@hidden>
- Date: Wed, 31 Dec 2003 11:36:58 +0000
Walter Ian Kaye wrote on Tue, 30 Dec 2003 14:22:04 -0800:
>
At 09:32p +0000 12/30/2003, Nigel Garvey didst inscribe upon an
>
electronic papyrus:
>
>
>Here we go again:
>
>
>
> set theDate to (current date)
>
> set {year:y, day:d} to theDate
>
> copy theDate to b
>
> set b's month to January
>
>
>
> text 3 thru 8 of ((y * 10000 + (b - 2500000 - theDate) div -2500000 *
>
>100 + d) as string)
>
>
OK, I give. Can you please *explain* that 2500000 arithmetic?
>
There has to be a science behind it...
Sure. :-) As you probably know, a few years ago, Emmanuel came up with a
brilliant and fast method for getting the month number from a date -
which is now known as "French Vanilla", after Emmanuel's nationality and
the OSAX-free approach:
copy theDate to b
set b's month to January
set monthNum to 1 + (theDate - b + 1314864) div 2629728
The difference between theDate and the same day and time in January is
the same as the time from the beginning of the year to the beginning of
theDate's month. If you add approximately half a month and divide the
result by an average month, it reliably gives you the number of months in
the same year before theDate's month. Adding 1 then gives the number of
theDate's month. The large numbers don't have to be precise, but
obviously have to fall within a certain range. The imprecision is
discarded in the 'div' process.
Richard Hartman later realised that if you divide instead by the
approximate length of the *shortest* month, you don't need to add a half
month first.
set monthNum to 1 + (theDate - b) div 2500000
Here, the divisor can be any number of seconds between 28 * days and
about 29.5 * days. 2500000 happens to be a conveniently round number in
that range.
Both methods are vulnerable to a rarely encountered problem: they error
on dates before 1904. This is due to a long-standing bug in the
underlying Mac OS which affects date subtractions when both dates are
earlier than 1904. However, it turns out that there's no problem if the
later date is subtracted from the earlier one, so I use my own further
development of Emmanuel's idea - which I call "English Fudge", after my
own nationality and the scripting technique. ;-)
set monthNum to (b - 2500000 - theDate) div -2500000
Here, the original date is subtracted from the January date, instead of
vica versa. Since the result is a negative, the divisor's made negative
as well to return a positive result. When the original date's in January,
the two dates will be exactly the same, so a certain amount is subtracted
from b first to ensure that theDate is subtracted from something earlier.
The amount used is 2500000, because this makes the result of the division
1 more than it would have been otherwise, which saves having to add 1 as
well at the end.
NG
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.