Mercurial > p > roundup > code
annotate roundup/date.py @ 8562:9c3ec0a5c7fc
chore: remove __future print_funcion from code.
Not needed as of Python 3.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Wed, 08 Apr 2026 21:39:40 -0400 |
| parents | a73ac3752ac5 |
| children |
| rev | line source |
|---|---|
|
213
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
1 # |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/) |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
3 # This module is free software, and you may redistribute it and/or modify |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
4 # under the same terms as Python, so long as this copyright message and |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
5 # disclaimer are retained in their original form. |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
6 # |
| 214 | 7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR |
|
213
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
10 # POSSIBILITY OF SUCH DAMAGE. |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
11 # |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE, |
|
d45384bc6420
Added the copyright/license notice to (nearly) all files...
Richard Jones <richard@users.sourceforge.net>
parents:
206
diff
changeset
|
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
|
406
bdc2ea127ae9
Added module docstrings to all modules.
Jürgen Hermann <jhermann@users.sourceforge.net>
parents:
248
diff
changeset
|
17 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
18 """Date, time and time interval handling. |
|
406
bdc2ea127ae9
Added module docstrings to all modules.
Jürgen Hermann <jhermann@users.sourceforge.net>
parents:
248
diff
changeset
|
19 """ |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
20 __docformat__ = 'restructuredtext' |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
22 import calendar |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
23 import datetime |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
24 import re |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
25 |
|
7587
8f29e4ea05ce
fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents:
6856
diff
changeset
|
26 from roundup.anypy.datetime_ import utcnow |
|
8f29e4ea05ce
fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents:
6856
diff
changeset
|
27 |
|
2702
eb4e7b8d52a2
handle Py2.3+ datetime objects as Date specs [SF#971300]
Richard Jones <richard@users.sourceforge.net>
parents:
2539
diff
changeset
|
28 try: |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
29 import pytz |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
30 except ImportError: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
31 pytz = None |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
32 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
33 try: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
34 cmp |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
35 except NameError: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
36 # Python 3. |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
37 def cmp(a, b): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
38 return (a > b) - (a < b) |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
39 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
40 from roundup import i18n |
|
5438
e2382945d302
Python 3 preparation: avoid basestring.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5414
diff
changeset
|
41 from roundup.anypy.strings import is_us |
|
2702
eb4e7b8d52a2
handle Py2.3+ datetime objects as Date specs [SF#971300]
Richard Jones <richard@users.sourceforge.net>
parents:
2539
diff
changeset
|
42 |
|
3456
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
43 # no, I don't know why we must anchor the date RE when we only ever use it |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
44 # in a match() |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
45 date_re = re.compile(r'''^ |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
46 ((?P<y>\d\d\d\d)([/-](?P<m>\d\d?)([/-](?P<d>\d\d?))?)? # yyyy[-mm[-dd]] |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
47 |(?P<a>\d\d?)[/-](?P<b>\d\d?))? # or mm-dd |
|
8285
2bf0c4e7795e
fix: issue2551390 - Replace text input/calendar popup with native date input
John Rouillard <rouilj@ieee.org>
parents:
7587
diff
changeset
|
48 (?P<n>[.T])? # . or T |
|
3611
86d568dbab55
allow single digit seconds in date spec [SF#1447141]
Richard Jones <richard@users.sourceforge.net>
parents:
3586
diff
changeset
|
49 (((?P<H>\d?\d):(?P<M>\d\d))?(:(?P<S>\d\d?(\.\d+)?))?)? # hh:mm:ss |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
50 (?:(?P<tz>\s?[+-]\d{4})|(?P<o>[\d\smywd\-+]+))? # time-zone offset, offset |
|
3456
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
51 $''', re.VERBOSE) |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
52 serialised_date_re = re.compile(r''' |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
53 (\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d?(\.\d+)?) |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
54 ''', re.VERBOSE) |
|
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
55 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
56 _timedelta0 = datetime.timedelta(0) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
57 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
58 # load UTC tzinfo |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
59 if pytz: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
60 UTC = pytz.utc |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
61 else: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
62 # fallback implementation from Python Library Reference |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
63 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
64 class _UTC(datetime.tzinfo): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
65 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
66 """Universal Coordinated Time zoneinfo""" |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
67 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
68 def utcoffset(self, dt): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
69 return _timedelta0 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
70 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
71 def tzname(self, dt): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
72 return "UTC" |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
73 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
74 def dst(self, dt): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
75 return _timedelta0 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
76 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
77 def __repr__(self): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
78 return "<UTC>" |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
79 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
80 # pytz adjustments interface |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
81 # Note: pytz verifies that dt is naive datetime for localize() |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
82 # and not naive datetime for normalize(). |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
83 # In this implementation, we don't care. |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
84 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
85 def normalize(self, dt, is_dst=False): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
86 return dt.replace(tzinfo=self) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
87 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
88 def localize(self, dt, is_dst=False): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
89 return dt.replace(tzinfo=self) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
90 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
91 UTC = _UTC() |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
92 |
| 6193 | 93 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
94 # integral hours offsets were available in Roundup versions prior to 1.1.3 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
95 # and still are supported as a fallback if pytz module is not installed |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
96 class SimpleTimezone(datetime.tzinfo): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
97 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
98 """Simple zoneinfo with fixed numeric offset and no daylight savings""" |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
99 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
100 def __init__(self, offset=0, name=None): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
101 super(SimpleTimezone, self).__init__() |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
102 self.offset = offset |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
103 if name: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
104 self.name = name |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
105 else: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
106 self.name = "Etc/GMT%+d" % self.offset |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
107 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
108 def utcoffset(self, dt): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
109 return datetime.timedelta(hours=self.offset) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
110 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
111 def tzname(self, dt): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
112 return self.name |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
113 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
114 def dst(self, dt): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
115 return _timedelta0 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
116 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
117 def __repr__(self): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
118 return "<%s: %s>" % (self.__class__.__name__, self.name) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
119 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
120 # pytz adjustments interface |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
121 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
122 def normalize(self, dt): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
123 return dt.replace(tzinfo=self) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
124 |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
125 def localize(self, dt, is_dst=False): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
126 return dt.replace(tzinfo=self) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
127 |
| 6193 | 128 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
129 # simple timezones with fixed offset |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
130 _tzoffsets = dict(GMT=0, UCT=0, EST=5, MST=7, HST=10) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
131 |
| 6193 | 132 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
133 def get_timezone(tz): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
134 # if tz is None, return None (will result in naive datetimes) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
135 # XXX should we return UTC for None? |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
136 if tz is None: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
137 return None |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
138 # try integer offset first for backward compatibility |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
139 try: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
140 utcoffset = int(tz) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
141 except (TypeError, ValueError): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
142 pass |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
143 else: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
144 if utcoffset == 0: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
145 return UTC |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
146 else: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
147 return SimpleTimezone(utcoffset) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
148 # tz is a timezone name |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
149 if pytz: |
|
6380
d76291836523
Fix issue2551128 - Impossible to validate a user with unknown timezone
John Rouillard <rouilj@ieee.org>
parents:
6193
diff
changeset
|
150 try: |
|
d76291836523
Fix issue2551128 - Impossible to validate a user with unknown timezone
John Rouillard <rouilj@ieee.org>
parents:
6193
diff
changeset
|
151 return pytz.timezone(tz) |
|
d76291836523
Fix issue2551128 - Impossible to validate a user with unknown timezone
John Rouillard <rouilj@ieee.org>
parents:
6193
diff
changeset
|
152 except pytz.exceptions.UnknownTimeZoneError: |
|
d76291836523
Fix issue2551128 - Impossible to validate a user with unknown timezone
John Rouillard <rouilj@ieee.org>
parents:
6193
diff
changeset
|
153 pass |
|
d76291836523
Fix issue2551128 - Impossible to validate a user with unknown timezone
John Rouillard <rouilj@ieee.org>
parents:
6193
diff
changeset
|
154 |
|
d76291836523
Fix issue2551128 - Impossible to validate a user with unknown timezone
John Rouillard <rouilj@ieee.org>
parents:
6193
diff
changeset
|
155 if tz == "UTC": |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
156 return UTC |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
157 elif tz in _tzoffsets: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
158 return SimpleTimezone(_tzoffsets[tz], tz) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
159 else: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
160 raise KeyError(tz) |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
161 |
| 6193 | 162 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
163 def _utc_to_local(y,m,d,H,M,S,tz): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
164 TZ = get_timezone(tz) |
|
4856
392a055fdc21
issue2117897: Fixed two more rounding to 60.0 seconds in date.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4773
diff
changeset
|
165 S = min(S, 59.999) |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
166 frac = S - int(S) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
167 dt = datetime.datetime(y, m, d, H, M, int(S), tzinfo=UTC) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
168 y,m,d,H,M,S = dt.astimezone(TZ).timetuple()[:6] |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
169 S = S + frac |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
170 return (y,m,d,H,M,S) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
171 |
| 6193 | 172 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
173 def _local_to_utc(y,m,d,H,M,S,tz): |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
174 TZ = get_timezone(tz) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
175 dt = datetime.datetime(y,m,d,H,M,int(S)) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
176 y,m,d,H,M,S = TZ.localize(dt).utctimetuple()[:6] |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
177 return (y,m,d,H,M,S) |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
178 |
| 6193 | 179 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
180 def test_ini(t): |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
181 """ Monkey-patch to make doctest think it's always time t: |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
182 """ |
| 6193 | 183 u = Date.now |
| 184 d = datetime.datetime.strptime(t, '%Y-%m-%d.%H:%M:%S.%f') | |
| 185 Date.now = lambda x: d | |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
186 return u |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
187 |
| 6193 | 188 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
189 def test_fin(u): |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
190 """ Undo monkey patch above |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
191 """ |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
192 Date.now = u |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
193 |
| 6193 | 194 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
195 class Date: |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
196 ''' |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
197 As strings, date-and-time stamps are specified with the date in |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
198 international standard format (yyyy-mm-dd) joined to the time |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
199 (hh:mm:ss) by a period ("."). Dates in this form can be easily compared |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
200 and are fairly readable when printed. An example of a valid stamp is |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
201 "2000-06-24.13:03:59". We'll call this the "full date format". When |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
202 Timestamp objects are printed as strings, they appear in the full date |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
203 format with the time always given in UTC. The full date format is |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
204 always exactly 19 characters long. |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
205 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
206 For user input, some partial forms are also permitted: the whole time |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
207 or just the seconds may be omitted; and the whole date may be omitted |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
208 or just the year may be omitted. If the time is given, the time is |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
209 interpreted in the user's local time zone. The Date constructor takes |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
210 care of these conversions. In the following examples, suppose that yyyy |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
211 is the current year, mm is the current month, and dd is the current day |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
212 of the month; and suppose that the user is on Eastern Standard Time. |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
213 |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
214 Note that Date conversion from user inputs will use the local |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
215 timezone, either from the database user (some database schemas have |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
216 a timezone property for a user) or from a default in the roundup |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
217 configuration. Roundup will store all times in UTC in the database |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
218 but display the time to the user in their local timezone as |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
219 configured. In the following examples the timezone correction for |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
220 Eastern Standard Time (GMT-5, no DST) will be applied explicitly via |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
221 an offset, but times are given in UTC in the output. |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
222 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
223 Examples:: |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
224 |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
225 |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
226 make doctest think it's always 2000-06-26.00:34:02: |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
227 >>> u = test_ini('2000-06-26.00:34:02.0') |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
228 |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
229 >>> Date("2000-04-17-0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
230 <Date 2000-04-17.05:00:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
231 >>> Date("01-25-0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
232 <Date 2000-01-25.05:00:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
233 >>> Date("2000-04-17.03:45-0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
234 <Date 2000-04-17.08:45:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
235 >>> Date("08-13.22:13-0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
236 <Date 2000-08-14.03:13:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
237 >>> Date("11-07.09:32:43-0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
238 <Date 2000-11-07.14:32:43.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
239 >>> Date("14:25-0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
240 <Date 2000-06-26.19:25:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
241 >>> Date("8:47:11-0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
242 <Date 2000-06-26.13:47:11.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
243 >>> Date("2003 -0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
244 <Date 2003-01-01.05:00:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
245 >>> Date("2003-06 -0500") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
246 <Date 2003-06-01.05:00:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
247 |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
248 "." means "right now": |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
249 >>> Date(".") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
250 <Date 2000-06-26.00:34:02.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
251 |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
252 >>> test_fin(u) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
253 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
254 The Date class should understand simple date expressions of the form |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
255 stamp + interval and stamp - interval. When adding or subtracting |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
256 intervals involving months or years, the components are handled |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
257 separately. For example, when evaluating "2000-06-25 + 1m 10d", we |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
258 first add one month to get 2000-07-25, then add 10 days to get |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
259 2000-08-04 (rather than trying to decide whether 1m 10d means 38 or 40 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
260 or 41 days). Example usage:: |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
261 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
262 make doctest think it's always 2000-06-26.00:34:02: |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
263 >>> u = test_ini('2000-06-26.00:34:02.0') |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
264 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
265 >>> Date(".") |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
266 <Date 2000-06-26.00:34:02.000> |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
267 >>> _.local(-5) |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
268 <Date 2000-06-25.19:34:02.000> |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
269 >>> Date(". + 2d") |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
270 <Date 2000-06-28.00:34:02.000> |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
271 >>> Date("1997-04-17", -5) |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
272 <Date 1997-04-17.05:00:00.000> |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
273 >>> Date("01-25", -5) |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
274 <Date 2000-01-25.05:00:00.000> |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
275 >>> Date("08-13.22:13", -5) |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
276 <Date 2000-08-14.03:13:00.000> |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
277 >>> Date("14:25", -5) |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
278 <Date 2000-06-26.19:25:00.000> |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
279 |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
280 The date format 'yyyymmddHHMMSS' (year, month, day, hour, |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
281 minute, second) is the serialisation format returned by the serialise() |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
282 method, and is accepted as an argument on instatiation. |
|
2188
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
283 |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
284 In addition, a timezone specifier can be appended to the date format. |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
285 The timezone specifier is a sign ("+" or "-") followed by a 4-digit |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
286 number as in the RFC 2822 date format. |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
287 The first two digits indicate the number of hours, while the last two |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
288 digits indicate the number of minutes the time is offset from |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
289 Coordinated Universal Time (UTC). The "+" or "-" sign indicate whether |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
290 the time is ahead of (east of) or behind (west of) UTC. Note that a |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
291 given timezone specifier *overrides* an offset given to the Date |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
292 constructor. Examples:: |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
293 |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
294 >>> Date ("2000-08-14+0200") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
295 <Date 2000-08-13.22:00:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
296 >>> Date ("08-15.22:00+0200") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
297 <Date 2000-08-15.20:00:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
298 >>> Date ("08-15.22:47+0200") |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
299 <Date 2000-08-15.20:47:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
300 >>> Date ("08-15.22:47+0200", offset = 5) |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
301 <Date 2000-08-15.20:47:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
302 >>> Date ("08-15.22:47", offset = 5) |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
303 <Date 2000-08-15.17:47:00.000> |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
304 |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
305 The date class handles basic arithmetic, but note that arithmetic |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
306 cannot be combined with timezone offsets (see last example):: |
|
2188
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
307 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
308 >>> x=test_ini('2004-04-06.22:04:20.766830') |
|
2188
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
309 >>> d1=Date('.') |
|
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
310 >>> d1 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
311 <Date 2004-04-06.22:04:20.767> |
|
2188
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
312 >>> d2=Date('2003-07-01') |
|
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
313 >>> d2 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
314 <Date 2003-07-01.00:00:00.000> |
|
2188
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
315 >>> d1-d2 |
|
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
316 <Interval + 280d 22:04:20> |
|
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
317 >>> i1=_ |
|
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
318 >>> d2+i1 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
319 <Date 2004-04-06.22:04:20.000> |
|
2188
e170d3f917ad
add more doc to date.py
Richard Jones <richard@users.sourceforge.net>
parents:
2102
diff
changeset
|
320 >>> d1-i1 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
321 <Date 2003-07-01.00:00:00.000> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
322 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
323 >>> test_fin(u) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
324 ''' |
|
2262
a13ec40cf8f5
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2250
diff
changeset
|
325 |
|
8473
a73ac3752ac5
refactor: mke Date class use __slots__
John Rouillard <rouilj@ieee.org>
parents:
8285
diff
changeset
|
326 __slots__ = ("year", "month", "day", "hour", "minute", "second", |
|
a73ac3752ac5
refactor: mke Date class use __slots__
John Rouillard <rouilj@ieee.org>
parents:
8285
diff
changeset
|
327 "_", "ngettext", "translator") |
|
a73ac3752ac5
refactor: mke Date class use __slots__
John Rouillard <rouilj@ieee.org>
parents:
8285
diff
changeset
|
328 |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
329 def __init__(self, spec='.', offset=0, add_granularity=False, |
| 6193 | 330 translator=i18n): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
331 """Construct a date given a specification and a time zone offset. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
332 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
333 'spec' |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
334 is a full date or a partial form, with an optional added or |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
335 subtracted interval. Or a date 9-tuple. |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
336 'offset' |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
337 is the local time zone offset from GMT in hours. |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
338 'translator' |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
339 is i18n module or one of gettext translation classes. |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
340 It must have attributes 'gettext' and 'ngettext', |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
341 serving as translation functions. |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
342 """ |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
343 self.setTranslator(translator) |
|
4472
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
344 # Python 2.3+ datetime object |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
345 # common case when reading from database: avoid double-conversion |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
346 if isinstance(spec, datetime.datetime): |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
347 if offset == 0: |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
348 self.year, self.month, self.day, self.hour, self.minute, \ |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
349 self.second = spec.timetuple()[:6] |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
350 else: |
|
6856
c27276e0bdce
flake8 undefined symbol fixes.
John Rouillard <rouilj@ieee.org>
parents:
6380
diff
changeset
|
351 # FIXME: what is tz supposed to be? is it the trailing |
|
c27276e0bdce
flake8 undefined symbol fixes.
John Rouillard <rouilj@ieee.org>
parents:
6380
diff
changeset
|
352 # tz on the spec? Is it the offset? |
|
c27276e0bdce
flake8 undefined symbol fixes.
John Rouillard <rouilj@ieee.org>
parents:
6380
diff
changeset
|
353 tz = None |
|
4472
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
354 TZ = get_timezone(tz) |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
355 self.year, self.month, self.day, self.hour, self.minute, \ |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
356 self.second = TZ.localize(spec).utctimetuple()[:6] |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
357 self.second += spec.microsecond/1000000. |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
358 return |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
359 |
| 6193 | 360 if isinstance(spec, type('')): |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
361 self.set(spec, offset=offset, add_granularity=add_granularity) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
362 return |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
363 elif hasattr(spec, 'tuple'): |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
364 spec = spec.tuple() |
|
2539
92510df07670
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2416
diff
changeset
|
365 elif isinstance(spec, Date): |
|
92510df07670
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2416
diff
changeset
|
366 spec = spec.get_tuple() |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
367 try: |
|
75
fa5aea4c212e
Fixed offset handling (shoulda read the spec a little better)
Richard Jones <richard@users.sourceforge.net>
parents:
47
diff
changeset
|
368 y,m,d,H,M,S,x,x,x = spec |
|
4856
392a055fdc21
issue2117897: Fixed two more rounding to 60.0 seconds in date.py
Thomas Arendsen Hein <thomas@intevation.de>
parents:
4773
diff
changeset
|
369 S = min(S, 59.999) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
370 frac = S - int(S) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
371 self.year, self.month, self.day, self.hour, self.minute, \ |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
372 self.second = _local_to_utc(y, m, d, H, M, S, offset) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
373 # we lost the fractional part |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
374 self.second = self.second + frac |
|
4773
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
375 # making sure we match the precision of serialise() |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
376 self.second = min(self.second, 59.999) |
| 6193 | 377 except Exception: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
378 raise ValueError('Unknown spec %r' % (spec,)) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
379 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
380 def now(self): |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
381 """ To be able to override for testing |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
382 """ |
|
7587
8f29e4ea05ce
fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents:
6856
diff
changeset
|
383 return utcnow() |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
384 |
|
3456
9c080e19f307
tighten up Date parsing to not allow 'M/D/YY' (or 'D/M/YY) [SF#1290550]
Richard Jones <richard@users.sourceforge.net>
parents:
3349
diff
changeset
|
385 def set(self, spec, offset=0, date_re=date_re, |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
386 serialised_re=serialised_date_re, add_granularity=False): |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
387 ''' set the date to the value in spec |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
388 ''' |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
389 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
390 m = serialised_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
391 if m is not None: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
392 # we're serialised - easy! |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
393 g = m.groups() |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
394 (self.year, self.month, self.day, self.hour, self.minute) = \ |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
395 map(int, g[:5]) |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
396 self.second = float(g[5]) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
397 return |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
398 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
399 # not serialised data, try usual format |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
400 m = date_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
401 if m is None: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
402 raise ValueError(self._('Not a date spec: %r ' |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
403 '("yyyy-mm-dd", "mm-dd", "HH:MM", "HH:MM:SS" or ' |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
404 '"yyyy-mm-dd.HH:MM:SS.SSS")' % spec)) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
405 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
406 info = m.groupdict() |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
407 |
| 4072 | 408 # If add_granularity is true, construct the maximum time given |
| 409 # the precision of the input. For example, given the input | |
| 410 # "12:15", construct "12:15:59". Or, for "2008", construct | |
| 411 # "2008-12-31.23:59:59". | |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
412 if add_granularity: |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
413 for gran in 'SMHdmy': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
414 if info[gran] is not None: |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
415 if gran == 'S': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
416 raise ValueError |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
417 elif gran == 'M': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
418 add_granularity = Interval('00:01') |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
419 elif gran == 'H': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
420 add_granularity = Interval('01:00') |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
421 else: |
| 6193 | 422 add_granularity = Interval('+1%s' % gran) |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
423 break |
| 4072 | 424 else: |
| 425 raise ValueError(self._('Could not determine granularity')) | |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
426 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
427 # get the current date as our default |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
428 dt = self.now() |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
429 y,m,d,H,M,S,x,x,x = dt.timetuple() |
|
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
430 S += dt.microsecond/1000000. |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
431 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
432 # whether we need to convert to UTC |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
433 adjust = False |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
434 |
|
1599
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
435 if info['y'] is not None or info['a'] is not None: |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
436 if info['y'] is not None: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
437 y = int(info['y']) |
|
1599
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
438 m,d = (1,1) |
|
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
439 if info['m'] is not None: |
|
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
440 m = int(info['m']) |
|
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
441 if info['d'] is not None: |
|
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
442 d = int(info['d']) |
|
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
443 if info['a'] is not None: |
|
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
444 m = int(info['a']) |
|
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
445 d = int(info['b']) |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
446 H = 0 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
447 M = S = 0 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
448 adjust = True |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
449 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
450 # override hour, minute, second parts |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
451 if info['H'] is not None and info['M'] is not None: |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
452 H = int(info['H']) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
453 M = int(info['M']) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
454 S = 0 |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
455 if info['S'] is not None: |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
456 S = float(info['S']) |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
457 adjust = True |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
458 |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
459 if info.get('tz', None): |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
460 offset = 0 |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
461 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
462 # now handle the adjustment of hour |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
463 frac = S - int(S) |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
464 dt = datetime.datetime(y,m,d,H,M,int(S), int(frac * 1000000.)) |
|
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
465 y, m, d, H, M, S, x, x, x = dt.timetuple() |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
466 if adjust: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
467 y, m, d, H, M, S = _local_to_utc(y, m, d, H, M, S, offset) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
468 self.year, self.month, self.day, self.hour, self.minute, \ |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
469 self.second = y, m, d, H, M, S |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
470 # we lost the fractional part along the way |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
471 self.second += dt.microsecond/1000000. |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
472 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
473 if info.get('o', None): |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
474 try: |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
475 self.applyInterval(Interval(info['o'], allowdate=0)) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
476 except ValueError: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
477 raise ValueError(self._('%r not a date / time spec ' |
|
3151
6feac4fcf883
Various bug fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
3028
diff
changeset
|
478 '"yyyy-mm-dd", "mm-dd", "HH:MM", "HH:MM:SS" or ' |
| 6193 | 479 '"yyyy-mm-dd.HH:MM:SS.SSS"') % (spec,)) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
480 |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
481 if info.get('tz', None): |
| 6193 | 482 tz = info['tz'].strip() |
| 483 sign = [-1, 1][tz[0] == '-'] | |
| 484 minute = int(tz[3:], 10) | |
| 485 hour = int(tz[1:3], 10) | |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
486 self.applyInterval(Interval((0, 0, 0, hour, minute, 0), sign=sign)) |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
487 |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
488 # adjust by added granularity |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
489 if add_granularity: |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
490 self.applyInterval(add_granularity) |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
491 self.applyInterval(Interval('- 00:00:01')) |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
492 |
|
861
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
493 def addInterval(self, interval): |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
494 ''' Add the interval to this date, returning the date tuple |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
495 ''' |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
496 # do the basic calc |
|
861
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
497 sign = interval.sign |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
498 year = self.year + sign * interval.year |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
499 month = self.month + sign * interval.month |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
500 day = self.day + sign * interval.day |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
501 hour = self.hour + sign * interval.hour |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
502 minute = self.minute + sign * interval.minute |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
503 # Intervals work on whole seconds |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
504 second = int(self.second) + sign * interval.second |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
505 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
506 # now cope with under- and over-flow |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
507 # first do the time |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
508 while (second < 0 or second > 59 or minute < 0 or minute > 59 or |
|
1941
bcc65c5b86e6
fixed date arithmetic to not allow day-of-month == 0 [SF#853306]
Richard Jones <richard@users.sourceforge.net>
parents:
1930
diff
changeset
|
509 hour < 0 or hour > 23): |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
510 if second < 0: minute -= 1; second += 60 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
511 elif second > 59: minute += 1; second -= 60 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
512 if minute < 0: hour -= 1; minute += 60 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
513 elif minute > 59: hour += 1; minute -= 60 |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
514 if hour < 0: day -= 1; hour += 24 |
|
1941
bcc65c5b86e6
fixed date arithmetic to not allow day-of-month == 0 [SF#853306]
Richard Jones <richard@users.sourceforge.net>
parents:
1930
diff
changeset
|
515 elif hour > 23: day += 1; hour -= 24 |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
516 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
517 # fix up the month so we're within range |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
518 while month < 1 or month > 12: |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
519 if month < 1: year -= 1; month += 12 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
520 if month > 12: year += 1; month -= 12 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
521 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
522 # now do the days, now that we know what month we're in |
|
1941
bcc65c5b86e6
fixed date arithmetic to not allow day-of-month == 0 [SF#853306]
Richard Jones <richard@users.sourceforge.net>
parents:
1930
diff
changeset
|
523 def get_mdays(year, month): |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
524 if month == 2 and calendar.isleap(year): return 29 |
|
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
525 else: return calendar.mdays[month] |
|
1941
bcc65c5b86e6
fixed date arithmetic to not allow day-of-month == 0 [SF#853306]
Richard Jones <richard@users.sourceforge.net>
parents:
1930
diff
changeset
|
526 |
| 6193 | 527 while month < 1 or month > 12 or day < 1 or \ |
| 528 day > get_mdays(year, month): | |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
529 # now to day under/over |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
530 if day < 1: |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
531 # When going backwards, decrement month, then increment days |
|
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
532 month -= 1 |
| 6193 | 533 day += get_mdays(year, month) |
| 534 elif day > get_mdays(year, month): | |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
535 # When going forwards, decrement days, then increment month |
| 6193 | 536 day -= get_mdays(year, month) |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
537 month += 1 |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
538 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
539 # possibly fix up the month so we're within range |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
540 while month < 1 or month > 12: |
| 6193 | 541 if month < 1: year -= 1; month += 12; day += 31 |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
542 if month > 12: year += 1; month -= 12 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
543 |
|
861
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
544 return (year, month, day, hour, minute, second, 0, 0, 0) |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
545 |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
546 def differenceDate(self, other): |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
547 "Return the difference between this date and another date" |
|
2189
d39068bc6ef9
implement differenceDate ;)
Richard Jones <richard@users.sourceforge.net>
parents:
2188
diff
changeset
|
548 return self - other |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
549 |
|
861
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
550 def applyInterval(self, interval): |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
551 ''' Apply the interval to this date |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
552 ''' |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
553 self.year, self.month, self.day, self.hour, self.minute, \ |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
554 self.second, x, x, x = self.addInterval(interval) |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
555 |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
556 def __add__(self, interval): |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
557 """Add an interval to this date to produce another date. |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
558 """ |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
559 return Date(self.addInterval(interval), translator=self.translator) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
560 |
|
1103
db787cef1385
handled some XXXs
Richard Jones <richard@users.sourceforge.net>
parents:
1094
diff
changeset
|
561 # deviates from spec to allow subtraction of dates as well |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
562 def __sub__(self, other): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
563 """ Subtract: |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
564 1. an interval from this date to produce another date. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
565 2. a date from this date to produce an interval. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
566 """ |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
567 if isinstance(other, Interval): |
|
1094
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
568 other = Interval(other.get_tuple()) |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
569 other.sign *= -1 |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
570 return self.__add__(other) |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
571 |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
572 assert isinstance(other, Date), 'May only subtract Dates or Intervals' |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
573 |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
574 return self.dateDelta(other) |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
575 |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
576 def dateDelta(self, other): |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
577 """ Produce an Interval of the difference between this date |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
578 and another date. Only returns days:hours:minutes:seconds. |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
579 """ |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
580 # Returning intervals larger than a day is almost |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
581 # impossible - months, years, weeks, are all so imprecise. |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
582 a = calendar.timegm((self.year, self.month, self.day, self.hour, |
| 6193 | 583 self.minute, self.second, 0, 0, 0)) |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
584 b = calendar.timegm((other.year, other.month, other.day, |
| 6193 | 585 other.hour, other.minute, other.second, 0, 0, 0)) |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
586 # intervals work in whole seconds |
|
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
587 diff = int(a - b) |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
588 if diff > 0: |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
589 sign = 1 |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
590 else: |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
591 sign = -1 |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
592 diff = -diff |
| 6193 | 593 S = diff % 60 |
| 594 M = (diff//60) % 60 | |
| 595 H = (diff//(60*60)) % 24 | |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
596 d = diff//(24*60*60) |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
597 return Interval((0, 0, d, H, M, S), sign=sign, |
| 6193 | 598 translator=self.translator) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
599 |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
600 def __cmp__(self, other, int_seconds=0): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
601 """Compare this date to another date.""" |
|
543
22e0edf7da6e
lots of date/interval related changes: more relaxed date format for input
Richard Jones <richard@users.sourceforge.net>
parents:
501
diff
changeset
|
602 if other is None: |
|
22e0edf7da6e
lots of date/interval related changes: more relaxed date format for input
Richard Jones <richard@users.sourceforge.net>
parents:
501
diff
changeset
|
603 return 1 |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
604 for attr in ('year', 'month', 'day', 'hour', 'minute'): |
|
890
a568596dbea7
Unit tests and a few fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
861
diff
changeset
|
605 if not hasattr(other, attr): |
|
a568596dbea7
Unit tests and a few fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
861
diff
changeset
|
606 return 1 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
607 r = cmp(getattr(self, attr), getattr(other, attr)) |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
608 if r: return r |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
609 if not hasattr(other, 'second'): |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
610 return 1 |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
611 if int_seconds: |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
612 return cmp(int(self.second), int(other.second)) |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
613 return cmp(self.second, other.second) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
614 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
615 def __lt__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
616 return self.__cmp__(other) < 0 |
| 6193 | 617 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
618 def __le__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
619 return self.__cmp__(other) <= 0 |
| 6193 | 620 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
621 def __eq__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
622 return self.__cmp__(other) == 0 |
| 6193 | 623 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
624 def __ne__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
625 return self.__cmp__(other) != 0 |
| 6193 | 626 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
627 def __gt__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
628 return self.__cmp__(other) > 0 |
| 6193 | 629 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
630 def __ge__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
631 return self.__cmp__(other) >= 0 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
632 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
633 def __str__(self): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
634 """Return this date as a string in the yyyy-mm-dd.hh:mm:ss format.""" |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
635 return self.formal() |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
636 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
637 def formal(self, sep='.', sec='%02d'): |
| 6193 | 638 f = '%%04d-%%02d-%%02d%s%%02d:%%02d:%s' % (sep, sec) |
| 639 return f % (self.year, self.month, self.day, self.hour, self.minute, | |
| 640 self.second) | |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
641 |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
642 def isoformat(self): |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
643 ''' Represent the date/time in isoformat standard |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
644 |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
645 Originally needed for xml output support using |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
646 dicttoxml in the rest interface. |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
647 ''' |
| 6193 | 648 f = '%%04d-%%02d-%%02d%s%%02d:%%02d:%s' % ("T", "%02.6d") |
| 649 return f % (self.year, self.month, self.day, self.hour, self.minute, | |
| 650 self.second) | |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
651 |
|
1272
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
652 def pretty(self, format='%d %B %Y'): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
653 ''' print up the date date using a pretty format... |
|
1272
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
654 |
|
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
655 Note that if the day is zero, and the day appears first in the |
|
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
656 format, then the day number will be removed from output. |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
657 ''' |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
658 dt = datetime.datetime(self.year, self.month, self.day, self.hour, |
|
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
659 self.minute, int(self.second), |
| 6193 | 660 int((self.second - int(self.second)) * 1000000.)) |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
661 str = dt.strftime(format) |
|
3028
de242e68c69b
fix py2.4 strftime() API change bug [SF#1087746]
Richard Jones <richard@users.sourceforge.net>
parents:
2981
diff
changeset
|
662 |
|
1272
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
663 # handle zero day by removing it |
|
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
664 if format.startswith('%d') and str[0] == '0': |
|
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
665 return ' ' + str[1:] |
|
585
c08fb4921eda
%e is a UNIXism and not documented for Python
Jürgen Hermann <jhermann@users.sourceforge.net>
parents:
543
diff
changeset
|
666 return str |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
667 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
668 def __repr__(self): |
| 6193 | 669 return '<Date %s>' % self.formal(sec='%06.3f') |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
670 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
671 def local(self, offset): |
|
1318
46a1951fdb14
fix Date.local()
Richard Jones <richard@users.sourceforge.net>
parents:
1272
diff
changeset
|
672 """ Return this date as yyyy-mm-dd.hh:mm:ss in a local time zone. |
|
4422
ad7176d22f43
- add comment to clarify semantics if pytz is installed
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4072
diff
changeset
|
673 The offset is a pytz tz offset if pytz is installed. |
|
1318
46a1951fdb14
fix Date.local()
Richard Jones <richard@users.sourceforge.net>
parents:
1272
diff
changeset
|
674 """ |
| 6193 | 675 y,m,d,H,M,S = _utc_to_local(self.year, self.month, self.day, |
| 676 self.hour, self.minute, self.second, offset) | |
| 677 return Date((y,m,d,H,M,S,0,0,0), translator=self.translator) | |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
678 |
|
2359
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
679 def __deepcopy__(self, memo): |
|
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
680 return Date((self.year, self.month, self.day, self.hour, |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
681 self.minute, self.second, 0, 0, 0), translator=self.translator) |
|
2359
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
682 |
|
47
34491d9b91a0
Storing only marshallable data in the db - no nasty pickled class references.
Richard Jones <richard@users.sourceforge.net>
parents:
26
diff
changeset
|
683 def get_tuple(self): |
|
34491d9b91a0
Storing only marshallable data in the db - no nasty pickled class references.
Richard Jones <richard@users.sourceforge.net>
parents:
26
diff
changeset
|
684 return (self.year, self.month, self.day, self.hour, self.minute, |
| 6193 | 685 self.second, 0, 0, 0) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
686 |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
687 def serialise(self): |
|
4773
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
688 """ Return serialised string for self's datetime. |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
689 |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
690 Uses '%06.3f' as format for self.second, which therefor |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
691 must be <=59.999 to work. Otherwise it will be rounded |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
692 to 60.000. |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
693 |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
694 """ |
| 6193 | 695 return '%04d%02d%02d%02d%02d%06.3f' % (self.year, self.month, |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
696 self.day, self.hour, self.minute, self.second) |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
697 |
|
1946
c538a64b94a7
Refactored CGI file serving so that FileClass contents are...
Richard Jones <richard@users.sourceforge.net>
parents:
1941
diff
changeset
|
698 def timestamp(self): |
|
c538a64b94a7
Refactored CGI file serving so that FileClass contents are...
Richard Jones <richard@users.sourceforge.net>
parents:
1941
diff
changeset
|
699 ''' return a UNIX timestamp for this date ''' |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
700 frac = self.second - int(self.second) |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
701 ts = calendar.timegm((self.year, self.month, self.day, self.hour, |
| 6193 | 702 self.minute, self.second, 0, 0, 0)) |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
703 # we lose the fractional part |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
704 return ts + frac |
|
1946
c538a64b94a7
Refactored CGI file serving so that FileClass contents are...
Richard Jones <richard@users.sourceforge.net>
parents:
1941
diff
changeset
|
705 |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
706 def setTranslator(self, translator): |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
707 """Replace the translation engine |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
708 |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
709 'translator' |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
710 is i18n module or one of gettext translation classes. |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
711 It must have attributes 'gettext' and 'ngettext', |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
712 serving as translation functions. |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
713 """ |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
714 self.translator = translator |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
715 self._ = translator.gettext |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
716 self.ngettext = translator.ngettext |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
717 |
|
3825
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
718 def fromtimestamp(cls, ts): |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
719 """Create a date object from a timestamp. |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
720 |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
721 The timestamp may be outside the gmtime year-range of |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
722 1902-2038. |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
723 """ |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
724 usec = int((ts - int(ts)) * 1000000.) |
| 6193 | 725 delta = datetime.timedelta(seconds=int(ts), microseconds=usec) |
|
3825
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
726 return cls(datetime.datetime(1970, 1, 1) + delta) |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
727 fromtimestamp = classmethod(fromtimestamp) |
|
f5bb1ad47268
Add a class-method as a constructor of a Date object from a timestamp.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3823
diff
changeset
|
728 |
| 6193 | 729 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
730 class Interval: |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
731 ''' |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
732 Date intervals are specified using the suffixes "y", "m", and "d". The |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
733 suffix "w" (for "week") means 7 days. Time intervals are specified in |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
734 hh:mm:ss format (the seconds may be omitted, but the hours and minutes |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
735 may not). |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
736 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
737 "3y" means three years |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
738 "2y 1m" means two years and one month |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
739 "1m 25d" means one month and 25 days |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
740 "2w 3d" means two weeks and three days |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
741 "1d 2:50" means one day, two hours, and 50 minutes |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
742 "14:00" means 14 hours |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
743 "0:04:33" means four minutes and 33 seconds |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
744 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
745 Example usage: |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
746 make doctest think it's always 2000-06-26.00:34:02: |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
747 >>> u = test_ini('2000-06-26.00:34:02.0') |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
748 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
749 >>> Interval(" 3w 1 d 2:00") |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
750 <Interval + 22d 2:00> |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
751 >>> Date(". + 2d") + Interval("- 3w") |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
752 <Date 2000-06-07.00:34:02.000> |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
753 >>> Interval('1:59:59') + Interval('00:00:01') |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
754 <Interval + 2:00> |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
755 >>> Interval('2:00') + Interval('- 00:00:01') |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
756 <Interval + 1:59:59> |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
757 >>> Interval('1y')/2 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
758 <Interval + 6m> |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
759 >>> Interval('1:00')/2 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
760 <Interval + 0:30> |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
761 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
762 [number of days between 2000-06-26.00:34:02 and 2003-03-18 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
763 >>> Interval('2003-03-18') |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
764 <Interval - 995d> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
765 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
766 [number of days between 2000-06-26.00:34:02 and 2003-03-14 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
767 >>> Interval('-4d 2003-03-18') |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
768 <Interval - 991d> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
769 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
770 >>> test_fin(u) |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
771 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
772 Interval arithmetic is handled in a couple of special ways, trying |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
773 to cater for the most common cases. Fundamentally, Intervals which |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
774 have both date and time parts will result in strange results in |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
775 arithmetic - because of the impossibility of handling day->month->year |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
776 over- and under-flows. Intervals may also be divided by some number. |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
777 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
778 Intervals are added to Dates in order of: |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
779 seconds, minutes, hours, years, months, days |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
780 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
781 Calculations involving months (eg '+2m') have no effect on days - only |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
782 days (or over/underflow from hours/mins/secs) will do that, and |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
783 days-per-month and leap years are accounted for. Leap seconds are not. |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
784 |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
785 The interval format 'syyyymmddHHMMSS' (sign, year, month, day, hour, |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
786 minute, second) is the serialisation format returned by the serialise() |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
787 method, and is accepted as an argument on instatiation. |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
788 |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
789 TODO: more examples, showing the order of addition operation |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
790 ''' |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
791 def __init__(self, spec, sign=1, allowdate=1, add_granularity=False, |
| 6193 | 792 translator=i18n): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
793 """Construct an interval given a specification.""" |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
794 self.setTranslator(translator) |
|
5394
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
795 try: |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
796 # Python 2. |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
797 arith_types = (int, float, long) |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
798 except NameError: |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
799 # Python 3. |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
800 arith_types = (int, float) |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
801 if isinstance(spec, arith_types): |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
802 self.from_seconds(spec) |
|
5438
e2382945d302
Python 3 preparation: avoid basestring.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5414
diff
changeset
|
803 elif is_us(spec): |
| 6193 | 804 self.set(spec, allowdate=allowdate, |
| 805 add_granularity=add_granularity) | |
|
2729
797725ec50c5
date.Interval() now accepts an Interval as a spec [SF#1041266]
Richard Jones <richard@users.sourceforge.net>
parents:
2702
diff
changeset
|
806 elif isinstance(spec, Interval): |
|
797725ec50c5
date.Interval() now accepts an Interval as a spec [SF#1041266]
Richard Jones <richard@users.sourceforge.net>
parents:
2702
diff
changeset
|
807 (self.sign, self.year, self.month, self.day, self.hour, |
|
797725ec50c5
date.Interval() now accepts an Interval as a spec [SF#1041266]
Richard Jones <richard@users.sourceforge.net>
parents:
2702
diff
changeset
|
808 self.minute, self.second) = spec.get_tuple() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
809 else: |
|
1094
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
810 if len(spec) == 7: |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
811 self.sign, self.year, self.month, self.day, self.hour, \ |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
812 self.minute, self.second = spec |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
813 self.second = int(self.second) |
|
1094
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
814 else: |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
815 # old, buggy spec form |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
816 self.sign = sign |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
817 self.year, self.month, self.day, self.hour, self.minute, \ |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
818 self.second = spec |
|
2100
62ed6505cbec
MySQL migration of old backend database to new, typed database complete.
Richard Jones <richard@users.sourceforge.net>
parents:
2098
diff
changeset
|
819 self.second = int(self.second) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
820 |
|
2359
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
821 def __deepcopy__(self, memo): |
|
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
822 return Interval((self.sign, self.year, self.month, self.day, |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
823 self.hour, self.minute, self.second), translator=self.translator) |
|
2359
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
824 |
| 5809 | 825 def set(self, spec, allowdate=1, interval_re=re.compile(r''' |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
826 \s*(?P<s>[-+])? # + or - |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
827 \s*((?P<y>\d+\s*)y)? # year |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
828 \s*((?P<m>\d+\s*)m)? # month |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
829 \s*((?P<w>\d+\s*)w)? # week |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
830 \s*((?P<d>\d+\s*)d)? # day |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
831 \s*(((?P<H>\d+):(?P<M>\d+))?(:(?P<S>\d+))?)? # time |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
832 \s*(?P<D> |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
833 (\d\d\d\d[/-])?(\d\d?)?[/-](\d\d?)? # [yyyy-]mm-dd |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
834 \.? # . |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
835 (\d?\d:\d\d)?(:\d\d)? # hh:mm:ss |
|
5811
7d276bb8b46d
More invalid escape sequence fixes.
John Rouillard <rouilj@ieee.org>
parents:
5809
diff
changeset
|
836 )?''', re.VERBOSE), serialised_re=re.compile(r''' |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
837 (?P<s>[+-])?1?(?P<y>([ ]{3}\d|\d{4}))(?P<m>\d{2})(?P<d>\d{2}) |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
838 (?P<H>\d{2})(?P<M>\d{2})(?P<S>\d{2})''', re.VERBOSE), |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
839 add_granularity=False): |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
840 ''' set the date to the value in spec |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
841 ''' |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
842 self.year = self.month = self.week = self.day = self.hour = \ |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
843 self.minute = self.second = 0 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
844 self.sign = 1 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
845 m = serialised_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
846 if not m: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
847 m = interval_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
848 if not m: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
849 raise ValueError(self._('Not an interval spec: "%s"' |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
850 ' ([+-] [#y] [#m] [#w] [#d] [[[H]H:MM]:SS] [date spec])' |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
851 % spec)) |
|
1542
3faabaeab6aa
fixed date unserialisation for *dbm backends
Richard Jones <richard@users.sourceforge.net>
parents:
1537
diff
changeset
|
852 else: |
|
3faabaeab6aa
fixed date unserialisation for *dbm backends
Richard Jones <richard@users.sourceforge.net>
parents:
1537
diff
changeset
|
853 allowdate = 0 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
854 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
855 # pull out all the info specified |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
856 info = m.groupdict() |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
857 if add_granularity: |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
858 for gran in 'SMHdwmy': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
859 if info[gran] is not None: |
| 6193 | 860 info[gran] = int(info[gran]) + (info['s'] == '-' and -1 or 1) |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
861 break |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
862 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
863 valid = 0 |
| 6193 | 864 for group, attr in {'y': 'year', 'm': 'month', 'w': 'week', 'd': 'day', |
| 865 'H': 'hour', 'M': 'minute', 'S': 'second'}.items(): | |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
866 if info.get(group, None) is not None: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
867 valid = 1 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
868 setattr(self, attr, int(info[group])) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
869 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
870 # make sure it's valid |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
871 if not valid and not info['D']: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
872 raise ValueError(self._('Not an interval spec: "%s"' |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
873 ' ([+-] [#y] [#m] [#w] [#d] [[[H]H:MM]:SS])' |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
874 % spec)) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
875 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
876 if self.week: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
877 self.day = self.day + self.week*7 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
878 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
879 if info['s'] is not None: |
| 6193 | 880 self.sign = {'+': 1, '-': -1}[info['s']] |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
881 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
882 # use a date spec if one is given |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
883 if allowdate and info['D'] is not None: |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
884 now = Date('.') |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
885 date = Date(info['D']) |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
886 # if no time part was specified, nuke it in the "now" date |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
887 if not date.hour or date.minute or date.second: |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
888 now.hour = now.minute = now.second = 0 |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
889 if date != now: |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
890 y = now - (date + self) |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
891 self.__init__(y.get_tuple()) |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
892 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
893 def __lt__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
894 """Compare this interval to another interval.""" |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
895 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
896 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
897 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
898 return False |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
899 return self.as_seconds() < other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
900 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
901 def __le__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
902 """Compare this interval to another interval.""" |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
903 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
904 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
905 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
906 return False |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
907 return self.as_seconds() <= other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
908 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
909 def __eq__(self, other): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
910 """Compare this interval to another interval.""" |
|
4461
5f82173d1684
Simplify and fix interval comparison.
Stefan Seefeld <stefan@seefeld.name>
parents:
4422
diff
changeset
|
911 |
|
4465
605f31a596b8
Simplify and fix interval comparison.
Stefan Seefeld <stefan@seefeld.name>
parents:
4461
diff
changeset
|
912 if other is None: |
|
605f31a596b8
Simplify and fix interval comparison.
Stefan Seefeld <stefan@seefeld.name>
parents:
4461
diff
changeset
|
913 # we are always larger than None |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
914 return False |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
915 return self.as_seconds() == other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
916 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
917 def __ne__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
918 """Compare this interval to another interval.""" |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
919 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
920 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
921 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
922 return True |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
923 return self.as_seconds() != other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
924 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
925 def __gt__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
926 """Compare this interval to another interval.""" |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
927 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
928 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
929 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
930 return True |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
931 return self.as_seconds() > other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
932 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
933 def __ge__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
934 """Compare this interval to another interval.""" |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
935 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
936 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
937 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
938 return True |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
939 return self.as_seconds() >= other.as_seconds() |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
940 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
941 def __str__(self): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
942 """Return this interval as a string.""" |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
943 l = [] |
| 6193 | 944 if self.year: l.append('%sy' % self.year) |
| 945 if self.month: l.append('%sm' % self.month) | |
| 946 if self.day: l.append('%sd' % self.day) | |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
947 if self.second: |
| 6193 | 948 l.append('%d:%02d:%02d' % (self.hour, self.minute, self.second)) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
949 elif self.hour or self.minute: |
| 6193 | 950 l.append('%d:%02d' % (self.hour, self.minute)) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
951 if l: |
| 6193 | 952 l.insert(0, {1: '+', -1: '-'}[self.sign]) |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
953 else: |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
954 l.append('00:00') |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
955 return ' '.join(l) |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
956 |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
957 def __add__(self, other): |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
958 if isinstance(other, Date): |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
959 # the other is a Date - produce a Date |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
960 return Date(other.addInterval(self), translator=self.translator) |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
961 elif isinstance(other, Interval): |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
962 # add the other Interval to this one |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
963 a = self.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
964 asgn = a[0] |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
965 b = other.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
966 bsgn = b[0] |
| 6193 | 967 i = [asgn*x + bsgn*y for x, y in zip(a[1:], b[1:])] |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
968 i.insert(0, 1) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
969 i = fixTimeOverflow(i) |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
970 return Interval(i, translator=self.translator) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
971 # nope, no idea what to do with this other... |
| 6193 | 972 raise TypeError("Can't add %r" % other) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
973 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
974 def __sub__(self, other): |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
975 if isinstance(other, Date): |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
976 # the other is a Date - produce a Date |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
977 interval = Interval(self.get_tuple()) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
978 interval.sign *= -1 |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
979 return Date(other.addInterval(interval), |
| 6193 | 980 translator=self.translator) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
981 elif isinstance(other, Interval): |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
982 # add the other Interval to this one |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
983 a = self.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
984 asgn = a[0] |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
985 b = other.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
986 bsgn = b[0] |
| 6193 | 987 i = [asgn*x - bsgn*y for x, y in zip(a[1:], b[1:])] |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
988 i.insert(0, 1) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
989 i = fixTimeOverflow(i) |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
990 return Interval(i, translator=self.translator) |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
991 # nope, no idea what to do with this other... |
| 6193 | 992 raise TypeError("Can't add %r" % other) |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
993 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
994 def __truediv__(self, other): |
|
1930
71056b09f2bf
Add tests for Interval.pretty().
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1894
diff
changeset
|
995 """ Divide this interval by an int value. |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
996 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
997 Can't divide years and months sensibly in the _same_ |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
998 calculation as days/time, so raise an error in that situation. |
|
1930
71056b09f2bf
Add tests for Interval.pretty().
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1894
diff
changeset
|
999 """ |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1000 try: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1001 other = float(other) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1002 except TypeError: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
1003 raise ValueError("Can only divide Intervals by numbers") |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1004 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1005 y, m, d, H, M, S = (self.year, self.month, self.day, |
| 6193 | 1006 self.hour, self.minute, self.second) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1007 if y or m: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1008 if d or H or M or S: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
1009 raise ValueError("Can't divide Interval with date and time") |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1010 months = self.year*12 + self.month |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1011 months *= self.sign |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1012 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1013 months = int(months/other) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1014 |
| 6193 | 1015 sign = months < 0 and -1 or 1 |
| 1016 m = months % 12 | |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1017 y = months // 12 |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1018 return Interval((sign, y, m, 0, 0, 0, 0), |
| 6193 | 1019 translator=self.translator) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1020 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1021 else: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1022 # handle a day/time division |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1023 seconds = S + M*60 + H*60*60 + d*60*60*24 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1024 seconds *= self.sign |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1025 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1026 seconds = int(seconds/other) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1027 |
| 6193 | 1028 sign = seconds < 0 and -1 or 1 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1029 seconds *= sign |
| 6193 | 1030 S = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1031 seconds //= 60 |
| 6193 | 1032 M = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1033 seconds //= 60 |
| 6193 | 1034 H = seconds % 24 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1035 d = seconds // 24 |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1036 return Interval((sign, 0, 0, d, H, M, S), |
| 6193 | 1037 translator=self.translator) |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1038 # Python 2 compatibility: |
|
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1039 __div__ = __truediv__ |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1040 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1041 def __repr__(self): |
| 6193 | 1042 return '<Interval %s>' % self.__str__() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1043 |
|
487
2f4c37d28108
I18N'ification
Richard Jones <richard@users.sourceforge.net>
parents:
406
diff
changeset
|
1044 def pretty(self): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1045 ''' print up the date date using one of these nice formats.. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1046 ''' |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1047 _quarters = self.minute // 15 |
|
720
5e0a75bfdd90
reverting to dates for intervals > 2 months sucks
Richard Jones <richard@users.sourceforge.net>
parents:
641
diff
changeset
|
1048 if self.year: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1049 s = self.ngettext("%(number)s year", "%(number)s years", |
| 6193 | 1050 self.year) % {'number': self.year} |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1051 elif self.month or self.day > 28: |
| 2416 | 1052 _months = max(1, int(((self.month * 30) + self.day) / 30)) |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1053 s = self.ngettext("%(number)s month", "%(number)s months", |
| 6193 | 1054 _months) % {'number': _months} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1055 elif self.day > 7: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1056 _weeks = int(self.day / 7) |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1057 s = self.ngettext("%(number)s week", "%(number)s weeks", |
| 6193 | 1058 _weeks) % {'number': _weeks} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1059 elif self.day > 1: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1060 # Note: singular form is not used |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1061 s = self.ngettext('%(number)s day', '%(number)s days', |
| 6193 | 1062 self.day) % {'number': self.day} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1063 elif self.day == 1 or self.hour > 12: |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1064 if self.sign > 0: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1065 return self._('tomorrow') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1066 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1067 return self._('yesterday') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1068 elif self.hour > 1: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1069 # Note: singular form is not used |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1070 s = self.ngettext('%(number)s hour', '%(number)s hours', |
| 6193 | 1071 self.hour) % {'number': self.hour} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1072 elif self.hour == 1: |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1073 if self.minute < 15: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1074 s = self._('an hour') |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1075 elif _quarters == 2: |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1076 s = self._('1 1/2 hours') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1077 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1078 s = self.ngettext('1 %(number)s/4 hours', |
| 6193 | 1079 '1 %(number)s/4 hours', |
| 1080 _quarters) % {'number': _quarters} | |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1081 elif self.minute < 1: |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1082 if self.sign > 0: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1083 return self._('in a moment') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1084 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1085 return self._('just now') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1086 elif self.minute == 1: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1087 # Note: used in expressions "in 1 minute" or "1 minute ago" |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1088 s = self._('1 minute') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1089 elif self.minute < 15: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1090 # Note: used in expressions "in 2 minutes" or "2 minutes ago" |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1091 s = self.ngettext('%(number)s minute', '%(number)s minutes', |
| 6193 | 1092 self.minute) % {'number': self.minute} |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1093 elif _quarters == 2: |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1094 s = self._('1/2 an hour') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1095 else: |
| 2416 | 1096 s = self.ngettext('%(number)s/4 hour', '%(number)s/4 hours', |
| 6193 | 1097 _quarters) % {'number': _quarters} |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1098 # XXX this is internationally broken |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1099 if self.sign < 0: |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1100 s = self._('%s ago') % s |
|
1413
4ae9d725bec4
make future intervals obvious too
Richard Jones <richard@users.sourceforge.net>
parents:
1412
diff
changeset
|
1101 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1102 s = self._('in %s') % s |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1103 return s |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1104 |
|
47
34491d9b91a0
Storing only marshallable data in the db - no nasty pickled class references.
Richard Jones <richard@users.sourceforge.net>
parents:
26
diff
changeset
|
1105 def get_tuple(self): |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
1106 return (self.sign, self.year, self.month, self.day, self.hour, |
| 6193 | 1107 self.minute, self.second) |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
1108 |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
1109 def serialise(self): |
|
1261
85d71588a1cf
fixed bug in Interval serialisation
Richard Jones <richard@users.sourceforge.net>
parents:
1192
diff
changeset
|
1110 sign = self.sign > 0 and '+' or '-' |
| 6193 | 1111 return '%s%04d%02d%02d%02d%02d%02d' % (sign, self.year, self.month, |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
1112 self.day, self.hour, self.minute, self.second) |
|
47
34491d9b91a0
Storing only marshallable data in the db - no nasty pickled class references.
Richard Jones <richard@users.sourceforge.net>
parents:
26
diff
changeset
|
1113 |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1114 def isoformat(self): |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1115 '''Represent interval as an ISO 8061 duration (absolute value) |
| 6193 | 1116 |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1117 Originally needed for xml output support using |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1118 dicttoxml in the rest interface. |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1119 ''' |
| 6193 | 1120 return 'P%04dY%02dM%02dDT%02dH%02dM%02dS' % (self.year, self.month, |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1121 self.day, self.hour, self.minute, self.second) |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1122 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1123 def as_seconds(self): |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1124 '''Calculate the Interval as a number of seconds. |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1125 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1126 Months are counted as 30 days, years as 365 days. Returns a Long |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1127 int. |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1128 ''' |
|
5380
64c4e43fbb84
Python 3 preparation: numeric literal syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
1129 n = self.year * 365 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1130 n = n + self.month * 30 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1131 n = n + self.day |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1132 n = n * 24 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1133 n = n + self.hour |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1134 n = n * 60 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1135 n = n + self.minute |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1136 n = n * 60 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1137 n = n + self.second |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1138 return n * self.sign |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1139 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1140 def from_seconds(self, val): |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1141 '''Figure my second, minute, hour and day values using a seconds |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1142 value. |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1143 ''' |
|
2981
ad04cb95e2b0
fix Interval.from_seconds() with float argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2729
diff
changeset
|
1144 val = int(val) |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1145 if val < 0: |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1146 self.sign = -1 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1147 val = -val |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1148 else: |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1149 self.sign = 1 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1150 self.second = val % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1151 val = val // 60 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1152 self.minute = val % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1153 val = val // 60 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1154 self.hour = val % 24 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1155 val = val // 24 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1156 self.day = val |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1157 self.month = self.year = 0 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1158 |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1159 def setTranslator(self, translator): |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1160 """Replace the translation engine |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1161 |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1162 'translator' |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1163 is i18n module or one of gettext translation classes. |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1164 It must have attributes 'gettext' and 'ngettext', |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1165 serving as translation functions. |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1166 """ |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1167 self.translator = translator |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1168 self._ = translator.gettext |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1169 self.ngettext = translator.ngettext |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1170 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1171 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1172 def fixTimeOverflow(time): |
|
1930
71056b09f2bf
Add tests for Interval.pretty().
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1894
diff
changeset
|
1173 """ Handle the overflow in the time portion (H, M, S) of "time": |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1174 (sign, y,m,d,H,M,S) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1175 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1176 Overflow and underflow will at most affect the _days_ portion of |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1177 the date. We do not overflow days to months as we don't know _how_ |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1178 to, generally. |
|
1930
71056b09f2bf
Add tests for Interval.pretty().
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1894
diff
changeset
|
1179 """ |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1180 # XXX we could conceivably use this function for handling regular dates |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1181 # XXX too - we just need to interrogate the month/year for the day |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1182 # XXX overflow... |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1183 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1184 sign, y, m, d, H, M, S = time |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1185 seconds = sign * (S + M*60 + H*60*60 + d*60*60*24) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1186 if seconds: |
| 6193 | 1187 sign = seconds < 0 and -1 or 1 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1188 seconds *= sign |
| 6193 | 1189 S = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1190 seconds //= 60 |
| 6193 | 1191 M = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1192 seconds //= 60 |
| 6193 | 1193 H = seconds % 24 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1194 d = seconds // 24 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1195 else: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1196 months = y*12 + m |
| 6193 | 1197 sign = months < 0 and -1 or 1 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1198 months *= sign |
| 6193 | 1199 m = months % 12 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1200 y = months//12 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1201 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1202 return (sign, y, m, d, H, M, S) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1203 |
| 6193 | 1204 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1205 class Range: |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1206 """Represents range between two values |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1207 Ranges can be created using one of theese two alternative syntaxes: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1208 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1209 1. Native english syntax:: |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1210 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1211 [[From] <value>][ To <value>] |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1212 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1213 Keywords "From" and "To" are case insensitive. Keyword "From" is |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1214 optional. |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1215 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1216 2. "Geek" syntax:: |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1217 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1218 [<value>][; <value>] |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1219 |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1220 Either first or second <value> can be omitted in both syntaxes. |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1221 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1222 Examples (consider local time is Sat Mar 8 22:07:48 EET 2003):: |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1223 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1224 make doctest think it's always 2000-06-26.00:34:02: |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1225 >>> u = test_ini('2003-03-08.20:07:48.0') |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1226 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1227 >>> Range("from 2-12 to 4-2", Date) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1228 <Range from 2003-02-12.00:00:00 to 2003-04-02.00:00:00> |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1229 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1230 >>> Range("18:00 to +2m", Date) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1231 <Range from 2003-03-08.18:00:00 to 2003-05-08.20:07:48> |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1232 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1233 >>> Range("tO +3d", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1234 <Range from None to 2003-03-11.20:07:48> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1235 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1236 >>> Range("12:00 to", Date) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1237 <Range from 2003-03-08.12:00:00 to None> |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1238 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1239 >>> Range("12:00;", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1240 <Range from 2003-03-08.12:00:00 to None> |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1241 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1242 >>> Range("2002-11-10; 2002-12-12", Date) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1243 <Range from 2002-11-10.00:00:00 to 2002-12-12.00:00:00> |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1244 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1245 >>> Range("; 20:00 +1d", Date) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1246 <Range from None to 2003-03-09.20:00:00> |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1247 |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1248 >>> Range("from 2003-02-16", Date) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1249 <Range from 2003-02-16.00:00:00 to None> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1250 |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1251 >>> Range("2003-02-16;", Date) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1252 <Range from 2003-02-16.00:00:00 to None> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1253 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1254 Granularity tests: |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1255 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1256 >>> Range("12:00", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1257 <Range from 2003-03-08.12:00:00 to 2003-03-08.12:00:59> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1258 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1259 >>> Range("2003-03-08", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1260 <Range from 2003-03-08.00:00:00 to 2003-03-08.23:59:59> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1261 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1262 >>> test_fin(u) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1263 |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1264 Range of Interval tests |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1265 |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1266 >>> Range ("from 0:50 to 2:00", Interval) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1267 <Range from + 0:50 to + 2:00> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1268 >>> Range ("from 0:50 to 1d 2:00", Interval) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1269 <Range from + 0:50 to + 1d 2:00> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1270 >>> Range ("from 5:50", Interval) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1271 <Range from + 5:50 to None> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1272 >>> Range ("to 0:05", Interval) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1273 <Range from None to + 0:05> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1274 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1275 """ |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1276 def __init__(self, spec, Type, allow_granularity=True, **params): |
|
1509
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1277 """Initializes Range of type <Type> from given <spec> string. |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1278 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1279 Sets two properties - from_value and to_value. None assigned to any of |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1280 this properties means "infinitum" (-infinitum to from_value and |
|
1509
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1281 +infinitum to to_value) |
|
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1282 |
|
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1283 The Type parameter here should be class itself (e.g. Date), not a |
|
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1284 class instance. |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1285 """ |
|
1509
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1286 self.range_type = Type |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1287 re_range = r'^(?:from)?(.+?)?to(.+?)?$' |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1288 re_range_no_to = r'^from(.+)(.)?$' |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1289 re_geek_range = r'^(.+?)?;(.+?)?$' |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1290 # Check which syntax to use |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1291 if ';' in spec: |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1292 # Geek |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1293 m = re.search(re_geek_range, spec.strip()) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1294 else: |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1295 # Native english |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1296 m = re.search(re_range, spec.strip(), re.IGNORECASE) |
| 6193 | 1297 if not m: |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1298 m = re.search(re_range_no_to, spec.strip(), re.IGNORECASE) |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1299 if m: |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1300 self.from_value, self.to_value = m.groups() |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1301 if self.from_value: |
|
1509
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1302 self.from_value = Type(self.from_value.strip(), **params) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1303 if self.to_value: |
|
1509
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1304 self.to_value = Type(self.to_value.strip(), **params) |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1305 else: |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1306 if allow_granularity: |
|
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1307 self.from_value = Type(spec, **params) |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1308 self.to_value = Type(spec, add_granularity=True, **params) |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1309 else: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
1310 raise ValueError("Invalid range") |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1311 |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1312 def __str__(self): |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1313 return "from %s to %s" % (self.from_value, self.to_value) |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1314 |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1315 def __repr__(self): |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1316 return "<Range %s>" % self.__str__() |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1317 |
| 6193 | 1318 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1319 def test_range(): |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1320 rspecs = ("from 2-12 to 4-2", "from 18:00 TO +2m", "12:00;", "tO +3d", |
|
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1321 "2002-11-10; 2002-12-12", "; 20:00 +1d", '2002-10-12') |
|
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1322 rispecs = ('from -1w 2d 4:32 to 4d', '-2w 1d') |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1323 for rspec in rspecs: |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4991
diff
changeset
|
1324 print('>>> Range("%s")' % rspec) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1325 print(repr(Range(rspec, Date))) |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4991
diff
changeset
|
1326 print() |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1327 for rspec in rispecs: |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4991
diff
changeset
|
1328 print('>>> Range("%s")' % rspec) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1329 print(repr(Range(rspec, Interval))) |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4991
diff
changeset
|
1330 print() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1331 |
| 6193 | 1332 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1333 def test(): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1334 intervals = (" 3w 1 d 2:00", " + 2d", "3w") |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1335 for interval in intervals: |
| 6193 | 1336 print('>>> Interval("%s")' % interval) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1337 print(repr(Interval(interval))) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1338 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1339 dates = (".", "2000-06-25.19:34:02", ". + 2d", "1997-04-17", "01-25", |
| 6193 | 1340 "08-13.22:13", "14:25", '2002-12') |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1341 for date in dates: |
| 6193 | 1342 print('>>> Date("%s")' % date) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1343 print(repr(Date(date))) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1344 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1345 sums = ((". + 2d", "3w"), (".", " 3w 1 d 2:00")) |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1346 for date, interval in sums: |
| 6193 | 1347 print('>>> Date("%s") + Interval("%s")' % (date, interval)) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1348 print(repr(Date(date) + Interval(interval))) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1349 |
| 6193 | 1350 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1351 if __name__ == '__main__': |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1352 test() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1353 |
|
3210
ea29d69f7415
fix error message formatting in Date.__init__;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3151
diff
changeset
|
1354 # vim: set filetype=python sts=4 sw=4 et si : |
