xep_0082.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. """
  2. SleekXMPP: The Sleek XMPP Library
  3. Copyright (C) 2011 Nathanael C. Fritz, Lance J.T. Stout
  4. This file is part of SleekXMPP.
  5. See the file LICENSE for copying permission.
  6. """
  7. import datetime as dt
  8. from sleekxmpp.plugins import BasePlugin, register_plugin
  9. from sleekxmpp.thirdparty import tzutc, tzoffset, parse_iso
  10. # =====================================================================
  11. # To make it easier for stanzas without direct access to plugin objects
  12. # to use the XEP-0082 utility methods, we will define them as top-level
  13. # functions and then just reference them in the plugin itself.
  14. def parse(time_str):
  15. """
  16. Convert a string timestamp into a datetime object.
  17. Arguments:
  18. time_str -- A formatted timestamp string.
  19. """
  20. return parse_iso(time_str)
  21. def format_date(time_obj):
  22. """
  23. Return a formatted string version of a date object.
  24. Format:
  25. YYYY-MM-DD
  26. Arguments:
  27. time_obj -- A date or datetime object.
  28. """
  29. if isinstance(time_obj, dt.datetime):
  30. time_obj = time_obj.date()
  31. return time_obj.isoformat()
  32. def format_time(time_obj):
  33. """
  34. Return a formatted string version of a time object.
  35. format:
  36. hh:mm:ss[.sss][TZD]
  37. arguments:
  38. time_obj -- A time or datetime object.
  39. """
  40. if isinstance(time_obj, dt.datetime):
  41. time_obj = time_obj.timetz()
  42. timestamp = time_obj.isoformat()
  43. if time_obj.tzinfo == tzutc():
  44. timestamp = timestamp[:-6]
  45. return '%sZ' % timestamp
  46. return timestamp
  47. def format_datetime(time_obj):
  48. """
  49. Return a formatted string version of a datetime object.
  50. Format:
  51. YYYY-MM-DDThh:mm:ss[.sss]TZD
  52. arguments:
  53. time_obj -- A datetime object.
  54. """
  55. timestamp = time_obj.isoformat('T')
  56. if time_obj.tzinfo == tzutc():
  57. timestamp = timestamp[:-6]
  58. return '%sZ' % timestamp
  59. return timestamp
  60. def date(year=None, month=None, day=None, obj=False):
  61. """
  62. Create a date only timestamp for the given instant.
  63. Unspecified components default to their current counterparts.
  64. Arguments:
  65. year -- Integer value of the year (4 digits)
  66. month -- Integer value of the month
  67. day -- Integer value of the day of the month.
  68. obj -- If True, return the date object instead
  69. of a formatted string. Defaults to False.
  70. """
  71. today = dt.datetime.utcnow()
  72. if year is None:
  73. year = today.year
  74. if month is None:
  75. month = today.month
  76. if day is None:
  77. day = today.day
  78. value = dt.date(year, month, day)
  79. if obj:
  80. return value
  81. return format_date(value)
  82. def time(hour=None, min=None, sec=None, micro=None, offset=None, obj=False):
  83. """
  84. Create a time only timestamp for the given instant.
  85. Unspecified components default to their current counterparts.
  86. Arguments:
  87. hour -- Integer value of the hour.
  88. min -- Integer value of the number of minutes.
  89. sec -- Integer value of the number of seconds.
  90. micro -- Integer value of the number of microseconds.
  91. offset -- Either a positive or negative number of seconds
  92. to offset from UTC to match a desired timezone,
  93. or a tzinfo object.
  94. obj -- If True, return the time object instead
  95. of a formatted string. Defaults to False.
  96. """
  97. now = dt.datetime.utcnow()
  98. if hour is None:
  99. hour = now.hour
  100. if min is None:
  101. min = now.minute
  102. if sec is None:
  103. sec = now.second
  104. if micro is None:
  105. micro = now.microsecond
  106. if offset is None:
  107. offset = tzutc()
  108. elif not isinstance(offset, dt.tzinfo):
  109. offset = tzoffset(None, offset)
  110. value = dt.time(hour, min, sec, micro, offset)
  111. if obj:
  112. return value
  113. return format_time(value)
  114. def datetime(year=None, month=None, day=None, hour=None,
  115. min=None, sec=None, micro=None, offset=None,
  116. separators=True, obj=False):
  117. """
  118. Create a datetime timestamp for the given instant.
  119. Unspecified components default to their current counterparts.
  120. Arguments:
  121. year -- Integer value of the year (4 digits)
  122. month -- Integer value of the month
  123. day -- Integer value of the day of the month.
  124. hour -- Integer value of the hour.
  125. min -- Integer value of the number of minutes.
  126. sec -- Integer value of the number of seconds.
  127. micro -- Integer value of the number of microseconds.
  128. offset -- Either a positive or negative number of seconds
  129. to offset from UTC to match a desired timezone,
  130. or a tzinfo object.
  131. obj -- If True, return the datetime object instead
  132. of a formatted string. Defaults to False.
  133. """
  134. now = dt.datetime.utcnow()
  135. if year is None:
  136. year = now.year
  137. if month is None:
  138. month = now.month
  139. if day is None:
  140. day = now.day
  141. if hour is None:
  142. hour = now.hour
  143. if min is None:
  144. min = now.minute
  145. if sec is None:
  146. sec = now.second
  147. if micro is None:
  148. micro = now.microsecond
  149. if offset is None:
  150. offset = tzutc()
  151. elif not isinstance(offset, dt.tzinfo):
  152. offset = tzoffset(None, offset)
  153. value = dt.datetime(year, month, day, hour,
  154. min, sec, micro, offset)
  155. if obj:
  156. return value
  157. return format_datetime(value)
  158. class XEP_0082(BasePlugin):
  159. """
  160. XEP-0082: XMPP Date and Time Profiles
  161. XMPP uses a subset of the formats allowed by ISO 8601 as a matter of
  162. pragmatism based on the relatively few formats historically used by
  163. the XMPP.
  164. Also see <http://www.xmpp.org/extensions/xep-0082.html>.
  165. Methods:
  166. date -- Create a time stamp using the Date profile.
  167. datetime -- Create a time stamp using the DateTime profile.
  168. time -- Create a time stamp using the Time profile.
  169. format_date -- Format an existing date object.
  170. format_datetime -- Format an existing datetime object.
  171. format_time -- Format an existing time object.
  172. parse -- Convert a time string into a Python datetime object.
  173. """
  174. name = 'xep_0082'
  175. description = 'XEP-0082: XMPP Date and Time Profiles'
  176. dependencies = set()
  177. def plugin_init(self):
  178. """Start the XEP-0082 plugin."""
  179. self.date = date
  180. self.datetime = datetime
  181. self.time = time
  182. self.format_date = format_date
  183. self.format_datetime = format_datetime
  184. self.format_time = format_time
  185. self.parse = parse
  186. register_plugin(XEP_0082)