Doing math with time

I’d like to be able to take a starting date stamp variable and an ending date stamp variable and subtract one from the other to get the difference in time…

Anyone know of a good way? This is where my liberal arts education falls right down.

set fromNow to do shell script "perl -e \"print time;\""
delay 10
set toNow to do shell script "perl -e \"print time;\""
toNow - fromNow
--10.0

Might be a simpler way, but this works. (I also overcame a liberal arts education.)

  • Dan

Assuming you want the time in seconds, this will do it (plain vanilla):

set t1 to time of (current date)
delay 3
set t2 to time of (current date)
t2 - t1

If you need finer control then “the tick” in Jon’s Commands OSAX or GetMilliSec in an osax by that name will do it. See http://osaxen.com/ to find either of them, but be aware that if you use them, every machine that runs your code must have them.

The date format I want to do math with is like this and it comes from the Terminal:

Jul 9 11:00:22

Is it possible to use perl or some other technique to convert it into an integer than can be operated on?

Without a year? If you add it then this (Nigel Garvey/Kai/Bell) script:

to makeStamp(Now, DelimD, DelimT)
	tell Now to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
		to set dateStamp to text -2 thru -1 & DelimD & text 4 thru 5 & DelimD & text 2 thru 3
	tell Now to tell ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as string) ¬
		to set timeStamp to text 2 thru 3 & DelimT & text 4 thru 5 & DelimT & text 6 thru 7
	return dateStamp & " " & timeStamp
end makeStamp

set D to "Jul 9 2007 11:00:22"
set TS to makeStamp((date D), "", "")
--> 070709 110022