DateTime.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. The DateTime package
  2. ====================
  3. Encapsulation of date/time values.
  4. Function Timezones()
  5. --------------------
  6. Returns the list of recognized timezone names:
  7. >>> from DateTime import Timezones
  8. >>> zones = set(Timezones())
  9. Almost all of the standard pytz timezones are included, with the exception
  10. of some commonly-used but ambiguous abbreviations, where historical Zope
  11. usage conflicts with the name used by pytz:
  12. >>> import pytz
  13. >>> [x for x in pytz.all_timezones if x not in zones]
  14. ['CET', 'EET', 'EST', 'MET', 'MST', 'WET']
  15. Class DateTime
  16. --------------
  17. DateTime objects represent instants in time and provide interfaces for
  18. controlling its representation without affecting the absolute value of
  19. the object.
  20. DateTime objects may be created from a wide variety of string or
  21. numeric data, or may be computed from other DateTime objects.
  22. DateTimes support the ability to convert their representations to many
  23. major timezones, as well as the ablility to create a DateTime object
  24. in the context of a given timezone.
  25. DateTime objects provide partial numerical behavior:
  26. * Two date-time objects can be subtracted to obtain a time, in days
  27. between the two.
  28. * A date-time object and a positive or negative number may be added to
  29. obtain a new date-time object that is the given number of days later
  30. than the input date-time object.
  31. * A positive or negative number and a date-time object may be added to
  32. obtain a new date-time object that is the given number of days later
  33. than the input date-time object.
  34. * A positive or negative number may be subtracted from a date-time
  35. object to obtain a new date-time object that is the given number of
  36. days earlier than the input date-time object.
  37. DateTime objects may be converted to integer, long, or float numbers
  38. of days since January 1, 1901, using the standard int, long, and float
  39. functions (Compatibility Note: int, long and float return the number
  40. of days since 1901 in GMT rather than local machine timezone).
  41. DateTime objects also provide access to their value in a float format
  42. usable with the python time module, provided that the value of the
  43. object falls in the range of the epoch-based time module.
  44. A DateTime object should be considered immutable; all conversion and numeric
  45. operations return a new DateTime object rather than modify the current object.
  46. A DateTime object always maintains its value as an absolute UTC time,
  47. and is represented in the context of some timezone based on the
  48. arguments used to create the object. A DateTime object's methods
  49. return values based on the timezone context.
  50. Note that in all cases the local machine timezone is used for
  51. representation if no timezone is specified.
  52. Constructor for DateTime
  53. ------------------------
  54. DateTime() returns a new date-time object. DateTimes may be created
  55. with from zero to seven arguments:
  56. * If the function is called with no arguments, then the current date/
  57. time is returned, represented in the timezone of the local machine.
  58. * If the function is invoked with a single string argument which is a
  59. recognized timezone name, an object representing the current time is
  60. returned, represented in the specified timezone.
  61. * If the function is invoked with a single string argument
  62. representing a valid date/time, an object representing that date/
  63. time will be returned.
  64. As a general rule, any date-time representation that is recognized
  65. and unambigous to a resident of North America is acceptable. (The
  66. reason for this qualification is that in North America, a date like:
  67. 2/1/1994 is interpreted as February 1, 1994, while in some parts of
  68. the world, it is interpreted as January 2, 1994.) A date/ time
  69. string consists of two components, a date component and an optional
  70. time component, separated by one or more spaces. If the time
  71. component is omited, 12:00am is assumed.
  72. Any recognized timezone name specified as the final element of the
  73. date/time string will be used for computing the date/time value.
  74. (If you create a DateTime with the string,
  75. "Mar 9, 1997 1:45pm US/Pacific", the value will essentially be the
  76. same as if you had captured time.time() at the specified date and
  77. time on a machine in that timezone). If no timezone is passed, then
  78. the timezone configured on the local machine will be used, **except**
  79. that if the date format matches ISO 8601 ('YYYY-MM-DD'), the instance
  80. will use UTC / CMT+0 as the timezone.
  81. o Returns current date/time, represented in US/Eastern:
  82. >>> from DateTime import DateTime
  83. >>> e = DateTime('US/Eastern')
  84. >>> e.timezone()
  85. 'US/Eastern'
  86. o Returns specified time, represented in local machine zone:
  87. >>> x = DateTime('1997/3/9 1:45pm')
  88. >>> x.parts() # doctest: +ELLIPSIS
  89. (1997, 3, 9, 13, 45, ...)
  90. o Specified time in local machine zone, verbose format:
  91. >>> y = DateTime('Mar 9, 1997 13:45:00')
  92. >>> y.parts() # doctest: +ELLIPSIS
  93. (1997, 3, 9, 13, 45, ...)
  94. >>> y == x
  95. True
  96. o Specified time in UTC via ISO 8601 rule:
  97. >>> z = DateTime('2014-03-24')
  98. >>> z.parts() # doctest: +ELLIPSIS
  99. (2014, 3, 24, 0, 0, ...)
  100. >>> z.timezone()
  101. 'GMT+0'
  102. The date component consists of year, month, and day values. The
  103. year value must be a one-, two-, or four-digit integer. If a one-
  104. or two-digit year is used, the year is assumed to be in the
  105. twentieth century. The month may an integer, from 1 to 12, a month
  106. name, or a month abreviation, where a period may optionally follow
  107. the abreviation. The day must be an integer from 1 to the number of
  108. days in the month. The year, month, and day values may be separated
  109. by periods, hyphens, forward, shashes, or spaces. Extra spaces are
  110. permitted around the delimiters. Year, month, and day values may be
  111. given in any order as long as it is possible to distinguish the
  112. components. If all three components are numbers that are less than
  113. 13, then a a month-day-year ordering is assumed.
  114. The time component consists of hour, minute, and second values
  115. separated by colons. The hour value must be an integer between 0
  116. and 23 inclusively. The minute value must be an integer between 0
  117. and 59 inclusively. The second value may be an integer value
  118. between 0 and 59.999 inclusively. The second value or both the
  119. minute and second values may be ommitted. The time may be followed
  120. by am or pm in upper or lower case, in which case a 12-hour clock is
  121. assumed.
  122. * If the DateTime function is invoked with a single Numeric argument,
  123. the number is assumed to be either a floating point value such as
  124. that returned by time.time() , or a number of days after January 1,
  125. 1901 00:00:00 UTC.
  126. A DateTime object is returned that represents either the gmt value
  127. of the time.time() float represented in the local machine's
  128. timezone, or that number of days after January 1, 1901. Note that
  129. the number of days after 1901 need to be expressed from the
  130. viewpoint of the local machine's timezone. A negative argument will
  131. yield a date-time value before 1901.
  132. * If the function is invoked with two numeric arguments, then the
  133. first is taken to be an integer year and the second argument is
  134. taken to be an offset in days from the beginning of the year, in the
  135. context of the local machine timezone. The date-time value returned
  136. is the given offset number of days from the beginning of the given
  137. year, represented in the timezone of the local machine. The offset
  138. may be positive or negative. Two-digit years are assumed to be in
  139. the twentieth century.
  140. * If the function is invoked with two arguments, the first a float
  141. representing a number of seconds past the epoch in gmt (such as
  142. those returned by time.time()) and the second a string naming a
  143. recognized timezone, a DateTime with a value of that gmt time will
  144. be returned, represented in the given timezone.
  145. >>> import time
  146. >>> t = time.time()
  147. Time t represented as US/Eastern:
  148. >>> now_east = DateTime(t, 'US/Eastern')
  149. Time t represented as US/Pacific:
  150. >>> now_west = DateTime(t, 'US/Pacific')
  151. Only their representations are different:
  152. >>> now_east.equalTo(now_west)
  153. True
  154. * If the function is invoked with three or more numeric arguments,
  155. then the first is taken to be an integer year, the second is taken
  156. to be an integer month, and the third is taken to be an integer day.
  157. If the combination of values is not valid, then a DateTimeError is
  158. raised. One- or two-digit years up to 69 are assumed to be in the
  159. 21st century, whereas values 70-99 are assumed to be 20th century.
  160. The fourth, fifth, and sixth arguments are floating point, positive
  161. or negative offsets in units of hours, minutes, and days, and
  162. default to zero if not given. An optional string may be given as
  163. the final argument to indicate timezone (the effect of this is as if
  164. you had taken the value of time.time() at that time on a machine in
  165. the specified timezone).
  166. If a string argument passed to the DateTime constructor cannot be
  167. parsed, it will raise SyntaxError. Invalid date, time, or
  168. timezone components will raise a DateTimeError.
  169. The module function Timezones() will return a list of the timezones
  170. recognized by the DateTime module. Recognition of timezone names is
  171. case-insensitive.
  172. Instance Methods for DateTime (IDateTime interface)
  173. ---------------------------------------------------
  174. Conversion and comparison methods
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  176. * ``timeTime()`` returns the date/time as a floating-point number in
  177. UTC, in the format used by the python time module. Note that it is
  178. possible to create date /time values with DateTime that have no
  179. meaningful value to the time module, and in such cases a
  180. DateTimeError is raised. A DateTime object's value must generally
  181. be between Jan 1, 1970 (or your local machine epoch) and Jan 2038 to
  182. produce a valid time.time() style value.
  183. >>> dt = DateTime('Mar 9, 1997 13:45:00 US/Eastern')
  184. >>> dt.timeTime()
  185. 857933100.0
  186. >>> DateTime('2040/01/01 UTC').timeTime()
  187. 2208988800.0
  188. >>> DateTime('1900/01/01 UTC').timeTime()
  189. -2208988800.0
  190. * ``toZone(z)`` returns a DateTime with the value as the current
  191. object, represented in the indicated timezone:
  192. >>> dt.toZone('UTC')
  193. DateTime('1997/03/09 18:45:00 UTC')
  194. >>> dt.toZone('UTC').equalTo(dt)
  195. True
  196. * ``isFuture()`` returns true if this object represents a date/time
  197. later than the time of the call:
  198. >>> dt.isFuture()
  199. False
  200. >>> DateTime('Jan 1 3000').isFuture() # not time-machine safe!
  201. True
  202. * ``isPast()`` returns true if this object represents a date/time
  203. earlier than the time of the call:
  204. >>> dt.isPast()
  205. True
  206. >>> DateTime('Jan 1 3000').isPast() # not time-machine safe!
  207. False
  208. * ``isCurrentYear()`` returns true if this object represents a
  209. date/time that falls within the current year, in the context of this
  210. object's timezone representation:
  211. >>> dt.isCurrentYear()
  212. False
  213. >>> DateTime().isCurrentYear()
  214. True
  215. * ``isCurrentMonth()`` returns true if this object represents a
  216. date/time that falls within the current month, in the context of
  217. this object's timezone representation:
  218. >>> dt.isCurrentMonth()
  219. False
  220. >>> DateTime().isCurrentMonth()
  221. True
  222. * ``isCurrentDay()`` returns true if this object represents a
  223. date/time that falls within the current day, in the context of this
  224. object's timezone representation:
  225. >>> dt.isCurrentDay()
  226. False
  227. >>> DateTime().isCurrentDay()
  228. True
  229. * ``isCurrentHour()`` returns true if this object represents a
  230. date/time that falls within the current hour, in the context of this
  231. object's timezone representation:
  232. >>> dt.isCurrentHour()
  233. False
  234. >>> DateTime().isCurrentHour()
  235. True
  236. * ``isCurrentMinute()`` returns true if this object represents a
  237. date/time that falls within the current minute, in the context of
  238. this object's timezone representation:
  239. >>> dt.isCurrentMinute()
  240. False
  241. >>> DateTime().isCurrentMinute()
  242. True
  243. * ``isLeapYear()`` returns true if the current year (in the context of
  244. the object's timezone) is a leap year:
  245. >>> dt.isLeapYear()
  246. False
  247. >>> DateTime('Mar 8 2004').isLeapYear()
  248. True
  249. * ``earliestTime()`` returns a new DateTime object that represents the
  250. earliest possible time (in whole seconds) that still falls within
  251. the current object's day, in the object's timezone context:
  252. >>> dt.earliestTime()
  253. DateTime('1997/03/09 00:00:00 US/Eastern')
  254. * ``latestTime()`` return a new DateTime object that represents the
  255. latest possible time (in whole seconds) that still falls within the
  256. current object's day, in the object's timezone context
  257. >>> dt.latestTime()
  258. DateTime('1997/03/09 23:59:59 US/Eastern')
  259. Component access
  260. ~~~~~~~~~~~~~~~~
  261. * ``parts()`` returns a tuple containing the calendar year, month,
  262. day, hour, minute second and timezone of the object
  263. >>> dt.parts() # doctest: +ELLIPSIS
  264. (1997, 3, 9, 13, 45, ... 'US/Eastern')
  265. * ``timezone()`` returns the timezone in which the object is represented:
  266. >>> dt.timezone() in Timezones()
  267. True
  268. * ``tzoffset()`` returns the timezone offset for the objects timezone:
  269. >>> dt.tzoffset()
  270. -18000
  271. * ``year()`` returns the calendar year of the object:
  272. >>> dt.year()
  273. 1997
  274. * ``month()`` retursn the month of the object as an integer:
  275. >>> dt.month()
  276. 3
  277. * ``Month()`` returns the full month name:
  278. >>> dt.Month()
  279. 'March'
  280. * ``aMonth()`` returns the abreviated month name:
  281. >>> dt.aMonth()
  282. 'Mar'
  283. * ``pMonth()`` returns the abreviated (with period) month name:
  284. >>> dt.pMonth()
  285. 'Mar.'
  286. * ``day()`` returns the integer day:
  287. >>> dt.day()
  288. 9
  289. * ``Day()`` returns the full name of the day of the week:
  290. >>> dt.Day()
  291. 'Sunday'
  292. * ``dayOfYear()`` returns the day of the year, in context of the
  293. timezone representation of the object:
  294. >>> dt.dayOfYear()
  295. 68
  296. * ``aDay()`` returns the abreviated name of the day of the week:
  297. >>> dt.aDay()
  298. 'Sun'
  299. * ``pDay()`` returns the abreviated (with period) name of the day of
  300. the week:
  301. >>> dt.pDay()
  302. 'Sun.'
  303. * ``dow()`` returns the integer day of the week, where Sunday is 0:
  304. >>> dt.dow()
  305. 0
  306. * ``dow_1()`` returns the integer day of the week, where sunday is 1:
  307. >>> dt.dow_1()
  308. 1
  309. * ``h_12()`` returns the 12-hour clock representation of the hour:
  310. >>> dt.h_12()
  311. 1
  312. * ``h_24()`` returns the 24-hour clock representation of the hour:
  313. >>> dt.h_24()
  314. 13
  315. * ``ampm()`` returns the appropriate time modifier (am or pm):
  316. >>> dt.ampm()
  317. 'pm'
  318. * ``hour()`` returns the 24-hour clock representation of the hour:
  319. >>> dt.hour()
  320. 13
  321. * ``minute()`` returns the minute:
  322. >>> dt.minute()
  323. 45
  324. * ``second()`` returns the second:
  325. >>> dt.second() == 0
  326. True
  327. * ``millis()`` returns the milliseconds since the epoch in GMT.
  328. >>> dt.millis() == 857933100000
  329. True
  330. strftime()
  331. ~~~~~~~~~~
  332. See ``tests/test_datetime.py``.
  333. General formats from previous DateTime
  334. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  335. * ``Date()`` return the date string for the object:
  336. >>> dt.Date()
  337. '1997/03/09'
  338. * ``Time()`` returns the time string for an object to the nearest
  339. second:
  340. >>> dt.Time()
  341. '13:45:00'
  342. * ``TimeMinutes()`` returns the time string for an object not showing
  343. seconds:
  344. >>> dt.TimeMinutes()
  345. '13:45'
  346. * ``AMPM()`` returns the time string for an object to the nearest second:
  347. >>> dt.AMPM()
  348. '01:45:00 pm'
  349. * ``AMPMMinutes()`` returns the time string for an object not showing
  350. seconds:
  351. >>> dt.AMPMMinutes()
  352. '01:45 pm'
  353. * ``PreciseTime()`` returns the time string for the object:
  354. >>> dt.PreciseTime()
  355. '13:45:00.000'
  356. * ``PreciseAMPM()`` returns the time string for the object:
  357. >>> dt.PreciseAMPM()
  358. '01:45:00.000 pm'
  359. * ``yy()`` returns the calendar year as a 2 digit string
  360. >>> dt.yy()
  361. '97'
  362. * ``mm()`` returns the month as a 2 digit string
  363. >>> dt.mm()
  364. '03'
  365. * ``dd()`` returns the day as a 2 digit string:
  366. >>> dt.dd()
  367. '09'
  368. * ``rfc822()`` returns the date in RFC 822 format:
  369. >>> dt.rfc822()
  370. 'Sun, 09 Mar 1997 13:45:00 -0500'
  371. New formats
  372. ~~~~~~~~~~~
  373. * ``fCommon()`` returns a string representing the object's value in
  374. the format: March 9, 1997 1:45 pm:
  375. >>> dt.fCommon()
  376. 'March 9, 1997 1:45 pm'
  377. * ``fCommonZ()`` returns a string representing the object's value in
  378. the format: March 9, 1997 1:45 pm US/Eastern:
  379. >>> dt.fCommonZ()
  380. 'March 9, 1997 1:45 pm US/Eastern'
  381. * ``aCommon()`` returns a string representing the object's value in
  382. the format: Mar 9, 1997 1:45 pm:
  383. >>> dt.aCommon()
  384. 'Mar 9, 1997 1:45 pm'
  385. * ``aCommonZ()`` return a string representing the object's value in
  386. the format: Mar 9, 1997 1:45 pm US/Eastern:
  387. >>> dt.aCommonZ()
  388. 'Mar 9, 1997 1:45 pm US/Eastern'
  389. * ``pCommon()`` returns a string representing the object's value in
  390. the format Mar. 9, 1997 1:45 pm:
  391. >>> dt.pCommon()
  392. 'Mar. 9, 1997 1:45 pm'
  393. * ``pCommonZ()`` returns a string representing the object's value in
  394. the format: Mar. 9, 1997 1:45 pm US/Eastern:
  395. >>> dt.pCommonZ()
  396. 'Mar. 9, 1997 1:45 pm US/Eastern'
  397. * ``ISO()`` returns a string with the date/time in ISO format. Note:
  398. this is not ISO 8601-format! See the ISO8601 and HTML4 methods below
  399. for ISO 8601-compliant output. Dates are output as: YYYY-MM-DD HH:MM:SS
  400. >>> dt.ISO()
  401. '1997-03-09 13:45:00'
  402. * ``ISO8601()`` returns the object in ISO 8601-compatible format
  403. containing the date, time with seconds-precision and the time zone
  404. identifier - see http://www.w3.org/TR/NOTE-datetime. Dates are
  405. output as: YYYY-MM-DDTHH:MM:SSTZD (T is a literal character, TZD is
  406. Time Zone Designator, format +HH:MM or -HH:MM).
  407. The ``HTML4()`` method below offers the same formatting, but
  408. converts to UTC before returning the value and sets the TZD"Z"
  409. >>> dt.ISO8601()
  410. '1997-03-09T13:45:00-05:00'
  411. * ``HTML4()`` returns the object in the format used in the HTML4.0
  412. specification, one of the standard forms in ISO8601. See
  413. http://www.w3.org/TR/NOTE-datetime. Dates are output as:
  414. YYYY-MM-DDTHH:MM:SSZ (T, Z are literal characters, the time is in
  415. UTC.):
  416. >>> dt.HTML4()
  417. '1997-03-09T18:45:00Z'
  418. * ``JulianDay()`` returns the Julian day according to
  419. http://www.tondering.dk/claus/cal/node3.html#sec-calcjd
  420. >>> dt.JulianDay()
  421. 2450517
  422. * ``week()`` returns the week number according to ISO
  423. see http://www.tondering.dk/claus/cal/node6.html#SECTION00670000000000000000
  424. >>> dt.week()
  425. 10
  426. Deprecated API
  427. ~~~~~~~~~~~~~~
  428. * DayOfWeek(): see Day()
  429. * Day_(): see pDay()
  430. * Mon(): see aMonth()
  431. * Mon_(): see pMonth
  432. General Services Provided by DateTime
  433. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  434. DateTimes can be repr()'ed; the result will be a string indicating how
  435. to make a DateTime object like this:
  436. >>> repr(dt)
  437. "DateTime('1997/03/09 13:45:00 US/Eastern')"
  438. When we convert them into a string, we get a nicer string that could
  439. actually be shown to a user:
  440. >>> str(dt)
  441. '1997/03/09 13:45:00 US/Eastern'
  442. The hash value of a DateTime is based on the date and time and is
  443. equal for different representations of the DateTime:
  444. >>> hash(dt)
  445. 3618678
  446. >>> hash(dt.toZone('UTC'))
  447. 3618678
  448. DateTime objects can be compared to other DateTime objects OR floating
  449. point numbers such as the ones which are returned by the python time
  450. module by using the equalTo method. Using this API, True is returned if the
  451. object represents a date/time equal to the specified DateTime or time module
  452. style time:
  453. >>> dt.equalTo(dt)
  454. True
  455. >>> dt.equalTo(dt.toZone('UTC'))
  456. True
  457. >>> dt.equalTo(dt.timeTime())
  458. True
  459. >>> dt.equalTo(DateTime())
  460. False
  461. Same goes for inequalities:
  462. >>> dt.notEqualTo(dt)
  463. False
  464. >>> dt.notEqualTo(dt.toZone('UTC'))
  465. False
  466. >>> dt.notEqualTo(dt.timeTime())
  467. False
  468. >>> dt.notEqualTo(DateTime())
  469. True
  470. Normal equality operations only work with datetime objects and take the
  471. timezone setting into account:
  472. >>> dt == dt
  473. True
  474. >>> dt == dt.toZone('UTC')
  475. False
  476. >>> dt == DateTime()
  477. False
  478. >>> dt != dt
  479. False
  480. >>> dt != dt.toZone('UTC')
  481. True
  482. >>> dt != DateTime()
  483. True
  484. But the other comparison operations compare the referenced moment in time and
  485. not the representation itself:
  486. >>> dt > dt
  487. False
  488. >>> DateTime() > dt
  489. True
  490. >>> dt > DateTime().timeTime()
  491. False
  492. >>> DateTime().timeTime() > dt
  493. True
  494. >>> dt.greaterThan(dt)
  495. False
  496. >>> DateTime().greaterThan(dt)
  497. True
  498. >>> dt.greaterThan(DateTime().timeTime())
  499. False
  500. >>> dt >= dt
  501. True
  502. >>> DateTime() >= dt
  503. True
  504. >>> dt >= DateTime().timeTime()
  505. False
  506. >>> DateTime().timeTime() >= dt
  507. True
  508. >>> dt.greaterThanEqualTo(dt)
  509. True
  510. >>> DateTime().greaterThanEqualTo(dt)
  511. True
  512. >>> dt.greaterThanEqualTo(DateTime().timeTime())
  513. False
  514. >>> dt < dt
  515. False
  516. >>> DateTime() < dt
  517. False
  518. >>> dt < DateTime().timeTime()
  519. True
  520. >>> DateTime().timeTime() < dt
  521. False
  522. >>> dt.lessThan(dt)
  523. False
  524. >>> DateTime().lessThan(dt)
  525. False
  526. >>> dt.lessThan(DateTime().timeTime())
  527. True
  528. >>> dt <= dt
  529. True
  530. >>> DateTime() <= dt
  531. False
  532. >>> dt <= DateTime().timeTime()
  533. True
  534. >>> DateTime().timeTime() <= dt
  535. False
  536. >>> dt.lessThanEqualTo(dt)
  537. True
  538. >>> DateTime().lessThanEqualTo(dt)
  539. False
  540. >>> dt.lessThanEqualTo(DateTime().timeTime())
  541. True
  542. Numeric Services Provided by DateTime
  543. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  544. A DateTime may be added to a number and a number may be added to a
  545. DateTime:
  546. >>> dt + 5
  547. DateTime('1997/03/14 13:45:00 US/Eastern')
  548. >>> 5 + dt
  549. DateTime('1997/03/14 13:45:00 US/Eastern')
  550. Two DateTimes cannot be added:
  551. >>> from DateTime.interfaces import DateTimeError
  552. >>> try:
  553. ... dt + dt
  554. ... print('fail')
  555. ... except DateTimeError:
  556. ... print('ok')
  557. ok
  558. Either a DateTime or a number may be subtracted from a DateTime,
  559. however, a DateTime may not be subtracted from a number:
  560. >>> DateTime('1997/03/10 13:45 US/Eastern') - dt
  561. 1.0
  562. >>> dt - 1
  563. DateTime('1997/03/08 13:45:00 US/Eastern')
  564. >>> 1 - dt
  565. Traceback (most recent call last):
  566. ...
  567. TypeError: unsupported operand type(s) for -: 'int' and 'DateTime'
  568. DateTimes can also be converted to integers (number of seconds since
  569. the epoch) and floats:
  570. >>> int(dt)
  571. 857933100
  572. >>> float(dt)
  573. 857933100.0