Commit 52347a7e authored by Hanno Schlichting's avatar Hanno Schlichting

Pull in DateTime from the new external package location

parent db20880e
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
##############################################################################
#
# Copyright (c) 2002 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
#
##############################################################################
from DateTime import DateTime
from DateTime import Timezones
This diff is collapsed.
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.
>>> d = DateTime('July 21, 1969 Moon/Eastern')
Traceback (most recent call last):
...
SyntaxError: July 21, 1969 Moon/Eastern
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')
Python Versions
---------------
Both pytz and DateTime should work under Python 2.3 as well as Python 2.4.
Internal Components
-------------------
The following are tests of internal components.
Cache
~~~~~
The DateTime class uses a new time zone cache.
>>> DateTime._tzinfo #doctest: +ELLIPSIS
<DateTime.pytz_support.PytzCache instance at ...>
The cache maps time zone names to time zone instances.
>>> cache = DateTime._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.
>>> cache._zidx #doctest: +ELLIPSIS
[... 'australia/yancowinna'... 'gmt+0500'... 'europe/isle_of_man'...]
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 = list(cache._zidx)
>>> idx.sort()
>>> keys = cache._zmap.keys()
>>> keys.sort()
>>> 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
This diff is collapsed.
##############################################################################
#
# 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.
#
##############################################################################
# This file is needed to make this a package.
##############################################################################
#
# Copyright (c) 2002 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
#
##############################################################################
from DateTime.DateTimeZone import _data
from time import time
class _timezone:
def __init__(self,data):
self.name,self.timect,self.typect, \
self.ttrans,self.tindex,self.tinfo,self.az=data
def default_index(self):
if self.timect == 0: return 0
for i in range(self.typect):
if self.tinfo[i][1] == 0: return i
return 0
def index(self,t=None):
t=t or time()
if self.timect==0: idx=(0, 0, 0)
elif t < self.ttrans[0]:
i=self.default_index()
idx=(i, ord(self.tindex[0]),i)
elif t >= self.ttrans[-1]:
if self.timect > 1:
idx=(ord(self.tindex[-1]),ord(self.tindex[-1]),
ord(self.tindex[-2]))
else:
idx=(ord(self.tindex[-1]),ord(self.tindex[-1]),
self.default_index())
else:
for i in range(self.timect-1):
if t < self.ttrans[i+1]:
if i==0: idx=(ord(self.tindex[0]),ord(self.tindex[1]),
self.default_index())
else: idx=(ord(self.tindex[i]),ord(self.tindex[i+1]),
ord(self.tindex[i-1]))
break
return idx
def info(self,t=None):
idx=self.index(t)[0]
zs =self.az[self.tinfo[idx][2]:]
return self.tinfo[idx][0],self.tinfo[idx][1],zs[:zs.find('\000')]
_zlst = ['Brazil/Acre','Brazil/DeNoronha','Brazil/East',
'Brazil/West','Canada/Atlantic','Canada/Central',
'Canada/Eastern','Canada/East-Saskatchewan',
'Canada/Mountain','Canada/Newfoundland',
'Canada/Pacific','Canada/Yukon',
'Chile/Continental','Chile/EasterIsland','CST','Cuba',
'Egypt','EST','GB-Eire','Greenwich','Hongkong','Iceland',
'Iran','Israel','Jamaica','Japan','Mexico/BajaNorte',
'Mexico/BajaSur','Mexico/General','MST','Poland','PST',
'Singapore','Turkey','Universal','US/Alaska','US/Aleutian',
'US/Arizona','US/Central','US/Eastern','US/East-Indiana',
'US/Hawaii','US/Indiana-Starke','US/Michigan',
'US/Mountain','US/Pacific','US/Samoa','UTC','UCT','GMT',
'GMT+0100','GMT+0200','GMT+0300','GMT+0400','GMT+0500',
'GMT+0600','GMT+0700','GMT+0800','GMT+0900','GMT+1000',
'GMT+1100','GMT+1200','GMT+1300','GMT-0100','GMT-0200',
'GMT-0300','GMT-0400','GMT-0500','GMT-0600','GMT-0700',
'GMT-0800','GMT-0900','GMT-1000','GMT-1100','GMT-1200',
'GMT+1',
'GMT+0130', 'GMT+0230', 'GMT+0330', 'GMT+0430', 'GMT+0530',
'GMT+0630', 'GMT+0730', 'GMT+0830', 'GMT+0930', 'GMT+1030',
'GMT+1130', 'GMT+1230',
'GMT-0130', 'GMT-0230', 'GMT-0330', 'GMT-0430', 'GMT-0530',
'GMT-0630', 'GMT-0730', 'GMT-0830', 'GMT-0930', 'GMT-1030',
'GMT-1130', 'GMT-1230',
'UT','BST','MEST','SST','FST','WADT','EADT','NZDT',
'WET','WAT','AT','AST','NT','IDLW','CET','MET',
'MEWT','SWT','FWT','EET','EEST','BT','ZP4','ZP5','ZP6',
'WAST','CCT','JST','EAST','GST','NZT','NZST','IDLE']
_zmap = {'aest':'GMT+1000', 'aedt':'GMT+1100',
'aus eastern standard time':'GMT+1000',
'sydney standard time':'GMT+1000',
'tasmania standard time':'GMT+1000',
'e. australia standard time':'GMT+1000',
'aus central standard time':'GMT+0930',
'cen. australia standard time':'GMT+0930',
'w. australia standard time':'GMT+0800',
'brazil/acre':'Brazil/Acre',
'brazil/denoronha':'Brazil/DeNoronha',
'brazil/east':'Brazil/East','brazil/west':'Brazil/West',
'canada/atlantic':'Canada/Atlantic',
'canada/central':'Canada/Central',
'canada/eastern':'Canada/Eastern',
'canada/east-saskatchewan':'Canada/East-Saskatchewan',
'canada/mountain':'Canada/Mountain',
'canada/newfoundland':'Canada/Newfoundland',
'canada/pacific':'Canada/Pacific','canada/yukon':'Canada/Yukon',
'central europe standard time':'GMT+0100',
'chile/continental':'Chile/Continental',
'chile/easterisland':'Chile/EasterIsland',
'cst':'US/Central','cuba':'Cuba','est':'US/Eastern','egypt':'Egypt',
'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',
'gb-eire':'GB-Eire','gmt':'GMT',
'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',
'greenwich':'Greenwich','hongkong':'Hongkong',
'iceland':'Iceland','iran':'Iran','israel':'Israel',
'jamaica':'Jamaica','japan':'Japan',
'mexico/bajanorte':'Mexico/BajaNorte',
'mexico/bajasur':'Mexico/BajaSur','mexico/general':'Mexico/General',
'mst':'US/Mountain','pst':'US/Pacific','poland':'Poland',
'singapore':'Singapore','turkey':'Turkey','universal':'Universal',
'utc':'Universal','uct':'Universal','us/alaska':'US/Alaska',
'us/aleutian':'US/Aleutian','us/arizona':'US/Arizona',
'us/central':'US/Central','us/eastern':'US/Eastern',
'us/east-indiana':'US/East-Indiana','us/hawaii':'US/Hawaii',
'us/indiana-starke':'US/Indiana-Starke','us/michigan':'US/Michigan',
'us/mountain':'US/Mountain','us/pacific':'US/Pacific',
'us/samoa':'US/Samoa',
'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'
}
timezones = dict((name, _timezone(data)) for name, data in _data.iteritems())
\ No newline at end of file
This diff is collapsed.
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