Re: Coerce time
Re: Coerce time
- Subject: Re: Coerce time
- From: Harald E Brandt <email@hidden>
- Date: Mon, 1 Dec 2003 00:55:20 +0100
+--> John Delacour wrote 03-08-20:
At 2:42 pm -0500 20/8/03, Rich Carroll wrote:
Is there a simple way to coerce "August 20, 2003" to 08/20/03?
I can figure out a complicated series of steps to do it "manually," I'm just
hoping someone knows a better way.
You can do it in the shell or with perl:
set d1 to "1/1/70"
set d2 to "August 20, 2003"
set d to (get date d2) - (get date d1) -- or (current date) - (get date d1)
do shell script "perl -e '@t = localtime " & d & ";
printf qq~d/d/d~, $t[4]+1,$t[3],$t[5] ;'"
+-
Just beware that this is locale dependent.
Not only d2, but more importantly: d1 is US-only!
Might be OK for Rich, but don't distribute the code...
If you hand Perl the difference between the date and current date the
thing is avoided.
For those interested, I give you the whole handler I did a year ago
or so, and which is locale independent (i.e should be internationally
OK). Adapt to US date style if you want that instead of the ISO
standard.
on isodate(arg)
-- Will do conversion between an AppleScript date and a
datestring given in the form "yyyy-mm-dd".
-- Give it either form, and it will compute the other form.
-- Dates must be within years 1902 and 2038.
-- Conversion to AppleScript date will set the time to around
noon, +- 1 hour depending on
-- if a daylight saving switch has occured since the argument
date (a problem caused by
-- AppleScript not taking change of DLST into account when
calculating elapsed time,
-- whereas Perl does take that into account!).
-- Harald E Brandt, bragit.com
if class of arg is string then
if length of arg is not 10 then error "wrong datestring format"
do shell script "perl -e '$_ = q(" & arg & ");
use Time::Local;
/(\\d{4})-(\\d{2})-(\\d{2})/;
print time - timelocal(0,0,12,$3,$2-1,$1);
#noon to avoid possible DLST problems
'"
(current date) - (result as number)
else if class of arg is date then
set time of arg to 43200 --noon to avoid possible DLST problems
(current date) - arg
do shell script "perl -e '$_ = " & result & ";
@_ = localtime (time - $_);
printf q(s-d-d), (1900+$_[5], 1+$_[4], $_[3]);
'"
else
error "wrong class given to isodate handler"
end if
end isodate
_______________________________________________
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.