Re: Date - 1 day in Address Book
Re: Date - 1 day in Address Book
- Subject: Re: Date - 1 day in Address Book
- From: Paul Berkowitz <email@hidden>
- Date: Mon, 21 Jul 2003 08:42:34 -0700
On 7/21/03 7:05 AM, "Steve Roy" <email@hidden> wrote:
>
>
Here's a weird one. I have a user who says that dates imported into Address
>
Book
>
with my Address Book Importer application are wrongly rolled back by one day.
>
I
>
can't reproduce it and he seems to be the only one who sees it. His date
>
format
>
is Switzerland (Italian). I tried setting my system like his without seeing
>
this
>
problem.
>
>
With an iteration of small scripts sent to him, I was able to narrow it down
>
to
>
this. The following works, which shows that it's not a bug in AppleScript.
>
>
set d to "2.9.1970"
>
set theDate to date d
>
theDate as string
>
--> "MercoledC,, 2 settembre 1970 0:00:00"
>
>
However the following demonstrates the problem. The script doesn't report any
>
error, but Address Book shows the date as September 1st instead of September
>
2nd.
>
>
set d to "2.9.1970"
>
set theDate to date d
>
tell application "Address Book"
>
set thePerson to make new person at end with properties {first name:"Foo",
>
last name:"Bar"}
>
tell thePerson
>
set birth date to theDate
>
end tell
>
save addressbook
>
end tell
>
>
Is that a known bug in Address Book?
Yes. It's most definitely a known bug. It's not to do with format but with
time zone. Address Book uses date objects (not text), but when making dates
(birth dates) by AppleScript entries uses GMT instead of local time. So if
you are east of GMT - the rest of Europe, Asia, Africa, Australasia, and
also Britain, Ireland, Portugal in the summer, GMT is earlier than local
time. So if you are setting the date to "2.9.1970" with no time, that means
midnight 0:00:00. Even one hour earlier than that puts the date a day
earlier! So that's what's happening.
You'll notice, if you _get_ a birthday by AppleScript from an entry you made
manually in the UI, that Address Book actually uses 12:00:00 (pm) for its
dates. So a "quick and dirty" fix is to set birthdays by AppleScript to
12:00:00 pm too. Then only people in Australia and New Zealand and anywhere
else whose time is 12 hours or more later than GMT will get the wrong
birthday. To get it right, starting from dates without time (midnight), you
need this:
------------------------------
set theBirthdate to (date stringDate) + (12 * hours)
set theBirthdate to my AdjustABForGMT(theBirthdate)
tell application "Address Book" to set birth date of abPerson to
theBirthdate
to AdjustABForGMT(theBirthdate) -- stupid bug in AB up to OS 10.2.x ?
local hexOS, unixSecs, theBirthdate
set unixSecs to theBirthdate - (date "Thursday, January 1, 1970 12:00:00
AM")
set unixSecs to my NumberToString(unixSecs)
set theBirthdate to do shell script "date -r " & unixSecs & " \"+" &
UFormat & " %T\""
set theBirthdate to date theBirthdate
return theBirthdate
end AdjustABForGMT
---------------------------------
You can't do this via the scripting addition 'time to GMT' because it
doesn't know about summer time. This shell script works.
However, there's yet another bug in Address Book that comes up in certain
years at around the time of the changeover from standard time to summer time
(e.g. try April 28, 1950). There's a trick you can do there _before using
'save addressbook' to fix that. It incorporates yet a weird double-negative
to avoid yet a third bug - this time AppleScript's - adding large negative
numbers, which is how dates before February 1972 translate in date
arithmetic. If you don't do that then the birthdays appear as between 1835
and 1903!
---------------------------------------------
to TackyCheckBirthday(abperson, theBirthdate) -- gives back wrong time even
BEFORE save addressbook
local backBirthdate, correction
tell application "Address Book" to set backBirthdate to birth date of
(get properties of abperson)
if {backBirthdate} is not in {missing value, ""} then
if backBirthdate theBirthdate then
try -- just in case backBirthdate is 'undefined'
set correction to (theBirthdate - backBirthdate) --
subtraction should always be OK
if correction > 0 then
set theBirthdate to theBirthdate + correction -- 1 hr
usually, Daylight Savings error
else
set theBirthdate to theBirthdate - (-correction) --
avoid AS bug
end if
tell application "Address Book"
set birth date of abperson to theBirthdate
save addressbook -- or save for later
end tell
end try
end if
end if
return
end TackyCheckBirthday
------------------------------------------
--
Paul Berkowitz
_______________________________________________
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.