----- Original Message ----- From: "Eric Thomas" <eric@thomasfam.com> To: <zope@zope.org> Sent: Saturday, August 25, 2007 8:18 AM Subject: [Zope] Help with DateTime.earliestTime()
I've been having a site problem that I've tracked down to DateTime. Simply enough, the code snippets below both try to add 31 days to the first day in October. I'm expecting this to result in the first day of November.
Good Example:
from DateTime import DateTime start=DateTime('2005/10/01 01:00:00 GMT-5') print start 2005/10/01 01:00:00 GMT-5 earlyStart=start.earliestTime() print earlyStart 2005/10/01 print earlyStart+31 2005/11/01
Bad Example: (Here's the problem)
from DateTime import DateTime start=DateTime('2005/10/01 01:00:00 US/Central') print start 2005/10/01 01:00:00 US/Central earlyStart=start.earliestTime() print earlyStart 2005/10/01 print earlyStart+31 2005/10/31 23:00:00 US/Central
I've confirmed this occurs with several of the US/ timezones (US/Pacific, US/Alaska, etc)
Can anyone help me figure out why the resulting date is coming up 1 hour short of the expected 2005/11/01 ?
I don't know what your problem is, but here are some code snippets I used to get around a timezone problem I was having: # def custom timezone class class LocalTimezone(datetime.tzinfo): def utcoffset(self, dt): STDOFFSET = datetime.timedelta(seconds = -_time.timezone) ZERO = datetime.timedelta(0) if _time.daylight: DSTOFFSET = datetime.timedelta(seconds = -_time.altzone) else: DSTOFFSET = STDOFFSET if self._isdst(dt): return DSTOFFSET else: return STDOFFSET def dst(self, dt): STDOFFSET = datetime.timedelta(seconds = -_time.timezone) ZERO = datetime.timedelta(0) if _time.daylight: DSTOFFSET = datetime.timedelta(seconds = -_time.altzone) else: DSTOFFSET = STDOFFSET if self._isdst(dt): DSTDIFF = DSTOFFSET - STDOFFSET return DSTDIFF else: return ZERO def tzname(self, dt): return _time.tzname[self._isdst(dt)] def _isdst(self, dt): tt = (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second, dt.weekday(), 0, -1) stamp = _time.mktime(tt) tt = _time.localtime(stamp) return tt.tm_isdst > 0 # usage code... Local = LocalTimezone() chkDate = datetime.datetime.now(Local) now = datetime.datetime.now(Local) # see if we have to apply a local timezone offset if tzo: # get the number of hours less than UTC (GMT) localTZO = int(chkDate.strftime('%z')[0:-2]) # make sure we have the target tzo as an integer targetTZO = int(tzo) diffTZO = 0 if targetTZO < localTZO: diffTZO = -(-targetTZO + localTZO) elif targetTZO > localTZO: diffTZO = (-localTZO + targetTZO) if diffTZO: # we have to adjust our dates to account for the time zone difference chkDate = chkDate + datetime.timedelta(hours=diffTZO) now = now + datetime.timedelta(hours=diffTZO) # create a time delta object (a date/time object that is a duration) for 6 months maxDate = datetime.timedelta(weeks=24) # check for events within a 6 month window while chkDate < now + maxDate: This code was just ripped from an application, but may provide you with some ideas for a work-around. Good luck! Jonathan