Re: Coerce time
Re: Coerce time
- Subject: Re: Coerce time
- From: Arthur Knapp <email@hidden>
- Date: Fri, 5 Dec 2003 12:44:07 -0500
Date: Thu, 4 Dec 2003 20:55:05 -0600 (CST)
Subject: Re: Coerce time
From: "Joshua See" <email@hidden>
While rewriting a script for OS X, I discovered that the old
AppleScript
Guidebook format_date_using function still works fine in Panther. You
can
find it at
<http://lists.apple.com/archives/applescript-users/2003/Mar/07/
dateformatting.txt>
. Its not very popular on this list, though.
Yeah, there are far too many AppleScripters on this list who don't
seem to like using AppleScript.... ;-)
My date-formatting handler is a little more flexable and powerful
than the Guidebook version:
(NOTE: Inevitably, someone always complains about the length of the
script. Look, throw it into a compiled script file and load it via
"load script," you'll never have to look at the code again.)
on DateToString(d, s)
(*
* d = date
* s = format string, (case sensitive):
*
* yyyy year, 4 digits
* yy year, 2 digits
* mmmm month, full name
* mmm month, 3 character abbreviation
* mm month, 1 - 12, zero padded
* m month, 1 - 12
* dddd day, full name
* ddd day, 3 character abbreviation
* dd date, 1 - 31, zero padded
* d date, 1 - 31
* hh hour, 1 - 12, zero padded
* h hour, 1 - 12
* HH hour, 0 - 23, zero padded
* H hour, 0 - 23
* MM minutes, 0 - 59, zero padded
* M minutes, 0 - 59
* ss seconds, 0 - 59, zero padded
* s seconds, 0 - 59
* AP AM or PM
* zzzz 4 digit offset from GMT, +hhmm or -hhmm
* zz 2 digit offset from GMT, +hh or -hh
*
* ` escape character
* ~ null character
*
* Parsing Rules:
* Reading the format string from left to right, the script
* always obtains the longest possible match:
*
* "mmmmmmmmm" --> "mmmm" + "mmmm" + "m"
*
* The escape and null meta-characters can be used to avoid
* any possible ambiguity:
*
* "mmmmmmmm~m" --> "mmmm" + "mmmm" + "m"
* "mmmmmmm~mm" --> "mmmm" + "mmm" + "mm"
* "mmmmmm~mmm" --> "mmmm" + "mm" + "mmm"
* "mmmmm~mmmm" --> "mmmm" + "m" + "mmmm"
* "mmmm~mmmmm" --> "mmmm" + "mmmm" + "m"
* "mmm~mmmmmm" --> "mmm" + "mmmm" + "mm"
* "mm~mmmmmmm" --> "mm" + "mmmm" + "mmm"
* "m~mmmmmmmm" --> "m" + "mmmm" + "mmmm"
*
*
* set d to current date
*
* DateToString(d, "mmm~mm") --> Sep17
* DateToString(d, "mm~mmm") --> 17Sep
*
* DateToString(d, "Today is dddd.") --> "To17ay i30 Wednesday."
* DateToString(d, "To`day i`s dddd.") --> "Today is Wednesday."
*)
if (s's class is not string) or (s's length = 0) then return d as
string
set s to s & " " --> prevent out-of-bounds in parsing
set t to ""
considering case
repeat until s's length < 5
if (s starts with "yy") then
set v to d's year as string
if (s starts with "yyyy") then
set t to t & v
set s to s's text 5 thru -1
else
set t to t & v's text -2 thru -1
set s to s's text 3 thru -1
end if
else if (s starts with "m") then
if (s starts with "mmm") then
set v to d's month as string
if (s starts with "mmmm") then
set t to t & v
set s to s's text 5 thru -1
else
set t to t & v's text 1 thru 3
set s to s's text 4 thru -1
end if
else
(* Modified from:
* Nigel Garvey's "English Toffee," via
* Richard Hartman's "Vanille Amiricaine," via
* Emmanuel Livy's "French Vanilla"
*)
copy d to mm
set mm's month to January
set mm to (((mm - 2500000 - d) div -2500000 + 100) as string)'s
text -2 thru -1
if (s starts with "mm") then
set t to t & mm
set s to s's text 3 thru -1
else
set t to t & (mm as integer)
set s to s's text 2 thru -1
end if
end if
else if (s starts with "d") then
if (s starts with "ddd") then
set v to d's weekday as string
if (s starts with "dddd") then
set t to t & v
set s to s's text 5 thru -1
else
set t to t & v's text 1 thru 3
set s to s's text 4 thru -1
end if
else
set v to d's day as string
if (s starts with "dd") then
set t to t & ("0" & v)'s text -2 thru -1
set s to s's text 3 thru -1
else
set t to t & v
set s to s's text 2 thru -1
end if
end if
else if (s starts with "h") or (s starts with "H") then
set v to (d's time) div 60 div 60
if (s starts with "h") and (v > 12) then set v to v - 12
ignoring case
if (s starts with "hh") then
set t to t & ("0" & v)'s text -2 thru -1
set s to s's text 3 thru -1
else
set t to t & v
set s to s's text 2 thru -1
end if
end ignoring
else if (s starts with "M") then
set v to (d's time) div 60 mod 60
if (s starts with "MM") then
set t to t & ("0" & v)'s text -2 thru -1
set s to s's text 3 thru -1
else
set t to t & v
set s to s's text 2 thru -1
end if
else if (s starts with "s") then
set v to (d's time) mod 60
if (s starts with "ss") then
set t to t & ("0" & v)'s text -2 thru -1
set s to s's text 3 thru -1
else
set t to t & v
set s to s's text 2 thru -1
end if
else if (s starts with "AP") then
if ((d's time) div 60 div 60 < 12) then
set t to t & "AM"
else
set t to t & "PM"
end if
set s to s's text 3 thru -1
else if (s starts with "zz") then
tell (time to GMT) / 60 div 60 * 100 to if it < 0 then
set zzzz to "-" & ("0" & -it)'s text -4 thru -1
else
set zzzz to "+" & ("0" & it)'s text -4 thru -1
end if
if (s starts with "zzzz") then
set t to t & zzzz
set s to s's text 5 thru -1
else
set t to t & zzzz's text 1 thru 3
set s to s's text 3 thru -1
end if
else if (s starts with "`") then
set t to t & s's character 2
set s to s's text 3 thru -1
else if (s starts with "~") then
set s to s's text 2 thru -1
else
set t to t & s's character 1
set s to s's text 2 thru -1
end if
end repeat
end considering
return t
end DateToString
{ Arthur J. Knapp;
<
mailto:email@hidden>;
What...? Oh...!
}
_______________________________________________
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.