[Zope] Re: Can I use datetime and timedelta in zope?
Max M
maxm at mxm.dk
Mon Sep 5 08:48:53 EDT 2005
jai alai wrote:
> Kaixo;
>
> I try to use datetime and timedelta in Zope and
> returns a error....
>
> Error Type: ImportError
> Error Value: import of "datetime" is unauthorized
>
> I try use datetime and timedelta in an External Method
> and returns an error to ....
>
> Error Type: ImportError
> Error Value: No module named datetime
>
> There's any way to import this module in Zope or I
> must create a new product for it.
Use the DateTime module in Zope. If you need datetime and timedelta, you
can convert between DateTime and datetime with (untested):
from DateTime import DateTime
from datetime import tzinfo
ZERO = timedelta(0)
HOUR = timedelta(hours=1)
STDOFFSET = timedelta(seconds = -_time.timezone)
if _time.daylight:
DSTOFFSET = timedelta(seconds = -_time.altzone)
else:
DSTOFFSET = STDOFFSET
DSTDIFF = DSTOFFSET - STDOFFSET
class LocalTimezone(tzinfo):
"""
Timezone of the machine where the code is running
"""
def utcoffset(self, dt):
if self._isdst(dt):
return DSTOFFSET
else:
return STDOFFSET
def dst(self, dt):
if self._isdst(dt):
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
def dt2DT(dt):
"Converts Python datetime to Zope DateTime"
try:
# convert dt to local timezone
ltz = dt.astimezone(LocalTimezone())
return DateTime(*ltz.timetuple()[:6])
except:
return DateTime(dt.year, dt.month, dt.day, 0, 0, 0)
def DT2dt(DT):
"""
Converts Zope DateTime to Python datetime, Zope DateTime is allways utc
"""
return datetime.fromtimestamp(DT.timeTime(), LocalTimezone())
--
hilsen/regards Max M, Denmark
http://www.mxm.dk/
IT's Mad Science
More information about the Zope
mailing list