Timezone manipulation help

I am attempting to correct a timezone problem in entourage email messages. I am trying to modify the date of the message to display UTC - 7, where the headers of the email show UTC.

The script encludes the following statement
set time sent of theMess to date theDate

and I have (perhaps mistakenly) been beating my head looking through the Applescript Language Guide ((c) 1999!!!) on the Apple Developer website and trying the various examples for doing arithmetic on dates with no success. I am ssuming the guide is badly out of date.

Can anyone suggest a possible solution to correcting the time offset in theDate, or date?

Thanks!

Hi,

You can use the ‘time to gmt’ standard addition command to get the difference between gmt and local time. This is negative or zero seconds, so subtracting will add the seconds to the current date. Something like this:

set cur_date to (current date)
set gmt_secs to (time to GMT) – negative or 0
set utc_date to (cur_date - gmt_secs) – adding seconds

from there, you can convert to utc format.

gl,

Thanks Kel,

This sort of helps - I am able to make changes now to the time sentd. However I am not able to apply logic to the right part of the header - I am only able to make changes to the existing time received.

Here’s the specific case:

I have an email header that has:
+++++++++++++++++++++++++++++++++++++++++++++++++
"Return-path: victor@foo.com
Envelope-to: abc@domaindomain.com
Delivery-date: Fri, 1 Apr 2005 21:55:23 +0000
Received: from host56a.simplicato.com ([207.99.47.56])
by atlas.domain.com with esmtp (Exim 4.43)
id 1DJeyA-0008PB-BZ
for abc@domain.com; Fri, 1 Apr 2005 21:55:23 +0000
Received: from localhost (localhost.simplicato.com [127.0.0.1])
by host56a.simplicato.com (Postfix) with ESMTP id 6BCEE2890A2
for abc@domain.com; Fri, 1 Apr 2005 17:55:06 -0400 (EDT)
Received: from host56a.simplicato.com ([127.0.0.1])
by localhost (host56a.simplicato.com [127.0.0.1]) (amavisd-new, port 10024)
with ESMTP id 63355-04 for abc@domain.com;
Fri, 1 Apr 2005 17:55:06 -0400 (EDT)
+++++++++++++++++++++++++++++++++++++++++++++++++

However, the received time that shows up in Entourage is
Sun, 3 Apr 2005 11:12:07 which is the time that my system crashed, and Entourage proceeded to reload all of my inbox, and used the current time to set the received time in Entourage. It did this to every one of the emails in my system, and I need to correct them.

So Barry Wainwright has a well-known script that fixes Entourage time stamps. His script is right here at applescript.net, and, slightly modified to handle time received instead of sent is:

+++++++++++++++++++++++++++++++++++++++++++++++++
– Correct the date v1.0 25th June 2002
– a script for Microsoft Entourage by Barry Wainwright
– Resets the date of messages to the date of the last received header
– useful for messages with badly formatted (or just plain wrong) dates

tell application “Microsoft Entourage”
set theMessages to current messages
if theMessages is {} then
display dialog “No messages selected!” buttons {“Stop”} default button 1 with icon stop
return -99
end if

repeat with theMess in theMessages
	set theHeaders to the headers of theMess
	set AppleScript's text item delimiters to {return & space}
	set theHeaders to text items of theHeaders
	set AppleScript's text item delimiters to {space}
	set theHeaders to theHeaders as string
	set AppleScript's text item delimiters to {return & tab}
	set theHeaders to text items of theHeaders
	set AppleScript's text item delimiters to {space}
	set theHeaders to theHeaders as string
	set theHeaders to the paragraphs of theHeaders
	set receivedHeader to ""
	repeat with aHeader in theHeaders
		if aHeader starts with "Received:" then copy contents of aHeader to receivedHeader
                   if contents of receivedHeader is not "" then exit repeat
	end repeat
	set AppleScript's text item delimiters to {";","("}
	set theDate to (last text item of receivedHeader)
	set time sent of theMess to date theDate
end repeat

end tell
+++++++++++++++++++++++++++++++++++++++++++++++++

It works great, except it is unaware of the timezone in the header vs. the timezone on my machine. So it takes the time in the header, and converts it exactly without taking the offset into account. Running the sample header through the script returns a received time of

Fri, 1 Apr 2005 21:55:23

wheras the actual time received was this time - 0700, or

Fri, 1 Apr 2005 14:55:23.

How would I modify the script to give me that?

Many thanks

rlj

Hi rlj,

Before this line:

set time sent of theMess to date theDate

add this line:

set theDate to ((date theDate) - (time to GMT)) as string

It adds the seconds difference in your time zone to theDate.

EDITTED: wait, you want the time back 7 hours or forward 7 hours or gmt?

If you want it back 7 hours:

set theDate to ((date theDate) - 7 * hours) as string

To set it forward, add instead of subtract.

gl,

Hello Kel,

I tried your suggestion. However it seems to fail, no matter which of teh options I use:

set AppleScript’s text item delimiters to {“;”, “(”}
set theDate to (last text item of receivedHeader)
set theDate to ((date theDate) - (time to GMT)) as string
– set theDate to ((date theDate) - 7 * hours) as string
set time received of theMess to myDate

returns:

time to GMT
-25200
get date " Thu, 07 Apr 2005 21:55:23 +0000"
“Microsoft Entourage got an error: Can’t get date " Thu, 07 Apr 2005 21:55:23 +0000”."

Any suggestions? Can the +0000 be causing the problem?

Thanks
rlj

hi rlj,

this line:

get date " Thu, 07 Apr 2005 21:55:23 +0000"

is impropper. I don’t see how you could coerce the date before and now it doesn’t work. One thing I noticed is that the text item delimiters are unnecessary as is anyway because it has always been that you can’t use more than 1 text item delimiter. On the other hand you say that the script works other than the date being in utc (or gmt). All I did was take the string date and add (actually subtract) the difference between the gmt. Where it’s going wrong is not with the adjustment in the date but with the original script.

Are you sure that the original script does anything?

Things can be deceiving sometimes. The text parsing in the script looks fishy.

I could not get Entourage to show the header, but I can get it in a script if truth be told that the original script doesn’t work.

Check it out. I’m not too good at parsing but can probably create something that works.

Later,

I just ran this line through the Script Editor:

set s to “Thu, 07 Apr 2005 21:55:23 +0000”
date s

and got this:

date “Thursday, April 7, 2005 9:55:23 PM”

hmm …

Hold on to your horses!

That’s just 24 hour time in am pm

This might be it. Sometimes, using keywords from scripting additions in tell app blocks don’t work because the same words are used in the app. So you need to coerce it out of the tell block. This was my original thought, but you said that it worked (the coercion to date). Again I’m thinking that it didn’t work.

I’ll look it tomorrow. Trying to have fun tonight.

Later,

Is this anywhere near what you want?

tell application "Microsoft Entourage"
  set theMessages to current messages
  if theMessages is {} then
    display dialog "No messages selected!" buttons {"Stop"} default button 1 with icon stop
    return -99
  end if
  
  set astid to AppleScript's text item delimiters
  repeat with theMess in theMessages
    set AppleScript's text item delimiters to "Received:"
    set topReceivedLine to text item 2 of theHeaders
    set AppleScript's text item delimiters to ";"
    set theDate to text from first word to last word of (last text item of topReceivedLine)
    set AppleScript's text item delimiters to space
    set baseDate to (date (text 1 thru text item 5 of theDate))
    set timeDiff to (time to GMT) - (text item 6 of theDate) / 100 * hours
    set time received of theMess to baseDate + timeDiff
  end repeat
  set AppleScript's text item delimiters to astid
end tell