[Tutor] Formating from hhmms to hh:mm:ss
John Fouhy
john at fouhy.net
Mon Sep 8 03:41:31 CEST 2008
2008/9/8 Wayne Watson <sierra_mtnview at sbcglobal.net>:
> def adjust_ftime(atime, sec):
> # yyyymmdd_hhmmss, seconds in, new yyyymmdd_hhmmss out
> ts = atime[1:-7] # use time stamp portion
> ayear = int(ts[0:4])
> amonth = int(ts[4:6])
> aday = int(ts[6:8])
> ahour = int(ts[9:11])
> aminute = int(ts[11:13])
> asecond = int(ts[13:15])
> print ayear, amonth, aday, asecond
> dt1 = datetime.datetime(ayear, amonth, aday, ahour, aminute, asecond)
> print dt1, type(dt1)
> delta = datetime.timedelta(seconds = 200)
> dt2 = dt1 + delta
> print dt2, type(dt2)
> print "what next? I need the result 20080321_113405"
Hi Wayne,
The functions you are looking for are strptime and strftime. As follows:
>>> import datetime
>>> timeStr = '20080321_113045'
>>> dt1 = datetime.datetime.strptime(timeStr, '%Y%m%d_%H%M%S')
>>> dt2 = dt1 + datetime.timedelta(seconds=200)
>>> print dt2.strftime('%Y%m%d_%H%M%S')
20080321_113405
You can find the format codes for strptime and strftime in the
documentation for the time module.
Hope this helps!
--
John.
More information about the Tutor
mailing list