Module win32timezone
win32timezone: Module for handling datetime.tzinfo time zones using the windows registry for time zone information. The time zone names are dependent on the registry entries defined by the operating system.
Comments
Currently, this module only supports the Windows NT line of products and not Windows 95/98/Me.This module may be tested using the doctest module.
Written by Jason R. Coombs (jaraco@jaraco.com). Copyright © 2003-2009. All Rights Reserved.
This module is licenced for use in Mark Hammond's pywin32 library under the same terms as the pywin32 library.
To use this time zone module with the datetime module, simply pass the TimeZoneInfo object to the datetime constructor. For example,
>>> import win32timezone, datetime >>> assert 'Mountain Standard Time' in win32timezone.TimeZoneInfo.get_sorted_time_zone_names() >>> MST = win32timezone.TimeZoneInfo('Mountain Standard Time') >>> now = datetime.datetime.now(MST)The now object is now a time-zone aware object, and daylight savings- aware methods may be called on it.
>>> now.utcoffset() in (datetime.timedelta(-1, 61200), datetime.timedelta(-1, 64800)) True(note that the result of utcoffset call will be different based on when now was generated, unless standard time is always used)
>>> now = datetime.datetime.now(TimeZoneInfo('Mountain Standard Time', True)) >>> now.utcoffset() datetime.timedelta(-1, 61200)To convert from one timezone to another, just use the astimezone method.>>> aug2 = datetime.datetime(2003, 8, 2, tzinfo = MST) >>> tuple(aug2.utctimetuple()) (2003, 8, 2, 6, 0, 0, 5, 214, 0) >>> nov2 = datetime.datetime(2003, 11, 25, tzinfo = MST) >>> tuple(nov2.utctimetuple()) (2003, 11, 25, 7, 0, 0, 1, 329, 0)
>>> aug2.isoformat() '2003-08-02T00:00:00-06:00' >>> aug2est = aug2.astimezone(win32timezone.TimeZoneInfo('Eastern Standard Time')) >>> aug2est.isoformat() '2003-08-02T02:00:00-04:00'calling the displayName member will return the display name as set in the registry.
>>> est = win32timezone.TimeZoneInfo('Eastern Standard Time') >>> str(est.displayName) '(GMT-05:00) Eastern Time (US & Canada)'To get the complete list of available time zone keys,>>> gmt = win32timezone.TimeZoneInfo('GMT Standard Time', True) >>> str(gmt.displayName) '(GMT) Greenwich Mean Time : Dublin, Edinburgh, Lisbon, London'
>>> zones = win32timezone.TimeZoneInfo.get_all_time_zones()If you want to get them in an order that's sorted longitudinally
>>> zones = win32timezone.TimeZoneInfo.get_sorted_time_zones()TimeZoneInfo now supports being pickled and comparison
>>> import pickle >>> tz = win32timezone.TimeZoneInfo('China Standard Time') >>> tz == pickle.loads(pickle.dumps(tz)) TrueIt's possible to construct a TimeZoneInfo from a TimeZoneDescription including the currently-defined zone.
>>> tz = win32timezone.TimeZoneInfo(TimeZoneDefinition.current()) >>> tz == pickle.loads(pickle.dumps(tz)) TrueMicrosoft now has a patch for handling time zones in 2007 (see http://support.microsoft.com/gp/cp_dst)>>> aest = win32timezone.TimeZoneInfo('AUS Eastern Standard Time') >>> est = win32timezone.TimeZoneInfo('E. Australia Standard Time') >>> dt = datetime.datetime(2006, 11, 11, 1, 0, 0, tzinfo = aest) >>> estdt = dt.astimezone(est) >>> estdt.strftime('%Y-%m-%d %H:%M:%S') '2006-11-11 00:00:00'
>>> dt = datetime.datetime(2007, 1, 12, 1, 0, 0, tzinfo = aest) >>> estdt = dt.astimezone(est) >>> estdt.strftime('%Y-%m-%d %H:%M:%S') '2007-01-12 00:00:00'
>>> dt = datetime.datetime(2007, 6, 13, 1, 0, 0, tzinfo = aest) >>> estdt = dt.astimezone(est) >>> estdt.strftime('%Y-%m-%d %H:%M:%S') '2007-06-13 01:00:00'
As a result, patched systems will give an incorrect result for dates prior to the designated year except for Vista and its successors, which have dynamic time zone support.
>>> nov2_pre_change = datetime.datetime(2003, 11, 2, tzinfo = MST) >>> old_response = (2003, 11, 2, 7, 0, 0, 6, 306, 0) >>> incorrect_patch_response = (2003, 11, 2, 6, 0, 0, 6, 306, 0) >>> pre_response = nov2_pre_change.utctimetuple() >>> pre_response in (old_response, incorrect_patch_response) TrueFurthermore, unpatched systems pre-Vista will give an incorrect result for dates after 2007.
>>> nov2_post_change = datetime.datetime(2007, 11, 2, tzinfo = MST) >>> incorrect_unpatched_response = (2007, 11, 2, 7, 0, 0, 4, 306, 0) >>> new_response = (2007, 11, 2, 6, 0, 0, 4, 306, 0) >>> post_response = nov2_post_change.utctimetuple() >>> post_response in (new_response, incorrect_unpatched_response) TrueThere is a function you can call to get some capabilities of the time zone data.
>>> caps = GetTZCapabilities() >>> isinstance(caps, dict) True >>> 'MissingTZPatch' in caps True >>> 'DynamicTZSupport' in caps True>>> both_dates_correct = (pre_response == old_response and post_response == new_response) >>> old_dates_wrong = (pre_response == incorrect_patch_response) >>> new_dates_wrong = (post_response == incorrect_unpatched_response)
>>> caps['DynamicTZSupport'] == both_dates_correct True
>>> (not caps['DynamicTZSupport'] and caps['MissingTZPatch']) == new_dates_wrong True
>>> (not caps['DynamicTZSupport'] and not caps['MissingTZPatch']) == old_dates_wrong True
Methods
- GetTZCapabilities
- Run a few known tests to determine the capabilities of the time zone database
- GetSortedTimeZoneNames
- Return a list of time zone names that can be used to initialize TimeZoneInfo instances
- now
- Return the local time now with timezone awareness as enabled
- resolveMUITimeZone
- Resolve a multilingual user interface resource for the time zone name
- deprecated
- This is a decorator which can be used to mark functions
- utcnow
- Return the UTC time now with timezone awareness as enabled
Classes
- TimeZoneInfo
- Main class for handling Windows time zones.
- TimeZoneDefinition
- A time zone definition class based on the win32
- RangeMap
- A dictionary-like object that uses the keys as bounds for a range.