Re: Getting time components of date
Re: Getting time components of date
- Subject: Re: Getting time components of date
- From: Christopher Stone <email@hidden>
- Date: Mon, 22 Dec 2003 23:02:32 -0600
At 7:59 AM -0500 12/22/03, Chris Garaffa wrote:
While working to convert an AppleScript date to a MySQL DATETIME date
(yyyy-mm-dd hh:mm:ss) I ran into a small problem. While AppleScript
supports things like:
______________________________________________________________________
Here's Arthur Knapp's handler. On my system it creates the requisite
date string in ~ 0.0024 seconds.
As opposed to ~ 0.01 seconds for the shell script.
set d to current date
DateToString(d, "yyyy-mm-dd HH:MM:ss")
(* Modified from:
* Nigel Garvey's "English Toffee," via
* Richard Hartman's "Vanille Amiricaine," via
* Emmanuel Livy's "French Vanilla"
*
* Lots of lines of code... but very fast. :)
*)
on Date_mm(d)
copy d to b
set b's month to January
return (((b - 2500000 - d) div -2500000 + 100) as string)'s text -2 thru -1
end Date_mm
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 if (s starts with "mm") then
set t to t & Date_mm(d)
set s to s's text 3 thru -1
else
set t to t & (Date_mm(d) as integer)
set s to s's text 2 thru -1
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
Chris
_______________________________________________
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.