def timezone(zone): r''' Return a datetime.tzinfo implementation for the given timezone >>> from datetime import datetime, timedelta >>> utc = timezone('UTC') >>> eastern = timezone('US/Eastern') >>> eastern.zone 'US/Eastern' >>> timezone(u'US/Eastern') is eastern True >>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc) >>> loc_dt = utc_dt.astimezone(eastern) >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' >>> loc_dt.strftime(fmt) '2002-10-27 01:00:00 EST (-0500)' >>> (loc_dt - timedelta(minutes=10)).strftime(fmt) '2002-10-27 00:50:00 EST (-0500)' >>> eastern.normalize(loc_dt - timedelta(minutes=10)).strftime(fmt) '2002-10-27 01:50:00 EDT (-0400)' >>> (loc_dt + timedelta(minutes=10)).strftime(fmt) '2002-10-27 01:10:00 EST (-0500)' Raises UnknownTimeZoneError if passed an unknown zone. >>> timezone('Asia/Shangri-La') Traceback (most recent call last): ... UnknownTimeZoneError: 'Asia/Shangri-La' >>> timezone(u'\N{TRADE MARK SIGN}') Traceback (most recent call last): ... UnknownTimeZoneError: u'\u2122' ''' if zone.upper() == 'UTC': return pytz.utc try: zone = zone.encode('US-ASCII') except UnicodeEncodeError: # All valid timezones are ASCII raise pytz.UnknownTimeZoneError(zone) zone = pytz._unmunge_zone(zone) if zone not in pytz._tzinfo_cache: if zone in all_timezones: pytz._tzinfo_cache[zone] = pytz.build_tzinfo( zone, caching_tz_opener(zone)) else: raise pytz.UnknownTimeZoneError(zone) return pytz._tzinfo_cache[zone]
def timezone(zone): r''' Return a datetime.tzinfo implementation for the given timezone >>> from datetime import datetime, timedelta >>> utc = timezone('UTC') >>> eastern = timezone('US/Eastern') >>> eastern.zone 'US/Eastern' >>> timezone(u'US/Eastern') is eastern True >>> utc_dt = datetime(2002, 10, 27, 6, 0, 0, tzinfo=utc) >>> loc_dt = utc_dt.astimezone(eastern) >>> fmt = '%Y-%m-%d %H:%M:%S %Z (%z)' >>> loc_dt.strftime(fmt) '2002-10-27 01:00:00 EST (-0500)' >>> (loc_dt - timedelta(minutes=10)).strftime(fmt) '2002-10-27 00:50:00 EST (-0500)' >>> eastern.normalize(loc_dt - timedelta(minutes=10)).strftime(fmt) '2002-10-27 01:50:00 EDT (-0400)' >>> (loc_dt + timedelta(minutes=10)).strftime(fmt) '2002-10-27 01:10:00 EST (-0500)' Raises UnknownTimeZoneError if passed an unknown zone. >>> timezone('Asia/Shangri-La') Traceback (most recent call last): ... UnknownTimeZoneError: 'Asia/Shangri-La' >>> timezone(u'\N{TRADE MARK SIGN}') Traceback (most recent call last): ... UnknownTimeZoneError: u'\u2122' ''' if zone.upper() == 'UTC': return pytz.utc try: zone = zone.encode('US-ASCII') except UnicodeEncodeError: # All valid timezones are ASCII raise pytz.UnknownTimeZoneError(zone) zone = pytz._unmunge_zone(zone) if zone not in pytz._tzinfo_cache: if zone in all_timezones: pytz._tzinfo_cache[zone] =pytz.build_tzinfo(zone, caching_tz_opener(zone)) else: raise pytz.UnknownTimeZoneError(zone) return pytz._tzinfo_cache[zone]
from datetime import timedelta from datetime import tzinfo from datetime import datetime import os import pytz import pynuodb if os.path.exists('/etc/timezone'): with open('/etc/timezone') as file_: Local = pytz.timezone(file_.read().strip()) else: with open('/etc/localtime', 'rb') as file_: Local = pytz.build_tzinfo('localtime', file_) UTC = pytz.timezone('UTC') class _MyOffset(tzinfo): ''' A timezone class that uses the current offset for all times in the past and future. The database doesn't return an timezone offset to us, it just returns the timestamp it has, but cast into the client's current timezone. This class can be used to do exactly the same thing to the test val. ''' def utcoffset(self, dt): return Local.localize(datetime.now()).utcoffset() MyOffset = _MyOffset()
# print '\n\n\n\n%s\n\n\n\n\n' % xbmc.getRegion('time') # print '\n\n\n\n%s\n\n\n\n\n' % xbmc.getRegion('dateshort') # print '\n\n\n\n%s\n\n\n\n\n' % xbmc.getRegion('datelong') # print '\n\n\n\n%s\n\n\n\n\n' % xbmc.getRegion('meridiem') # print '\n\n\n\n%s\n\n\n\n\n' % xbmc.getRegion('tempunit') # print '\n\n\n\n%s\n\n\n\n\n' % xbmc.getRegion('speedunit') # print '\n\n\n\n%s\n\n\n\n\n' % xbmc.__date__ dateformat = xbmc.getRegion('dateshort') timeformat = xbmc.getRegion('time') datetimeformat = '%s %s' % (dateformat, timeformat) utc = pytz.utc eastern = timezone('US/Eastern') local = pytz.build_tzinfo('localtime', open('/etc/localtime', 'rb')) def myNow(): if hasattr(myNow, 'datetime'): return myNow.datetime myNow.datetime = datetime.now(local) # myNow.datetime = parser.parse('2015-04-26T00:09:25-04:00') return myNow.datetime def estNow(): if hasattr(estNow, 'datetime'): return estNow.datetime estNow.datetime = datetime.now(eastern) return estNow.datetime
from datetime import timedelta from datetime import tzinfo from datetime import datetime import os import pytz import pynuodb if os.path.exists('/etc/timezone'): with open('/etc/timezone') as file_: Local = pytz.timezone(file_.read().strip()) else: with open('/etc/localtime', 'rb') as file_: Local = pytz.build_tzinfo('localtime', file_) UTC = pytz.timezone('UTC') class _MyOffset(tzinfo): ''' A timezone class that uses the current offset for all times in the past and future. The database doesn't return an timezone offset to us, it just returns the timestamp it has, but cast into the client's current timezone. This class can be used to do exactly the same thing to the test val. ''' def utcoffset(self, dt): return Local.localize(datetime.now()).utcoffset()