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