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