interfaces.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. ##############################################################################
  2. #
  3. # Copyright (c) 2005 Zope Foundation and Contributors.
  4. #
  5. # This software is subject to the provisions of the Zope Public License,
  6. # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
  7. # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
  8. # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  9. # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
  10. # FOR A PARTICULAR PURPOSE
  11. #
  12. ##############################################################################
  13. from zope.interface import Interface
  14. class DateTimeError(Exception):
  15. pass
  16. class SyntaxError(DateTimeError):
  17. pass
  18. class DateError(DateTimeError):
  19. pass
  20. class TimeError(DateTimeError):
  21. pass
  22. class IDateTime(Interface):
  23. # Conversion and comparison methods
  24. #TODO determine whether this method really is part of the public API
  25. def localZone(ltm=None):
  26. '''Returns the time zone on the given date. The time zone
  27. can change according to daylight savings.'''
  28. def timeTime():
  29. """Return the date/time as a floating-point number in UTC, in
  30. the format used by the python time module. Note that it is
  31. possible to create date/time values with DateTime that have no
  32. meaningful value to the time module."""
  33. def toZone(z):
  34. """Return a DateTime with the value as the current object,
  35. represented in the indicated timezone."""
  36. def isFuture():
  37. """Return true if this object represents a date/time later
  38. than the time of the call"""
  39. def isPast():
  40. """Return true if this object represents a date/time earlier
  41. than the time of the call"""
  42. def isCurrentYear():
  43. """Return true if this object represents a date/time that
  44. falls within the current year, in the context of this
  45. object's timezone representation"""
  46. def isCurrentMonth():
  47. """Return true if this object represents a date/time that
  48. falls within the current month, in the context of this
  49. object's timezone representation"""
  50. def isCurrentDay():
  51. """Return true if this object represents a date/time that
  52. falls within the current day, in the context of this object's
  53. timezone representation"""
  54. def isCurrentHour():
  55. """Return true if this object represents a date/time that
  56. falls within the current hour, in the context of this object's
  57. timezone representation"""
  58. def isCurrentMinute():
  59. """Return true if this object represents a date/time that
  60. falls within the current minute, in the context of this
  61. object's timezone representation"""
  62. def isLeapYear():
  63. """Return true if the current year (in the context of the
  64. object's timezone) is a leap year"""
  65. def earliestTime():
  66. """Return a new DateTime object that represents the earliest
  67. possible time (in whole seconds) that still falls within the
  68. current object's day, in the object's timezone context"""
  69. def latestTime():
  70. """Return a new DateTime object that represents the latest
  71. possible time (in whole seconds) that still falls within the
  72. current object's day, in the object's timezone context"""
  73. def greaterThan(t):
  74. """Compare this DateTime object to another DateTime object OR
  75. a floating point number such as that which is returned by the
  76. python time module. Returns true if the object represents a
  77. date/time greater than the specified DateTime or time module
  78. style time. Revised to give more correct results through
  79. comparison of long integer milliseconds."""
  80. __gt__ = greaterThan
  81. def greaterThanEqualTo(t):
  82. """Compare this DateTime object to another DateTime object OR
  83. a floating point number such as that which is returned by the
  84. python time module. Returns true if the object represents a
  85. date/time greater than or equal to the specified DateTime or
  86. time module style time. Revised to give more correct results
  87. through comparison of long integer milliseconds."""
  88. __ge__ = greaterThanEqualTo
  89. def equalTo(t):
  90. """Compare this DateTime object to another DateTime object OR
  91. a floating point number such as that which is returned by the
  92. python time module. Returns true if the object represents a
  93. date/time equal to the specified DateTime or time module style
  94. time. Revised to give more correct results through comparison
  95. of long integer milliseconds."""
  96. __eq__ = equalTo
  97. def notEqualTo(t):
  98. """Compare this DateTime object to another DateTime object OR
  99. a floating point number such as that which is returned by the
  100. python time module. Returns true if the object represents a
  101. date/time not equal to the specified DateTime or time module
  102. style time. Revised to give more correct results through
  103. comparison of long integer milliseconds."""
  104. __ne__ = notEqualTo
  105. def lessThan(t):
  106. """Compare this DateTime object to another DateTime object OR
  107. a floating point number such as that which is returned by the
  108. python time module. Returns true if the object represents a
  109. date/time less than the specified DateTime or time module
  110. style time. Revised to give more correct results through
  111. comparison of long integer milliseconds."""
  112. __lt__ = lessThan
  113. def lessThanEqualTo(t):
  114. """Compare this DateTime object to another DateTime object OR
  115. a floating point number such as that which is returned by the
  116. python time module. Returns true if the object represents a
  117. date/time less than or equal to the specified DateTime or time
  118. module style time. Revised to give more correct results
  119. through comparison of long integer milliseconds."""
  120. __le__ = lessThanEqualTo
  121. # Component access
  122. def parts():
  123. """Return a tuple containing the calendar year, month, day,
  124. hour, minute second and timezone of the object"""
  125. def timezone():
  126. """Return the timezone in which the object is represented."""
  127. def tzoffset():
  128. """Return the timezone offset for the objects timezone."""
  129. def year():
  130. """Return the calendar year of the object"""
  131. def month():
  132. """Return the month of the object as an integer"""
  133. def Month():
  134. """Return the full month name"""
  135. def aMonth():
  136. """Return the abreviated month name."""
  137. def Mon():
  138. """Compatibility: see aMonth"""
  139. def pMonth():
  140. """Return the abreviated (with period) month name."""
  141. def Mon_():
  142. """Compatibility: see pMonth"""
  143. def day():
  144. """Return the integer day"""
  145. def Day():
  146. """Return the full name of the day of the week"""
  147. def DayOfWeek():
  148. """Compatibility: see Day"""
  149. def dayOfYear():
  150. """Return the day of the year, in context of the timezone
  151. representation of the object"""
  152. def aDay():
  153. """Return the abreviated name of the day of the week"""
  154. def pDay():
  155. """Return the abreviated (with period) name of the day of the
  156. week"""
  157. def Day_():
  158. """Compatibility: see pDay"""
  159. def dow():
  160. """Return the integer day of the week, where sunday is 0"""
  161. def dow_1():
  162. """Return the integer day of the week, where sunday is 1"""
  163. def h_12():
  164. """Return the 12-hour clock representation of the hour"""
  165. def h_24():
  166. """Return the 24-hour clock representation of the hour"""
  167. def ampm():
  168. """Return the appropriate time modifier (am or pm)"""
  169. def hour():
  170. """Return the 24-hour clock representation of the hour"""
  171. def minute():
  172. """Return the minute"""
  173. def second():
  174. """Return the second"""
  175. def millis():
  176. """Return the millisecond since the epoch in GMT."""
  177. def strftime(format):
  178. """Format the date/time using the *current timezone representation*."""
  179. # General formats from previous DateTime
  180. def Date():
  181. """Return the date string for the object."""
  182. def Time():
  183. """Return the time string for an object to the nearest second."""
  184. def TimeMinutes():
  185. """Return the time string for an object not showing seconds."""
  186. def AMPM():
  187. """Return the time string for an object to the nearest second."""
  188. def AMPMMinutes():
  189. """Return the time string for an object not showing seconds."""
  190. def PreciseTime():
  191. """Return the time string for the object."""
  192. def PreciseAMPM():
  193. """Return the time string for the object."""
  194. def yy():
  195. """Return calendar year as a 2 digit string"""
  196. def mm():
  197. """Return month as a 2 digit string"""
  198. def dd():
  199. """Return day as a 2 digit string"""
  200. def rfc822():
  201. """Return the date in RFC 822 format"""
  202. # New formats
  203. def fCommon():
  204. """Return a string representing the object's value in the
  205. format: March 1, 1997 1:45 pm"""
  206. def fCommonZ():
  207. """Return a string representing the object's value in the
  208. format: March 1, 1997 1:45 pm US/Eastern"""
  209. def aCommon():
  210. """Return a string representing the object's value in the
  211. format: Mar 1, 1997 1:45 pm"""
  212. def aCommonZ():
  213. """Return a string representing the object's value in the
  214. format: Mar 1, 1997 1:45 pm US/Eastern"""
  215. def pCommon():
  216. """Return a string representing the object's value in the
  217. format: Mar. 1, 1997 1:45 pm"""
  218. def pCommonZ():
  219. """Return a string representing the object's value
  220. in the format: Mar. 1, 1997 1:45 pm US/Eastern"""
  221. def ISO():
  222. """Return the object in ISO standard format. Note: this is
  223. *not* ISO 8601-format! See the ISO8601 and HTML4 methods below
  224. for ISO 8601-compliant output
  225. Dates are output as: YYYY-MM-DD HH:MM:SS
  226. """
  227. def ISO8601():
  228. """Return the object in ISO 8601-compatible format containing
  229. the date, time with seconds-precision and the time zone
  230. identifier - see http://www.w3.org/TR/NOTE-datetime
  231. Dates are output as: YYYY-MM-DDTHH:MM:SSTZD
  232. T is a literal character.
  233. TZD is Time Zone Designator, format +HH:MM or -HH:MM
  234. The HTML4 method below offers the same formatting, but
  235. converts to UTC before returning the value and sets the TZD"Z"
  236. """
  237. def HTML4():
  238. """Return the object in the format used in the HTML4.0
  239. specification, one of the standard forms in ISO8601. See
  240. http://www.w3.org/TR/NOTE-datetime
  241. Dates are output as: YYYY-MM-DDTHH:MM:SSZ
  242. T, Z are literal characters.
  243. The time is in UTC.
  244. """
  245. def JulianDay():
  246. """Return the Julian day according to
  247. http://www.tondering.dk/claus/cal/node3.html#sec-calcjd
  248. """
  249. def week():
  250. """Return the week number according to ISO
  251. see http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000
  252. """
  253. # Python operator and conversion API
  254. def __add__(other):
  255. """A DateTime may be added to a number and a number may be
  256. added to a DateTime; two DateTimes cannot be added."""
  257. __radd__ = __add__
  258. def __sub__(other):
  259. """Either a DateTime or a number may be subtracted from a
  260. DateTime, however, a DateTime may not be subtracted from a
  261. number."""
  262. def __repr__():
  263. """Convert a DateTime to a string that looks like a Python
  264. expression."""
  265. def __str__():
  266. """Convert a DateTime to a string."""
  267. def __hash__():
  268. """Compute a hash value for a DateTime"""
  269. def __int__():
  270. """Convert to an integer number of seconds since the epoch (gmt)"""
  271. def __long__():
  272. """Convert to a long-int number of seconds since the epoch (gmt)"""
  273. def __float__():
  274. """Convert to floating-point number of seconds since the epoch (gmt)"""