[pywin32-checkins] pywin32/win32/Lib win32timezone.py,1.18,1.19
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: Jason R. C. <ja...@us...> - 2009-01-14 17:51:26
|
Update of /cvsroot/pywin32/pywin32/win32/Lib In directory ddv4jf1.ch3.sourceforge.com:/tmp/cvs-serv25337 Modified Files: win32timezone.py Log Message: Reworked TimeZoneUTC to initialize with a TIME_ZONE_INFORMATION structure so it now is a subclass of TimeZoneInfo. Added now() and utcnow() for convenient access to timezone-aware times. Index: win32timezone.py =================================================================== RCS file: /cvsroot/pywin32/pywin32/win32/Lib/win32timezone.py,v retrieving revision 1.18 retrieving revision 1.19 diff -C2 -d -r1.18 -r1.19 *** win32timezone.py 14 Jan 2009 04:06:02 -0000 1.18 --- win32timezone.py 14 Jan 2009 17:51:17 -0000 1.19 *************** *** 183,187 **** def _locate_day(year, cutoff): """ ! Takes a pywintypes.Time object, such as retrieved from a TIME_ZONE_INFORMATION structure or call to GetTimeZoneInformation and interprets it based on the given year to identify the actual day. --- 183,187 ---- def _locate_day(year, cutoff): """ ! Takes a pywintoypes.Time object, such as retrieved from a TIME_ZONE_INFORMATION structure or call to GetTimeZoneInformation and interprets it based on the given year to identify the actual day. *************** *** 484,495 **** # end backward compatibility # A timezone info for utc - pywintypes uses a single instance of this class # to return SYSTEMTIME instances. ! # Lifted from Gustavo Niemeyer's PSF-licensed dateutil package. ! class TimeZoneUTC(datetime.tzinfo): ! """A UTC Time Zone instance that is compatible with TimeZoneInfo, but ! avoids access to the registry. ! >>> TimeZoneUTC() == GetUTCTimeZone() ! True """ def __new__(cls): --- 484,510 ---- # end backward compatibility + def utcnow(): + """ + Return the UTC time now with timezone awareness as enabled + by this module + >>> now = utcnow() + """ + now = datetime.datetime.utcnow() + now = now.replace(tzinfo=TimeZoneUTC()) + return now + + def now(): + """ + Return the local time now with timezone awareness as enabled + by this module + >>> now_local = now() + """ + return datetime.datetime.now(GetLocalTimeZone()) + # A timezone info for utc - pywintypes uses a single instance of this class # to return SYSTEMTIME instances. ! class TimeZoneUTC(TimeZoneInfo): ! """A UTC Time Zone instance that initializes statically (without ! accessing the registry or apis. """ def __new__(cls): *************** *** 498,534 **** return cls._instance except AttributeError: ! cls._instance = datetime.tzinfo.__new__(cls) return cls._instance ! ZERO = datetime.timedelta(0) ! def utcoffset(self, dt): ! return self.ZERO ! ! def dst(self, dt): ! return self.ZERO ! ! def tzname(self, dt): ! return "UTC" ! ! def __eq__(self, other): ! # Instance of this exact class? ! if isinstance(other, TimeZoneUTC): ! return True ! # allow comparisons against the regular TimeZoneInfo object. ! if not isinstance(other, TimeZoneInfo): ! return False ! # The following uses the staticInfo directly (ignoring any ! # dynamic info). This should compare the time zones using ! # the most current info. The two are equal if there is ! # no bias, no standard time bias, and no bias during dst. ! si = other.staticInfo ! same_bias = si.bias==self.ZERO ! same_standard_bias = si.standard_bias==self.ZERO ! no_dst = other.fixedStandardTime == True ! same_daylight_bias = no_dst or si.daylight_bias==self.ZERO ! return same_bias and same_standard_bias and same_daylight_bias ! ! def __ne__(self, other): ! return not self.__eq__(other) def __repr__(self): --- 513,529 ---- return cls._instance except AttributeError: ! tzi = cls._get_tzi() ! cls._instance = TimeZoneInfo.__new__(cls, tzi) return cls._instance ! def __init__(self): ! pass ! ! @classmethod ! def _get_tzi(cls): ! # create a TZI that represents UTC ! tzi = TIME_ZONE_INFORMATION() ! tzi.standardname = 'Universal Coordinated Time' ! return tzi def __repr__(self): |