[nzlug] Time Calculator
N Dempsey
NevilleD.AuckLug at sgr-a.net
Fri Jan 12 15:59:51 NZDT 2007
On Fri, 2007-01-12 at 08:45 +0800, Marvin Pascual wrote:
> Thanks to all who responded. All of your suggestions were good enough
> and powerful. But I'm just looking for a very simple time calculator
> that can just do a simple calculations like add, subtract, multiply
> and divide a time with the following format: "HH:MM:SS.MS". This
> time format comes from the "time" command.
With standard python time module you can pick your preferred format.
Except it does not handle fractions of a seconds.
$ cat ./timecalc2.py
#!/usr/bin/env python
from time import *
# linux has two routine for parsing, and then formatting time information.
# strptime/mktime - parse the time and convert it to pure seconds since 1970
# gmtime/strftime - format/convert seconds back to your preferred format.
#
# insert your preferred time stamp format here:
fmt="%H:%M:%S"
for inc in 1,10,100,1000,10000,100000,1000000:
time_string="13:02:51"
time_seconds=mktime(strptime(time_string,fmt))+HS/100.0+inc
print "time in %d seconds is: %s"%(inc,strftime(fmt,gmtime(time_seconds)))
$ ./timecalc2.py
time in 1 seconds is: 20:45:54
time in 10 seconds is: 20:46:03
time in 100 seconds is: 20:47:33
time in 1000 seconds is: 21:02:33
time in 10000 seconds is: 23:32:33
time in 100000 seconds is: 00:32:33
time in 1000000 seconds is: 10:32:33
If it is important to have easy access to hundredth of a second,
eg ISO 8601 format. YYYY-MM-DD HH:MM:SS.ss
Then you would need to download mx.DateTime plugin module
from http://sourceforge.net/projects/convertdatetime/
mx.DateTime also allows to you do calendar calculations like "Next
Friday" etc...
If you are doing a lot of these type of calculations and want to keep
your scripts tidy, then use python/perl. But if you are only doing this
once, then a bit of hacky shell is probably forgivable.
GLuck
N
More information about the NZLUG
mailing list