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