Mercurial > p > roundup > code
comparison roundup/date.py @ 2005:fc52d57c6c3e
documentation cleanup
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Wed, 11 Feb 2004 23:55:10 +0000 |
| parents | c538a64b94a7 |
| children | 18addf2a8596 |
comparison
equal
deleted
inserted
replaced
| 2004:1782fe36e7b8 | 2005:fc52d57c6c3e |
|---|---|
| 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" | 14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" |
| 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, | 15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
| 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | 16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 17 # | 17 # |
| 18 # $Id: date.py,v 1.59 2003-12-05 03:28:38 richard Exp $ | 18 # $Id: date.py,v 1.60 2004-02-11 23:55:08 richard Exp $ |
| 19 | 19 |
| 20 __doc__ = """ | 20 """Date, time and time interval handling. |
| 21 Date, time and time interval handling. | |
| 22 """ | 21 """ |
| 22 __docformat__ = 'restructuredtext' | |
| 23 | 23 |
| 24 import time, re, calendar, types | 24 import time, re, calendar, types |
| 25 from i18n import _ | 25 from i18n import _ |
| 26 | 26 |
| 27 def _add_granularity(src, order, value = 1): | 27 def _add_granularity(src, order, value = 1): |
| 49 or just the year may be omitted. If the time is given, the time is | 49 or just the year may be omitted. If the time is given, the time is |
| 50 interpreted in the user's local time zone. The Date constructor takes | 50 interpreted in the user's local time zone. The Date constructor takes |
| 51 care of these conversions. In the following examples, suppose that yyyy | 51 care of these conversions. In the following examples, suppose that yyyy |
| 52 is the current year, mm is the current month, and dd is the current day | 52 is the current year, mm is the current month, and dd is the current day |
| 53 of the month; and suppose that the user is on Eastern Standard Time. | 53 of the month; and suppose that the user is on Eastern Standard Time. |
| 54 Examples:: | |
| 54 | 55 |
| 55 "2000-04-17" means <Date 2000-04-17.00:00:00> | 56 "2000-04-17" means <Date 2000-04-17.00:00:00> |
| 56 "01-25" means <Date yyyy-01-25.00:00:00> | 57 "01-25" means <Date yyyy-01-25.00:00:00> |
| 57 "2000-04-17.03:45" means <Date 2000-04-17.08:45:00> | 58 "2000-04-17.03:45" means <Date 2000-04-17.08:45:00> |
| 58 "08-13.22:13" means <Date yyyy-08-14.03:13:00> | 59 "08-13.22:13" means <Date yyyy-08-14.03:13:00> |
| 67 stamp + interval and stamp - interval. When adding or subtracting | 68 stamp + interval and stamp - interval. When adding or subtracting |
| 68 intervals involving months or years, the components are handled | 69 intervals involving months or years, the components are handled |
| 69 separately. For example, when evaluating "2000-06-25 + 1m 10d", we | 70 separately. For example, when evaluating "2000-06-25 + 1m 10d", we |
| 70 first add one month to get 2000-07-25, then add 10 days to get | 71 first add one month to get 2000-07-25, then add 10 days to get |
| 71 2000-08-04 (rather than trying to decide whether 1m 10d means 38 or 40 | 72 2000-08-04 (rather than trying to decide whether 1m 10d means 38 or 40 |
| 72 or 41 days). | 73 or 41 days). Example usage:: |
| 73 | 74 |
| 74 Example usage: | |
| 75 >>> Date(".") | 75 >>> Date(".") |
| 76 <Date 2000-06-26.00:34:02> | 76 <Date 2000-06-26.00:34:02> |
| 77 >>> _.local(-5) | 77 >>> _.local(-5) |
| 78 "2000-06-25.19:34:02" | 78 "2000-06-25.19:34:02" |
| 79 >>> Date(". + 2d") | 79 >>> Date(". + 2d") |
| 93 ''' | 93 ''' |
| 94 | 94 |
| 95 def __init__(self, spec='.', offset=0, add_granularity=0): | 95 def __init__(self, spec='.', offset=0, add_granularity=0): |
| 96 """Construct a date given a specification and a time zone offset. | 96 """Construct a date given a specification and a time zone offset. |
| 97 | 97 |
| 98 'spec' is a full date or a partial form, with an optional | 98 'spec' |
| 99 added or subtracted interval. Or a date 9-tuple. | 99 is a full date or a partial form, with an optional added or |
| 100 'offset' is the local time zone offset from GMT in hours. | 100 subtracted interval. Or a date 9-tuple. |
| 101 'offset' | |
| 102 is the local time zone offset from GMT in hours. | |
| 101 """ | 103 """ |
| 102 if type(spec) == type(''): | 104 if type(spec) == type(''): |
| 103 self.set(spec, offset=offset, add_granularity=add_granularity) | 105 self.set(spec, offset=offset, add_granularity=add_granularity) |
| 104 else: | 106 else: |
| 105 y,m,d,H,M,S,x,x,x = spec | 107 y,m,d,H,M,S,x,x,x = spec |
| 658 y = months/12 | 660 y = months/12 |
| 659 | 661 |
| 660 return (sign, y, m, d, H, M, S) | 662 return (sign, y, m, d, H, M, S) |
| 661 | 663 |
| 662 class Range: | 664 class Range: |
| 663 """ | 665 """Represents range between two values |
| 664 Represents range between two values | |
| 665 Ranges can be created using one of theese two alternative syntaxes: | 666 Ranges can be created using one of theese two alternative syntaxes: |
| 666 | 667 |
| 667 1. Native english syntax: | 668 1. Native english syntax:: |
| 669 | |
| 668 [[From] <value>][ To <value>] | 670 [[From] <value>][ To <value>] |
| 669 Keywords "From" and "To" are case insensitive. Keyword "From" is optional. | 671 |
| 670 | 672 Keywords "From" and "To" are case insensitive. Keyword "From" is |
| 671 2. "Geek" syntax: | 673 optional. |
| 672 [<value>][; <value>] | 674 |
| 675 2. "Geek" syntax:: | |
| 676 | |
| 677 [<value>][; <value>] | |
| 673 | 678 |
| 674 Either first or second <value> can be omitted in both syntaxes. | 679 Either first or second <value> can be omitted in both syntaxes. |
| 675 | 680 |
| 676 Examples (consider local time is Sat Mar 8 22:07:48 EET 2003): | 681 Examples (consider local time is Sat Mar 8 22:07:48 EET 2003):: |
| 682 | |
| 677 >>> Range("from 2-12 to 4-2") | 683 >>> Range("from 2-12 to 4-2") |
| 678 <Range from 2003-02-12.00:00:00 to 2003-04-02.00:00:00> | 684 <Range from 2003-02-12.00:00:00 to 2003-04-02.00:00:00> |
| 679 | 685 |
| 680 >>> Range("18:00 TO +2m") | 686 >>> Range("18:00 TO +2m") |
| 681 <Range from 2003-03-08.18:00:00 to 2003-05-08.20:07:48> | 687 <Range from 2003-03-08.18:00:00 to 2003-05-08.20:07:48> |
