added forced package imports

This commit is contained in:
Nicholas St. Germain 2018-12-02 13:38:37 -06:00
parent 0e2ffdbbb1
commit ef9022c6eb
943 changed files with 125530 additions and 16 deletions

1940
lib/DateTime/DateTime.py Normal file

File diff suppressed because it is too large Load diff

785
lib/DateTime/DateTime.txt Normal file
View file

@ -0,0 +1,785 @@
The DateTime package
====================
Encapsulation of date/time values.
Function Timezones()
--------------------
Returns the list of recognized timezone names:
>>> from DateTime import Timezones
>>> zones = set(Timezones())
Almost all of the standard pytz timezones are included, with the exception
of some commonly-used but ambiguous abbreviations, where historical Zope
usage conflicts with the name used by pytz:
>>> import pytz
>>> [x for x in pytz.all_timezones if x not in zones]
['CET', 'EET', 'EST', 'MET', 'MST', 'WET']
Class DateTime
--------------
DateTime objects represent instants in time and provide interfaces for
controlling its representation without affecting the absolute value of
the object.
DateTime objects may be created from a wide variety of string or
numeric data, or may be computed from other DateTime objects.
DateTimes support the ability to convert their representations to many
major timezones, as well as the ablility to create a DateTime object
in the context of a given timezone.
DateTime objects provide partial numerical behavior:
* Two date-time objects can be subtracted to obtain a time, in days
between the two.
* A date-time object and a positive or negative number may be added to
obtain a new date-time object that is the given number of days later
than the input date-time object.
* A positive or negative number and a date-time object may be added to
obtain a new date-time object that is the given number of days later
than the input date-time object.
* A positive or negative number may be subtracted from a date-time
object to obtain a new date-time object that is the given number of
days earlier than the input date-time object.
DateTime objects may be converted to integer, long, or float numbers
of days since January 1, 1901, using the standard int, long, and float
functions (Compatibility Note: int, long and float return the number
of days since 1901 in GMT rather than local machine timezone).
DateTime objects also provide access to their value in a float format
usable with the python time module, provided that the value of the
object falls in the range of the epoch-based time module.
A DateTime object should be considered immutable; all conversion and numeric
operations return a new DateTime object rather than modify the current object.
A DateTime object always maintains its value as an absolute UTC time,
and is represented in the context of some timezone based on the
arguments used to create the object. A DateTime object's methods
return values based on the timezone context.
Note that in all cases the local machine timezone is used for
representation if no timezone is specified.
Constructor for DateTime
------------------------
DateTime() returns a new date-time object. DateTimes may be created
with from zero to seven arguments:
* If the function is called with no arguments, then the current date/
time is returned, represented in the timezone of the local machine.
* If the function is invoked with a single string argument which is a
recognized timezone name, an object representing the current time is
returned, represented in the specified timezone.
* If the function is invoked with a single string argument
representing a valid date/time, an object representing that date/
time will be returned.
As a general rule, any date-time representation that is recognized
and unambigous to a resident of North America is acceptable. (The
reason for this qualification is that in North America, a date like:
2/1/1994 is interpreted as February 1, 1994, while in some parts of
the world, it is interpreted as January 2, 1994.) A date/ time
string consists of two components, a date component and an optional
time component, separated by one or more spaces. If the time
component is omited, 12:00am is assumed.
Any recognized timezone name specified as the final element of the
date/time string will be used for computing the date/time value.
(If you create a DateTime with the string,
"Mar 9, 1997 1:45pm US/Pacific", the value will essentially be the
same as if you had captured time.time() at the specified date and
time on a machine in that timezone). If no timezone is passed, then
the timezone configured on the local machine will be used, **except**
that if the date format matches ISO 8601 ('YYYY-MM-DD'), the instance
will use UTC / CMT+0 as the timezone.
o Returns current date/time, represented in US/Eastern:
>>> from DateTime import DateTime
>>> e = DateTime('US/Eastern')
>>> e.timezone()
'US/Eastern'
o Returns specified time, represented in local machine zone:
>>> x = DateTime('1997/3/9 1:45pm')
>>> x.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, ...)
o Specified time in local machine zone, verbose format:
>>> y = DateTime('Mar 9, 1997 13:45:00')
>>> y.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, ...)
>>> y == x
True
o Specified time in UTC via ISO 8601 rule:
>>> z = DateTime('2014-03-24')
>>> z.parts() # doctest: +ELLIPSIS
(2014, 3, 24, 0, 0, ...)
>>> z.timezone()
'GMT+0'
The date component consists of year, month, and day values. The
year value must be a one-, two-, or four-digit integer. If a one-
or two-digit year is used, the year is assumed to be in the
twentieth century. The month may an integer, from 1 to 12, a month
name, or a month abreviation, where a period may optionally follow
the abreviation. The day must be an integer from 1 to the number of
days in the month. The year, month, and day values may be separated
by periods, hyphens, forward, shashes, or spaces. Extra spaces are
permitted around the delimiters. Year, month, and day values may be
given in any order as long as it is possible to distinguish the
components. If all three components are numbers that are less than
13, then a a month-day-year ordering is assumed.
The time component consists of hour, minute, and second values
separated by colons. The hour value must be an integer between 0
and 23 inclusively. The minute value must be an integer between 0
and 59 inclusively. The second value may be an integer value
between 0 and 59.999 inclusively. The second value or both the
minute and second values may be ommitted. The time may be followed
by am or pm in upper or lower case, in which case a 12-hour clock is
assumed.
* If the DateTime function is invoked with a single Numeric argument,
the number is assumed to be either a floating point value such as
that returned by time.time() , or a number of days after January 1,
1901 00:00:00 UTC.
A DateTime object is returned that represents either the gmt value
of the time.time() float represented in the local machine's
timezone, or that number of days after January 1, 1901. Note that
the number of days after 1901 need to be expressed from the
viewpoint of the local machine's timezone. A negative argument will
yield a date-time value before 1901.
* 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 days from the beginning of the given
year, represented in the timezone of the local machine. The offset
may be positive or negative. Two-digit years are assumed to be in
the twentieth century.
* If the function is invoked with two arguments, the first a float
representing a number of seconds past the epoch in gmt (such as
those returned by time.time()) and the second a string naming a
recognized timezone, a DateTime with a value of that gmt time will
be returned, represented in the given timezone.
>>> import time
>>> t = time.time()
Time t represented as US/Eastern:
>>> now_east = DateTime(t, 'US/Eastern')
Time t represented as US/Pacific:
>>> now_west = DateTime(t, 'US/Pacific')
Only their representations are different:
>>> now_east.equalTo(now_west)
True
* If the function is invoked with three or more numeric arguments,
then the first is taken to be an integer year, the second is taken
to be an integer month, and the third is taken to be an integer day.
If the combination of values is not valid, then a DateTimeError is
raised. One- or two-digit years up to 69 are assumed to be in the
21st century, whereas values 70-99 are assumed to be 20th century.
The fourth, fifth, and sixth arguments are floating point, positive
or negative offsets in units of hours, minutes, and days, and
default to zero if not given. An optional string may be given as
the final argument to indicate timezone (the effect of this is as if
you had taken the value of time.time() at that time on a machine in
the specified timezone).
If a string argument passed to the DateTime constructor cannot be
parsed, it will raise SyntaxError. Invalid date, time, or
timezone components will raise a DateTimeError.
The module function Timezones() will return a list of the timezones
recognized by the DateTime module. Recognition of timezone names is
case-insensitive.
Instance Methods for DateTime (IDateTime interface)
---------------------------------------------------
Conversion and comparison methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ``timeTime()`` returns the date/time as a floating-point number in
UTC, 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 time module, and in such cases a
DateTimeError is raised. A DateTime object's value must generally
be between Jan 1, 1970 (or your local machine epoch) and Jan 2038 to
produce a valid time.time() style value.
>>> dt = DateTime('Mar 9, 1997 13:45:00 US/Eastern')
>>> dt.timeTime()
857933100.0
>>> DateTime('2040/01/01 UTC').timeTime()
2208988800.0
>>> DateTime('1900/01/01 UTC').timeTime()
-2208988800.0
* ``toZone(z)`` returns a DateTime with the value as the current
object, represented in the indicated timezone:
>>> dt.toZone('UTC')
DateTime('1997/03/09 18:45:00 UTC')
>>> dt.toZone('UTC').equalTo(dt)
True
* ``isFuture()`` returns true if this object represents a date/time
later than the time of the call:
>>> dt.isFuture()
False
>>> DateTime('Jan 1 3000').isFuture() # not time-machine safe!
True
* ``isPast()`` returns true if this object represents a date/time
earlier than the time of the call:
>>> dt.isPast()
True
>>> DateTime('Jan 1 3000').isPast() # not time-machine safe!
False
* ``isCurrentYear()`` returns true if this object represents a
date/time that falls within the current year, in the context of this
object's timezone representation:
>>> dt.isCurrentYear()
False
>>> DateTime().isCurrentYear()
True
* ``isCurrentMonth()`` returns true if this object represents a
date/time that falls within the current month, in the context of
this object's timezone representation:
>>> dt.isCurrentMonth()
False
>>> DateTime().isCurrentMonth()
True
* ``isCurrentDay()`` returns true if this object represents a
date/time that falls within the current day, in the context of this
object's timezone representation:
>>> dt.isCurrentDay()
False
>>> DateTime().isCurrentDay()
True
* ``isCurrentHour()`` returns true if this object represents a
date/time that falls within the current hour, in the context of this
object's timezone representation:
>>> dt.isCurrentHour()
False
>>> DateTime().isCurrentHour()
True
* ``isCurrentMinute()`` returns true if this object represents a
date/time that falls within the current minute, in the context of
this object's timezone representation:
>>> dt.isCurrentMinute()
False
>>> DateTime().isCurrentMinute()
True
* ``isLeapYear()`` returns true if the current year (in the context of
the object's timezone) is a leap year:
>>> dt.isLeapYear()
False
>>> DateTime('Mar 8 2004').isLeapYear()
True
* ``earliestTime()`` returns a new DateTime object that represents the
earliest possible time (in whole seconds) that still falls within
the current object's day, in the object's timezone context:
>>> dt.earliestTime()
DateTime('1997/03/09 00:00:00 US/Eastern')
* ``latestTime()`` return a new DateTime object that represents the
latest possible time (in whole seconds) that still falls within the
current object's day, in the object's timezone context
>>> dt.latestTime()
DateTime('1997/03/09 23:59:59 US/Eastern')
Component access
~~~~~~~~~~~~~~~~
* ``parts()`` returns a tuple containing the calendar year, month,
day, hour, minute second and timezone of the object
>>> dt.parts() # doctest: +ELLIPSIS
(1997, 3, 9, 13, 45, ... 'US/Eastern')
* ``timezone()`` returns the timezone in which the object is represented:
>>> dt.timezone() in Timezones()
True
* ``tzoffset()`` returns the timezone offset for the objects timezone:
>>> dt.tzoffset()
-18000
* ``year()`` returns the calendar year of the object:
>>> dt.year()
1997
* ``month()`` retursn the month of the object as an integer:
>>> dt.month()
3
* ``Month()`` returns the full month name:
>>> dt.Month()
'March'
* ``aMonth()`` returns the abreviated month name:
>>> dt.aMonth()
'Mar'
* ``pMonth()`` returns the abreviated (with period) month name:
>>> dt.pMonth()
'Mar.'
* ``day()`` returns the integer day:
>>> dt.day()
9
* ``Day()`` returns the full name of the day of the week:
>>> dt.Day()
'Sunday'
* ``dayOfYear()`` returns the day of the year, in context of the
timezone representation of the object:
>>> dt.dayOfYear()
68
* ``aDay()`` returns the abreviated name of the day of the week:
>>> dt.aDay()
'Sun'
* ``pDay()`` returns the abreviated (with period) name of the day of
the week:
>>> dt.pDay()
'Sun.'
* ``dow()`` returns the integer day of the week, where Sunday is 0:
>>> dt.dow()
0
* ``dow_1()`` returns the integer day of the week, where sunday is 1:
>>> dt.dow_1()
1
* ``h_12()`` returns the 12-hour clock representation of the hour:
>>> dt.h_12()
1
* ``h_24()`` returns the 24-hour clock representation of the hour:
>>> dt.h_24()
13
* ``ampm()`` returns the appropriate time modifier (am or pm):
>>> dt.ampm()
'pm'
* ``hour()`` returns the 24-hour clock representation of the hour:
>>> dt.hour()
13
* ``minute()`` returns the minute:
>>> dt.minute()
45
* ``second()`` returns the second:
>>> dt.second() == 0
True
* ``millis()`` returns the milliseconds since the epoch in GMT.
>>> dt.millis() == 857933100000
True
strftime()
~~~~~~~~~~
See ``tests/test_datetime.py``.
General formats from previous DateTime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* ``Date()`` return the date string for the object:
>>> dt.Date()
'1997/03/09'
* ``Time()`` returns the time string for an object to the nearest
second:
>>> dt.Time()
'13:45:00'
* ``TimeMinutes()`` returns the time string for an object not showing
seconds:
>>> dt.TimeMinutes()
'13:45'
* ``AMPM()`` returns the time string for an object to the nearest second:
>>> dt.AMPM()
'01:45:00 pm'
* ``AMPMMinutes()`` returns the time string for an object not showing
seconds:
>>> dt.AMPMMinutes()
'01:45 pm'
* ``PreciseTime()`` returns the time string for the object:
>>> dt.PreciseTime()
'13:45:00.000'
* ``PreciseAMPM()`` returns the time string for the object:
>>> dt.PreciseAMPM()
'01:45:00.000 pm'
* ``yy()`` returns the calendar year as a 2 digit string
>>> dt.yy()
'97'
* ``mm()`` returns the month as a 2 digit string
>>> dt.mm()
'03'
* ``dd()`` returns the day as a 2 digit string:
>>> dt.dd()
'09'
* ``rfc822()`` returns the date in RFC 822 format:
>>> dt.rfc822()
'Sun, 09 Mar 1997 13:45:00 -0500'
New formats
~~~~~~~~~~~
* ``fCommon()`` returns a string representing the object's value in
the format: March 9, 1997 1:45 pm:
>>> dt.fCommon()
'March 9, 1997 1:45 pm'
* ``fCommonZ()`` returns a string representing the object's value in
the format: March 9, 1997 1:45 pm US/Eastern:
>>> dt.fCommonZ()
'March 9, 1997 1:45 pm US/Eastern'
* ``aCommon()`` returns a string representing the object's value in
the format: Mar 9, 1997 1:45 pm:
>>> dt.aCommon()
'Mar 9, 1997 1:45 pm'
* ``aCommonZ()`` return a string representing the object's value in
the format: Mar 9, 1997 1:45 pm US/Eastern:
>>> dt.aCommonZ()
'Mar 9, 1997 1:45 pm US/Eastern'
* ``pCommon()`` returns a string representing the object's value in
the format Mar. 9, 1997 1:45 pm:
>>> dt.pCommon()
'Mar. 9, 1997 1:45 pm'
* ``pCommonZ()`` returns a string representing the object's value in
the format: Mar. 9, 1997 1:45 pm US/Eastern:
>>> dt.pCommonZ()
'Mar. 9, 1997 1:45 pm US/Eastern'
* ``ISO()`` returns a string with the date/time in ISO format. 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
>>> dt.ISO()
'1997-03-09 13:45:00'
* ``ISO8601()`` returns the object in ISO 8601-compatible format
containing the date, time with seconds-precision and the time zone
identifier - see http://www.w3.org/TR/NOTE-datetime. Dates are
output as: YYYY-MM-DDTHH:MM:SSTZD (T is a literal character, TZD is
Time Zone Designator, format +HH:MM or -HH:MM).
The ``HTML4()`` method below offers the same formatting, but
converts to UTC before returning the value and sets the TZD"Z"
>>> dt.ISO8601()
'1997-03-09T13:45:00-05:00'
* ``HTML4()`` returns the object in the format used in the HTML4.0
specification, one of the standard forms in ISO8601. See
http://www.w3.org/TR/NOTE-datetime. Dates are output as:
YYYY-MM-DDTHH:MM:SSZ (T, Z are literal characters, the time is in
UTC.):
>>> dt.HTML4()
'1997-03-09T18:45:00Z'
* ``JulianDay()`` returns the Julian day according to
http://www.tondering.dk/claus/cal/node3.html#sec-calcjd
>>> dt.JulianDay()
2450517
* ``week()`` returns the week number according to ISO
see http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000
>>> dt.week()
10
Deprecated API
~~~~~~~~~~~~~~
* DayOfWeek(): see Day()
* Day_(): see pDay()
* Mon(): see aMonth()
* Mon_(): see pMonth
General Services Provided by DateTime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DateTimes can be repr()'ed; the result will be a string indicating how
to make a DateTime object like this:
>>> repr(dt)
"DateTime('1997/03/09 13:45:00 US/Eastern')"
When we convert them into a string, we get a nicer string that could
actually be shown to a user:
>>> str(dt)
'1997/03/09 13:45:00 US/Eastern'
The hash value of a DateTime is based on the date and time and is
equal for different representations of the DateTime:
>>> hash(dt)
3618678
>>> hash(dt.toZone('UTC'))
3618678
DateTime objects can be compared to other DateTime objects OR floating
point numbers such as the ones which are returned by the python time
module by using the equalTo method. Using this API, True is returned if the
object represents a date/time equal to the specified DateTime or time module
style time:
>>> dt.equalTo(dt)
True
>>> dt.equalTo(dt.toZone('UTC'))
True
>>> dt.equalTo(dt.timeTime())
True
>>> dt.equalTo(DateTime())
False
Same goes for inequalities:
>>> dt.notEqualTo(dt)
False
>>> dt.notEqualTo(dt.toZone('UTC'))
False
>>> dt.notEqualTo(dt.timeTime())
False
>>> dt.notEqualTo(DateTime())
True
Normal equality operations only work with datetime objects and take the
timezone setting into account:
>>> dt == dt
True
>>> dt == dt.toZone('UTC')
False
>>> dt == DateTime()
False
>>> dt != dt
False
>>> dt != dt.toZone('UTC')
True
>>> dt != DateTime()
True
But the other comparison operations compare the referenced moment in time and
not the representation itself:
>>> dt > dt
False
>>> DateTime() > dt
True
>>> dt > DateTime().timeTime()
False
>>> DateTime().timeTime() > dt
True
>>> dt.greaterThan(dt)
False
>>> DateTime().greaterThan(dt)
True
>>> dt.greaterThan(DateTime().timeTime())
False
>>> dt >= dt
True
>>> DateTime() >= dt
True
>>> dt >= DateTime().timeTime()
False
>>> DateTime().timeTime() >= dt
True
>>> dt.greaterThanEqualTo(dt)
True
>>> DateTime().greaterThanEqualTo(dt)
True
>>> dt.greaterThanEqualTo(DateTime().timeTime())
False
>>> dt < dt
False
>>> DateTime() < dt
False
>>> dt < DateTime().timeTime()
True
>>> DateTime().timeTime() < dt
False
>>> dt.lessThan(dt)
False
>>> DateTime().lessThan(dt)
False
>>> dt.lessThan(DateTime().timeTime())
True
>>> dt <= dt
True
>>> DateTime() <= dt
False
>>> dt <= DateTime().timeTime()
True
>>> DateTime().timeTime() <= dt
False
>>> dt.lessThanEqualTo(dt)
True
>>> DateTime().lessThanEqualTo(dt)
False
>>> dt.lessThanEqualTo(DateTime().timeTime())
True
Numeric Services Provided by DateTime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A DateTime may be added to a number and a number may be added to a
DateTime:
>>> dt + 5
DateTime('1997/03/14 13:45:00 US/Eastern')
>>> 5 + dt
DateTime('1997/03/14 13:45:00 US/Eastern')
Two DateTimes cannot be added:
>>> from DateTime.interfaces import DateTimeError
>>> try:
... dt + dt
... print('fail')
... except DateTimeError:
... print('ok')
ok
Either a DateTime or a number may be subtracted from a DateTime,
however, a DateTime may not be subtracted from a number:
>>> DateTime('1997/03/10 13:45 US/Eastern') - dt
1.0
>>> dt - 1
DateTime('1997/03/08 13:45:00 US/Eastern')
>>> 1 - dt
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for -: 'int' and 'DateTime'
DateTimes can also be converted to integers (number of seconds since
the epoch) and floats:
>>> int(dt)
857933100
>>> float(dt)
857933100.0

17
lib/DateTime/__init__.py Normal file
View file

@ -0,0 +1,17 @@
##############################################################################
#
# Copyright (c) 2002 Zope Foundation and Contributors.
#
# 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
#
##############################################################################
from .DateTime import DateTime
from .DateTime import Timezones
__all__ = ('DateTime', 'Timezones')

375
lib/DateTime/interfaces.py Normal file
View file

@ -0,0 +1,375 @@
##############################################################################
#
# Copyright (c) 2005 Zope Foundation and Contributors.
#
# 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
#
##############################################################################
from zope.interface import Interface
class DateTimeError(Exception):
pass
class SyntaxError(DateTimeError):
pass
class DateError(DateTimeError):
pass
class TimeError(DateTimeError):
pass
class IDateTime(Interface):
# Conversion and comparison methods
#TODO determine whether this method really is part of the public API
def localZone(ltm=None):
'''Returns the time zone on the given date. The time zone
can change according to daylight savings.'''
def timeTime():
"""Return the date/time as a floating-point number in UTC, 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 time module."""
def toZone(z):
"""Return a DateTime with the value as the current object,
represented in the indicated timezone."""
def isFuture():
"""Return true if this object represents a date/time later
than the time of the call"""
def isPast():
"""Return true if this object represents a date/time earlier
than the time of the call"""
def isCurrentYear():
"""Return true if this object represents a date/time that
falls within the current year, in the context of this
object's timezone representation"""
def isCurrentMonth():
"""Return true if this object represents a date/time that
falls within the current month, in the context of this
object's timezone representation"""
def isCurrentDay():
"""Return true if this object represents a date/time that
falls within the current day, in the context of this object's
timezone representation"""
def isCurrentHour():
"""Return true if this object represents a date/time that
falls within the current hour, in the context of this object's
timezone representation"""
def isCurrentMinute():
"""Return true if this object represents a date/time that
falls within the current minute, in the context of this
object's timezone representation"""
def isLeapYear():
"""Return true if the current year (in the context of the
object's timezone) is a leap year"""
def earliestTime():
"""Return a new DateTime object that represents the earliest
possible time (in whole seconds) that still falls within the
current object's day, in the object's timezone context"""
def latestTime():
"""Return a new DateTime object that represents the latest
possible time (in whole seconds) that still falls within the
current object's day, in the object's timezone context"""
def greaterThan(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
python time module. Returns true if the object represents a
date/time greater than the specified DateTime or time module
style time. Revised to give more correct results through
comparison of long integer milliseconds."""
__gt__ = greaterThan
def greaterThanEqualTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
python time module. Returns true if the object represents a
date/time greater than or equal to the specified DateTime or
time module style time. Revised to give more correct results
through comparison of long integer milliseconds."""
__ge__ = greaterThanEqualTo
def equalTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
python time module. Returns true if the object represents a
date/time equal to the specified DateTime or time module style
time. Revised to give more correct results through comparison
of long integer milliseconds."""
__eq__ = equalTo
def notEqualTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
python time module. Returns true if the object represents a
date/time not equal to the specified DateTime or time module
style time. Revised to give more correct results through
comparison of long integer milliseconds."""
__ne__ = notEqualTo
def lessThan(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
python time module. Returns true if the object represents a
date/time less than the specified DateTime or time module
style time. Revised to give more correct results through
comparison of long integer milliseconds."""
__lt__ = lessThan
def lessThanEqualTo(t):
"""Compare this DateTime object to another DateTime object OR
a floating point number such as that which is returned by the
python time module. Returns true if the object represents a
date/time less than or equal to the specified DateTime or time
module style time. Revised to give more correct results
through comparison of long integer milliseconds."""
__le__ = lessThanEqualTo
# Component access
def parts():
"""Return a tuple containing the calendar year, month, day,
hour, minute second and timezone of the object"""
def timezone():
"""Return the timezone in which the object is represented."""
def tzoffset():
"""Return the timezone offset for the objects timezone."""
def year():
"""Return the calendar year of the object"""
def month():
"""Return the month of the object as an integer"""
def Month():
"""Return the full month name"""
def aMonth():
"""Return the abreviated month name."""
def Mon():
"""Compatibility: see aMonth"""
def pMonth():
"""Return the abreviated (with period) month name."""
def Mon_():
"""Compatibility: see pMonth"""
def day():
"""Return the integer day"""
def Day():
"""Return the full name of the day of the week"""
def DayOfWeek():
"""Compatibility: see Day"""
def dayOfYear():
"""Return the day of the year, in context of the timezone
representation of the object"""
def aDay():
"""Return the abreviated name of the day of the week"""
def pDay():
"""Return the abreviated (with period) name of the day of the
week"""
def Day_():
"""Compatibility: see pDay"""
def dow():
"""Return the integer day of the week, where sunday is 0"""
def dow_1():
"""Return the integer day of the week, where sunday is 1"""
def h_12():
"""Return the 12-hour clock representation of the hour"""
def h_24():
"""Return the 24-hour clock representation of the hour"""
def ampm():
"""Return the appropriate time modifier (am or pm)"""
def hour():
"""Return the 24-hour clock representation of the hour"""
def minute():
"""Return the minute"""
def second():
"""Return the second"""
def millis():
"""Return the millisecond since the epoch in GMT."""
def strftime(format):
"""Format the date/time using the *current timezone representation*."""
# General formats from previous DateTime
def Date():
"""Return the date string for the object."""
def Time():
"""Return the time string for an object to the nearest second."""
def TimeMinutes():
"""Return the time string for an object not showing seconds."""
def AMPM():
"""Return the time string for an object to the nearest second."""
def AMPMMinutes():
"""Return the time string for an object not showing seconds."""
def PreciseTime():
"""Return the time string for the object."""
def PreciseAMPM():
"""Return the time string for the object."""
def yy():
"""Return calendar year as a 2 digit string"""
def mm():
"""Return month as a 2 digit string"""
def dd():
"""Return day as a 2 digit string"""
def rfc822():
"""Return the date in RFC 822 format"""
# New formats
def fCommon():
"""Return a string representing the object's value in the
format: March 1, 1997 1:45 pm"""
def fCommonZ():
"""Return a string representing the object's value in the
format: March 1, 1997 1:45 pm US/Eastern"""
def aCommon():
"""Return a string representing the object's value in the
format: Mar 1, 1997 1:45 pm"""
def aCommonZ():
"""Return a string representing the object's value in the
format: Mar 1, 1997 1:45 pm US/Eastern"""
def pCommon():
"""Return a string representing the object's value in the
format: Mar. 1, 1997 1:45 pm"""
def pCommonZ():
"""Return a string representing the object's value
in the format: Mar. 1, 1997 1:45 pm US/Eastern"""
def ISO():
"""Return the object in ISO standard format. 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
"""
def ISO8601():
"""Return the object in ISO 8601-compatible format containing
the date, time with seconds-precision and the time zone
identifier - see http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSTZD
T is a literal character.
TZD is Time Zone Designator, format +HH:MM or -HH:MM
The HTML4 method below offers the same formatting, but
converts to UTC before returning the value and sets the TZD"Z"
"""
def HTML4():
"""Return the object in the format used in the HTML4.0
specification, one of the standard forms in ISO8601. See
http://www.w3.org/TR/NOTE-datetime
Dates are output as: YYYY-MM-DDTHH:MM:SSZ
T, Z are literal characters.
The time is in UTC.
"""
def JulianDay():
"""Return the Julian day according to
http://www.tondering.dk/claus/cal/node3.html#sec-calcjd
"""
def week():
"""Return the week number according to ISO
see http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000
"""
# Python operator and conversion API
def __add__(other):
"""A DateTime may be added to a number and a number may be
added to a DateTime; two DateTimes cannot be added."""
__radd__ = __add__
def __sub__(other):
"""Either a DateTime or a number may be subtracted from a
DateTime, however, a DateTime may not be subtracted from a
number."""
def __repr__():
"""Convert a DateTime to a string that looks like a Python
expression."""
def __str__():
"""Convert a DateTime to a string."""
def __hash__():
"""Compute a hash value for a DateTime"""
def __int__():
"""Convert to an integer number of seconds since the epoch (gmt)"""
def __long__():
"""Convert to a long-int number of seconds since the epoch (gmt)"""
def __float__():
"""Convert to floating-point number of seconds since the epoch (gmt)"""

192
lib/DateTime/pytz.txt Normal file
View file

@ -0,0 +1,192 @@
Pytz Support
============
Allows the pytz package to be used for time zone information. The
advantage of using pytz is that it has a more complete and up to date
time zone and daylight savings time database.
Usage
-----
You don't have to do anything special to make it work.
>>> from DateTime import DateTime, Timezones
>>> d = DateTime('March 11, 2007 US/Eastern')
Daylight Savings
----------------
In 2007 daylight savings time in the US was changed. The Energy Policy
Act of 2005 mandates that DST will start on the second Sunday in March
and end on the first Sunday in November.
In 2007, the start and stop dates are March 11 and November 4,
respectively. These dates are different from previous DST start and
stop dates. In 2006, the dates were the first Sunday in April (April
2, 2006) and the last Sunday in October (October 29, 2006).
Let's make sure that DateTime can deal with this, since the primary
motivation to use pytz for time zone information is the fact that it
is kept up to date with daylight savings changes.
>>> DateTime('March 11, 2007 US/Eastern').tzoffset()
-18000
>>> DateTime('March 12, 2007 US/Eastern').tzoffset()
-14400
>>> DateTime('November 4, 2007 US/Eastern').tzoffset()
-14400
>>> DateTime('November 5, 2007 US/Eastern').tzoffset()
-18000
Let's compare this to 2006.
>>> DateTime('April 2, 2006 US/Eastern').tzoffset()
-18000
>>> DateTime('April 3, 2006 US/Eastern').tzoffset()
-14400
>>> DateTime('October 29, 2006 US/Eastern').tzoffset()
-14400
>>> DateTime('October 30, 2006 US/Eastern').tzoffset()
-18000
Time Zones
---------
DateTime can use pytz's large database of time zones. Here are some
examples:
>>> d = DateTime('Pacific/Kwajalein')
>>> d = DateTime('America/Shiprock')
>>> d = DateTime('Africa/Ouagadougou')
Of course pytz doesn't know about everything.
>>> from DateTime.interfaces import SyntaxError
>>> try:
... d = DateTime('July 21, 1969 Moon/Eastern')
... print('fail')
... except SyntaxError:
... print('ok')
ok
You can still use zone names that DateTime defines that aren't part of
the pytz database.
>>> d = DateTime('eet')
>>> d = DateTime('iceland')
These time zones use DateTimes database. So it's preferable to use the
official time zone name.
One trickiness is that DateTime supports some zone name
abbreviations. Some of these map to pytz names, so these abbreviations
will give you time zone date from pytz. Notable among abbreviations
that work this way are 'est', 'cst', 'mst', and 'pst'.
Let's verify that 'est' picks up the 2007 daylight savings time changes.
>>> DateTime('March 11, 2007 est').tzoffset()
-18000
>>> DateTime('March 12, 2007 est').tzoffset()
-14400
>>> DateTime('November 4, 2007 est').tzoffset()
-14400
>>> DateTime('November 5, 2007 est').tzoffset()
-18000
You can get a list of time zones supported by calling the Timezones() function.
>>> Timezones() #doctest: +ELLIPSIS
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', ...]
Note that you can mess with this list without hurting things.
>>> t = Timezones()
>>> t.remove('US/Eastern')
>>> d = DateTime('US/Eastern')
Internal Components
-------------------
The following are tests of internal components.
Cache
~~~~~
The DateTime class uses a new time zone cache.
>>> from DateTime.DateTime import _TZINFO
>>> _TZINFO #doctest: +ELLIPSIS
<DateTime.pytz_support.PytzCache ...>
The cache maps time zone names to time zone instances.
>>> cache = _TZINFO
>>> tz = cache['GMT+730']
>>> tz = cache['US/Mountain']
The cache also must provide a few attributes for use by the DateTime
class.
The _zlst attribute is a list of supported time zone names.
>>> cache._zlst #doctest: +ELLIPSIS
['Africa/Abidjan'... 'Africa/Accra'... 'IDLE'... 'NZST'... 'NZT'...]
The _zidx attribute is a list of lower-case and possibly abbreviated
time zone names that can be mapped to offical zone names.
>>> 'australia/yancowinna' in cache._zidx
True
>>> 'europe/isle_of_man' in cache._zidx
True
>>> 'gmt+0500' in cache._zidx
True
Note that there are more items in _zidx than in _zlst since there are
multiple names for some time zones.
>>> len(cache._zidx) > len(cache._zlst)
True
Each entry in _zlst should also be present in _zidx in lower case form.
>>> for name in cache._zlst:
... if not name.lower() in cache._zidx:
... print("Error %s not in _zidx" % name.lower())
The _zmap attribute maps the names in _zidx to official names in _zlst.
>>> cache._zmap['africa/abidjan']
'Africa/Abidjan'
>>> cache._zmap['gmt+1']
'GMT+1'
>>> cache._zmap['gmt+0100']
'GMT+1'
>>> cache._zmap['utc']
'UTC'
Let's make sure that _zmap and _zidx agree.
>>> idx = set(cache._zidx)
>>> keys = set(cache._zmap.keys())
>>> idx == keys
True
Timezone objects
~~~~~~~~~~~~~~~~
The timezone instances have only one public method info(). It returns
a tuple of (offset, is_dst, name). The method takes a timestamp, which
is used to determine dst information.
>>> t1 = DateTime('November 4, 00:00 2007 US/Mountain').timeTime()
>>> t2 = DateTime('November 4, 02:00 2007 US/Mountain').timeTime()
>>> tz.info(t1)
(-21600, 1, 'MDT')
>>> tz.info(t2)
(-25200, 0, 'MST')
If you don't pass any arguments to info it provides daylight savings
time information as of today.
>>> tz.info() in ((-21600, 1, 'MDT'), (-25200, 0, 'MST'))
True

View file

@ -0,0 +1,259 @@
##############################################################################
#
# Copyright (c) 2007 Zope Foundation and Contributors.
#
# 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
#
##############################################################################
from datetime import datetime, timedelta
import pytz
import pytz.reference
from pytz.tzinfo import StaticTzInfo, memorized_timedelta
from .interfaces import DateTimeError
EPOCH = datetime.utcfromtimestamp(0).replace(tzinfo=pytz.utc)
_numeric_timezone_data = {
'GMT': ('GMT', 0, 1, [], '', [(0, 0, 0)], 'GMT\000'),
'GMT+0': ('GMT+0', 0, 1, [], '', [(0, 0, 0)], 'GMT+0000\000'),
'GMT+1': ('GMT+1', 0, 1, [], '', [(3600, 0, 0)], 'GMT+0100\000'),
'GMT+2': ('GMT+2', 0, 1, [], '', [(7200, 0, 0)], 'GMT+0200\000'),
'GMT+3': ('GMT+3', 0, 1, [], '', [(10800, 0, 0)], 'GMT+0300\000'),
'GMT+4': ('GMT+4', 0, 1, [], '', [(14400, 0, 0)], 'GMT+0400\000'),
'GMT+5': ('GMT+5', 0, 1, [], '', [(18000, 0, 0)], 'GMT+0500\000'),
'GMT+6': ('GMT+6', 0, 1, [], '', [(21600, 0, 0)], 'GMT+0600\000'),
'GMT+7': ('GMT+7', 0, 1, [], '', [(25200, 0, 0)], 'GMT+0700\000'),
'GMT+8': ('GMT+8', 0, 1, [], '', [(28800, 0, 0)], 'GMT+0800\000'),
'GMT+9': ('GMT+9', 0, 1, [], '', [(32400, 0, 0)], 'GMT+0900\000'),
'GMT+10': ('GMT+10', 0, 1, [], '', [(36000, 0, 0)], 'GMT+1000\000'),
'GMT+11': ('GMT+11', 0, 1, [], '', [(39600, 0, 0)], 'GMT+1100\000'),
'GMT+12': ('GMT+12', 0, 1, [], '', [(43200, 0, 0)], 'GMT+1200\000'),
'GMT+13': ('GMT+13', 0, 1, [], '', [(46800, 0, 0)], 'GMT+1300\000'),
'GMT-1': ('GMT-1', 0, 1, [], '', [(-3600, 0, 0)], 'GMT-0100\000'),
'GMT-2': ('GMT-2', 0, 1, [], '', [(-7200, 0, 0)], 'GMT-0200\000'),
'GMT-3': ('GMT-3', 0, 1, [], '', [(-10800, 0, 0)], 'GMT-0300\000'),
'GMT-4': ('GMT-4', 0, 1, [], '', [(-14400, 0, 0)], 'GMT-0400\000'),
'GMT-5': ('GMT-5', 0, 1, [], '', [(-18000, 0, 0)], 'GMT-0500\000'),
'GMT-6': ('GMT-6', 0, 1, [], '', [(-21600, 0, 0)], 'GMT-0600\000'),
'GMT-7': ('GMT-7', 0, 1, [], '', [(-25200, 0, 0)], 'GMT-0700\000'),
'GMT-8': ('GMT-8', 0, 1, [], '', [(-28800, 0, 0)], 'GMT-0800\000'),
'GMT-9': ('GMT-9', 0, 1, [], '', [(-32400, 0, 0)], 'GMT-0900\000'),
'GMT-10': ('GMT-10', 0, 1, [], '', [(-36000, 0, 0)], 'GMT-1000\000'),
'GMT-11': ('GMT-11', 0, 1, [], '', [(-39600, 0, 0)], 'GMT-1100\000'),
'GMT-12': ('GMT-12', 0, 1, [], '', [(-43200, 0, 0)], 'GMT-1200\000'),
'GMT+0130': ('GMT+0130', 0, 1, [], '', [(5400, 0, 0)], 'GMT+0130\000'),
'GMT+0230': ('GMT+0230', 0, 1, [], '', [(9000, 0, 0)], 'GMT+0230\000'),
'GMT+0330': ('GMT+0330', 0, 1, [], '', [(12600, 0, 0)], 'GMT+0330\000'),
'GMT+0430': ('GMT+0430', 0, 1, [], '', [(16200, 0, 0)], 'GMT+0430\000'),
'GMT+0530': ('GMT+0530', 0, 1, [], '', [(19800, 0, 0)], 'GMT+0530\000'),
'GMT+0630': ('GMT+0630', 0, 1, [], '', [(23400, 0, 0)], 'GMT+0630\000'),
'GMT+0730': ('GMT+0730', 0, 1, [], '', [(27000, 0, 0)], 'GMT+0730\000'),
'GMT+0830': ('GMT+0830', 0, 1, [], '', [(30600, 0, 0)], 'GMT+0830\000'),
'GMT+0930': ('GMT+0930', 0, 1, [], '', [(34200, 0, 0)], 'GMT+0930\000'),
'GMT+1030': ('GMT+1030', 0, 1, [], '', [(37800, 0, 0)], 'GMT+1030\000'),
'GMT+1130': ('GMT+1130', 0, 1, [], '', [(41400, 0, 0)], 'GMT+1130\000'),
'GMT+1230': ('GMT+1230', 0, 1, [], '', [(45000, 0, 0)], 'GMT+1230\000'),
'GMT-0130': ('GMT-0130', 0, 1, [], '', [(-5400, 0, 0)], 'GMT-0130\000'),
'GMT-0230': ('GMT-0230', 0, 1, [], '', [(-9000, 0, 0)], 'GMT-0230\000'),
'GMT-0330': ('GMT-0330', 0, 1, [], '', [(-12600, 0, 0)], 'GMT-0330\000'),
'GMT-0430': ('GMT-0430', 0, 1, [], '', [(-16200, 0, 0)], 'GMT-0430\000'),
'GMT-0530': ('GMT-0530', 0, 1, [], '', [(-19800, 0, 0)], 'GMT-0530\000'),
'GMT-0630': ('GMT-0630', 0, 1, [], '', [(-23400, 0, 0)], 'GMT-0630\000'),
'GMT-0730': ('GMT-0730', 0, 1, [], '', [(-27000, 0, 0)], 'GMT-0730\000'),
'GMT-0830': ('GMT-0830', 0, 1, [], '', [(-30600, 0, 0)], 'GMT-0830\000'),
'GMT-0930': ('GMT-0930', 0, 1, [], '', [(-34200, 0, 0)], 'GMT-0930\000'),
'GMT-1030': ('GMT-1030', 0, 1, [], '', [(-37800, 0, 0)], 'GMT-1030\000'),
'GMT-1130': ('GMT-1130', 0, 1, [], '', [(-41400, 0, 0)], 'GMT-1130\000'),
'GMT-1230': ('GMT-1230', 0, 1, [], '', [(-45000, 0, 0)], 'GMT-1230\000'),
}
# These are the timezones not in pytz.common_timezones
_old_zlst = [
'AST', 'AT', 'BST', 'BT', 'CCT',
'CET', 'CST', 'Cuba', 'EADT', 'EAST',
'EEST', 'EET', 'EST', 'Egypt', 'FST',
'FWT', 'GB-Eire', 'GMT+0100', 'GMT+0130', 'GMT+0200',
'GMT+0230', 'GMT+0300', 'GMT+0330', 'GMT+0400', 'GMT+0430',
'GMT+0500', 'GMT+0530', 'GMT+0600', 'GMT+0630', 'GMT+0700',
'GMT+0730', 'GMT+0800', 'GMT+0830', 'GMT+0900', 'GMT+0930',
'GMT+1', 'GMT+1000', 'GMT+1030', 'GMT+1100', 'GMT+1130',
'GMT+1200', 'GMT+1230', 'GMT+1300', 'GMT-0100', 'GMT-0130',
'GMT-0200', 'GMT-0300', 'GMT-0400', 'GMT-0500', 'GMT-0600',
'GMT-0630', 'GMT-0700', 'GMT-0730', 'GMT-0800', 'GMT-0830',
'GMT-0900', 'GMT-0930', 'GMT-1000', 'GMT-1030', 'GMT-1100',
'GMT-1130', 'GMT-1200', 'GMT-1230', 'GST', 'Greenwich',
'Hongkong', 'IDLE', 'IDLW', 'Iceland', 'Iran',
'Israel', 'JST', 'Jamaica', 'Japan', 'MEST',
'MET', 'MEWT', 'MST', 'NT', 'NZDT',
'NZST', 'NZT', 'PST', 'Poland', 'SST',
'SWT', 'Singapore', 'Turkey', 'UCT', 'UT',
'Universal', 'WADT', 'WAST', 'WAT', 'WET',
'ZP4', 'ZP5', 'ZP6',
]
_old_zmap = {
'aest': 'GMT+10', 'aedt': 'GMT+11',
'aus eastern standard time': 'GMT+10',
'sydney standard time': 'GMT+10',
'tasmania standard time': 'GMT+10',
'e. australia standard time': 'GMT+10',
'aus central standard time': 'GMT+0930',
'cen. australia standard time': 'GMT+0930',
'w. australia standard time': 'GMT+8',
'central europe standard time': 'GMT+1',
'eastern standard time': 'US/Eastern',
'us eastern standard time': 'US/Eastern',
'central standard time': 'US/Central',
'mountain standard time': 'US/Mountain',
'pacific standard time': 'US/Pacific',
'mst': 'US/Mountain', 'pst': 'US/Pacific',
'cst': 'US/Central', 'est': 'US/Eastern',
'gmt+0000': 'GMT+0', 'gmt+0': 'GMT+0',
'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+0700': 'GMT+7', 'gmt+0800': 'GMT+8', 'gmt+0900': 'GMT+9',
'gmt+1000': 'GMT+10', 'gmt+1100': 'GMT+11', 'gmt+1200': 'GMT+12',
'gmt+1300': 'GMT+13',
'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-0700': 'GMT-7', 'gmt-0800': 'GMT-8', 'gmt-0900': 'GMT-9',
'gmt-1000': 'GMT-10', 'gmt-1100': 'GMT-11', 'gmt-1200': 'GMT-12',
'gmt+1': 'GMT+1', 'gmt+2': 'GMT+2', 'gmt+3': 'GMT+3',
'gmt+4': 'GMT+4', 'gmt+5': 'GMT+5', 'gmt+6': 'GMT+6',
'gmt+7': 'GMT+7', 'gmt+8': 'GMT+8', 'gmt+9': 'GMT+9',
'gmt+10': 'GMT+10', 'gmt+11': 'GMT+11', 'gmt+12': 'GMT+12',
'gmt+13': 'GMT+13',
'gmt-1': 'GMT-1', 'gmt-2': 'GMT-2', 'gmt-3': 'GMT-3',
'gmt-4': 'GMT-4', 'gmt-5': 'GMT-5', 'gmt-6': 'GMT-6',
'gmt-7': 'GMT-7', 'gmt-8': 'GMT-8', 'gmt-9': 'GMT-9',
'gmt-10': 'GMT-10', 'gmt-11': 'GMT-11', 'gmt-12': 'GMT-12',
'gmt+130': 'GMT+0130', 'gmt+0130': 'GMT+0130',
'gmt+230': 'GMT+0230', 'gmt+0230': 'GMT+0230',
'gmt+330': 'GMT+0330', 'gmt+0330': 'GMT+0330',
'gmt+430': 'GMT+0430', 'gmt+0430': 'GMT+0430',
'gmt+530': 'GMT+0530', 'gmt+0530': 'GMT+0530',
'gmt+630': 'GMT+0630', 'gmt+0630': 'GMT+0630',
'gmt+730': 'GMT+0730', 'gmt+0730': 'GMT+0730',
'gmt+830': 'GMT+0830', 'gmt+0830': 'GMT+0830',
'gmt+930': 'GMT+0930', 'gmt+0930': 'GMT+0930',
'gmt+1030': 'GMT+1030',
'gmt+1130': 'GMT+1130',
'gmt+1230': 'GMT+1230',
'gmt-130': 'GMT-0130', 'gmt-0130': 'GMT-0130',
'gmt-230': 'GMT-0230', 'gmt-0230': 'GMT-0230',
'gmt-330': 'GMT-0330', 'gmt-0330': 'GMT-0330',
'gmt-430': 'GMT-0430', 'gmt-0430': 'GMT-0430',
'gmt-530': 'GMT-0530', 'gmt-0530': 'GMT-0530',
'gmt-630': 'GMT-0630', 'gmt-0630': 'GMT-0630',
'gmt-730': 'GMT-0730', 'gmt-0730': 'GMT-0730',
'gmt-830': 'GMT-0830', 'gmt-0830': 'GMT-0830',
'gmt-930': 'GMT-0930', 'gmt-0930': 'GMT-0930',
'gmt-1030': 'GMT-1030',
'gmt-1130': 'GMT-1130',
'gmt-1230': 'GMT-1230',
'ut': 'Universal',
'bst': 'GMT+1', 'mest': 'GMT+2', 'sst': 'GMT+2',
'fst': 'GMT+2', 'wadt': 'GMT+8', 'eadt': 'GMT+11', 'nzdt': 'GMT+13',
'wet': 'GMT', 'wat': 'GMT-1', 'at': 'GMT-2', 'ast': 'GMT-4',
'nt': 'GMT-11', 'idlw': 'GMT-12', 'cet': 'GMT+1', 'cest': 'GMT+2',
'met': 'GMT+1',
'mewt': 'GMT+1', 'swt': 'GMT+1', 'fwt': 'GMT+1', 'eet': 'GMT+2',
'eest': 'GMT+3',
'bt': 'GMT+3', 'zp4': 'GMT+4', 'zp5': 'GMT+5', 'zp6': 'GMT+6',
'wast': 'GMT+7', 'cct': 'GMT+8', 'jst': 'GMT+9', 'east': 'GMT+10',
'gst': 'GMT+10', 'nzt': 'GMT+12', 'nzst': 'GMT+12', 'idle': 'GMT+12',
'ret': 'GMT+4', 'ist': 'GMT+0530', 'edt': 'GMT-4',
}
# some timezone definitions of the "-0400" are not working
# when upgrading
for hour in range(0, 13):
hour = hour
fhour = str(hour)
if len(fhour) == 1:
fhour = '0' + fhour
_old_zmap['-%s00' % fhour] = 'GMT-%i' % hour
_old_zmap['+%s00' % fhour] = 'GMT+%i' % hour
def _static_timezone_factory(data):
zone = data[0]
cls = type(zone, (StaticTzInfo,), dict(
zone=zone,
_utcoffset=memorized_timedelta(data[5][0][0]),
_tzname=data[6][:-1])) # strip the trailing null
return cls()
_numeric_timezones = dict((key, _static_timezone_factory(data))
for key, data in _numeric_timezone_data.items())
class Timezone:
"""
Timezone information returned by PytzCache.__getitem__
Adapts datetime.tzinfo object to DateTime._timezone interface
"""
def __init__(self, tzinfo):
self.tzinfo = tzinfo
def info(self, t=None):
if t is None:
dt = datetime.utcnow().replace(tzinfo=pytz.utc)
else:
# can't use utcfromtimestamp past 2038
dt = EPOCH + timedelta(0, t)
# need to normalize tzinfo for the datetime to deal with
# daylight savings time.
normalized_dt = self.tzinfo.normalize(dt.astimezone(self.tzinfo))
normalized_tzinfo = normalized_dt.tzinfo
offset = normalized_tzinfo.utcoffset(normalized_dt)
secs = offset.days * 24 * 60 * 60 + offset.seconds
dst = normalized_tzinfo.dst(normalized_dt)
if dst == timedelta(0):
is_dst = 0
else:
is_dst = 1
return secs, is_dst, normalized_tzinfo.tzname(normalized_dt)
class PytzCache:
"""
Reimplementation of the DateTime._cache class that uses for timezone info
"""
_zlst = pytz.common_timezones + _old_zlst # used by DateTime.TimeZones
_zmap = dict((name.lower(), name) for name in pytz.all_timezones)
_zmap.update(_old_zmap) # These must take priority
_zidx = _zmap.keys()
def __getitem__(self, key):
name = self._zmap.get(key.lower(), key) # fallback to key
try:
return Timezone(pytz.timezone(name))
except pytz.UnknownTimeZoneError:
try:
return Timezone(_numeric_timezones[name])
except KeyError:
raise DateTimeError('Unrecognized timezone: %s' % key)

View file

@ -0,0 +1,15 @@
##############################################################################
#
# Copyright (c) 2003 Zope Foundation 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.
#
##############################################################################
# This file is needed to make this a package.

View file

@ -0,0 +1,57 @@
1970-01-01 (1970, 1, 4)
1970-01-02 (1970, 1, 5)
1970-01-30 (1970, 5, 5)
1970-01-31 (1970, 5, 6)
1970-02-01 (1970, 5, 7)
1970-02-02 (1970, 6, 1)
1970-02-28 (1970, 9, 6)
1970-03-01 (1970, 9, 7)
1970-03-30 (1970, 14, 1)
1970-03-31 (1970, 14, 2)
1970-04-01 (1970, 14, 3)
1970-09-30 (1970, 40, 3)
1970-10-01 (1970, 40, 4)
1970-10-02 (1970, 40, 5)
1970-10-03 (1970, 40, 6)
1970-10-04 (1970, 40, 7)
1970-10-05 (1970, 41, 1)
1971-01-02 (1970, 53, 6)
1971-01-03 (1970, 53, 7)
1971-01-04 (1971, 1, 1)
1971-01-05 (1971, 1, 2)
1971-12-31 (1971, 52, 5)
1972-01-01 (1971, 52, 6)
1972-01-02 (1971, 52, 7)
1972-01-03 (1972, 1, 1)
1972-01-04 (1972, 1, 2)
1972-12-30 (1972, 52, 6)
1972-12-31 (1972, 52, 7)
1973-01-01 (1973, 1, 1)
1973-01-02 (1973, 1, 2)
1973-12-29 (1973, 52, 6)
1973-12-30 (1973, 52, 7)
1973-12-31 (1974, 1, 1)
1974-01-01 (1974, 1, 2)
1998-12-30 (1998, 53, 3)
1998-12-31 (1998, 53, 4)
1999-01-01 (1998, 53, 5)
1999-01-02 (1998, 53, 6)
1999-01-03 (1998, 53, 7)
1999-01-04 (1999, 1, 1)
1999-01-05 (1999, 1, 2)
1999-12-30 (1999, 52, 4)
1999-12-31 (1999, 52, 5)
2000-01-01 (1999, 52, 6)
2000-01-02 (1999, 52, 7)
2000-01-03 (2000, 1, 1)
2000-01-04 (2000, 1, 2)
2000-01-05 (2000, 1, 3)
2000-01-06 (2000, 1, 4)
2000-01-07 (2000, 1, 5)
2000-01-08 (2000, 1, 6)
2000-01-09 (2000, 1, 7)
2000-01-10 (2000, 2, 1)
2019-12-28 (2019, 52, 6)
2019-12-29 (2019, 52, 7)
2019-12-30 (2020, 1, 1)
2019-12-31 (2020, 1, 2)

View file

@ -0,0 +1,686 @@
##############################################################################
#
# Copyright (c) 2003 Zope Foundation 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.
#
##############################################################################
from datetime import date, datetime, tzinfo, timedelta
import math
import platform
import os
import sys
import time
import unittest
import pytz
from DateTime.DateTime import _findLocalTimeZoneName
from DateTime import DateTime
if sys.version_info > (3, ):
import pickle
unicode = str
PY3K = True
else:
import cPickle as pickle
PY3K = False
try:
__file__
except NameError:
f = sys.argv[0]
else:
f = __file__
IS_PYPY = getattr(platform, 'python_implementation', lambda: None)() == 'PyPy'
DATADIR = os.path.dirname(os.path.abspath(f))
del f
ZERO = timedelta(0)
class FixedOffset(tzinfo):
"""Fixed offset in minutes east from UTC."""
def __init__(self, offset, name):
self.__offset = timedelta(minutes=offset)
self.__name = name
def utcoffset(self, dt):
return self.__offset
def tzname(self, dt):
return self.__name
def dst(self, dt):
return ZERO
class DateTimeTests(unittest.TestCase):
def _compare(self, dt1, dt2):
'''Compares the internal representation of dt1 with
the representation in dt2. Allows sub-millisecond variations.
Primarily for testing.'''
self.assertEqual(round(dt1._t, 3), round(dt2._t, 3))
self.assertEqual(round(dt1._d, 9), round(dt2._d, 9))
self.assertEqual(round(dt1.time, 9), round(dt2.time, 9))
self.assertEqual(dt1.millis(), dt2.millis())
self.assertEqual(dt1._micros, dt2._micros)
def testBug1203(self):
# 01:59:60 occurred in old DateTime
dt = DateTime(7200, 'GMT')
self.assertTrue(str(dt).find('60') < 0, dt)
def testDSTInEffect(self):
# Checks GMT offset for a DST date in the US/Eastern time zone
dt = DateTime(2000, 5, 9, 15, 0, 0, 'US/Eastern')
self.assertEqual(dt.toZone('GMT').hour(), 19,
(dt, dt.toZone('GMT')))
def testDSTNotInEffect(self):
# Checks GMT offset for a non-DST date in the US/Eastern time zone
dt = DateTime(2000, 11, 9, 15, 0, 0, 'US/Eastern')
self.assertEqual(dt.toZone('GMT').hour(), 20,
(dt, dt.toZone('GMT')))
def testAddPrecision(self):
# Precision of serial additions
dt = DateTime()
self.assertEqual(str(dt + 0.10 + 3.14 + 6.76 - 10), str(dt),
dt)
def testConstructor3(self):
# Constructor from date/time string
dt = DateTime()
dt1s = '%d/%d/%d %d:%d:%f %s' % (
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second(),
dt.timezone())
dt1 = DateTime(dt1s)
# Compare representations as it's the
# only way to compare the dates to the same accuracy
self.assertEqual(repr(dt), repr(dt1))
def testConstructor4(self):
# Constructor from time float
dt = DateTime()
dt1 = DateTime(float(dt))
self._compare(dt, dt1)
def testConstructor5(self):
# Constructor from time float and timezone
dt = DateTime()
dt1 = DateTime(float(dt), dt.timezone())
self.assertEqual(str(dt), str(dt1), (dt, dt1))
dt1 = DateTime(float(dt), unicode(dt.timezone()))
self.assertEqual(str(dt), str(dt1), (dt, dt1))
def testConstructor6(self):
# Constructor from year and julian date
# This test must normalize the time zone, or it *will* break when
# DST changes!
dt1 = DateTime(2000, 5.500000578705)
dt = DateTime('2000/1/5 12:00:00.050 pm %s' % dt1.localZone())
self._compare(dt, dt1)
def testConstructor7(self):
# Constructor from parts
dt = DateTime()
dt1 = DateTime(
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second(),
dt.timezone())
# Compare representations as it's the
# only way to compare the dates to the same accuracy
self.assertEqual(repr(dt), repr(dt1))
def testDayOfWeek(self):
# Compare to the datetime.date value to make it locale independent
expected = date(2000, 6, 16).strftime('%A')
# strftime() used to always be passed a day of week of 0
dt = DateTime('2000/6/16')
s = dt.strftime('%A')
self.assertEqual(s, expected, (dt, s))
def testOldDate(self):
# Fails when an 1800 date is displayed with negative signs
dt = DateTime('1830/5/6 12:31:46.213 pm')
dt1 = dt.toZone('GMT+6')
self.assertTrue(str(dt1).find('-') < 0, (dt, dt1))
def testSubtraction(self):
# Reconstruction of a DateTime from its parts, with subtraction
# this also tests the accuracy of addition and reconstruction
dt = DateTime()
dt1 = dt - 3.141592653
dt2 = DateTime(
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second())
dt3 = dt2 - 3.141592653
self.assertEqual(dt1, dt3, (dt, dt1, dt2, dt3))
def testTZ1add(self):
# Time zone manipulation: add to a date
dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertTrue((dt + 1.0).equalTo(dt1))
def testTZ1sub(self):
# Time zone manipulation: subtract from a date
dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertTrue((dt1 - 1.0).equalTo(dt))
def testTZ1diff(self):
# Time zone manipulation: diff two dates
dt = DateTime('1997/3/8 1:45am GMT-4')
dt1 = DateTime('1997/3/9 1:45pm GMT+8')
self.assertEqual(dt1 - dt, 1.0, (dt, dt1))
def test_compare_methods(self):
# Compare two dates using several methods
dt = DateTime('1997/1/1')
dt1 = DateTime('1997/2/2')
self.assertTrue(dt1.greaterThan(dt))
self.assertTrue(dt1.greaterThanEqualTo(dt))
self.assertTrue(dt.lessThan(dt1))
self.assertTrue(dt.lessThanEqualTo(dt1))
self.assertTrue(dt.notEqualTo(dt1))
self.assertFalse(dt.equalTo(dt1))
def test_compare_methods_none(self):
# Compare a date to None
dt = DateTime('1997/1/1')
self.assertTrue(dt.greaterThan(None))
self.assertTrue(dt.greaterThanEqualTo(None))
self.assertFalse(dt.lessThan(None))
self.assertFalse(dt.lessThanEqualTo(None))
self.assertTrue(dt.notEqualTo(None))
self.assertFalse(dt.equalTo(None))
def test_pickle(self):
dt = DateTime()
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_with_tz(self):
dt = DateTime('2002/5/2 8:00am GMT+8')
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_with_numerical_tz(self):
for dt_str in ('2007/01/02 12:34:56.789 +0300',
'2007/01/02 12:34:56.789 +0430',
'2007/01/02 12:34:56.789 -1234'):
dt = DateTime(dt_str)
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_with_micros(self):
dt = DateTime('2002/5/2 8:00:14.123 GMT+8')
data = pickle.dumps(dt, 1)
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_old(self):
dt = DateTime('2002/5/2 8:00am GMT+0')
data = ('(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05'
'_amonq\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq'
'\x08h\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU'
'\x04Thu.q\x0cU\x05_fdayq\rU\x08Thursdayq\x0eU\x03_pmq\x0fU\x02amq'
'\x10U\x02_tq\x11GA\xcehy\x00\x00\x00\x00U\x07_minuteq\x12K\x00U'
'\x07_microsq\x13L1020326400000000L\nU\x02_dq\x14G@\xe2\x12j\xaa'
'\xaa\xaa\xabU\x07_secondq\x15G\x00\x00\x00\x00\x00\x00\x00\x00U'
'\x03_tzq\x16U\x05GMT+0q\x17U\x06_monthq\x18K\x05U'
'\x0f_timezone_naiveq\x19I00\nU\x04_dayq\x1aK\x02U\x05_yearq'
'\x1bM\xd2\x07U\x08_nearsecq\x1cG\x00\x00\x00\x00\x00\x00\x00'
'\x00U\x07_pmhourq\x1dK\x08U\n_dayoffsetq\x1eK\x04U\x04timeq'
'\x1fG?\xd5UUUV\x00\x00ub.')
if PY3K:
data = data.encode('latin-1')
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def test_pickle_old_without_micros(self):
dt = DateTime('2002/5/2 8:00am GMT+0')
data = ('(cDateTime.DateTime\nDateTime\nq\x01Noq\x02}q\x03(U\x05'
'_amonq\x04U\x03Mayq\x05U\x05_adayq\x06U\x03Thuq\x07U\x05_pmonq'
'\x08h\x05U\x05_hourq\tK\x08U\x05_fmonq\nh\x05U\x05_pdayq\x0bU'
'\x04Thu.q\x0cU\x05_fdayq\rU\x08Thursdayq\x0eU\x03_pmq\x0fU'
'\x02amq\x10U\x02_tq\x11GA\xcehy\x00\x00\x00\x00U\x07_minuteq'
'\x12K\x00U\x02_dq\x13G@\xe2\x12j\xaa\xaa\xaa\xabU\x07_secondq'
'\x14G\x00\x00\x00\x00\x00\x00\x00\x00U\x03_tzq\x15U\x05GMT+0q'
'\x16U\x06_monthq\x17K\x05U\x0f_timezone_naiveq\x18I00\nU'
'\x04_dayq\x19K\x02U\x05_yearq\x1aM\xd2\x07U\x08_nearsecq'
'\x1bG\x00\x00\x00\x00\x00\x00\x00\x00U\x07_pmhourq\x1cK\x08U'
'\n_dayoffsetq\x1dK\x04U\x04timeq\x1eG?\xd5UUUV\x00\x00ub.')
if PY3K:
data = data.encode('latin-1')
new = pickle.loads(data)
for key in DateTime.__slots__:
self.assertEqual(getattr(dt, key), getattr(new, key))
def testTZ2(self):
# Time zone manipulation test 2
dt = DateTime()
dt1 = dt.toZone('GMT')
s = dt.second()
s1 = dt1.second()
self.assertEqual(s, s1, (dt, dt1, s, s1))
def testTZDiffDaylight(self):
# Diff dates across daylight savings dates
dt = DateTime('2000/6/8 1:45am US/Eastern')
dt1 = DateTime('2000/12/8 12:45am US/Eastern')
self.assertEqual(dt1 - dt, 183, (dt, dt1, dt1 - dt))
def testY10KDate(self):
# Comparison of a Y10K date and a Y2K date
dt = DateTime('10213/09/21')
dt1 = DateTime(2000, 1, 1)
dsec = (dt.millis() - dt1.millis()) / 1000.0
ddays = math.floor((dsec / 86400.0) + 0.5)
self.assertEqual(ddays, 3000000, ddays)
def test_tzoffset(self):
# Test time-zone given as an offset
# GMT
dt = DateTime('Tue, 10 Sep 2001 09:41:03 GMT')
self.assertEqual(dt.tzoffset(), 0)
# Timezone by name, a timezone that hasn't got daylightsaving.
dt = DateTime('Tue, 2 Mar 2001 09:41:03 GMT+3')
self.assertEqual(dt.tzoffset(), 10800)
# Timezone by name, has daylightsaving but is not in effect.
dt = DateTime('Tue, 21 Jan 2001 09:41:03 PST')
self.assertEqual(dt.tzoffset(), -28800)
# Timezone by name, with daylightsaving in effect
dt = DateTime('Tue, 24 Aug 2001 09:41:03 PST')
self.assertEqual(dt.tzoffset(), -25200)
# A negative numerical timezone
dt = DateTime('Tue, 24 Jul 2001 09:41:03 -0400')
self.assertEqual(dt.tzoffset(), -14400)
# A positive numerical timzone
dt = DateTime('Tue, 6 Dec 1966 01:41:03 +0200')
self.assertEqual(dt.tzoffset(), 7200)
# A negative numerical timezone with minutes.
dt = DateTime('Tue, 24 Jul 2001 09:41:03 -0637')
self.assertEqual(dt.tzoffset(), -23820)
# A positive numerical timezone with minutes.
dt = DateTime('Tue, 24 Jul 2001 09:41:03 +0425')
self.assertEqual(dt.tzoffset(), 15900)
def testISO8601(self):
# ISO8601 reference dates
ref0 = DateTime('2002/5/2 8:00am GMT')
ref1 = DateTime('2002/5/2 8:00am US/Eastern')
ref2 = DateTime('2006/11/6 10:30 GMT')
ref3 = DateTime('2004/06/14 14:30:15 GMT-3')
ref4 = DateTime('2006/01/01 GMT')
# Basic tests
# Though this is timezone naive and according to specification should
# be interpreted in the local timezone, to preserve backwards
# compatibility with previously expected behaviour.
isoDt = DateTime('2002-05-02T08:00:00')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08:00:00Z')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08:00:00+00:00')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08:00:00-04:00')
self.assertTrue(ref1.equalTo(isoDt))
isoDt = DateTime('2002-05-02 08:00:00-04:00')
self.assertTrue(ref1.equalTo(isoDt))
# Bug 1386: the colon in the timezone offset is optional
isoDt = DateTime('2002-05-02T08:00:00-0400')
self.assertTrue(ref1.equalTo(isoDt))
# Bug 2191: date reduced formats
isoDt = DateTime('2006-01-01')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('200601-01')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('20060101')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('2006-01')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('200601')
self.assertTrue(ref4.equalTo(isoDt))
isoDt = DateTime('2006')
self.assertTrue(ref4.equalTo(isoDt))
# Bug 2191: date/time separators are also optional
isoDt = DateTime('20020502T08:00:00')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T080000')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('20020502T080000')
self.assertTrue(ref0.equalTo(isoDt))
# Bug 2191: timezones with only one digit for hour
isoDt = DateTime('20020502T080000+0')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('20020502 080000-4')
self.assertTrue(ref1.equalTo(isoDt))
isoDt = DateTime('20020502T080000-400')
self.assertTrue(ref1.equalTo(isoDt))
isoDt = DateTime('20020502T080000-4:00')
self.assertTrue(ref1.equalTo(isoDt))
# Bug 2191: optional seconds/minutes
isoDt = DateTime('2002-05-02T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-05-02T08')
self.assertTrue(ref0.equalTo(isoDt))
# Bug 2191: week format
isoDt = DateTime('2002-W18-4T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002-W184T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002W18-4T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002W184T08')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2004-W25-1T14:30:15-03:00')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('2004-W25T14:30:15-03:00')
self.assertTrue(ref3.equalTo(isoDt))
# Bug 2191: day of year format
isoDt = DateTime('2002-122T0800')
self.assertTrue(ref0.equalTo(isoDt))
isoDt = DateTime('2002122T0800')
self.assertTrue(ref0.equalTo(isoDt))
# Bug 2191: hours/minutes fractions
isoDt = DateTime('2006-11-06T10.5')
self.assertTrue(ref2.equalTo(isoDt))
isoDt = DateTime('2006-11-06T10,5')
self.assertTrue(ref2.equalTo(isoDt))
isoDt = DateTime('20040614T1430.25-3')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('2004-06-14T1430,25-3')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('2004-06-14T14:30.25-3')
self.assertTrue(ref3.equalTo(isoDt))
isoDt = DateTime('20040614T14:30,25-3')
self.assertTrue(ref3.equalTo(isoDt))
# ISO8601 standard format
iso8601_string = '2002-05-02T08:00:00-04:00'
iso8601DT = DateTime(iso8601_string)
self.assertEqual(iso8601_string, iso8601DT.ISO8601())
# ISO format with no timezone
isoDt = DateTime('2006-01-01 00:00:00')
self.assertTrue(ref4.equalTo(isoDt))
def testJulianWeek(self):
# Check JulianDayWeek function
fn = os.path.join(DATADIR, 'julian_testdata.txt')
with open(fn, 'r') as fd:
lines = fd.readlines()
for line in lines:
d = DateTime(line[:10])
result_from_mx = tuple(map(int, line[12:-2].split(',')))
self.assertEqual(result_from_mx[1], d.week())
def testCopyConstructor(self):
d = DateTime('2004/04/04')
self.assertEqual(DateTime(d), d)
self.assertEqual(str(DateTime(d)), str(d))
d2 = DateTime('1999/04/12 01:00:00')
self.assertEqual(DateTime(d2), d2)
self.assertEqual(str(DateTime(d2)), str(d2))
def testCopyConstructorPreservesTimezone(self):
# test for https://bugs.launchpad.net/zope2/+bug/200007
# This always worked in the local timezone, so we need at least
# two tests with different zones to be sure at least one of them
# is not local.
d = DateTime('2004/04/04')
self.assertEqual(DateTime(d).timezone(), d.timezone())
d2 = DateTime('2008/04/25 12:00:00 EST')
self.assertEqual(DateTime(d2).timezone(), d2.timezone())
self.assertEqual(str(DateTime(d2)), str(d2))
d3 = DateTime('2008/04/25 12:00:00 PST')
self.assertEqual(DateTime(d3).timezone(), d3.timezone())
self.assertEqual(str(DateTime(d3)), str(d3))
def testRFC822(self):
# rfc822 conversion
dt = DateTime('2002-05-02T08:00:00+00:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0000')
dt = DateTime('2002-05-02T08:00:00+02:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 +0200')
dt = DateTime('2002-05-02T08:00:00-02:00')
self.assertEqual(dt.rfc822(), 'Thu, 02 May 2002 08:00:00 -0200')
# Checking that conversion from local time is working.
dt = DateTime()
dts = dt.rfc822().split(' ')
times = dts[4].split(':')
_isDST = time.localtime(time.time())[8]
if _isDST:
offset = time.altzone
else:
offset = time.timezone
self.assertEqual(dts[0], dt.aDay() + ',')
self.assertEqual(int(dts[1]), dt.day())
self.assertEqual(dts[2], dt.aMonth())
self.assertEqual(int(dts[3]), dt.year())
self.assertEqual(int(times[0]), dt.h_24())
self.assertEqual(int(times[1]), dt.minute())
self.assertEqual(int(times[2]), int(dt.second()))
self.assertEqual(dts[5], "%+03d%02d" % divmod((-offset / 60), 60))
def testInternationalDateformat(self):
for year in (1990, 2001, 2020):
for month in (1, 12):
for day in (1, 12, 28, 31):
try:
d_us = DateTime("%d/%d/%d" % (year, month, day))
except Exception:
continue
d_int = DateTime("%d.%d.%d" % (day, month, year),
datefmt="international")
self.assertEqual(d_us, d_int)
d_int = DateTime("%d/%d/%d" % (day, month, year),
datefmt="international")
self.assertEqual(d_us, d_int)
def test_intl_format_hyphen(self):
d_jan = DateTime('2011-01-11 GMT')
d_nov = DateTime('2011-11-01 GMT')
d_us = DateTime('11-01-2011 GMT')
d_int = DateTime('11-01-2011 GMT', datefmt="international")
self.assertNotEqual(d_us, d_int)
self.assertEqual(d_us, d_nov)
self.assertEqual(d_int, d_jan)
def test_calcTimezoneName(self):
from DateTime.interfaces import TimeError
timezone_dependent_epoch = 2177452800
try:
DateTime()._calcTimezoneName(timezone_dependent_epoch, 0)
except TimeError:
self.fail('Zope Collector issue #484 (negative time bug): '
'TimeError raised')
def testStrftimeTZhandling(self):
# strftime timezone testing
# This is a test for collector issue #1127
format = '%Y-%m-%d %H:%M %Z'
dt = DateTime('Wed, 19 Nov 2003 18:32:07 -0215')
dt_string = dt.strftime(format)
dt_local = dt.toZone(_findLocalTimeZoneName(0))
dt_localstring = dt_local.strftime(format)
self.assertEqual(dt_string, dt_localstring)
def testStrftimeFarDates(self):
# Checks strftime in dates <= 1900 or >= 2038
dt = DateTime('1900/01/30')
self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/1900')
dt = DateTime('2040/01/30')
self.assertEqual(dt.strftime('%d/%m/%Y'), '30/01/2040')
def testZoneInFarDates(self):
# Checks time zone in dates <= 1900 or >= 2038
dt1 = DateTime('2040/01/30 14:33 GMT+1')
dt2 = DateTime('2040/01/30 11:33 GMT-2')
self.assertEqual(dt1.strftime('%d/%m/%Y %H:%M'),
dt2.strftime('%d/%m/%Y %H:%M'))
def testStrftimeUnicode(self):
if IS_PYPY:
# Using Non-Ascii characters for strftime doesn't work in PyPy
# https://bitbucket.org/pypy/pypy/issues/2161/pypy3-strftime-does-not-accept-unicode
return
dt = DateTime('2002-05-02T08:00:00+00:00')
uchar = b'\xc3\xa0'.decode('utf-8')
ok = dt.strftime('Le %d/%m/%Y a %Hh%M').replace('a', uchar)
ustr = b'Le %d/%m/%Y \xc3\xa0 %Hh%M'.decode('utf-8')
self.assertEqual(dt.strftime(ustr), ok)
def testTimezoneNaiveHandling(self):
# checks that we assign timezone naivity correctly
dt = DateTime('2007-10-04T08:00:00+00:00')
self.assertFalse(dt.timezoneNaive(),
'error with naivity handling in __parse_iso8601')
dt = DateTime('2007-10-04T08:00:00Z')
self.assertFalse(dt.timezoneNaive(),
'error with naivity handling in __parse_iso8601')
dt = DateTime('2007-10-04T08:00:00')
self.assertTrue(dt.timezoneNaive(),
'error with naivity handling in __parse_iso8601')
dt = DateTime('2007/10/04 15:12:33.487618 GMT+1')
self.assertFalse(dt.timezoneNaive(),
'error with naivity handling in _parse')
dt = DateTime('2007/10/04 15:12:33.487618')
self.assertTrue(dt.timezoneNaive(),
'error with naivity handling in _parse')
dt = DateTime()
self.assertFalse(dt.timezoneNaive(),
'error with naivity for current time')
s = '2007-10-04T08:00:00'
dt = DateTime(s)
self.assertEqual(s, dt.ISO8601())
s = '2007-10-04T08:00:00+00:00'
dt = DateTime(s)
self.assertEqual(s, dt.ISO8601())
def testConversions(self):
sdt0 = datetime.now() # this is a timezone naive datetime
dt0 = DateTime(sdt0)
self.assertTrue(dt0.timezoneNaive(), (sdt0, dt0))
sdt1 = datetime(2007, 10, 4, 18, 14, 42, 580, pytz.utc)
dt1 = DateTime(sdt1)
self.assertFalse(dt1.timezoneNaive(), (sdt1, dt1))
# convert back
sdt2 = dt0.asdatetime()
self.assertEqual(sdt0, sdt2)
sdt3 = dt1.utcdatetime() # this returns a timezone naive datetime
self.assertEqual(sdt1.hour, sdt3.hour)
dt4 = DateTime('2007-10-04T10:00:00+05:00')
sdt4 = datetime(2007, 10, 4, 5, 0)
self.assertEqual(dt4.utcdatetime(), sdt4)
self.assertEqual(dt4.asdatetime(), sdt4.replace(tzinfo=pytz.utc))
dt5 = DateTime('2007-10-23 10:00:00 US/Eastern')
tz = pytz.timezone('US/Eastern')
sdt5 = datetime(2007, 10, 23, 10, 0, tzinfo=tz)
dt6 = DateTime(sdt5)
self.assertEqual(dt5.asdatetime(), sdt5)
self.assertEqual(dt6.asdatetime(), sdt5)
self.assertEqual(dt5, dt6)
self.assertEqual(dt5.asdatetime().tzinfo, tz)
self.assertEqual(dt6.asdatetime().tzinfo, tz)
def testBasicTZ(self):
# psycopg2 supplies it's own tzinfo instances, with no `zone` attribute
tz = FixedOffset(60, 'GMT+1')
dt1 = datetime(2008, 8, 5, 12, 0, tzinfo=tz)
DT = DateTime(dt1)
dt2 = DT.asdatetime()
offset1 = dt1.tzinfo.utcoffset(dt1)
offset2 = dt2.tzinfo.utcoffset(dt2)
self.assertEqual(offset1, offset2)
def testEDTTimezone(self):
# should be able to parse EDT timezones: see lp:599856.
dt = DateTime("Mon, 28 Jun 2010 10:12:25 EDT")
self.assertEqual(dt.Day(), 'Monday')
self.assertEqual(dt.day(), 28)
self.assertEqual(dt.Month(), 'June')
self.assertEqual(dt.timezone(), 'GMT-4')
def testParseISO8601(self):
parsed = DateTime()._parse_iso8601('2010-10-10')
self.assertEqual(parsed, (2010, 10, 10, 0, 0, 0, 'GMT+0000'))
def test_interface(self):
from DateTime.interfaces import IDateTime
self.assertTrue(IDateTime.providedBy(DateTime()))
def test_security(self):
dt = DateTime()
self.assertEqual(dt.__roles__, None)
self.assertEqual(dt.__allow_access_to_unprotected_subobjects__, 1)
def test_suite():
import doctest
return unittest.TestSuite([
unittest.makeSuite(DateTimeTests),
doctest.DocFileSuite('DateTime.txt', package='DateTime'),
doctest.DocFileSuite('pytz.txt', package='DateTime'),
])