Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions format_date/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import locale
import sys
import time
import os

import pytz
from pytz.exceptions import UnknownTimeZoneError
Expand All @@ -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.)
Expand Down