2 SleekXMPP: The Sleek XMPP Library
3 Copyright (C) 2010 Nathanael C. Fritz
4 This file is part of SleekXMPP.
6 See the file LICENSE for copying permission.
12 from sleekxmpp
.xmlstream
import ElementBase
13 from sleekxmpp
.plugins
import xep_0082
14 from sleekxmpp
.thirdparty
import tzutc
, tzoffset
17 class EntityTime(ElementBase
):
20 The <time> element represents the local time for an XMPP agent.
21 The time is expressed in UTC to make synchronization easier
22 between entities, but the offset for the local timezone is also
25 Example <time> stanzas:
27 <time xmlns="urn:xmpp:time">
28 <utc>2011-07-03T11:37:12.234569</utc>
34 time -- The local time for the entity (updates utc and tzo).
35 utc -- The UTC equivalent to local time.
36 tzo -- The local timezone offset from UTC.
39 get_time -- Return local time datetime object.
40 set_time -- Set UTC and TZO fields.
41 del_time -- Remove both UTC and TZO fields.
42 get_utc -- Return datetime object of UTC time.
43 set_utc -- Set the UTC time.
44 get_tzo -- Return tzinfo object.
45 set_tzo -- Set the local timezone offset.
49 namespace
= 'urn:xmpp:time'
50 plugin_attrib
= 'entity_time'
51 interfaces
= set(('tzo', 'utc', 'time'))
52 sub_interfaces
= interfaces
54 def set_time(self
, value
):
56 Set both the UTC and TZO fields given a time object.
59 value -- A datetime object or properly formatted
63 if not isinstance(value
, dt
.datetime
):
64 date
= xep_0082
.parse(value
)
66 self
['tzo'] = date
.tzinfo
70 Return the entity's local time based on the UTC and TZO data.
74 return date
.astimezone(tz
)
77 """Remove both the UTC and TZO fields."""
83 Return the timezone offset from UTC as a tzinfo object.
85 tzo
= self
._get
_sub
_text
('tzo')
88 time
= xep_0082
.parse('00:00:00%s' % tzo
)
91 def set_tzo(self
, value
):
93 Set the timezone offset from UTC.
96 value -- Either a tzinfo object or the number of
97 seconds (positive or negative) to offset.
99 time
= xep_0082
.time(offset
=value
)
100 if xep_0082
.parse(time
).tzinfo
== tzutc():
101 self
._set
_sub
_text
('tzo', 'Z')
103 self
._set
_sub
_text
('tzo', time
[-6:])
107 Return the time in UTC as a datetime object.
109 value
= self
._get
_sub
_text
('utc')
111 return xep_0082
.parse(xep_0082
.datetime())
112 return xep_0082
.parse('%sZ' % value
)
114 def set_utc(self
, value
):
119 value -- A datetime object or properly formatted
123 if not isinstance(value
, dt
.datetime
):
124 date
= xep_0082
.parse(value
)
125 date
= date
.astimezone(tzutc())
126 value
= xep_0082
.format_datetime(date
)[:-1]
127 self
._set
_sub
_text
('utc', value
)