Commit 4dac16a6 authored by Dorneles Tremea's avatar Dorneles Tremea

Extended DateTime parser for better support to the ISO8601 specification. Normalized docstrings.

parent 35546974
...@@ -9,13 +9,16 @@ Zope Changes ...@@ -9,13 +9,16 @@ Zope Changes
Bugs fixed Bugs fixed
- Collector #2191: extended DateTime parser for better support
to the ISO8601 specification.
- Reworking of _cached_result in Shared.DC.ZRDB.DA.DA: - Reworking of _cached_result in Shared.DC.ZRDB.DA.DA:
- fixed KeyError reported in Collector #2212 - fixed KeyError reported in Collector #2212
- fixed two memory leaks that occurred under high load - fixed two memory leaks that occurred under high load
- fixed broken cache keys for people using the obscure - fixed broken cache keys for people using the obscure
Shared.DC.ZRDB.DA.DA.connection_hook Shared.DC.ZRDB.DA.DA.connection_hook
- fixed incorrect cache ordering resulting in newer results - fixed incorrect cache ordering resulting in newer results
...@@ -45,7 +48,7 @@ Zope Changes ...@@ -45,7 +48,7 @@ Zope Changes
- Call setDefaultSkin on new requests created as the result of - Call setDefaultSkin on new requests created as the result of
ConflictError retries. ConflictError retries.
- Collector #2189: Fix logging of errors during product refresh. - Collector #2189: Fix logging of errors during product refresh.
- Collector #2185: Log username for FCGI requests. - Collector #2185: Log username for FCGI requests.
...@@ -141,9 +144,9 @@ Zope Changes ...@@ -141,9 +144,9 @@ Zope Changes
- Collector #2072: Applied patch to fix problem with overly restrictive - Collector #2072: Applied patch to fix problem with overly restrictive
__bobo_traverse__ security and tests. __bobo_traverse__ security and tests.
- Added missing Zope 3 packages: zope.app.intid, zope.app.keyreference, - Added missing Zope 3 packages: zope.app.intid, zope.app.keyreference,
zope.app.session, zope.contentprovider, zope.viewlet zope.app.session, zope.contentprovider, zope.viewlet
- Updated Five to bugfix release 1.3.5. - Updated Five to bugfix release 1.3.5.
......
...@@ -70,7 +70,52 @@ to_month=tm[yr%4==0 and (yr%100!=0 or yr%400==0)][mo] ...@@ -70,7 +70,52 @@ to_month=tm[yr%4==0 and (yr%100!=0 or yr%400==0)][mo]
EPOCH =(to_year+to_month+dy+(hr/24.0+mn/1440.0+sc/86400.0))*86400 EPOCH =(to_year+to_month+dy+(hr/24.0+mn/1440.0+sc/86400.0))*86400
jd1901 =2415385L jd1901 =2415385L
numericTimeZoneMatch=re.compile(r'[+-][0-9][0-9][0-9][0-9]').match #TS numericTimeZoneMatch = re.compile(r'[+-][0-9][0-9][0-9][0-9]').match
iso8601Match = re.compile(r'''
(?P<year>\d\d\d\d) # four digits year
(?:-? # one optional dash
(?: # followed by:
(?P<year_day>\d\d\d # three digits year day
(?!\d)) # when there's no fourth digit
| # or:
W # one W
(?P<week>\d\d) # two digits week
(?:-? # one optional dash
(?P<week_day>\d) # one digit week day
)? # week day is optional
| # or:
(?P<month>\d\d)? # two digits month
(?:-? # one optional dash
(?P<day>\d\d)? # two digits day
)? # after day is optional
) #
)? # after year is optional
(?:[T ] # one T or one whitespace
(?P<hour>\d\d) # two digits hour
(?::? # one optional colon
(?P<minute>\d\d)? # two digits minute
(?::? # one optional colon
(?P<second>\d\d)? # two digits second
(?:[.,] # one dot or one comma
(?P<fraction>\d+) # n digits fraction
)? # after second is optional
)? # after minute is optional
)? # after hour is optional
(?: # timezone:
Z # one Z
| # or:
(?P<signal>[-+]) # one plus or one minus as signal
(?P<hour_off>\d # one digit for hour offset...
(?:\d(?!\d$) # ...or two, if not the last two digits
)?) # second hour offset digit is optional
(?::? # one optional colon
(?P<min_off>\d\d) # two digits minute offset
)? # after hour offset is optional
)? # timezone is optional
)? # time is optional
(?P<garbage>.*) # store the extra garbage
''', re.VERBOSE).match
class _timezone: class _timezone:
def __init__(self,data): def __init__(self,data):
...@@ -112,8 +157,6 @@ class _timezone: ...@@ -112,8 +157,6 @@ class _timezone:
return self.tinfo[idx][0],self.tinfo[idx][1],zs[:zs.find('\000')] return self.tinfo[idx][0],self.tinfo[idx][1],zs[:zs.find('\000')]
class _cache: class _cache:
_zlst=['Brazil/Acre','Brazil/DeNoronha','Brazil/East', _zlst=['Brazil/Acre','Brazil/DeNoronha','Brazil/East',
...@@ -150,7 +193,6 @@ class _cache: ...@@ -150,7 +193,6 @@ class _cache:
'MEWT','SWT','FWT','EET','EEST','BT','ZP4','ZP5','ZP6', 'MEWT','SWT','FWT','EET','EEST','BT','ZP4','ZP5','ZP6',
'WAST','CCT','JST','EAST','GST','NZT','NZST','IDLE'] 'WAST','CCT','JST','EAST','GST','NZT','NZST','IDLE']
_zmap={'aest':'GMT+1000', 'aedt':'GMT+1100', _zmap={'aest':'GMT+1000', 'aedt':'GMT+1100',
'aus eastern standard time':'GMT+1000', 'aus eastern standard time':'GMT+1000',
'sydney standard time':'GMT+1000', 'sydney standard time':'GMT+1000',
...@@ -183,7 +225,6 @@ class _cache: ...@@ -183,7 +225,6 @@ class _cache:
'gmt+0000':'GMT+0', 'gmt+0':'GMT+0', 'gmt+0000':'GMT+0', 'gmt+0':'GMT+0',
'gmt+0100':'GMT+1', 'gmt+0200':'GMT+2', 'gmt+0300':'GMT+3', 'gmt+0100':'GMT+1', 'gmt+0200':'GMT+2', 'gmt+0300':'GMT+3',
'gmt+0400':'GMT+4', 'gmt+0500':'GMT+5', 'gmt+0600':'GMT+6', 'gmt+0400':'GMT+4', 'gmt+0500':'GMT+5', 'gmt+0600':'GMT+6',
'gmt+0700':'GMT+7', 'gmt+0800':'GMT+8', 'gmt+0900':'GMT+9', 'gmt+0700':'GMT+7', 'gmt+0800':'GMT+8', 'gmt+0900':'GMT+9',
...@@ -264,7 +305,8 @@ class _cache: ...@@ -264,7 +305,8 @@ class _cache:
self._d,self._zidx={},self._zmap.keys() self._d,self._zidx={},self._zmap.keys()
def __getitem__(self,k): def __getitem__(self,k):
try: n=self._zmap[k.lower()] try:
n=self._zmap[k.lower()]
except KeyError: except KeyError:
if numericTimeZoneMatch(k) == None: if numericTimeZoneMatch(k) == None:
raise DateTimeError,'Unrecognized timezone: %s' % k raise DateTimeError,'Unrecognized timezone: %s' % k
...@@ -275,7 +317,6 @@ class _cache: ...@@ -275,7 +317,6 @@ class _cache:
return z return z
def _findLocalTimeZoneName(isDST): def _findLocalTimeZoneName(isDST):
if not daylight: if not daylight:
# Daylight savings does not occur in this time zone. # Daylight savings does not occur in this time zone.
...@@ -370,11 +411,16 @@ def _julianday(yr,mo,dy): ...@@ -370,11 +411,16 @@ def _julianday(yr,mo,dy):
m=-m m=-m
y=y-m/12L-1L y=y-m/12L-1L
m=12L-m%12L m=12L-m%12L
if y > 0L: yr_correct=0L if y > 0L:
else: yr_correct=3L yr_correct=0L
if m < 3L: y, m=y-1L,m+12L else:
if y*10000L+m*100L+d > 15821014L: b=2L-y/100L+y/400L yr_correct=3L
else: b=0L if m < 3L:
y,m=y-1L,m+12L
if y*10000L+m*100L+d > 15821014L:
b=2L-y/100L+y/400L
else:
b=0L
return (1461L*y-yr_correct)/4L+306001L*(m+1L)/10000L+d+1720994L+b return (1461L*y-yr_correct)/4L+306001L*(m+1L)/10000L+d+1720994L+b
def _calendarday(j): def _calendarday(j):
...@@ -393,9 +439,9 @@ def _calendarday(j): ...@@ -393,9 +439,9 @@ def _calendarday(j):
return int(yr),int(mo),int(dy) return int(yr),int(mo),int(dy)
def _tzoffset(tz, t): def _tzoffset(tz, t):
"""Returns the offset in seconds to GMT from a specific timezone (tz) at """Returns the offset in seconds to GMT from a specific timezone (tz) at
a specific time (t). NB! The _tzoffset result is the same same sign as a specific time (t). NB! The _tzoffset result is the same same sign as
the time zone, i.e. GMT+2 has a 7200 second offset. This is the opposite the time zone, i.e. GMT+2 has a 7200 second offset. This is the opposite
sign of time.timezone which (confusingly) is -7200 for GMT+2.""" sign of time.timezone which (confusingly) is -7200 for GMT+2."""
try: try:
return DateTime._tzinfo[tz].info(t)[0] return DateTime._tzinfo[tz].info(t)[0]
...@@ -438,10 +484,10 @@ def safelocaltime(t): ...@@ -438,10 +484,10 @@ def safelocaltime(t):
'of this Python implementation.' % float(t) 'of this Python implementation.' % float(t)
def _tzoffset2rfc822zone(seconds): def _tzoffset2rfc822zone(seconds):
"""Takes an offset, such as from _tzoffset(), and returns an rfc822 """Takes an offset, such as from _tzoffset(), and returns an rfc822
compliant zone specification. Please note that the result of compliant zone specification. Please note that the result of
_tzoffset() is the negative of what time.localzone and time.altzone is.""" _tzoffset() is the negative of what time.localzone and time.altzone is."""
return "%+03d%02d" % divmod( (seconds/60), 60) return "%+03d%02d" % divmod( (seconds/60), 60)
def _tzoffset2iso8601zone(seconds): def _tzoffset2iso8601zone(seconds):
"""Takes an offset, such as from _tzoffset(), and returns an ISO 8601 """Takes an offset, such as from _tzoffset(), and returns an ISO 8601
...@@ -512,9 +558,8 @@ class DateTime: ...@@ -512,9 +558,8 @@ class DateTime:
except: except:
raise SyntaxError('Unable to parse %s, %s' % (args, kw)) raise SyntaxError('Unable to parse %s, %s' % (args, kw))
def _parse_args(self, *args, **kw): def _parse_args(self, *args, **kw):
"""Return a new date-time object """Return a new date-time object.
A DateTime object always maintains its value as an absolute A DateTime object always maintains its value as an absolute
UTC time, and is represented in the context of some timezone UTC time, and is represented in the context of some timezone
...@@ -526,10 +571,9 @@ class DateTime: ...@@ -526,10 +571,9 @@ class DateTime:
DateTimes may be created with from zero to seven arguments. DateTimes may be created with from zero to seven arguments.
- If the function is called with no arguments or with None,
- If the function is called with no arguments or with None,
then the current date/time is returned, represented in the then the current date/time is returned, represented in the
timezone of the local machine. timezone of the local machine.
- If the function is invoked with a single string argument - If the function is invoked with a single string argument
which is a recognized timezone name, an object representing which is a recognized timezone name, an object representing
...@@ -541,23 +585,23 @@ class DateTime: ...@@ -541,23 +585,23 @@ class DateTime:
that date/time will be returned. that date/time will be returned.
As a general rule, any date-time representation that is As a general rule, any date-time representation that is
recognized and unambigous to a resident of North America is recognized and unambigous to a resident of North America
acceptable.(The reason for this qualification is that is acceptable. The reason for this qualification is that
in North America, a date like: 2/1/1994 is interpreted in North America, a date like: 2/1/1994 is interpreted
as February 1, 1994, while in some parts of the world, as February 1, 1994, while in some parts of the world,
it is interpreted as January 2, 1994.) A date/time it is interpreted as January 2, 1994.
string consists of two components, a date component and
an optional time component, separated by one or more A date/time string consists of two components, a date
spaces. If the time component is omited, 12:00am is component and an optional time component, separated by one
assumed. Any recognized timezone name specified as the or more spaces. If the time component is omited, 12:00am is
final element of the date/time string will be used for assumed. Any recognized timezone name specified as the final
computing the date/time value. (If you create a DateTime element of the date/time string will be used for computing
with the string 'Mar 9, 1997 1:45pm US/Pacific', the the date/time value. If you create a DateTime with the
value will essentially be the same as if you had captured string 'Mar 9, 1997 1:45pm US/Pacific', the value will
time.time() at the specified date and time on a machine in essentially be the same as if you had captured time.time()
that timezone) at the specified date and time on a machine in that timezone:
<PRE>
<PRE>
e=DateTime('US/Eastern') e=DateTime('US/Eastern')
# returns current date/time, represented in US/Eastern. # returns current date/time, represented in US/Eastern.
...@@ -566,20 +610,13 @@ class DateTime: ...@@ -566,20 +610,13 @@ class DateTime:
y=DateTime('Mar 9, 1997 13:45:00') y=DateTime('Mar 9, 1997 13:45:00')
# y is equal to x # y is equal to x
</PRE> </PRE>
New in Zope 2.4:
The DateTime constructor automatically detects and handles
ISO8601 compliant dates (YYYY-MM-DDThh:ss:mmTZD).
See http://www.w3.org/TR/NOTE-datetime for full specs.
The date component consists of year, month, and day The date component consists of year, month, and day
values. The year value must be a one-, two-, or values. The year value must be a one-, two-, or
four-digit integer. If a one- or two-digit year is four-digit integer. If a one- or two-digit year is
used, the year is assumed to be in the twentieth used, the year is assumed to be in the twentieth
century. The month may an integer, from 1 to 12, a century. The month may be an integer, from 1 to 12, a
month name, or a month abreviation, where a period may month name, or a month abreviation, where a period may
optionally follow the abreviation. The day must be an optionally follow the abreviation. The day must be an
integer from 1 to the number of days in the month. The integer from 1 to the number of days in the month. The
...@@ -601,28 +638,47 @@ class DateTime: ...@@ -601,28 +638,47 @@ class DateTime:
followed by am or pm in upper or lower case, in which followed by am or pm in upper or lower case, in which
case a 12-hour clock is assumed. case a 12-hour clock is assumed.
- If the DateTime function is invoked with a single New in Zope 2.4:
Numeric argument, the number is assumed to be The DateTime constructor automatically detects and handles
a floating point value such as that returned by ISO8601 compliant dates (YYYY-MM-DDThh:ss:mmTZD).
time.time().
New in Zope 2.9.6:
The existing ISO8601 parser was extended to support almost
the whole ISO8601 specification. New formats includes:
<PRE>
y=DateTime('1993-045')
# returns the 45th day from 1993, which is 14th February
w=DateTime('1993-W06-7')
# returns the 7th day from the 6th week from 1993, which
# is also 14th February
</PRE>
See http://en.wikipedia.org/wiki/ISO_8601 for full specs.
A DateTime object is returned that represents - If the DateTime function is invoked with a single Numeric
the gmt value of the time.time() float represented in argument, the number is assumed to be a floating point value
the local machine's timezone. such as that returned by time.time().
A DateTime object is returned that represents the GMT value
of the time.time() float represented in the local machine's
timezone.
- If the DateTime function is invoked with a single - If the DateTime function is invoked with a single argument
argument that is a DateTime instane, a copy of the that is a DateTime instane, a copy of the passed object will
passed object will be created. be created.
- If the function is invoked with two numeric arguments, then
the first is taken to be an integer year and the second
argument is taken to be an offset in days from the beginning
of the year, in the context of the local machine timezone.
- If the function is invoked with two numeric arguments,
then the first is taken to be an integer year and the
second argument is taken to be an offset in days from
the beginning of the year, in the context of the local
machine timezone.
The date-time value returned is the given offset number of The date-time value returned is the given offset number of
days from the beginning of the given year, represented in days from the beginning of the given year, represented in
the timezone of the local machine. The offset may be positive the timezone of the local machine. The offset may be positive
or negative. or negative.
Two-digit years are assumed to be in the twentieth Two-digit years are assumed to be in the twentieth
century. century.
...@@ -632,6 +688,7 @@ class DateTime: ...@@ -632,6 +688,7 @@ class DateTime:
second a string naming a recognized timezone, a DateTime second a string naming a recognized timezone, a DateTime
with a value of that gmt time will be returned, represented with a value of that gmt time will be returned, represented
in the given timezone. in the given timezone.
<PRE> <PRE>
import time import time
t=time.time() t=time.time()
...@@ -644,7 +701,6 @@ class DateTime: ...@@ -644,7 +701,6 @@ class DateTime:
# now_east == now_west # now_east == now_west
# only their representations are different # only their representations are different
</PRE> </PRE>
- If the function is invoked with three or more numeric - If the function is invoked with three or more numeric
...@@ -663,7 +719,7 @@ class DateTime: ...@@ -663,7 +719,7 @@ class DateTime:
at that time on a machine in the specified timezone). at that time on a machine in the specified timezone).
New in Zope 2.7: New in Zope 2.7:
A new keyword parameter "datefmt" can be passed to the A new keyword parameter "datefmt" can be passed to the
constructor. If set to "international", the constructor constructor. If set to "international", the constructor
is forced to treat ambigious dates as "days before month is forced to treat ambigious dates as "days before month
before year". This useful if you need to parse non-US before year". This useful if you need to parse non-US
...@@ -679,7 +735,8 @@ class DateTime: ...@@ -679,7 +735,8 @@ class DateTime:
The module function Timezones() will return a list of the The module function Timezones() will return a list of the
timezones recognized by the DateTime module. Recognition of timezones recognized by the DateTime module. Recognition of
timezone names is case-insensitive.""" #' timezone names is case-insensitive.
"""
datefmt = kw.get('datefmt', getDefaultDateFormat()) datefmt = kw.get('datefmt', getDefaultDateFormat())
d=t=s=None d=t=s=None
...@@ -688,7 +745,7 @@ class DateTime: ...@@ -688,7 +745,7 @@ class DateTime:
if ac==10: if ac==10:
# Internal format called only by DateTime # Internal format called only by DateTime
yr,mo,dy,hr,mn,sc,tz,t,d,s=args yr,mo,dy,hr,mn,sc,tz,t,d,s=args
elif ac == 11: elif ac == 11:
# Internal format that includes milliseconds. # Internal format that includes milliseconds.
yr,mo,dy,hr,mn,sc,tz,t,d,s,millisecs=args yr,mo,dy,hr,mn,sc,tz,t,d,s,millisecs=args
...@@ -710,7 +767,9 @@ class DateTime: ...@@ -710,7 +767,9 @@ class DateTime:
raise SyntaxError, arg raise SyntaxError, arg
if isinstance(arg, DateTime): if isinstance(arg, DateTime):
""" Construct a new DateTime instance from a given DateTime instance """ """Construct a new DateTime instance from a given
DateTime instance.
"""
t = arg.timeTime() t = arg.timeTime()
lt = safelocaltime(t) lt = safelocaltime(t)
tz = self.localZone(lt) tz = self.localZone(lt)
...@@ -731,12 +790,13 @@ class DateTime: ...@@ -731,12 +790,13 @@ class DateTime:
elif isinstance(arg, (unicode, str)): elif isinstance(arg, (unicode, str)):
# Date/time string # Date/time string
if arg.find(' ')==-1 and arg[4]=='-': iso8601 = iso8601Match(arg.strip())
fields_iso8601 = iso8601 and iso8601.groupdict() or {}
if fields_iso8601 and not fields_iso8601.get('garbage'):
yr,mo,dy,hr,mn,sc,tz=self._parse_iso8601(arg) yr,mo,dy,hr,mn,sc,tz=self._parse_iso8601(arg)
else: else:
yr,mo,dy,hr,mn,sc,tz=self._parse(arg, datefmt) yr,mo,dy,hr,mn,sc,tz=self._parse(arg, datefmt)
if not self._validDate(yr,mo,dy): if not self._validDate(yr,mo,dy):
raise DateError, 'Invalid date: %s' % arg raise DateError, 'Invalid date: %s' % arg
if not self._validTime(hr,mn,int(sc)): if not self._validTime(hr,mn,int(sc)):
...@@ -958,8 +1018,10 @@ class DateTime: ...@@ -958,8 +1018,10 @@ class DateTime:
st= st.strip() st= st.strip()
sp=st.split() sp=st.split()
tz=sp[-1] tz=sp[-1]
if tz and (tz.lower() in ValidZones): st=' '.join(sp[:-1]) if tz and (tz.lower() in ValidZones):
else: tz = None # Decide later, since the default time zone st=' '.join(sp[:-1])
else:
tz = None # Decide later, since the default time zone
# could depend on the date. # could depend on the date.
ints,dels=[],[] ints,dels=[],[]
...@@ -969,7 +1031,8 @@ class DateTime: ...@@ -969,7 +1031,8 @@ class DateTime:
if i < l and st[i] in delimiters: if i < l and st[i] in delimiters:
d=st[i] d=st[i]
i=i+1 i=i+1
else: d='' else:
d=''
while i < l and st[i] in spaces : i=i+1 while i < l and st[i] in spaces : i=i+1
# The float pattern needs to look back 1 character, because it # The float pattern needs to look back 1 character, because it
...@@ -1001,7 +1064,6 @@ class DateTime: ...@@ -1001,7 +1064,6 @@ class DateTime:
ints.append(v) ints.append(v)
continue continue
ts_results = wordpat.match(st, i) ts_results = wordpat.match(st, i)
if ts_results: if ts_results:
o,s=ts_results.group(0),ts_results.group(0).lower() o,s=ts_results.group(0),ts_results.group(0).lower()
...@@ -1114,7 +1176,6 @@ class DateTime: ...@@ -1114,7 +1176,6 @@ class DateTime:
del ints[0] del ints[0]
if ints: raise SyntaxError,st if ints: raise SyntaxError,st
tod_int = int(math.floor(tod)) tod_int = int(math.floor(tod))
ms = tod - tod_int ms = tod - tod_int
hr,mn,sc = _calcHMS(tod_int, ms) hr,mn,sc = _calcHMS(tod_int, ms)
...@@ -1127,36 +1188,39 @@ class DateTime: ...@@ -1127,36 +1188,39 @@ class DateTime:
return year,month,day,hr,mn,sc,tz return year,month,day,hr,mn,sc,tz
# Internal methods # Internal methods
def __getinitargs__(self): return (None,) def __getinitargs__(self):
return (None,)
def _validDate(self,y,m,d): def _validDate(self,y,m,d):
if m<1 or m>12 or y<0 or d<1 or d>31: return 0 if m<1 or m>12 or y<0 or d<1 or d>31:
return 0
return d<=self._month_len[(y%4==0 and (y%100!=0 or y%400==0))][m] return d<=self._month_len[(y%4==0 and (y%100!=0 or y%400==0))][m]
def _validTime(self,h,m,s): def _validTime(self,h,m,s):
return h>=0 and h<=23 and m>=0 and m<=59 and s>=0 and s < 60 return h>=0 and h<=23 and m>=0 and m<=59 and s>=0 and s < 60
def __getattr__(self, name): def __getattr__(self, name):
if '%' in name: return strftimeFormatter(self, name) if '%' in name:
return strftimeFormatter(self, name)
raise AttributeError, name raise AttributeError, name
# Conversion and comparison methods # Conversion and comparison methods
def timeTime(self): def timeTime(self):
"""Return the date/time as a floating-point number in UTC, """Return the date/time as a floating-point number in UTC,
in the format used by the python time module. in the format used by the python time module.
Note that it is possible to create date/time values
with DateTime that have no meaningful value to the Note that it is possible to create date/time values with
time module.""" DateTime that have no meaningful value to the time module.
"""
return self._t return self._t
def toZone(self, z): def toZone(self, z):
"""Return a DateTime with the value as the current """Return a DateTime with the value as the current
object, represented in the indicated timezone.""" object, represented in the indicated timezone.
"""
t,tz=self._t,self._tzinfo._zmap[z.lower()] t,tz=self._t,self._tzinfo._zmap[z.lower()]
millis = self.millis() millis = self.millis()
#if (t>0 and ((t/86400.0) < 24837)):
try: try:
# Try to use time module for speed. # Try to use time module for speed.
yr,mo,dy,hr,mn,sc=safegmtime(t+_tzoffset(tz, t))[:6] yr,mo,dy,hr,mn,sc=safegmtime(t+_tzoffset(tz, t))[:6]
...@@ -1179,41 +1243,47 @@ class DateTime: ...@@ -1179,41 +1243,47 @@ class DateTime:
def isFuture(self): def isFuture(self):
"""Return true if this object represents a date/time """Return true if this object represents a date/time
later than the time of the call""" later than the time of the call.
"""
return (self._t > time()) return (self._t > time())
def isPast(self): def isPast(self):
"""Return true if this object represents a date/time """Return true if this object represents a date/time
earlier than the time of the call""" earlier than the time of the call.
"""
return (self._t < time()) return (self._t < time())
def isCurrentYear(self): def isCurrentYear(self):
"""Return true if this object represents a date/time """Return true if this object represents a date/time
that falls within the current year, in the context that falls within the current year, in the context
of this object\'s timezone representation""" of this object\'s timezone representation.
"""
t=time() t=time()
return safegmtime(t+_tzoffset(self._tz, t))[0]==self._year return safegmtime(t+_tzoffset(self._tz, t))[0]==self._year
def isCurrentMonth(self): def isCurrentMonth(self):
"""Return true if this object represents a date/time """Return true if this object represents a date/time
that falls within the current month, in the context that falls within the current month, in the context
of this object\'s timezone representation""" of this object\'s timezone representation.
"""
t=time() t=time()
gmt=safegmtime(t+_tzoffset(self._tz, t)) gmt=safegmtime(t+_tzoffset(self._tz, t))
return gmt[0]==self._year and gmt[1]==self._month return gmt[0]==self._year and gmt[1]==self._month
def isCurrentDay(self): def isCurrentDay(self):
"""Return true if this object represents a date/time """Return true if this object represents a date/time
that falls within the current day, in the context that falls within the current day, in the context
of this object\'s timezone representation""" of this object\'s timezone representation.
"""
t=time() t=time()
gmt=safegmtime(t+_tzoffset(self._tz, t)) gmt=safegmtime(t+_tzoffset(self._tz, t))
return gmt[0]==self._year and gmt[1]==self._month and gmt[2]==self._day return gmt[0]==self._year and gmt[1]==self._month and gmt[2]==self._day
def isCurrentHour(self): def isCurrentHour(self):
"""Return true if this object represents a date/time """Return true if this object represents a date/time
that falls within the current hour, in the context that falls within the current hour, in the context
of this object\'s timezone representation""" of this object\'s timezone representation.
"""
t=time() t=time()
gmt=safegmtime(t+_tzoffset(self._tz, t)) gmt=safegmtime(t+_tzoffset(self._tz, t))
return (gmt[0]==self._year and gmt[1]==self._month and return (gmt[0]==self._year and gmt[1]==self._month and
...@@ -1221,8 +1291,9 @@ class DateTime: ...@@ -1221,8 +1291,9 @@ class DateTime:
def isCurrentMinute(self): def isCurrentMinute(self):
"""Return true if this object represents a date/time """Return true if this object represents a date/time
that falls within the current minute, in the context that falls within the current minute, in the context
of this object\'s timezone representation""" of this object\'s timezone representation.
"""
t=time() t=time()
gmt=safegmtime(t+_tzoffset(self._tz, t)) gmt=safegmtime(t+_tzoffset(self._tz, t))
return (gmt[0]==self._year and gmt[1]==self._month and return (gmt[0]==self._year and gmt[1]==self._month and
...@@ -1231,26 +1302,30 @@ class DateTime: ...@@ -1231,26 +1302,30 @@ class DateTime:
def earliestTime(self): def earliestTime(self):
"""Return a new DateTime object that represents the earliest """Return a new DateTime object that represents the earliest
possible time (in whole seconds) that still falls within possible time (in whole seconds) that still falls within
the current object\'s day, in the object\'s timezone context""" the current object\'s day, in the object\'s timezone context.
"""
return self.__class__(self._year,self._month,self._day,0,0,0,self._tz) return self.__class__(self._year,self._month,self._day,0,0,0,self._tz)
def latestTime(self): def latestTime(self):
"""Return a new DateTime object that represents the latest """Return a new DateTime object that represents the latest
possible time (in whole seconds) that still falls within possible time (in whole seconds) that still falls within
the current object\'s day, in the object\'s timezone context""" the current object\'s day, in the object\'s timezone context.
"""
return self.__class__(self._year,self._month,self._day, return self.__class__(self._year,self._month,self._day,
23,59,59,self._tz) 23,59,59,self._tz)
def greaterThan(self,t): def greaterThan(self,t):
"""Compare this DateTime object to another DateTime object """Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned OR a floating point number such as that which is returned
by the python time module. Returns true if the object by the python time module.
represents a date/time greater than the specified DateTime
or time module style time. Returns true if the object represents a date/time greater
Revised to give more correct results through comparison of than the specified DateTime or time module style time.
long integer milliseconds.
""" Revised to give more correct results through comparison of
long integer milliseconds.
"""
# Optimized for sorting speed # Optimized for sorting speed
try: try:
return (self._millis > t._millis) return (self._millis > t._millis)
...@@ -1263,13 +1338,16 @@ class DateTime: ...@@ -1263,13 +1338,16 @@ class DateTime:
def greaterThanEqualTo(self,t): def greaterThanEqualTo(self,t):
"""Compare this DateTime object to another DateTime object """Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned OR a floating point number such as that which is returned
by the python time module. Returns true if the object by the python time module.
represents a date/time greater than or equal to the
specified DateTime or time module style time. Returns true if the object represents a date/time greater
Revised to give more correct results through comparison of than or equal to the specified DateTime or time module style
long integer milliseconds. time.
"""
Revised to give more correct results through comparison of
long integer milliseconds.
"""
# Optimized for sorting speed # Optimized for sorting speed
try: try:
return (self._millis >= t._millis) return (self._millis >= t._millis)
...@@ -1282,13 +1360,15 @@ class DateTime: ...@@ -1282,13 +1360,15 @@ class DateTime:
def equalTo(self,t): def equalTo(self,t):
"""Compare this DateTime object to another DateTime object """Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned OR a floating point number such as that which is returned
by the python time module. Returns true if the object by the python time module.
represents a date/time equal to the specified DateTime
or time module style time. Returns true if the object represents a date/time equal to
Revised to give more correct results through comparison of the specified DateTime or time module style time.
long integer milliseconds.
""" Revised to give more correct results through comparison of
long integer milliseconds.
"""
# Optimized for sorting speed # Optimized for sorting speed
try: try:
return (self._millis == t._millis) return (self._millis == t._millis)
...@@ -1301,13 +1381,15 @@ class DateTime: ...@@ -1301,13 +1381,15 @@ class DateTime:
def notEqualTo(self,t): def notEqualTo(self,t):
"""Compare this DateTime object to another DateTime object """Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned OR a floating point number such as that which is returned
by the python time module. Returns true if the object by the python time module.
represents a date/time not equal to the specified DateTime
or time module style time. Returns true if the object represents a date/time not equal
Revised to give more correct results through comparison of to the specified DateTime or time module style time.
long integer milliseconds.
""" Revised to give more correct results through comparison of
long integer milliseconds.
"""
# Optimized for sorting speed # Optimized for sorting speed
try: try:
return (self._millis != t._millis) return (self._millis != t._millis)
...@@ -1320,13 +1402,15 @@ class DateTime: ...@@ -1320,13 +1402,15 @@ class DateTime:
def lessThan(self,t): def lessThan(self,t):
"""Compare this DateTime object to another DateTime object """Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned OR a floating point number such as that which is returned
by the python time module. Returns true if the object by the python time module.
represents a date/time less than the specified DateTime
or time module style time. Returns true if the object represents a date/time less than
Revised to give more correct results through comparison of the specified DateTime or time module style time.
long integer milliseconds.
""" Revised to give more correct results through comparison of
long integer milliseconds.
"""
# Optimized for sorting speed # Optimized for sorting speed
try: try:
return (self._millis < t._millis) return (self._millis < t._millis)
...@@ -1339,13 +1423,15 @@ class DateTime: ...@@ -1339,13 +1423,15 @@ class DateTime:
def lessThanEqualTo(self,t): def lessThanEqualTo(self,t):
"""Compare this DateTime object to another DateTime object """Compare this DateTime object to another DateTime object
OR a floating point number such as that which is returned OR a floating point number such as that which is returned
by the python time module. Returns true if the object by the python time module.
represents a date/time less than or equal to the specified
DateTime or time module style time. Returns true if the object represents a date/time less than
Revised to give more correct results through comparison of or equal to the specified DateTime or time module style time.
long integer milliseconds.
""" Revised to give more correct results through comparison of
long integer milliseconds.
"""
# Optimized for sorting speed # Optimized for sorting speed
try: try:
return (self._millis <= t._millis) return (self._millis <= t._millis)
...@@ -1357,13 +1443,15 @@ class DateTime: ...@@ -1357,13 +1443,15 @@ class DateTime:
__le__ = lessThanEqualTo __le__ = lessThanEqualTo
def isLeapYear(self): def isLeapYear(self):
"""Return true if the current year (in the context of the object\'s """Return true if the current year (in the context of the
timezone) is a leap year""" object\'s timezone) is a leap year.
"""
return self._year%4==0 and (self._year%100!=0 or self._year%400==0) return self._year%4==0 and (self._year%100!=0 or self._year%400==0)
def dayOfYear(self): def dayOfYear(self):
"""Return the day of the year, in context of """Return the day of the year, in context of the timezone
the timezone representation of the object""" representation of the object.
"""
d=int(self._d+(_tzoffset(self._tz, self._t)/86400.0)) d=int(self._d+(_tzoffset(self._tz, self._t)/86400.0))
return int((d+jd1901)-_julianday(self._year,1,0)) return int((d+jd1901)-_julianday(self._year,1,0))
...@@ -1371,7 +1459,8 @@ class DateTime: ...@@ -1371,7 +1459,8 @@ class DateTime:
# Component access # Component access
def parts(self): def parts(self):
"""Return a tuple containing the calendar year, month, """Return a tuple containing the calendar year, month,
day, hour, minute second and timezone of the object""" day, hour, minute second and timezone of the object.
"""
return self._year, self._month, self._day, self._hour, \ return self._year, self._month, self._day, self._hour, \
self._minute, self._second, self._tz self._minute, self._second, self._tz
...@@ -1384,15 +1473,15 @@ class DateTime: ...@@ -1384,15 +1473,15 @@ class DateTime:
return _tzoffset(self._tz, self._t) return _tzoffset(self._tz, self._t)
def year(self): def year(self):
"""Return the calendar year of the object""" """Return the calendar year of the object."""
return self._year return self._year
def month(self): def month(self):
"""Return the month of the object as an integer""" """Return the month of the object as an integer."""
return self._month return self._month
def Month(self): def Month(self):
"""Return the full month name""" """Return the full month name."""
return self._fmon return self._fmon
def aMonth(self): def aMonth(self):
...@@ -1400,7 +1489,7 @@ class DateTime: ...@@ -1400,7 +1489,7 @@ class DateTime:
return self._amon return self._amon
def Mon(self): def Mon(self):
"""Compatibility: see aMonth""" """Compatibility: see aMonth."""
return self._amon return self._amon
def pMonth(self): def pMonth(self):
...@@ -1408,63 +1497,63 @@ class DateTime: ...@@ -1408,63 +1497,63 @@ class DateTime:
return self._pmon return self._pmon
def Mon_(self): def Mon_(self):
"""Compatibility: see pMonth""" """Compatibility: see pMonth."""
return self._pmon return self._pmon
def day(self): def day(self):
"""Return the integer day""" """Return the integer day."""
return self._day return self._day
def Day(self): def Day(self):
"""Return the full name of the day of the week""" """Return the full name of the day of the week."""
return self._fday return self._fday
def DayOfWeek(self): def DayOfWeek(self):
"""Compatibility: see Day""" """Compatibility: see Day."""
return self._fday return self._fday
def aDay(self): def aDay(self):
"""Return the abreviated name of the day of the week""" """Return the abreviated name of the day of the week."""
return self._aday return self._aday
def pDay(self): def pDay(self):
"""Return the abreviated (with period) name of the day of the week""" """Return the abreviated (with period) name of the day of the week."""
return self._pday return self._pday
def Day_(self): def Day_(self):
"""Compatibility: see pDay""" """Compatibility: see pDay."""
return self._pday return self._pday
def dow(self): def dow(self):
"""Return the integer day of the week, where sunday is 0""" """Return the integer day of the week, where sunday is 0."""
return self._dayoffset return self._dayoffset
def dow_1(self): def dow_1(self):
"""Return the integer day of the week, where sunday is 1""" """Return the integer day of the week, where sunday is 1."""
return self._dayoffset+1 return self._dayoffset+1
def h_12(self): def h_12(self):
"""Return the 12-hour clock representation of the hour""" """Return the 12-hour clock representation of the hour."""
return self._pmhour return self._pmhour
def h_24(self): def h_24(self):
"""Return the 24-hour clock representation of the hour""" """Return the 24-hour clock representation of the hour."""
return self._hour return self._hour
def ampm(self): def ampm(self):
"""Return the appropriate time modifier (am or pm)""" """Return the appropriate time modifier (am or pm)."""
return self._pm return self._pm
def hour(self): def hour(self):
"""Return the 24-hour clock representation of the hour""" """Return the 24-hour clock representation of the hour."""
return self._hour return self._hour
def minute(self): def minute(self):
"""Return the minute""" """Return the minute."""
return self._minute return self._minute
def second(self): def second(self):
"""Return the second""" """Return the second."""
return self._second return self._second
def millis(self): def millis(self):
...@@ -1481,12 +1570,12 @@ class DateTime: ...@@ -1481,12 +1570,12 @@ class DateTime:
return millis return millis
def strftime(self, format): def strftime(self, format):
# Format the date/time using the *current timezone representation*. """Format the date/time using the *current timezone representation*."""
x = _calcDependentSecond2(self._year, self._month, self._day, x = _calcDependentSecond2(self._year, self._month, self._day,
self._hour, self._minute, self._second) self._hour, self._minute, self._second)
ltz = self._calcTimezoneName(x, 0) ltz = self._calcTimezoneName(x, 0)
tzdiff = _tzoffset(ltz, self._t) - _tzoffset(self._tz, self._t) tzdiff = _tzoffset(ltz, self._t) - _tzoffset(self._tz, self._t)
zself = self + tzdiff/86400.0 zself = self + tzdiff/86400.0
microseconds = int((zself._second - zself._nearsec) * 1000000) microseconds = int((zself._second - zself._nearsec) * 1000000)
# Note: in older versions strftime() accepted also unicode strings # Note: in older versions strftime() accepted also unicode strings
...@@ -1498,12 +1587,11 @@ class DateTime: ...@@ -1498,12 +1587,11 @@ class DateTime:
if isinstance(format, unicode): if isinstance(format, unicode):
format = format.encode('utf-8') format = format.encode('utf-8')
format_is_unicode = True format_is_unicode = True
ds = datetime(zself._year, zself._month, zself._day, zself._hour, ds = datetime(zself._year, zself._month, zself._day, zself._hour,
zself._minute, int(zself._nearsec), zself._minute, int(zself._nearsec),
microseconds).strftime(format) microseconds).strftime(format)
return format_is_unicode and unicode(ds, 'utf-8') or ds return format_is_unicode and unicode(ds, 'utf-8') or ds
# General formats from previous DateTime # General formats from previous DateTime
def Date(self): def Date(self):
"""Return the date string for the object.""" """Return the date string for the object."""
...@@ -1536,21 +1624,20 @@ class DateTime: ...@@ -1536,21 +1624,20 @@ class DateTime:
self._pmhour,self._minute,self._second,self._pm) self._pmhour,self._minute,self._second,self._pm)
def yy(self): def yy(self):
"""Return calendar year as a 2 digit string""" """Return calendar year as a 2 digit string."""
return str(self._year)[-2:] return str(self._year)[-2:]
def mm(self): def mm(self):
"""Return month as a 2 digit string""" """Return month as a 2 digit string."""
return '%02d' % self._month return '%02d' % self._month
def dd(self): def dd(self):
"""Return day as a 2 digit string""" """Return day as a 2 digit string."""
return '%02d' % self._day return '%02d' % self._day
def rfc822(self): def rfc822(self):
"""Return the date in RFC 822 format""" """Return the date in RFC 822 format."""
tzoffset = _tzoffset2rfc822zone(_tzoffset(self._tz, self._t)) tzoffset = _tzoffset2rfc822zone(_tzoffset(self._tz, self._t))
return '%s, %2.2d %s %d %2.2d:%2.2d:%2.2d %s' % ( return '%s, %2.2d %s %d %2.2d:%2.2d:%2.2d %s' % (
self._aday,self._day,self._amon,self._year, self._aday,self._day,self._amon,self._year,
self._hour,self._minute,self._nearsec,tzoffset) self._hour,self._minute,self._nearsec,tzoffset)
...@@ -1558,51 +1645,57 @@ class DateTime: ...@@ -1558,51 +1645,57 @@ class DateTime:
# New formats # New formats
def fCommon(self): def fCommon(self):
"""Return a string representing the object\'s value """Return a string representing the object\'s value
in the format: March 1, 1997 1:45 pm""" in the format: March 1, 1997 1:45 pm.
"""
return '%s %s, %4.4d %s:%2.2d %s' % ( return '%s %s, %4.4d %s:%2.2d %s' % (
self._fmon,self._day,self._year,self._pmhour, self._fmon,self._day,self._year,self._pmhour,
self._minute,self._pm) self._minute,self._pm)
def fCommonZ(self): def fCommonZ(self):
"""Return a string representing the object\'s value """Return a string representing the object\'s value
in the format: March 1, 1997 1:45 pm US/Eastern""" in the format: March 1, 1997 1:45 pm US/Eastern.
"""
return '%s %s, %4.4d %d:%2.2d %s %s' % ( return '%s %s, %4.4d %d:%2.2d %s %s' % (
self._fmon,self._day,self._year,self._pmhour, self._fmon,self._day,self._year,self._pmhour,
self._minute,self._pm,self._tz) self._minute,self._pm,self._tz)
def aCommon(self): def aCommon(self):
"""Return a string representing the object\'s value """Return a string representing the object\'s value
in the format: Mar 1, 1997 1:45 pm""" in the format: Mar 1, 1997 1:45 pm.
"""
return '%s %s, %4.4d %s:%2.2d %s' % ( return '%s %s, %4.4d %s:%2.2d %s' % (
self._amon,self._day,self._year,self._pmhour, self._amon,self._day,self._year,self._pmhour,
self._minute,self._pm) self._minute,self._pm)
def aCommonZ(self): def aCommonZ(self):
"""Return a string representing the object\'s value """Return a string representing the object\'s value
in the format: Mar 1, 1997 1:45 pm US/Eastern""" in the format: Mar 1, 1997 1:45 pm US/Eastern.
"""
return '%s %s, %4.4d %d:%2.2d %s %s' % ( return '%s %s, %4.4d %d:%2.2d %s %s' % (
self._amon,self._day,self._year,self._pmhour, self._amon,self._day,self._year,self._pmhour,
self._minute,self._pm,self._tz) self._minute,self._pm,self._tz)
def pCommon(self): def pCommon(self):
"""Return a string representing the object\'s value """Return a string representing the object\'s value
in the format: Mar. 1, 1997 1:45 pm""" in the format: Mar. 1, 1997 1:45 pm.
"""
return '%s %s, %4.4d %s:%2.2d %s' % ( return '%s %s, %4.4d %s:%2.2d %s' % (
self._pmon,self._day,self._year,self._pmhour, self._pmon,self._day,self._year,self._pmhour,
self._minute,self._pm) self._minute,self._pm)
def pCommonZ(self): def pCommonZ(self):
"""Return a string representing the object\'s value """Return a string representing the object\'s value
in the format: Mar. 1, 1997 1:45 pm US/Eastern""" in the format: Mar. 1, 1997 1:45 pm US/Eastern.
"""
return '%s %s, %4.4d %d:%2.2d %s %s' % ( return '%s %s, %4.4d %d:%2.2d %s %s' % (
self._pmon,self._day,self._year,self._pmhour, self._pmon,self._day,self._year,self._pmhour,
self._minute,self._pm,self._tz) self._minute,self._pm,self._tz)
def ISO(self): def ISO(self):
"""Return the object in ISO standard format. Note: """Return the object in ISO standard format.
this is *not* ISO 8601-format! See the ISO8601 and
HTML4 methods below for ISO 8601-compliant output Note: this is *not* ISO 8601-format! See the ISO8601 and
HTML4 methods below for ISO 8601-compliant output.
Dates are output as: YYYY-MM-DD HH:MM:SS Dates are output as: YYYY-MM-DD HH:MM:SS
""" """
...@@ -1611,33 +1704,33 @@ class DateTime: ...@@ -1611,33 +1704,33 @@ class DateTime:
self._hour, self._minute, self._second) self._hour, self._minute, self._second)
def ISO8601(self): def ISO8601(self):
"""Return the object in ISO 8601-compatible format containing """Return the object in ISO 8601-compatible format containing the
the date, time with seconds-precision and the time zone date, time with seconds-precision and the time zone identifier.
identifier - see http://www.w3.org/TR/NOTE-datetime
See: http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSTZD Dates are output as: YYYY-MM-DDTHH:MM:SSTZD
T is a literal character. T is a literal character.
TZD is Time Zone Designator, format +HH:MM or -HH:MM TZD is Time Zone Designator, format +HH:MM or -HH:MM
The HTML4 method below offers the same formatting, but converts The HTML4 method below offers the same formatting, but converts
to UTC before returning the value and sets the TZD "Z" to UTC before returning the value and sets the TZD "Z".
""" """
tzoffset = _tzoffset2iso8601zone(_tzoffset(self._tz, self._t)) tzoffset = _tzoffset2iso8601zone(_tzoffset(self._tz, self._t))
return "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d%s" % (
return "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d%s" % (
self._year, self._month, self._day, self._year, self._month, self._day,
self._hour, self._minute, self._second, tzoffset) self._hour, self._minute, self._second, tzoffset)
def HTML4(self): def HTML4(self):
"""Return the object in the format used in the HTML4.0 specification, """Return the object in the format used in the HTML4.0 specification,
one of the standard forms in ISO8601. See one of the standard forms in ISO8601.
http://www.w3.org/TR/NOTE-datetime
See: http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSZ Dates are output as: YYYY-MM-DDTHH:MM:SSZ
T, Z are literal characters. T, Z are literal characters.
The time is in UTC. The time is in UTC.
""" """
newdate = self.toZone('UTC') newdate = self.toZone('UTC')
return "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2dZ" % ( return "%0.4d-%0.2d-%0.2dT%0.2d:%0.2d:%0.2dZ" % (
newdate._year, newdate._month, newdate._day, newdate._year, newdate._month, newdate._day,
...@@ -1645,7 +1738,8 @@ class DateTime: ...@@ -1645,7 +1738,8 @@ class DateTime:
def __add__(self,other): def __add__(self,other):
"""A DateTime may be added to a number and a number may be """A DateTime may be added to a number and a number may be
added to a DateTime; two DateTimes cannot be added.""" added to a DateTime; two DateTimes cannot be added.
"""
if hasattr(other,'_t'): if hasattr(other,'_t'):
raise DateTimeError,'Cannot add two DateTimes' raise DateTimeError,'Cannot add two DateTimes'
o=float(other) o=float(other)
...@@ -1657,12 +1751,14 @@ class DateTime: ...@@ -1657,12 +1751,14 @@ class DateTime:
x = _calcDependentSecond(tz, t) x = _calcDependentSecond(tz, t)
yr,mo,dy,hr,mn,sc = _calcYMDHMS(x, ms) yr,mo,dy,hr,mn,sc = _calcYMDHMS(x, ms)
return self.__class__(yr,mo,dy,hr,mn,sc,self._tz,t,d,s) return self.__class__(yr,mo,dy,hr,mn,sc,self._tz,t,d,s)
__radd__=__add__ __radd__=__add__
def __sub__(self,other): def __sub__(self,other):
"""Either a DateTime or a number may be subtracted from a """Either a DateTime or a number may be subtracted from a
DateTime, however, a DateTime may not be subtracted from DateTime, however, a DateTime may not be subtracted from
a number.""" a number.
"""
if hasattr(other, '_d'): if hasattr(other, '_d'):
if 0: # This logic seems right but is incorrect. if 0: # This logic seems right but is incorrect.
my_t = self._t + _tzoffset(self._tz, self._t) my_t = self._t + _tzoffset(self._tz, self._t)
...@@ -1673,14 +1769,15 @@ class DateTime: ...@@ -1673,14 +1769,15 @@ class DateTime:
return self.__add__(-(other)) return self.__add__(-(other))
def __repr__(self): def __repr__(self):
"""Convert a DateTime to a string that """Convert a DateTime to a string that looks like a Python
looks like a Python expression.""" expression.
"""
return '%s(\'%s\')' % (self.__class__.__name__,str(self)) return '%s(\'%s\')' % (self.__class__.__name__,str(self))
def __str__(self): def __str__(self):
"""Convert a DateTime to a string.""" """Convert a DateTime to a string."""
y,m,d =self._year,self._month,self._day y,m,d = self._year,self._month,self._day
h,mn,s,t=self._hour,self._minute,self._second,self._tz h,mn,s,t = self._hour,self._minute,self._second,self._tz
if h == mn == s == 0: if h == mn == s == 0:
# hh:mm:ss all zero -- suppress the time. # hh:mm:ss all zero -- suppress the time.
return '%4.4d/%2.2d/%2.2d' % (y, m, d) return '%4.4d/%2.2d/%2.2d' % (y, m, d)
...@@ -1696,15 +1793,17 @@ class DateTime: ...@@ -1696,15 +1793,17 @@ class DateTime:
y, m, d, h, mn, s, t) y, m, d, h, mn, s, t)
def __cmp__(self,obj): def __cmp__(self,obj):
"""Compare a DateTime with another DateTime object, or """Compare a DateTime with another DateTime object, or a
a float such as those returned by time.time(). float such as those returned by time.time().
NOTE: __cmp__ support is provided for backward NOTE: __cmp__ support is provided for backward compatibility
compatibility only, and mixing DateTimes with only, and mixing DateTimes with ExtensionClasses could cause
ExtensionClasses could cause __cmp__ to break. __cmp__ to break.
You should use the methods lessThan, greaterThan,
lessThanEqualTo, greaterThanEqualTo, equalTo and You should use the methods lessThan, greaterThan, lessThanEqualTo,
notEqualTo to avoid potential problems later!!""" greaterThanEqualTo, equalTo and notEqualTo to avoid potential
problems later!
"""
# Optimized for sorting speed. # Optimized for sorting speed.
try: try:
return cmp(self._millis, obj._millis) return cmp(self._millis, obj._millis)
...@@ -1714,20 +1813,20 @@ class DateTime: ...@@ -1714,20 +1813,20 @@ class DateTime:
return cmp(self._t,obj) return cmp(self._t,obj)
def __hash__(self): def __hash__(self):
"""Compute a hash value for a DateTime""" """Compute a hash value for a DateTime."""
return int(((self._year%100*12+self._month)*31+ return int(((self._year%100*12+self._month)*31+
self._day+self.time)*100) self._day+self.time)*100)
def __int__(self): def __int__(self):
"""Convert to an integer number of seconds since the epoch (gmt)""" """Convert to an integer number of seconds since the epoch (gmt)."""
return int(self.millis() / 1000) return int(self.millis() / 1000)
def __long__(self): def __long__(self):
"""Convert to a long-int number of seconds since the epoch (gmt)""" """Convert to a long-int number of seconds since the epoch (gmt)."""
return long(self.millis() / 1000) return long(self.millis() / 1000)
def __float__(self): def __float__(self):
"""Convert to floating-point number of seconds since the epoch (gmt)""" """Convert to floating-point number of seconds since the epoch (gmt)."""
return float(self._t) return float(self._t)
def _parse_iso8601(self,s): def _parse_iso8601(self,s):
...@@ -1738,56 +1837,75 @@ class DateTime: ...@@ -1738,56 +1837,75 @@ class DateTime:
'Not an ISO 8601 compliant date string: "%s"' % s) 'Not an ISO 8601 compliant date string: "%s"' % s)
def __parse_iso8601(self,s): def __parse_iso8601(self,s):
""" parse an ISO 8601 compliant date """ """Parse an ISO 8601 compliant date.
year=0
month=day=1
hour=minute=seconds=hour_off=min_off=0
datereg = re.compile('([0-9]{4})(-([0-9][0-9]))?(-([0-9][0-9]))?')
timereg = re.compile('T([0-9]{2})(:([0-9][0-9]))?(:([0-9][0-9]))?(\.[0-9]{1,20})?')
zonereg = re.compile('([+-][0-9][0-9])(:?([0-9][0-9]))')
# Date part
fields = datereg.split(s.strip(), 1)
if fields[1]: year = int(fields[1])
if fields[3]: month = int(fields[3])
if fields[5]: day = int(fields[5])
t = fields[6]
if t:
if not fields[5]:
# Specifying time requires specifying a day.
raise IndexError
fields = timereg.split(t)
if fields[1]: hour = int(fields[1])
if fields[3]: minute = int(fields[3])
if fields[5]: seconds = int(fields[5])
if fields[6]: seconds = seconds+float(fields[6])
z = fields[7]
if z and z.startswith('Z'):
# Waaaa! This is wrong, since 'Z' and '+HH:MM'
# are supposed to be mutually exclusive.
# It's only here to prevent breaking 2.7 beta.
z = z[1:]
if z:
fields = zonereg.split(z)
hour_off = int(fields[1])
min_off = int(fields[3])
if fields[4]:
# Garbage after time zone
raise IndexError
return year,month,day,hour,minute,seconds,'GMT%+03d%02d' % (hour_off,min_off)
def JulianDay(self): See: http://www.omg.org/docs/ISO-stds/06-08-01.pdf
""" """
Return the Julian day according to month = day = week_day = 1
http://www.tondering.dk/claus/cal/node3.html#sec-calcjd year = hour = minute = seconds = hour_off = min_off = 0
iso8601 = iso8601Match(s.strip())
fields = iso8601 and iso8601.groupdict() or {}
if not iso8601 or fields.get('garbage'):
raise IndexError
if fields['year']:
year = int(fields['year'])
if fields['month']:
month = int(fields['month'])
if fields['day']:
day = int(fields['day'])
if fields['year_day']:
d = DateTime('%s-01-01' % year) + int(fields['year_day']) - 1
month = d.month()
day = d.day()
if fields['week']:
week = int(fields['week'])
if fields['week_day']:
week_day = int(fields['week_day'])
d = DateTime('%s-01-04' % year)
d = d - (d.dow()+6) % 7 + week * 7 + week_day - 8
month = d.month()
day = d.day()
if fields['hour']:
hour = int(fields['hour'])
if fields['minute']:
minute = int(fields['minute'])
elif fields['fraction']:
minute = 60.0 * float('0.%s' % fields['fraction'])
seconds, minute = math.modf(minute)
minute = int(minute)
seconds = 60.0 * seconds
# Avoid reprocess when handling seconds, bellow
fields['fraction'] = None
if fields['second']:
seconds = int(fields['second'])
if fields['fraction']:
seconds = seconds + float('0.%s' % fields['fraction'])
elif fields['fraction']:
seconds = 60.0 * float('0.%s' % fields['fraction'])
if fields['hour_off']:
hour_off = int(fields['hour_off'])
if fields['signal'] == '-':
hour_off *= -1
if fields['min_off']:
min_off = int(fields['min_off'])
tz = 'GMT%+03d%02d' % (hour_off, min_off)
return year, month, day, hour, minute, seconds, tz
def JulianDay(self):
"""Return the Julian day.
See: http://www.tondering.dk/claus/cal/node3.html#sec-calcjd
""" """
a = (14 - self._month)/12 #integer division, discard remainder a = (14 - self._month)/12 #integer division, discard remainder
y = self._year + 4800 - a y = self._year + 4800 - a
...@@ -1795,9 +1913,9 @@ class DateTime: ...@@ -1795,9 +1913,9 @@ class DateTime:
return self._day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 - 32045 return self._day + (153*m+2)/5 + 365*y + y/4 - y/100 + y/400 - 32045
def week(self): def week(self):
""" """Return the week number according to ISO.
Return the week number according to ISO
see http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000 See: http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000
""" """
J = self.JulianDay() J = self.JulianDay()
d4 = (J + 31741 - (J % 7)) % 146097 % 36524 % 1461 d4 = (J + 31741 - (J % 7)) % 146097 % 36524 % 1461
...@@ -1806,9 +1924,7 @@ class DateTime: ...@@ -1806,9 +1924,7 @@ class DateTime:
return d1/7 + 1 return d1/7 + 1
def encode(self, out): def encode(self, out):
""" """Encode value for XML-RPC."""
Encode value for XML-RPC
"""
out.write('<value><dateTime.iso8601>') out.write('<value><dateTime.iso8601>')
out.write(self.ISO8601()) out.write(self.ISO8601())
out.write('</dateTime.iso8601></value>\n') out.write('</dateTime.iso8601></value>\n')
...@@ -1820,11 +1936,11 @@ class strftimeFormatter: ...@@ -1820,11 +1936,11 @@ class strftimeFormatter:
self._dt=dt self._dt=dt
self._f=format self._f=format
def __call__(self): return self._dt.strftime(self._f) def __call__(self):
return self._dt.strftime(self._f)
# Module methods # Module methods
def Timezones(): def Timezones():
"""Return the list of recognized timezone names""" """Return the list of recognized timezone names"""
return _cache._zlst return _cache._zlst
##############################################################################
# To run these tests, use: #
# python unittest.py DateTime.tests.suite # Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import math import math
import os import os
...@@ -38,30 +48,30 @@ class DateTimeTests(unittest.TestCase): ...@@ -38,30 +48,30 @@ class DateTimeTests(unittest.TestCase):
math.floor(dt2.time * 86400000.0)) math.floor(dt2.time * 86400000.0))
def testBug1203(self): def testBug1203(self):
'''01:59:60 occurred in old DateTime''' # 01:59:60 occurred in old DateTime
dt = DateTime(7200, 'GMT') dt = DateTime(7200, 'GMT')
self.assert_(str(dt).find('60') < 0, dt) self.assert_(str(dt).find('60') < 0, dt)
def testDSTInEffect(self): def testDSTInEffect(self):
'''Checks GMT offset for a DST date in the US/Eastern time zone''' # Checks GMT offset for a DST date in the US/Eastern time zone
dt = DateTime(2000, 5, 9, 15, 0, 0, 'US/Eastern') dt = DateTime(2000, 5, 9, 15, 0, 0, 'US/Eastern')
self.assertEqual(dt.toZone('GMT').hour(), 19, self.assertEqual(dt.toZone('GMT').hour(), 19,
(dt, dt.toZone('GMT'))) (dt, dt.toZone('GMT')))
def testDSTNotInEffect(self): def testDSTNotInEffect(self):
'''Checks GMT offset for a non-DST date in the US/Eastern time zone''' # Checks GMT offset for a non-DST date in the US/Eastern time zone
dt = DateTime(2000, 11, 9, 15, 0, 0, 'US/Eastern') dt = DateTime(2000, 11, 9, 15, 0, 0, 'US/Eastern')
self.assertEqual(dt.toZone('GMT').hour(), 20, self.assertEqual(dt.toZone('GMT').hour(), 20,
(dt, dt.toZone('GMT'))) (dt, dt.toZone('GMT')))
def testAddPrecision(self): def testAddPrecision(self):
'''Precision of serial additions''' # Precision of serial additions
dt = DateTime() dt = DateTime()
self.assertEqual(str(dt + 0.10 + 3.14 + 6.76 - 10), str(dt), self.assertEqual(str(dt + 0.10 + 3.14 + 6.76 - 10), str(dt),
dt) dt)
def testConstructor3(self): def testConstructor3(self):
'''Constructor from date/time string''' # Constructor from date/time string
dt = DateTime() dt = DateTime()
dt1s = '%d/%d/%d %d:%d:%f %s' % ( dt1s = '%d/%d/%d %d:%d:%f %s' % (
dt.year(), dt.year(),
...@@ -77,19 +87,19 @@ class DateTimeTests(unittest.TestCase): ...@@ -77,19 +87,19 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(repr(dt),repr(dt1)) self.assertEqual(repr(dt),repr(dt1))
def testConstructor4(self): def testConstructor4(self):
'''Constructor from time float''' # Constructor from time float
dt = DateTime() dt = DateTime()
dt1 = DateTime(float(dt)) dt1 = DateTime(float(dt))
self._compare(dt,dt1) self._compare(dt,dt1)
def testConstructor5(self): def testConstructor5(self):
'''Constructor from time float and timezone''' # Constructor from time float and timezone
dt = DateTime() dt = DateTime()
dt1 = DateTime(float(dt), dt.timezone()) dt1 = DateTime(float(dt), dt.timezone())
self.assertEqual(str(dt), str(dt1), (dt, dt1)) self.assertEqual(str(dt), str(dt1), (dt, dt1))
def testConstructor6(self): def testConstructor6(self):
'''Constructor from year and julian date''' # Constructor from year and julian date
# This test must normalize the time zone, or it *will* break when # This test must normalize the time zone, or it *will* break when
# DST changes! # DST changes!
dt1 = DateTime(2000, 5.500000578705) dt1 = DateTime(2000, 5.500000578705)
...@@ -97,7 +107,7 @@ class DateTimeTests(unittest.TestCase): ...@@ -97,7 +107,7 @@ class DateTimeTests(unittest.TestCase):
self._compare(dt, dt1) self._compare(dt, dt1)
def testConstructor7(self): def testConstructor7(self):
'''Constructor from parts''' # Constructor from parts
dt = DateTime() dt = DateTime()
dt1 = DateTime( dt1 = DateTime(
dt.year(), dt.year(),
...@@ -112,19 +122,19 @@ class DateTimeTests(unittest.TestCase): ...@@ -112,19 +122,19 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(repr(dt), repr(dt1)) self.assertEqual(repr(dt), repr(dt1))
def testDayOfWeek(self): def testDayOfWeek(self):
'''strftime() used to always be passed a day of week of 0.''' # strftime() used to always be passed a day of week of 0
dt = DateTime('2000/6/16') dt = DateTime('2000/6/16')
s = dt.strftime('%A') s = dt.strftime('%A')
self.assertEqual(s, 'Friday', (dt, s)) self.assertEqual(s, 'Friday', (dt, s))
def testOldDate(self): def testOldDate(self):
'''Fails when an 1800 date is displayed with negative signs''' # Fails when an 1800 date is displayed with negative signs
dt = DateTime('1830/5/6 12:31:46.213 pm') dt = DateTime('1830/5/6 12:31:46.213 pm')
dt1 = dt.toZone('GMT+6') dt1 = dt.toZone('GMT+6')
self.assert_(str(dt1).find('-') < 0, (dt, dt1)) self.assert_(str(dt1).find('-') < 0, (dt, dt1))
def testSubtraction(self): def testSubtraction(self):
'''Reconstruction of a DateTime from its parts, with subtraction''' # Reconstruction of a DateTime from its parts, with subtraction
dt = DateTime() dt = DateTime()
dt1 = dt - 3.141592653 dt1 = dt - 3.141592653
dt2 = DateTime( dt2 = DateTime(
...@@ -138,25 +148,25 @@ class DateTimeTests(unittest.TestCase): ...@@ -138,25 +148,25 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(dt1, dt3, (dt, dt1, dt2, dt3)) self.assertEqual(dt1, dt3, (dt, dt1, dt2, dt3))
def testTZ1add(self): def testTZ1add(self):
'''Time zone manipulation: add to a date''' # Time zone manipulation: add to a date
dt = DateTime('1997/3/8 1:45am GMT-4') dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8') dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertEqual(dt + 1.0, dt1, (dt, dt1)) self.assertEqual(dt + 1.0, dt1, (dt, dt1))
def testTZ1sub(self): def testTZ1sub(self):
'''Time zone manipulation: subtract from a date''' # Time zone manipulation: subtract from a date
dt = DateTime('1997/3/8 1:45am GMT-4') dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8') dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertEqual(dt1 - 1.0, dt, (dt, dt1)) self.assertEqual(dt1 - 1.0, dt, (dt, dt1))
def testTZ1diff(self): def testTZ1diff(self):
'''Time zone manipulation: diff two dates''' # Time zone manipulation: diff two dates
dt = DateTime('1997/3/8 1:45am GMT-4') dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8') dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertEqual(dt1 - dt, 1.0, (dt, dt1)) self.assertEqual(dt1 - dt, 1.0, (dt, dt1))
def testCompareMethods(self): def testCompareMethods(self):
'''Compare two dates using several methods''' # Compare two dates using several methods
dt = DateTime('1997/1/1') dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2') dt1 = DateTime('1997/2/2')
self.failUnless(dt1.greaterThan(dt)) self.failUnless(dt1.greaterThan(dt))
...@@ -167,7 +177,7 @@ class DateTimeTests(unittest.TestCase): ...@@ -167,7 +177,7 @@ class DateTimeTests(unittest.TestCase):
self.failUnless(not dt.equalTo(dt1)) self.failUnless(not dt.equalTo(dt1))
def testCompareOperations(self, dt=None, dt1=None): def testCompareOperations(self, dt=None, dt1=None):
"""Compare two dates using several operations""" # Compare two dates using several operations
if dt is None: if dt is None:
dt = DateTime('1997/1/1') dt = DateTime('1997/1/1')
if dt1 is None: if dt1 is None:
...@@ -180,7 +190,7 @@ class DateTimeTests(unittest.TestCase): ...@@ -180,7 +190,7 @@ class DateTimeTests(unittest.TestCase):
self.failUnless(not (dt == dt1)) self.failUnless(not (dt == dt1))
def testUpgradeOldInstances(self): def testUpgradeOldInstances(self):
"""Compare dates that don't have the _millis attribute yet.""" # Compare dates that don't have the _millis attribute yet
dt = DateTime('1997/1/1') dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2') dt1 = DateTime('1997/2/2')
del dt._millis del dt._millis
...@@ -188,7 +198,7 @@ class DateTimeTests(unittest.TestCase): ...@@ -188,7 +198,7 @@ class DateTimeTests(unittest.TestCase):
self.testCompareOperations(dt, dt1) self.testCompareOperations(dt, dt1)
def testTZ2(self): def testTZ2(self):
'''Time zone manipulation test 2''' # Time zone manipulation test 2
dt = DateTime() dt = DateTime()
dt1 = dt.toZone('GMT') dt1 = dt.toZone('GMT')
s = dt.second() s = dt.second()
...@@ -196,13 +206,13 @@ class DateTimeTests(unittest.TestCase): ...@@ -196,13 +206,13 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(s, s1, (dt, dt1, s, s1)) self.assertEqual(s, s1, (dt, dt1, s, s1))
def testTZDiffDaylight(self): def testTZDiffDaylight(self):
'''Diff dates across daylight savings dates''' # Diff dates across daylight savings dates
dt = DateTime('2000/6/8 1:45am US/Eastern') dt = DateTime('2000/6/8 1:45am US/Eastern')
dt1 = DateTime('2000/12/8 12:45am US/Eastern') dt1 = DateTime('2000/12/8 12:45am US/Eastern')
self.assertEqual(dt1 - dt, 183, (dt, dt1, dt1 - dt)) self.assertEqual(dt1 - dt, 183, (dt, dt1, dt1 - dt))
def testY10KDate(self): def testY10KDate(self):
'''Comparison of a Y10K date and a Y2K date''' # Comparison of a Y10K date and a Y2K date
dt = DateTime('10213/09/21') dt = DateTime('10213/09/21')
dt1 = DateTime(2000, 1, 1) dt1 = DateTime(2000, 1, 1)
...@@ -212,7 +222,7 @@ class DateTimeTests(unittest.TestCase): ...@@ -212,7 +222,7 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(ddays, 3000000L, ddays) self.assertEqual(ddays, 3000000L, ddays)
def test_tzoffset(self): def test_tzoffset(self):
'''Test time-zone given as an offset''' # Test time-zone given as an offset
# GMT # GMT
dt = DateTime('Tue, 10 Sep 2001 09:41:03 GMT') dt = DateTime('Tue, 10 Sep 2001 09:41:03 GMT')
...@@ -247,38 +257,106 @@ class DateTimeTests(unittest.TestCase): ...@@ -247,38 +257,106 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(dt.tzoffset(), 15900) self.assertEqual(dt.tzoffset(), 15900)
def testISO8601(self): def testISO8601(self):
''' iso 8601 dates ''' # ISO8601 reference dates
ref0 = DateTime('2002/5/2 8:00am GMT') ref0 = DateTime('2002/5/2 8:00am GMT')
ref1 = DateTime('2002/5/2 8:00am US/Eastern') ref1 = DateTime('2002/5/2 8:00am US/Eastern')
ref2 = DateTime('2006/11/6 10:30 UTC')
ref3 = DateTime('2004/06/14 14:30:15 GMT-3')
ref4 = DateTime('2006/01/01 UTC')
# Basic tests
isoDt = DateTime('2002-05-02T08:00:00') isoDt = DateTime('2002-05-02T08:00:00')
self.assertEqual( ref0, isoDt) self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002-05-02T08:00:00Z') isoDt = DateTime('2002-05-02T08:00:00Z')
self.assertEqual( ref0, isoDt) self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002-05-02T08:00:00-04:00') isoDt = DateTime('2002-05-02T08:00:00-04:00')
self.assertEqual( ref1, isoDt) self.assertEqual(ref1, isoDt)
isoDt = DateTime('2002-05-02 08:00:00-04:00')
self.assertEqual(ref1, isoDt)
# Bug 1386: the colon in the timezone offset is optional # Bug 1386: the colon in the timezone offset is optional
isoDt = DateTime('2002-05-02T08:00:00-0400') isoDt = DateTime('2002-05-02T08:00:00-0400')
self.assertEqual( ref1, isoDt) self.assertEqual(ref1, isoDt)
dgood = '2002-05-02' # Bug 2191: date reduced formats
tgood = 'T08:00:00-04:00' isoDt = DateTime('2006-01-01')
for dbad in '2002-5-2', '2002-10-2', '2002-2-10', '02-2-10': self.assertEqual(ref4, isoDt)
self.assertRaises(DateTime.SyntaxError, DateTime, dbad) isoDt = DateTime('200601-01')
self.assertRaises(DateTime.SyntaxError, DateTime, dbad + tgood) self.assertEqual(ref4, isoDt)
for tbad in '08:00', 'T8:00': #, 'T08:00Z-04:00': isoDt = DateTime('20060101')
self.assertRaises(DateTime.SyntaxError, DateTime, dgood + tbad) self.assertEqual(ref4, isoDt)
isoDt = DateTime('2006-01')
self.assertEqual(ref4, isoDt)
isoDt = DateTime('200601')
self.assertEqual(ref4, isoDt)
isoDt = DateTime('2006')
self.assertEqual(ref4, isoDt)
# Bug 2191: date/time separators are also optional
isoDt = DateTime('20020502T08:00:00')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002-05-02T080000')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('20020502T080000')
self.assertEqual(ref0, isoDt)
# Bug 2191: timezones with only one digit for hour
isoDt = DateTime('20020502T080000+0')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('20020502 080000-4')
self.assertEqual(ref1, isoDt)
isoDt = DateTime('20020502T080000-400')
self.assertEqual(ref1, isoDt)
isoDt = DateTime('20020502T080000-4:00')
self.assertEqual(ref1, isoDt)
# Bug 2191: optional seconds/minutes
isoDt = DateTime('2002-05-02T0800')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002-05-02T08')
self.assertEqual(ref0, isoDt)
# Bug 2191: week format
isoDt = DateTime('2002-W18-4T0800')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002-W184T0800')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002W18-4T0800')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002W184T08')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('2004-W25-1T14:30:15-03:00')
self.assertEqual(ref3, isoDt)
isoDt = DateTime('2004-W25T14:30:15-03:00')
self.assertEqual(ref3, isoDt)
# Bug 2191: day of year format
isoDt = DateTime('2002-122T0800')
self.assertEqual(ref0, isoDt)
isoDt = DateTime('2002122T0800')
self.assertEqual(ref0, isoDt)
# Bug 2191: hours/minutes fractions
isoDt = DateTime('2006-11-06T10.5')
self.assertEqual(ref2, isoDt)
isoDt = DateTime('2006-11-06T10,5')
self.assertEqual(ref2, isoDt)
isoDt = DateTime('20040614T1430.25-3')
self.assertEqual(ref3, isoDt)
isoDt = DateTime('2004-06-14T1430,25-3')
self.assertEqual(ref3, isoDt)
isoDt = DateTime('2004-06-14T14:30.25-3')
self.assertEqual(ref3, isoDt)
isoDt = DateTime('20040614T14:30,25-3')
self.assertEqual(ref3, isoDt)
# ISO8601 standard format
iso8601_string = '2002-05-02T08:00:00-04:00' iso8601_string = '2002-05-02T08:00:00-04:00'
iso8601DT = DateTime(iso8601_string) iso8601DT = DateTime(iso8601_string)
self.assertEqual(iso8601_string, iso8601DT.ISO8601()) self.assertEqual(iso8601_string, iso8601DT.ISO8601())
def testJulianWeek(self): def testJulianWeek(self):
""" check JulianDayWeek function """ # Check JulianDayWeek function
try: try:
import gzip import gzip
except ImportError: except ImportError:
...@@ -300,7 +378,7 @@ class DateTimeTests(unittest.TestCase): ...@@ -300,7 +378,7 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(DateTime(d), d) self.assertEqual(DateTime(d), d)
def testRFC822(self): def testRFC822(self):
'''rfc822 conversion''' # rfc822 conversion
dt = DateTime('2002-05-02T08:00:00+00:00') dt = DateTime('2002-05-02T08:00:00+00:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0000') self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0000')
...@@ -315,9 +393,10 @@ class DateTimeTests(unittest.TestCase): ...@@ -315,9 +393,10 @@ class DateTimeTests(unittest.TestCase):
dts = dt.rfc822().split(' ') dts = dt.rfc822().split(' ')
times = dts[4].split(':') times = dts[4].split(':')
_isDST = time.localtime(time.time())[8] _isDST = time.localtime(time.time())[8]
if _isDST: offset = time.altzone if _isDST:
else: offset = time.timezone offset = time.altzone
else:
offset = time.timezone
self.assertEqual(dts[0], dt.aDay() + ',') self.assertEqual(dts[0], dt.aDay() + ',')
self.assertEqual(int(dts[1]), dt.day()) self.assertEqual(int(dts[1]), dt.day())
self.assertEqual(dts[2], dt.aMonth()) self.assertEqual(dts[2], dt.aMonth())
...@@ -327,14 +406,14 @@ class DateTimeTests(unittest.TestCase): ...@@ -327,14 +406,14 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(int(times[2]), int(dt.second())) self.assertEqual(int(times[2]), int(dt.second()))
self.assertEqual(dts[5], "%+03d%02d" % divmod( (-offset/60), 60) ) self.assertEqual(dts[5], "%+03d%02d" % divmod( (-offset/60), 60) )
def testInternationalDateformat(self): def testInternationalDateformat(self):
for year in range(1990, 2020): for year in range(1990, 2020):
for month in range (1,13): for month in range (1, 13):
for day in range(1,32): for day in range(1, 32):
try: d_us = DateTime("%d/%d/%d" % (year,month,day)) try:
except: continue d_us = DateTime("%d/%d/%d" % (year,month,day))
except:
continue
d_int = DateTime("%d.%d.%d" % (day,month,year), d_int = DateTime("%d.%d.%d" % (day,month,year),
datefmt="international") datefmt="international")
...@@ -351,9 +430,9 @@ class DateTimeTests(unittest.TestCase): ...@@ -351,9 +430,9 @@ class DateTimeTests(unittest.TestCase):
except DateTime.TimeError: except DateTime.TimeError:
self.fail('Zope Collector issue #484 (negative time bug): ' self.fail('Zope Collector issue #484 (negative time bug): '
'TimeError raised') 'TimeError raised')
def testStrftimeTZhandling(self): def testStrftimeTZhandling(self):
'''strftime timezone testing''' # strftime timezone testing
# This is a test for collector issue #1127 # This is a test for collector issue #1127
format = '%Y-%m-%d %H:%M %Z' format = '%Y-%m-%d %H:%M %Z'
dt = DateTime('Wed, 19 Nov 2003 18:32:07 -0215') dt = DateTime('Wed, 19 Nov 2003 18:32:07 -0215')
...@@ -363,14 +442,14 @@ class DateTimeTests(unittest.TestCase): ...@@ -363,14 +442,14 @@ class DateTimeTests(unittest.TestCase):
self.assertEqual(dt_string, dt_localstring) self.assertEqual(dt_string, dt_localstring)
def testStrftimeFarDates(self): def testStrftimeFarDates(self):
'''Checks strftime in dates <= 1900 or >= 2038''' # Checks strftime in dates <= 1900 or >= 2038
dt = DateTime('1900/01/30') dt = DateTime('1900/01/30')
self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/1900') self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/1900')
dt = DateTime('2040/01/30') dt = DateTime('2040/01/30')
self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/2040') self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/2040')
def testZoneInFarDates(self): def testZoneInFarDates(self):
'''Checks time zone in dates <= 1900 or >= 2038''' # Checks time zone in dates <= 1900 or >= 2038
dt1 = DateTime('2040/01/30 14:33 GMT+1') dt1 = DateTime('2040/01/30 14:33 GMT+1')
dt2 = DateTime('2040/01/30 11:33 GMT-2') dt2 = DateTime('2040/01/30 11:33 GMT-2')
self.assertEqual(dt1.strftime('%d/%m/%Y %H:%M'), self.assertEqual(dt1.strftime('%d/%m/%Y %H:%M'),
...@@ -381,6 +460,7 @@ class DateTimeTests(unittest.TestCase): ...@@ -381,6 +460,7 @@ class DateTimeTests(unittest.TestCase):
ok = dt.strftime('Le %d/%m/%Y a %Hh%M').replace('a', u'\xe0') ok = dt.strftime('Le %d/%m/%Y a %Hh%M').replace('a', u'\xe0')
self.assertEqual(dt.strftime(u'Le %d/%m/%Y \xe0 %Hh%M'), ok) self.assertEqual(dt.strftime(u'Le %d/%m/%Y \xe0 %Hh%M'), ok)
def test_suite(): def test_suite():
return unittest.makeSuite(DateTimeTests) return unittest.makeSuite(DateTimeTests)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment