• 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: Getting the time of day
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Getting the time of day


  • Subject: Re: Getting the time of day
  • From: "Arthur J. Knapp" <email@hidden>
  • Date: Wed, 24 Sep 2003 13:51:05 -0400

> From: "Lyle Petro" <email@hidden>
> Subject: Getting the time of day
> Date: Wed, 24 Sep 2003 08:18:56 -0600

> I would like to be able to determine the time of day via AppleScript.

set d to current date --> date "Wednesday, September 24, 2003 12:35:22 PM"

d as string --> "Wednesday, September 24, 2003 12:35:22 PM"
d's time --> 45329, seconds since midnight
d's time string --> "12:35:35 PM", subject to local settings

<http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScript
LangGuide/AppleScript.2d.html>


(* Modified from:
* Nigel Garvey's "English Toffee," via
* Richard Hartman's "Vanille Amiricaine," via
* Emmanuel Livy's "French Vanilla"
*)
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


Lots of lines of code... but very fast. :)


{ Arthur Knapp, of <http://www.STELLARViSIONs.com>
a r t h u r @ s t e l l a r v i s i o n s . c o m

"I'm the Green Fairy. The hills are alive...!"
}
_______________________________________________
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.

  • Prev by Date: Re: Monitor color depth
  • Next by Date: do shell procces in background
  • Previous by thread: Re: Getting the time of day
  • Next by thread: Re: When = == ¡Á
  • Index(es):
    • Date
    • Thread