• 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
Re: Mac dates ==> UNIX dates
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mac dates ==> UNIX dates


  • Subject: Re: Mac dates ==> UNIX dates
  • From: kai <email@hidden>
  • Date: Sat, 21 Oct 2006 15:38:43 +0100


On 20 Oct 2006, at 20:28, Marc Myers wrote:

Is there a simple way to convert a Mac date into an integer that can be used as the -r parameter in the UNIX "date" command?

Currently, I'm using "date" in a shell script to generate a date/ time string representing the UNIX Epoch. I coerce that into a Mac date. Then I subtract the Epoch date from the desired date/time, giving me a result in seconds in scientific notation. I use a text routine to convert the scientific notation to an integer (as text). That integer can be plugged into the "date" command's -r parameter.

on formDate(theDate)
--get time 0 for the Unix side
set theEpoch to date (do shell script "date -r 0 +\"%a %b %e %Y %H: %M:%S\"")

--current time in seconds from time 0, as text (for next bit)
set theDiff to (theDate - theEpoch) as text

--convert the scientific notation to an "integer" string
if theDiff contains "E" then
set thePower to (last character of theDiff) + 1
set theDiff to (first character of theDiff & (characters 3 thru -4 of theDiff as text))
repeat until (length of theDiff) = thePower
set theDiff to theDiff & "0"
end repeat
end if

--do the shell script
return (do shell script ("date -r " & theDiff & " +\"%D %T\""))
end formDate



Is there a simpler way to do a conversion? From the man page, it sounds like curl_getdate(3) would do it, but it doesn't seem to be installed. It sure would be nice if AppleScript had a "long integer" class...

It looks like the ultimate aim of the handler is to return a given date in a specific format, Marc. If that's the case, have you considered trying a little vanilla flavouring? The following, for example, executes here about 100-150 times faster than the above routine. (Handler contains one line; watch for wraps.)


-----------
on formDate(theDate)
tell theDate as «class isot» as string to text 6 thru 7 & "/" & text 9 thru 10 & "/" & text 3 thru 4 & " " & text 12 thru 19
end formDate
-----------


---
kai


_______________________________________________ Do not post admin requests to the list. They will be ignored. AppleScript-Users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: Archives: http://lists.apple.com/mailman//archives/applescript-users

This email sent to email@hidden
  • Follow-Ups:
    • Re: Mac dates ==> UNIX dates
      • From: KOENIG Yvan <email@hidden>
    • Re: Mac dates ==> UNIX dates
      • From: Philip Aker <email@hidden>
References: 
 >Mac dates ==> UNIX dates (From: Marc Myers <email@hidden>)

  • Prev by Date: Re: Mac dates ==> UNIX dates
  • Next by Date: Re: Date trouble
  • Previous by thread: Re: Mac dates ==> UNIX dates
  • Next by thread: Re: Mac dates ==> UNIX dates
  • Index(es):
    • Date
    • Thread