LEFT | RIGHT |
1 :mod:`datetime` --- Basic date and time types | 1 :mod:`datetime` --- Basic date and time types |
2 ============================================= | 2 ============================================= |
3 | 3 |
4 .. module:: datetime | 4 .. module:: datetime |
5 :synopsis: Basic date and time types. | 5 :synopsis: Basic date and time types. |
6 .. moduleauthor:: Tim Peters <tim@zope.com> | 6 .. moduleauthor:: Tim Peters <tim@zope.com> |
7 .. sectionauthor:: Tim Peters <tim@zope.com> | 7 .. sectionauthor:: Tim Peters <tim@zope.com> |
8 .. sectionauthor:: A.M. Kuchling <amk@amk.ca> | 8 .. sectionauthor:: A.M. Kuchling <amk@amk.ca> |
9 .. sectionauthor:: Jess Austin <jess.austin@gmail.com> | 9 .. sectionauthor:: Jess Austin <jess.austin@gmail.com> |
10 | 10 |
(...skipping 720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
731 >>> d.strftime("%d/%m/%y") | 731 >>> d.strftime("%d/%m/%y") |
732 '11/03/02' | 732 '11/03/02' |
733 >>> d.strftime("%A %d. %B %Y") | 733 >>> d.strftime("%A %d. %B %Y") |
734 'Monday 11. March 2002' | 734 'Monday 11. March 2002' |
735 | 735 |
736 Example of working with :class:`date` and :class:`monthdelta`. We have a | 736 Example of working with :class:`date` and :class:`monthdelta`. We have a |
737 dictionary of accounts associated with sorted lists of their invoice dates, | 737 dictionary of accounts associated with sorted lists of their invoice dates, |
738 and we're looking for missing invoices: | 738 and we're looking for missing invoices: |
739 | 739 |
740 >>> from datetime import date, monthdelta | 740 >>> from datetime import date, monthdelta |
741 >>> invoices = {123:[date(2008, x, 1) for x in range(1, 8)] + | 741 >>> invoices = {123: [date(2008, 1, 31), |
742 ... [date(2008, 12, 1)], | 742 ... date(2008, 2, 29), |
743 ... 456:[date(2008, 1, 1)] + | 743 ... date(2008, 3, 31), |
744 ... [date(2008, x, 1) for x in range(5, 9)] + | 744 ... date(2008, 4, 30), |
745 ... [date(2008, 11, 1), date(2008, 12, 1)]} | 745 ... date(2008, 5, 31), |
| 746 ... date(2008, 6, 30), |
| 747 ... date(2008, 7, 31), |
| 748 ... date(2008, 12, 31)], |
| 749 ... 456: [date(2008, 1, 1), |
| 750 ... date(2008, 5, 1), |
| 751 ... date(2008, 6, 1), |
| 752 ... date(2008, 7, 1), |
| 753 ... date(2008, 8, 1), |
| 754 ... date(2008, 11, 1), |
| 755 ... date(2008, 12, 1)]} |
746 >>> for account, dates in invoices.items(): | 756 >>> for account, dates in invoices.items(): |
747 ... a = dates[0] | 757 ... a = dates[0] |
748 ... for b in dates[1:]: | 758 ... for b in dates[1:]: |
749 ... if b - monthdelta(1) > a: | 759 ... if b - monthdelta(1) > a: |
750 ... print('account', account, 'missing between', a, 'and', b) | 760 ... print('account', account, 'missing between', a, 'and', b) |
751 ... a = b | 761 ... a = b |
752 ...· | 762 ...· |
753 account 456 missing between 2008-01-01 and 2008-05-01 | 763 account 456 missing between 2008-01-01 and 2008-05-01 |
754 account 456 missing between 2008-08-01 and 2008-11-01 | 764 account 456 missing between 2008-08-01 and 2008-11-01 |
755 account 123 missing between 2008-07-01 and 2008-12-01 | 765 account 123 missing between 2008-07-31 and 2008-12-31 |
756 | 766 |
757 | 767 |
758 .. _datetime-datetime: | 768 .. _datetime-datetime: |
759 | 769 |
760 :class:`datetime` Objects | 770 :class:`datetime` Objects |
761 ------------------------- | 771 ------------------------- |
762 | 772 |
763 A :class:`datetime` object is a single object containing all the information | 773 A :class:`datetime` object is a single object containing all the information |
764 from a :class:`date` object and a :class:`time` object. Like a :class:`date` | 774 from a :class:`date` object and a :class:`time` object. Like a :class:`date` |
765 object, :class:`datetime` assumes the current Gregorian calendar extended in | 775 object, :class:`datetime` assumes the current Gregorian calendar extended in |
(...skipping 1138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1904 >>> from datetime import date, monthmod | 1914 >>> from datetime import date, monthmod |
1905 >>> monthmod(date(2008, 1, 14), date(2009, 4, 2)) | 1915 >>> monthmod(date(2008, 1, 14), date(2009, 4, 2)) |
1906 (datetime.monthdelta(14), datetime.timedelta(19)) | 1916 (datetime.monthdelta(14), datetime.timedelta(19)) |
1907 >>> date(2008, 1, 14) + _[0] + _[1] | 1917 >>> date(2008, 1, 14) + _[0] + _[1] |
1908 datetime.date(2009, 4, 2) | 1918 datetime.date(2009, 4, 2) |
1909 >>> monthmod(date(2009, 4, 2), date(2008, 1, 14)) | 1919 >>> monthmod(date(2009, 4, 2), date(2008, 1, 14)) |
1910 (datetime.monthdelta(-15), datetime.timedelta(12)) | 1920 (datetime.monthdelta(-15), datetime.timedelta(12)) |
1911 >>> date(2009, 4, 2) + _[0] + _[1] | 1921 >>> date(2009, 4, 2) + _[0] + _[1] |
1912 datetime.date(2008, 1, 14) | 1922 datetime.date(2008, 1, 14) |
1913 | 1923 |
LEFT | RIGHT |