Bump to 1.3.1
[slixmpp.git] / sleekxmpp / plugins / xep_0202 / stanza.py
blobb6ccc960163ebc4d171ff357a7c08008fe1f49b8
1 """
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.
7 """
9 import logging
10 import datetime as dt
12 from sleekxmpp.xmlstream import ElementBase
13 from sleekxmpp.plugins import xep_0082
14 from sleekxmpp.thirdparty import tzutc, tzoffset
17 class EntityTime(ElementBase):
19 """
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
23 included.
25 Example <time> stanzas:
26 <iq type="result">
27 <time xmlns="urn:xmpp:time">
28 <utc>2011-07-03T11:37:12.234569</utc>
29 <tzo>-07:00</tzo>
30 </time>
31 </iq>
33 Stanza Interface:
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.
38 Methods:
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.
46 """
48 name = 'time'
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):
55 """
56 Set both the UTC and TZO fields given a time object.
58 Arguments:
59 value -- A datetime object or properly formatted
60 string equivalent.
61 """
62 date = value
63 if not isinstance(value, dt.datetime):
64 date = xep_0082.parse(value)
65 self['utc'] = date
66 self['tzo'] = date.tzinfo
68 def get_time(self):
69 """
70 Return the entity's local time based on the UTC and TZO data.
71 """
72 date = self['utc']
73 tz = self['tzo']
74 return date.astimezone(tz)
76 def del_time(self):
77 """Remove both the UTC and TZO fields."""
78 del self['utc']
79 del self['tzo']
81 def get_tzo(self):
82 """
83 Return the timezone offset from UTC as a tzinfo object.
84 """
85 tzo = self._get_sub_text('tzo')
86 if tzo == '':
87 tzo = 'Z'
88 time = xep_0082.parse('00:00:00%s' % tzo)
89 return time.tzinfo
91 def set_tzo(self, value):
92 """
93 Set the timezone offset from UTC.
95 Arguments:
96 value -- Either a tzinfo object or the number of
97 seconds (positive or negative) to offset.
98 """
99 time = xep_0082.time(offset=value)
100 if xep_0082.parse(time).tzinfo == tzutc():
101 self._set_sub_text('tzo', 'Z')
102 else:
103 self._set_sub_text('tzo', time[-6:])
105 def get_utc(self):
107 Return the time in UTC as a datetime object.
109 value = self._get_sub_text('utc')
110 if value == '':
111 return xep_0082.parse(xep_0082.datetime())
112 return xep_0082.parse('%sZ' % value)
114 def set_utc(self, value):
116 Set the time in UTC.
118 Arguments:
119 value -- A datetime object or properly formatted
120 string equivalent.
122 date = value
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)