Re: Specific Date-Time Formatting
Re: Specific Date-Time Formatting
- Subject: Re: Specific Date-Time Formatting
- From: Christopher Nebel <email@hidden>
- Date: Thu, 3 Nov 2005 21:16:12 -0800
On Nov 3, 2005, at 9:06 AM, Chris Tangora wrote:
Hi All, I found this in the archive and wanted to know if there was
an option to get the date from two days in the future. I'm trying
to make script that will look ahead for what the month and day will
be in numerical format. I'm using the shell script "date" to get
the current date, but I want to get that same info as + 2 days.
Does anybody know how to do this? I like the terminal / shell
options rather than changing the text item delimiters, sorting out,
etc. But you gotta work with what you have.
"man date" will give you the answer:
-r <seconds> Print out the date and time that is <seconds>
from the Epoch.
It somewhat unhelpfully doesn't tell you when the Epoch is, but a
quick experiment ("date -r 0") can tell you that -- it's midnight Jan
1, 1970 GMT. (Or you could chase down the cross-references; it's
defined in mktime(3) and referenced in strftime(3), which you had to
read to find out how to write formatting strings for date(1).)
While subtracting one date from another in AppleScript will give you
the difference between them in seconds, and "time to GMT" will give
you the correct time zone adjustment (again in seconds), composing a
proper shell script is complicated by AppleScript wanting to use
exponential notation for numbers that large -- date(1) won't accept
them. You could, of course, write your own number-to-string routine
-- I'm sure many people here would be delighted to help you, it's a
popular sport -- but a shorter solution would be to exploit date(1)
itself to give you the current time, and do the entire thing in the
shell command:
do shell script "date -r $((`date +%s` + 86400*2))"
Decoded, if you're not familiar with this sort of thing:
"date -r blah" runs date(1) using the 'blah' as the time,
expressed as a number of seconds after the Epoch.
"$(( ... ))" arithmetically evaluates the stuff inside.
"`...`" expands to the output of the command inside.
"date +%s" returns the current date as the number of seconds
since the Epoch.
As Michelle points out, you may be able to do what you want in pure
AppleScript using the various properties of "date" objects, but if
you're keen on using date(1), then there you are.
--Chris Nebel
AppleScript and Automator Engineering
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden