~ubuntu-branches/ubuntu/quantal/python-tz/quantal

« back to all changes in this revision

Viewing changes to pytz/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Brian Sutherland
  • Date: 2007-05-09 12:01:36 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20070509120136-oi18syaota63tyzy
Tags: 2007d-1
* New upstream release (Closes: 415458)
* Revert patch ignoring arch files, it's not necessary any and conflicted
  with upstream.
* Patch upstream to use /usr/share/zoneinfo and remove the included version,
  also depend on tzdata package. (Closes: 416202)
* Write and install a testrunner that can run the pytz tests in
  /usr/lib/python-tz/test-pytxX.Y which tests pythonX.Y.
* Build depends on python-central > 0.5, debhelper > 5.0.38
* Remove workaround for python-central/debhelper breakage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
'''
10
10
 
11
11
# The Olson database has historically been updated about 4 times a year
12
 
OLSON_VERSION = '2007c'
 
12
OLSON_VERSION = '2007d'
13
13
VERSION = OLSON_VERSION
14
14
#VERSION = OLSON_VERSION + '.2'
15
15
__version__ = OLSON_VERSION
17
17
OLSEN_VERSION = OLSON_VERSION # Old releases had this misspelling
18
18
 
19
19
__all__ = [
20
 
    'timezone', 'all_timezones', 'common_timezones', 'utc',
21
 
    'AmbiguousTimeError', 'country_timezones', '_',
 
20
    'timezone', 'utc', 'country_timezones',
 
21
    'AmbiguousTimeError', 'UnknownTimeZoneError',
 
22
    'all_timezones', 'all_timezones_set',
 
23
    'common_timezones', 'common_timezones_set',
22
24
    ]
23
25
 
24
26
import sys, datetime, os.path, gettext
31
33
from tzinfo import AmbiguousTimeError, unpickler
32
34
from tzfile import build_tzinfo
33
35
 
 
36
# Use 2.3 sets module implementation if set builtin is not available
 
37
try:
 
38
    set
 
39
except NameError:
 
40
    from sets import Set as set
 
41
 
34
42
 
35
43
def open_resource(name):
36
44
    """Open a resource from the zoneinfo subdir for reading.
37
45
 
38
46
    Uses the pkg_resources module if available.
39
47
    """
40
 
    if resource_stream is not None:
41
 
        return resource_stream(__name__, 'zoneinfo/' + name)
42
 
    else:
43
 
        name_parts = name.lstrip('/').split('/')
44
 
        for part in name_parts:
45
 
            if part == os.path.pardir or os.path.sep in part:
46
 
                raise ValueError('Bad path segment: %r' % part)
47
 
        filename = os.path.join(os.path.dirname(__file__),
48
 
                                'zoneinfo', *name_parts)
49
 
        return open(filename, 'rb')
 
48
    # Patched in Debian, use the system zoninfo from the tzdata package
 
49
    name_parts = name.lstrip('/').split('/')
 
50
    for part in name_parts:
 
51
        if part == os.path.pardir or os.path.sep in part:
 
52
            raise ValueError('Bad path segment: %r' % part)
 
53
    filename = os.path.join('/usr/share/zoneinfo', *name_parts)
 
54
    return open(filename, 'rb')
50
55
        
51
56
 
52
57
# Enable this when we get some translations?
63
68
#     """Translate a timezone name using the current locale, returning Unicode"""
64
69
#     return t.ugettext(timezone_name)
65
70
 
 
71
 
 
72
class UnknownTimeZoneError(KeyError):
 
73
    '''Exception raised when pytz is passed an unknown timezone.
 
74
 
 
75
    >>> isinstance(UnknownTimeZoneError(), LookupError)
 
76
    True
 
77
 
 
78
    This class is actually a subclass of KeyError to provide backwards
 
79
    compatibility with code relying on the undocumented behavior of earlier
 
80
    pytz releases.
 
81
 
 
82
    >>> isinstance(UnknownTimeZoneError(), KeyError)
 
83
    True
 
84
    '''
 
85
    pass
 
86
 
 
87
 
66
88
_tzinfo_cache = {}
67
89
 
68
90
def timezone(zone):
84
106
    '2002-10-27 01:50:00 EDT (-0400)'
85
107
    >>> (loc_dt + timedelta(minutes=10)).strftime(fmt)
86
108
    '2002-10-27 01:10:00 EST (-0500)'
 
109
 
 
110
    Raises UnknownTimeZoneError if passed an unknown zone.
 
111
 
 
112
    >>> timezone('Asia/Shangri-La')
 
113
    Traceback (most recent call last):
 
114
    ...
 
115
    UnknownTimeZoneError: 'Asia/Shangri-La'
87
116
    '''
88
117
    if zone.upper() == 'UTC':
89
118
        return utc
90
119
 
91
120
    zone = _unmunge_zone(zone)
92
121
    if zone not in _tzinfo_cache:
93
 
        _tzinfo_cache[zone] = build_tzinfo(zone, open_resource(zone))
 
122
        if zone in all_timezones_set:
 
123
            _tzinfo_cache[zone] = build_tzinfo(zone, open_resource(zone))
 
124
        else:
 
125
            raise UnknownTimeZoneError(zone)
94
126
    
95
127
    return _tzinfo_cache[zone]
96
128
 
221
253
                _country_timezones_cache[code] = [zone]
222
254
    return _country_timezones_cache[iso3166_code]
223
255
 
 
256
 
224
257
# Time-zone info based solely on fixed offsets
225
258
 
226
259
class _FixedOffset(datetime.tzinfo):
260
293
            raise ValueError, 'Naive time - no tzinfo set'
261
294
        return dt.replace(tzinfo=self)
262
295
 
 
296
 
263
297
def FixedOffset(offset, _tzinfos = {}):
264
298
    """return a fixed-offset timezone based off a number of minutes.
265
299
    
308
342
        True
309
343
        >>> pickle.loads(pickle.dumps(two)) is two
310
344
        True
311
 
 
312
345
    """
313
 
 
314
346
    if offset == 0:
315
347
        return UTC
316
348
 
326
358
 
327
359
FixedOffset.__safe_for_unpickling__ = True
328
360
 
 
361
 
329
362
def _test():
330
363
    import doctest, os, sys
331
364
    sys.path.insert(0, os.pardir)
489
522
 'America/Rankin_Inlet',
490
523
 'America/Recife',
491
524
 'America/Regina',
 
525
 'America/Resolute',
492
526
 'America/Rio_Branco',
493
527
 'America/Rosario',
494
528
 'America/Santiago',
786
820
 'US/Pacific-New',
787
821
 'US/Samoa',
788
822
 'UTC']
 
823
common_timezones_set = set(common_timezones)
789
824
 
790
825
all_timezones = \
791
826
['Africa/Abidjan',
963
998
 'America/Rankin_Inlet',
964
999
 'America/Recife',
965
1000
 'America/Regina',
 
1001
 'America/Resolute',
966
1002
 'America/Rio_Branco',
967
1003
 'America/Rosario',
968
1004
 'America/Santiago',
1338
1374
 'WET',
1339
1375
 'Zulu',
1340
1376
 'posixrules']
 
1377
all_timezones_set = set(all_timezones)