[Tutor] incrementing one minute

Dave Angel davea at ieee.org
Mon Mar 30 18:50:48 CEST 2009

mktime() and gmtime() are not inverses of each other.  The first assumes local time, and the latter gmt (or utc).  So unless you happen to be in England, and not in daylight savings time, you'd expect a problem.

mktime() is documented as the inverse of localtime(), according to the docs.  I'd assume they'd both make the same time adjustments for your location.  However, there's some ambiguity if the time you're looking at is in standard time, while you're currently in daylight savings.  So I'd look for an answer that only used UTC (or Greenwich Mean time).

Try time.gmtime(), and calendar.timegm()

pa yo wrote:

> I need to add one minute to a string that has a date and a time in
> e.g:  200903281346 should become 200903281347
> the following script converts the string into time and adds one
> minute; but somehow I also add an hour and I don't understand why.
> ====================
> import time
> #set the initial time as a string and convert it into time format:
> fromtimestring = '200903281346'
> fromtimetime = time.strptime(fromtimestring, "%Y%m%d%H%M")
> #convert this time format into UNIX time (seconds since the start of UNIX time):
> fromtimeseconds = time.mktime(fromtimetime)
> #add 60 seconds and reformat the result into the YYYYMMDDHHMM format
> totimeseconds = fromtimeseconds + 60
> totimetime = time.gmtime(totimeseconds)
> # convert the new time into a string:
> totimestring = time.strftime("%Y%m%d%H%M", totimetime)
> #print the results:
> print (fromtimestring)
> print (fromtimetime)
> print (totimetime)
> print (totimestring)
> ================
> any help or suggestions would be much appreciated.
> Payo

More information about the Tutor mailing list