[Tutor] Question on Time calculations.
Magnus Lycka
magnus@thinkware.se
Wed Jan 15 07:33:03 2003
I assume this was meant for the list. (My private
consultation service is not for free... ;)
At 20:58 2003-01-14 -0600, SA wrote:
>Here is what I have so far using the "time" module(most of this is pseudo
>code):
>
>a => entered date in format ("Mon Jan 14 20:35:13 2003)
>a is then converted to seconds relative to the epoch by
> b = mktime(strptime(a))
>I can then do the same for the end time c
>
>all that is left is the math-->
>
>d = (c-b)/(60*60)
>
>now I have a floating point number for hours.
>
>Now my question is how do I get Python to take everything right of the
>decimal point and convert to minutes?
You can multiply it with 60!
print "%02i:%02.0f" % (d, 60 * (d%1))
As I said, much it's better to use mxDateTime
http://www.egenix.com/files/python/mxDateTime.html
or the new datetime in python 2.3
http://www.python.org/dev/doc/devel/lib/module-datetime.html
Example:
>>> from mx import DateTime
>>> x = DateTime.Parser.DateTimeFromString("Mon Jan 14 20:35:13 2003")
>>> y = DateTime.now()
>>> d = y - x
>>> print d
16:35:36.37
>>> print d.hours
16.5934372222
>>> print d.hour
16
>>> print d.minute
35
>>> print d.second
36.3739999533
>>> print d.tuple()
(0, 16, 35, 36.373999953269958)
>>> print d.strftime("%H:%M:%S")
16:35:36
or
>>> DateTime.today().iso_week
(2003, 3, 3)
For the cost of a quick download, you get a package
with all date and time features than you will ever need! :)
The time module only works in a small time interval, 1970
to early 2038 (on 32 bit computers). mxDateTime has a much
wider scope. For instance you can do:
>>> b = DateTime.Date(1,1,1)
>>> e = DateTime.Date(100000,12,31)
>>> print b, e, e-b
0001-01-01 00:00:00.00 100000-12-31 00:00:00.00 36524249:00:00:00.00
You can also do nice things like getting the fifth of next month.
>>> print DateTime.today() + DateTime.RelativeDate(months = 1, day = 5)
2003-02-05 00:00:00.00
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se