2 Slixmpp: The Slick XMPP Library
3 Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
4 This file is part of Slixmpp.
6 See the file LICENSE for copying permission.
11 from slixmpp
.plugins
import BasePlugin
, register_plugin
12 from slixmpp
.thirdparty
import tzutc
, tzoffset
, parse_iso
15 # =====================================================================
16 # To make it easier for stanzas without direct access to plugin objects
17 # to use the XEP-0082 utility methods, we will define them as top-level
18 # functions and then just reference them in the plugin itself.
22 Convert a string timestamp into a datetime object.
25 time_str -- A formatted timestamp string.
27 return parse_iso(time_str
)
30 def format_date(time_obj
):
32 Return a formatted string version of a date object.
38 time_obj -- A date or datetime object.
40 if isinstance(time_obj
, dt
.datetime
):
41 time_obj
= time_obj
.date()
42 return time_obj
.isoformat()
45 def format_time(time_obj
):
47 Return a formatted string version of a time object.
53 time_obj -- A time or datetime object.
55 if isinstance(time_obj
, dt
.datetime
):
56 time_obj
= time_obj
.timetz()
57 timestamp
= time_obj
.isoformat()
58 if time_obj
.tzinfo
== tzutc():
59 timestamp
= timestamp
[:-6]
60 return '%sZ' % timestamp
64 def format_datetime(time_obj
):
66 Return a formatted string version of a datetime object.
69 YYYY-MM-DDThh:mm:ss[.sss]TZD
72 time_obj -- A datetime object.
74 timestamp
= time_obj
.isoformat('T')
75 if time_obj
.tzinfo
== tzutc():
76 timestamp
= timestamp
[:-6]
77 return '%sZ' % timestamp
81 def date(year
=None, month
=None, day
=None, obj
=False):
83 Create a date only timestamp for the given instant.
85 Unspecified components default to their current counterparts.
88 year -- Integer value of the year (4 digits)
89 month -- Integer value of the month
90 day -- Integer value of the day of the month.
91 obj -- If True, return the date object instead
92 of a formatted string. Defaults to False.
94 today
= dt
.datetime
.utcnow()
101 value
= dt
.date(year
, month
, day
)
104 return format_date(value
)
107 def time(hour
=None, min=None, sec
=None, micro
=None, offset
=None, obj
=False):
109 Create a time only timestamp for the given instant.
111 Unspecified components default to their current counterparts.
114 hour -- Integer value of the hour.
115 min -- Integer value of the number of minutes.
116 sec -- Integer value of the number of seconds.
117 micro -- Integer value of the number of microseconds.
118 offset -- Either a positive or negative number of seconds
119 to offset from UTC to match a desired timezone,
121 obj -- If True, return the time object instead
122 of a formatted string. Defaults to False.
124 now
= dt
.datetime
.utcnow()
132 micro
= now
.microsecond
135 elif not isinstance(offset
, dt
.tzinfo
):
136 offset
= tzoffset(None, offset
)
137 value
= dt
.time(hour
, min, sec
, micro
, offset
)
140 return format_time(value
)
143 def datetime(year
=None, month
=None, day
=None, hour
=None,
144 min=None, sec
=None, micro
=None, offset
=None,
145 separators
=True, obj
=False):
147 Create a datetime timestamp for the given instant.
149 Unspecified components default to their current counterparts.
152 year -- Integer value of the year (4 digits)
153 month -- Integer value of the month
154 day -- Integer value of the day of the month.
155 hour -- Integer value of the hour.
156 min -- Integer value of the number of minutes.
157 sec -- Integer value of the number of seconds.
158 micro -- Integer value of the number of microseconds.
159 offset -- Either a positive or negative number of seconds
160 to offset from UTC to match a desired timezone,
162 obj -- If True, return the datetime object instead
163 of a formatted string. Defaults to False.
165 now
= dt
.datetime
.utcnow()
179 micro
= now
.microsecond
182 elif not isinstance(offset
, dt
.tzinfo
):
183 offset
= tzoffset(None, offset
)
185 value
= dt
.datetime(year
, month
, day
, hour
,
186 min, sec
, micro
, offset
)
189 return format_datetime(value
)
192 class XEP_0082(BasePlugin
):
195 XEP-0082: XMPP Date and Time Profiles
197 XMPP uses a subset of the formats allowed by ISO 8601 as a matter of
198 pragmatism based on the relatively few formats historically used by
201 Also see <http://www.xmpp.org/extensions/xep-0082.html>.
204 date -- Create a time stamp using the Date profile.
205 datetime -- Create a time stamp using the DateTime profile.
206 time -- Create a time stamp using the Time profile.
207 format_date -- Format an existing date object.
208 format_datetime -- Format an existing datetime object.
209 format_time -- Format an existing time object.
210 parse -- Convert a time string into a Python datetime object.
214 description
= 'XEP-0082: XMPP Date and Time Profiles'
217 def plugin_init(self
):
218 """Start the XEP-0082 plugin."""
220 self
.datetime
= datetime
222 self
.format_date
= format_date
223 self
.format_datetime
= format_datetime
224 self
.format_time
= format_time
228 register_plugin(XEP_0082
)