WBC date library [was Re: Leopard Date Bugs]
WBC date library [was Re: Leopard Date Bugs]
- Subject: WBC date library [was Re: Leopard Date Bugs]
- From: Loren Ryter <email@hidden>
- Date: Sun, 18 Nov 2007 23:12:26 -0500
- Thread-topic: WBC date library [was Re: Leopard Date Bugs]
>
> When I make the internationalization-aware date object creating
> routine in AppleScript, it needs hundreds of lines.
> Yes I did :-).
>
Care to share what you're talking about?
Well, here is my library I worked up after this that I'm pretty sure works
on all systems. I'd be interested to know it works on Naganoya-San's "true"
(not iwayuru ;-) Japanese system...
(*
WBC's dateLib
Designed specifically to avoid whenever possible converting dates to strings
and back
Because this is broken on Leopard's international system
(c) 2007 Wooden Brain Concepts
http://www.woodenbrain.com
*)
--force recompile while changing international settings
set a to 5
set y1k_ds to "1/1/1000"
set y2k_ds to "1/1/2000"
set cd to current date
set today_midnight to my midnight(cd)
try
set y1k_isod to ((date y1k_ds) as «class isot») as date
on error
set y1k_isod to "ISO error"
end try
set y1k_dummy to my dummydate_year(1000)
set y1k_dummy2 to my pluck_date(y1k_ds)
set y2k_dummy to my dummydate_full(1, 1, 2000, 0, 0, 0)
set y2k_dummy2 to my dummydate_string(y2k_ds)
set old_date to my dummydate_full(1, 1, 2000, 3, 49, 3)
set time_of_old_date_but_today to my that_time_on_this_date(old_date, cd)
return {today_midnight, time_of_old_date_but_today, y1k_isod, y1k_dummy,
y1k_dummy2, y2k_dummy, y2k_dummy2, y1k_dummy as Unicode text, date string of
y1k_dummy2, y2k_dummy as string, y2k_dummy2 as string}
on midnight(d)
-- returns the midnight date of the given date object
set tmpdate to (d) - ((hours of (d)) * hours) - ((minutes of (d)) *
minutes) - (seconds of (d))
return tmpdate
end midnight
on dummydate_year(tyr)
-- returns Jan 1 on the year given as date
set tmpdate to current date
set year of tmpdate to tyr
set month of tmpdate to 1
set day of tmpdate to 1
set hours of tmpdate to 0
set minutes of tmpdate to 0
set seconds of tmpdate to 0
return tmpdate
end dummydate_year
on dummydate_mdy(tmo, tdy, tyr)
-- returns the midnight time on the month, day, year given
set tmpdate to current date
set year of tmpdate to tyr
set month of tmpdate to tmo
set day of tmpdate to tdy
set hours of tmpdate to 0
set minutes of tmpdate to 0
set seconds of tmpdate to 0
return tmpdate
end dummydate_mdy
on dummydate_full(tmo, tdy, tyr, thr, tmin, tsec)
-- returns the date on the month, day, year, hours, mins, secs given
set tmpdate to current date
set year of tmpdate to tyr
set month of tmpdate to tmo
set day of tmpdate to tdy
set hours of tmpdate to thr
set minutes of tmpdate to tmin
set seconds of tmpdate to tsec
return tmpdate
end dummydate_full
on dummydate_string(ts)
-- returns a date from a string as "1/1/2000" or "1-1-2000"; on error
returns current date
set ts to my replace(ts, "-", "/")
set ods to AppleScript's text item delimiters
set AppleScript's text item delimiters to "/"
set tmpdate to current date
try
set year of tmpdate to (text item 3 of ts) as integer
set month of tmpdate to (text item 1 of ts) as integer
set day of tmpdate to (text item 2 of ts) as integer
set hours of tmpdate to 0
set minutes of tmpdate to 0
set seconds of tmpdate to 0
on error
set tmpdate to current date
end try
set AppleScript's text item delimiters to ods
return tmpdate
end dummydate_string
on pluck_date_string(ts)
-- returns the sub-string of a long string with the first "word"
containing "/" or "-"; or "" if not found
-- ie returns "1/1/2000" if given "Mon 1-1-2000 3:39:39 PM"
set ds to ""
set ts to my replace(ts, "-", "/")
set ods to AppleScript's text item delimiters
set AppleScript's text item delimiters to " "
set ct to (count text items of ts)
if ct > 0 then
repeat with i from 1 to ct
set wd to item i of (text items of ts)
if wd contains "/" then
set ds to wd
exit repeat
end if
end repeat
end if
set AppleScript's text item delimiters to ods
if ds "" then
return (ds as string)
else
return ""
end if
end pluck_date_string
on pluck_date(ts)
-- combines above two; give a string with a date word in it, returns
date (at midnight, no time) rather than a string
return (my dummydate_string(my pluck_date_string(ts)))
end pluck_date
on that_time_on_this_date(that_time, this_date)
-- first date object given is time of concern, second date object is
another date; returns first date's time on second date's date
-- ie, if second parameter is Current date, then it will return the
first parameter's time but today
set tmpdate to current date
set year of tmpdate to year of this_date
set month of tmpdate to month of this_date
set day of tmpdate to day of this_date
set hours of tmpdate to (hours of that_time)
set minutes of tmpdate to (minutes of that_time)
set seconds of tmpdate to (seconds of that_time)
return tmpdate
end that_time_on_this_date
on hms_on_this_date(hr, mn, sc, this_date)
-- returns given time in hours, minutes, seconds on given date's date
set tmpdate to current date
set year of tmpdate to year of this_date
set month of tmpdate to month of this_date
set day of tmpdate to day of this_date
set hours of tmpdate to hr
set minutes of tmpdate to mn
set seconds of tmpdate to sc
return tmpdate
end hms_on_this_date
on replace(sourcetext, search, replacement)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to the search
set the textItemList to every text item of the sourcetext
set AppleScript's text item delimiters to the replacement
set the output to the textItemList as string
set AppleScript's text item delimiters to oldDelim
return output
end replace
_______________________________________________
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/archives/applescript-users
This email sent to email@hidden