diff --git a/format_date/__init__.py b/format_date/__init__.py index 9870932..e305789 100644 --- a/format_date/__init__.py +++ b/format_date/__init__.py @@ -2,6 +2,7 @@ import locale import sys import time +import os import pytz from pytz.exceptions import UnknownTimeZoneError @@ -20,6 +21,39 @@ basestring = str +if os.name == 'nt': + import ctypes + from ctypes import Structure + from ctypes.wintypes import WORD + + class SYSTEMTIME(Structure): + _fields_ = [ + ("wYear", WORD), + ("wMonth", WORD), + ("wDayOfWeek", WORD), + ("wDay", WORD), + ("wHour", WORD), + ("wMinute", WORD), + ("wSecond", WORD), + ("wMilliseconds", WORD) + ] + + class datetime(datetime): + '''Provide correct now on Windows, even if user switches system tz''' + @classmethod + def now(cls, tz=None): + system_time = SYSTEMTIME() + ctypes.windll.kernel32.GetLocalTime(ctypes.byref(system_time)) + return cls(system_time.wYear, + system_time.wMonth, + system_time.wDay, + system_time.wHour, + system_time.wMinute, + system_time.wSecond, + system_time.wMilliseconds * 1000, + tzinfo=tz) + + class LocalTimezone(tzinfo): """Helper class which extends datetime.tzinfo and implements the 'local timezone'. (Read: captures the platform's idea of local time.)