Time: use classes from standard library

This commit is contained in:
André Apitzsch 2018-08-19 14:53:52 +02:00 committed by Philipp Hörist
parent e128a3f676
commit 5d853e902a
1 changed files with 6 additions and 33 deletions

View File

@ -25,8 +25,6 @@ from gajim.common.nec import NetworkIncomingEvent
log = logging.getLogger('gajim.c.m.entity_time') log = logging.getLogger('gajim.c.m.entity_time')
ZERO = datetime.timedelta(0)
class EntityTime: class EntityTime:
def __init__(self, con): def __init__(self, con):
@ -79,7 +77,7 @@ class EntityTime:
tzo = '0:0' tzo = '0:0'
try: try:
tzoh, tzom = tzo.split(':') tzoh, tzom = tzo.split(':')
except Exception as e: except Exception:
log.warning('Wrong tzo node: %s', stanza) log.warning('Wrong tzo node: %s', stanza)
return return
utc_time = time_.getTag('utc').getData() utc_time = time_.getTag('utc').getData()
@ -106,8 +104,11 @@ class EntityTime:
e, stanza.getFrom()) e, stanza.getFrom())
return return
t = t.replace(tzinfo=UTC()) utc = datetime.timezone(datetime.timedelta(0))
return t.astimezone(contact_tz(tzoh, tzom)).strftime('%c') t = t.replace(tzinfo=utc)
utc_offset = datetime.timedelta(hours=int(tzoh), minutes=int(tzom))
contact_tz = datetime.timezone(utc_offset, "remote timezone")
return t.astimezone(contact_tz).strftime('%c')
def _answer_request(self, con, stanza): def _answer_request(self, con, stanza):
log.info('%s asked for the time', stanza.getFrom()) log.info('%s asked for the time', stanza.getFrom())
@ -138,33 +139,5 @@ class TimeResultReceivedEvent(NetworkIncomingEvent):
return True return True
class UTC(datetime.tzinfo):
def utcoffset(self, dt):
return ZERO
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return ZERO
class contact_tz(datetime.tzinfo):
def __init__(self, tzoh, tzom):
self._tzoh = tzoh
self._tzom = tzom
def utcoffset(self, dt):
return datetime.timedelta(hours=int(self._tzoh),
minutes=int(self._tzom))
def tzname(self, dt):
return "remote timezone"
def dst(self, dt):
return ZERO
def get_instance(*args, **kwargs): def get_instance(*args, **kwargs):
return EntityTime(*args, **kwargs), 'EntityTime' return EntityTime(*args, **kwargs), 'EntityTime'