[Tutor] incrementing one minute
roadierich at googlemail.com
Mon Mar 30 17:07:00 CEST 2009
2009/3/30 pa yo <payo2000 at gmail.com>:
> I need to add one minute to a string that has a date and a time in
> YYYYMMDDHHMM format.
> 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.
> Tutor maillist - Tutor at python.org
When does it add an hour? Is it only on specific input strings, or is
it arbitrary? If its the former, what input strings does it happen
on? Can you spot any pattern to the inputs that it occurs on?
It's possible that it's getting confused with dates either side of the
British Summertime change.
Also, your variable names are rather cryptic... you might consider
using studlyCapsWithCapitalInitialLetters or
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
More information about the Tutor