From be5688007bfa8cdd8fffa38dc618becd7557d559 Mon Sep 17 00:00:00 2001 From: Thom Nichols Date: Wed, 14 Jul 2010 11:05:29 -0400 Subject: moved parsing logic into TimeElement to aid reuse --- sleekxmpp/plugins/xep_0202.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'sleekxmpp') diff --git a/sleekxmpp/plugins/xep_0202.py b/sleekxmpp/plugins/xep_0202.py index 332fd2de..4e309a79 100644 --- a/sleekxmpp/plugins/xep_0202.py +++ b/sleekxmpp/plugins/xep_0202.py @@ -46,11 +46,9 @@ class xep_0202(base.base_plugin): iq = self.xmpp.Iq( stream=self.xmpp, sto=to, stype='get', xml = ET.Element(_XMLNS + 'time') ) resp = iq.send(iq) # wait for response - time_str = resp.find(_XMLNS + 'time/utc').text - dt_format = '%Y-%m-%dT%H:%M:%S' - if time_str.find('.') > -1: dt_format += '.%f' # milliseconds in format + return TimeElement( - datetime.strptime( time_str, dt_format + 'Z' ), + resp.find(_XMLNS + 'time/utc').text, xml.find(_XMLNS + 'time/tzo').text ) def _handle_get(self,xml): @@ -66,7 +64,15 @@ class TimeElement: """ def __init__(self, utc=None, tzo="Z"): - self.utc = datetime.utcnow() if utc is None else utc + if utc is None: + self.utc = datetime.utcnow() + elif type(utc) is str: # parse ISO string + dt_format = '%Y-%m-%dT%H:%M:%S' + if utc.find('.') > -1: dt_format += '.%f' # milliseconds in format + self.utc = datetime.strptime( time_str, dt_format + 'Z' ) + elif type(utc) is float: # parse posix timestamp + self.utc = datetime.utcfromtimestamp() + else: self.utc = utc self.tzo = tzo def to_xml(self): @@ -78,3 +84,6 @@ class TimeElement: child.text = datetime.isoformat(self.utc) + "Z" time.append( child ) return time + + def __str__(self): + return ET.tostring( self.to_xml() ) -- cgit v1.2.3