Re: [OT] Shell scripting
Re: [OT] Shell scripting
- Subject: Re: [OT] Shell scripting
- From: Paul Berkowitz <email@hidden>
- Date: Fri, 26 Dec 2003 22:48:50 -0800
On 12/26/03 8:44 PM, "Walter Ian Kaye" <email@hidden> wrote:
>
At 03:22p -1000 12/26/2003, Reinhold Penner didst inscribe upon an
>
electronic papyrus:
>
>
> I was wondering if there is a shell way to apply the formatting to
>
> an arbitrary date string. For example get the creation/modification
>
> date of a file and format that as easily as below.
>
>
Not with `date`, but maybe some other program that calls strftime()
>
would allow it. The hard part is discovering what program that might
>
be.
You can do it with 'date' using the -r switch. It's not so simple as all
that since you have to convert the time into seconds from Jan 1, 1970 GMT.
But it can be done.
Let's say you've obtained an AppleScript date 'theDate'.
-------------------------------------
set theDate to date "Thursday, November 27, 2003 8:37:26 AM"
set timeDiff to time to GMT
set unixSecs to theDate - (date "Thursday, January 1, 1970 12:00:00 AM")
- timeDiff --local adjustment
set unixSecs to my NumberToString(unixSecs)
set dateStamp to "File modified at " & (do shell script "date -r " &
unixSecs & " \"+%H:%M on %Y-%m-%d\"")
--> "File modified at 08:37 on 2003-11-27"
on NumberToString(bigNumber)
set bigNumber to bigNumber as string
if bigNumber does not contain "E+" then return bigNumber
set {tids, AppleScript's text item delimiters} to {AppleScript's text
item delimiters, {"E+"}}
set {basicReal, powersOfTen} to {bigNumber's text item 1, (bigNumber's
text item 2) as integer}
set AppleScript's text item delimiters to {"."}
try
set {integerPart, decimalPart} to basicReal's {text item 1, text
item 2}
on error
set AppleScript's text item delimiters to {","}
set {integerPart, decimalPart} to basicReal's {text item 1, text
item 2}
end try
set AppleScript's text item delimiters to tids
set n to count decimalPart
repeat (powersOfTen - n) times
set decimalPart to decimalPart & "0"
end repeat
set bigNumber to integerPart & decimalPart
return bigNumber
end NumberToString
------------------------------
When playing with this, make sure that the dates you try out are in the same
part of daylight-savings-year as you are when you run the script, or you get
out by an hour. There are ways of dealing with that too.
--
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.