Caught out by daylight saving :-(
andrew cooke
andrew at acooke.org
Mon Mar 30 08:11:19 EDT 2009
these are utilities i use that might help:
def parse_utc_date(day, formats=None):
'''
Return the epoch for a given UTC date.
'''
if day is None:
return time()
if formats is None:
formats = ('%Y-%m-%d %H:%M:%S %Z', '%Y-%m-%d %Z', '%Y-%B-%d %Z')
day = day.strip()
if not day.endswith('UTC'):
day += ' UTC'
exception = None
for format in formats:
try:
return timegm(strptime(day, format))
except Exception, e:
#LOG.debug(format_exc())
exception = e
if exception:
raise ValueError(_('Could not parse %s: %s') % (day, exception))
else:
raise ValueError(_('Could not parse %s') % (day))
def format_utc_date(epoch, format='%Y-%m-%d %H:%M:%S UTC'):
date_ = datetime.utcfromtimestamp(epoch)
return date_.strftime(format)
andrew
CinnamonDonkey wrote:
> Hi All,
>
> I had the following bit of code which was working fine until we went
> into Daylight saving this weekend, now the result is an hour out.
>
> timeString = "20090330 15:45:23"
>
> timeFormat = '%Y-%m-%d %H:%M:%S'
>
> modificationTime = datetime.datetime.utcfromtimestamp( time.mktime
> ( time.strptime( timeString, timeFormat ) ) )
> minutesToAdvance = datetime.timedelta( minutes=5 )
>
> modificationTime = modificationTime + minutesToAdvance
>
> datetimeString = str ( modificationTime ).replace( ' ', 'T' )
>
>
> The expected result should be:
>
> datetimeString = "20090330T15:50:23"
>
> But instead I get:
>
> datetimeString = "20090330T14:50:23"
>
> I believe it is going wrong at either the mktime() or utcfromtimestamp
> () stage.
>
> What is the correct way to fix this compensating for daylight saving
> automatically?
>
> Regards,
> SHaun >8)
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
More information about the Python-list
mailing list