diff options
author | Thom Nichols <tmnichols@gmail.com> | 2010-07-14 11:05:29 -0400 |
---|---|---|
committer | Thom Nichols <tmnichols@gmail.com> | 2010-07-14 11:05:29 -0400 |
commit | be5688007bfa8cdd8fffa38dc618becd7557d559 (patch) | |
tree | 73308ff83cd1c6c9b26bf0956d7495a54536017c | |
parent | ad7c1b06f4622d094fc6e3cc42b61bdb63b858f6 (diff) | |
download | slixmpp-be5688007bfa8cdd8fffa38dc618becd7557d559.tar.gz slixmpp-be5688007bfa8cdd8fffa38dc618becd7557d559.tar.bz2 slixmpp-be5688007bfa8cdd8fffa38dc618becd7557d559.tar.xz slixmpp-be5688007bfa8cdd8fffa38dc618becd7557d559.zip |
moved parsing logic into TimeElement to aid reuse
-rw-r--r-- | sleekxmpp/plugins/xep_0202.py | 19 |
1 files changed, 14 insertions, 5 deletions
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() ) |