Mercurial > p > roundup > code
annotate roundup/date.py @ 7998:851ddd72f9ce
issue2550983 - help_commands_html unused
Make the command htmlhelp trigger the command.
The output is ugly however. But clean this up and we could generate
html docs for the current admin.py version replacing existing docs
similar to how the doc is now done for config.ini.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 26 May 2024 00:33:43 -0400 |
| parents | 8f29e4ea05ce |
| children | 2bf0c4e7795e |
| 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 |
|
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
|
49 (?P<n>\.)? # . |
|
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 |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
327 def __init__(self, spec='.', offset=0, add_granularity=False, |
| 6193 | 328 translator=i18n): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
329 """Construct a date given a specification and a time zone offset. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
330 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
331 'spec' |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
332 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
|
333 subtracted interval. Or a date 9-tuple. |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
334 'offset' |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
335 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
|
336 'translator' |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
337 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
|
338 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
|
339 serving as translation functions. |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
340 """ |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
341 self.setTranslator(translator) |
|
4472
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
342 # Python 2.3+ datetime object |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
343 # common case when reading from database: avoid double-conversion |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
344 if isinstance(spec, datetime.datetime): |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
345 if offset == 0: |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
346 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
|
347 self.second = spec.timetuple()[:6] |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
348 else: |
|
6856
c27276e0bdce
flake8 undefined symbol fixes.
John Rouillard <rouilj@ieee.org>
parents:
6380
diff
changeset
|
349 # 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
|
350 # tz on the spec? Is it the offset? |
|
c27276e0bdce
flake8 undefined symbol fixes.
John Rouillard <rouilj@ieee.org>
parents:
6380
diff
changeset
|
351 tz = None |
|
4472
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
352 TZ = get_timezone(tz) |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
353 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
|
354 self.second = TZ.localize(spec).utctimetuple()[:6] |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
355 self.second += spec.microsecond/1000000. |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
356 return |
|
34dce76bb202
Multilink fixes and optimizations:
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
4465
diff
changeset
|
357 |
| 6193 | 358 if isinstance(spec, type('')): |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
359 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
|
360 return |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
361 elif hasattr(spec, 'tuple'): |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
362 spec = spec.tuple() |
|
2539
92510df07670
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2416
diff
changeset
|
363 elif isinstance(spec, Date): |
|
92510df07670
*** empty log message ***
Richard Jones <richard@users.sourceforge.net>
parents:
2416
diff
changeset
|
364 spec = spec.get_tuple() |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
365 try: |
|
75
fa5aea4c212e
Fixed offset handling (shoulda read the spec a little better)
Richard Jones <richard@users.sourceforge.net>
parents:
47
diff
changeset
|
366 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
|
367 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
|
368 frac = S - int(S) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
369 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
|
370 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
|
371 # we lost the fractional part |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
372 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
|
373 # 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
|
374 self.second = min(self.second, 59.999) |
| 6193 | 375 except Exception: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
376 raise ValueError('Unknown spec %r' % (spec,)) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
377 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
378 def now(self): |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
379 """ To be able to override for testing |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
380 """ |
|
7587
8f29e4ea05ce
fix: issue2551278 - datetime.datetime.utcnow deprecation.
John Rouillard <rouilj@ieee.org>
parents:
6856
diff
changeset
|
381 return utcnow() |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
382 |
|
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
|
383 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
|
384 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
|
385 ''' 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
|
386 ''' |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
387 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
388 m = serialised_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
389 if m is not None: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
390 # we're serialised - easy! |
|
2098
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
391 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
|
392 (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
|
393 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
|
394 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
|
395 return |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
396 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
397 # 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
|
398 m = date_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
399 if m is None: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
400 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
|
401 '("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
|
402 '"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
|
403 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
404 info = m.groupdict() |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
405 |
| 4072 | 406 # If add_granularity is true, construct the maximum time given |
| 407 # the precision of the input. For example, given the input | |
| 408 # "12:15", construct "12:15:59". Or, for "2008", construct | |
| 409 # "2008-12-31.23:59:59". | |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
410 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
|
411 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
|
412 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
|
413 if gran == 'S': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
414 raise ValueError |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
415 elif gran == 'M': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
416 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
|
417 elif gran == 'H': |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
418 add_granularity = Interval('01:00') |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
419 else: |
| 6193 | 420 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
|
421 break |
| 4072 | 422 else: |
| 423 raise ValueError(self._('Could not determine granularity')) | |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
424 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
425 # get the current date as our default |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
426 dt = self.now() |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
427 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
|
428 S += dt.microsecond/1000000. |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
429 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
430 # 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
|
431 adjust = False |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
432 |
|
1599
cc96bf971b33
extended date syntax to make range searches even more useful
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1597
diff
changeset
|
433 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
|
434 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
|
435 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
|
436 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
|
437 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
|
438 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
|
439 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
|
440 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
|
441 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
|
442 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
|
443 d = int(info['b']) |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
444 H = 0 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
445 M = S = 0 |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
446 adjust = True |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
447 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
448 # override hour, minute, second parts |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
449 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
|
450 H = int(info['H']) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
451 M = int(info['M']) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
452 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
|
453 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
|
454 S = float(info['S']) |
|
3619
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
455 adjust = True |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
456 |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
457 if info.get('tz', None): |
|
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
458 offset = 0 |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
459 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
460 # 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
|
461 frac = S - int(S) |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
462 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
|
463 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
|
464 if adjust: |
|
df7bff6f8a2f
full timezone support (based on patch [SF#1465296])
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3612
diff
changeset
|
465 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
|
466 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
|
467 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
|
468 # 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
|
469 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
|
470 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
471 if info.get('o', None): |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
472 try: |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
473 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
|
474 except ValueError: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
475 raise ValueError(self._('%r not a date / time spec ' |
|
3151
6feac4fcf883
Various bug fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
3028
diff
changeset
|
476 '"yyyy-mm-dd", "mm-dd", "HH:MM", "HH:MM:SS" or ' |
| 6193 | 477 '"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
|
478 |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
479 if info.get('tz', None): |
| 6193 | 480 tz = info['tz'].strip() |
| 481 sign = [-1, 1][tz[0] == '-'] | |
| 482 minute = int(tz[3:], 10) | |
| 483 hour = int(tz[1:3], 10) | |
|
4986
fe140bc0eaa9
Implement (and document) timezone offsets
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4985
diff
changeset
|
484 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
|
485 |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
486 # 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
|
487 if add_granularity: |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
488 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
|
489 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
|
490 |
|
861
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
491 def addInterval(self, interval): |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
492 ''' Add the interval to this date, returning the date tuple |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
493 ''' |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
494 # 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
|
495 sign = interval.sign |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
496 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
|
497 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
|
498 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
|
499 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
|
500 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
|
501 # 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
|
502 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
|
503 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
504 # now cope with under- and over-flow |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
505 # first do the time |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
506 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
|
507 hour < 0 or hour > 23): |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
508 if second < 0: minute -= 1; second += 60 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
509 elif second > 59: minute += 1; second -= 60 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
510 if minute < 0: hour -= 1; minute += 60 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
511 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
|
512 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
|
513 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
|
514 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
515 # 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
|
516 while month < 1 or month > 12: |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
517 if month < 1: year -= 1; month += 12 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
518 if month > 12: year += 1; month -= 12 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
519 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
520 # 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
|
521 def get_mdays(year, month): |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
522 if month == 2 and calendar.isleap(year): return 29 |
|
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
523 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
|
524 |
| 6193 | 525 while month < 1 or month > 12 or day < 1 or \ |
| 526 day > get_mdays(year, month): | |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
527 # 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
|
528 if day < 1: |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
529 # When going backwards, decrement month, then increment days |
|
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
530 month -= 1 |
| 6193 | 531 day += get_mdays(year, month) |
| 532 elif day > get_mdays(year, month): | |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
533 # When going forwards, decrement days, then increment month |
| 6193 | 534 day -= get_mdays(year, month) |
|
1885
deba54ed724f
Date arithmetic fixes.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1600
diff
changeset
|
535 month += 1 |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
536 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
537 # 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
|
538 while month < 1 or month > 12: |
| 6193 | 539 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
|
540 if month > 12: year += 1; month -= 12 |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
541 |
|
861
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
542 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
|
543 |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
544 def differenceDate(self, other): |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
545 "Return the difference between this date and another date" |
|
2189
d39068bc6ef9
implement differenceDate ;)
Richard Jones <richard@users.sourceforge.net>
parents:
2188
diff
changeset
|
546 return self - other |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
547 |
|
861
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
548 def applyInterval(self, interval): |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
549 ''' 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
|
550 ''' |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
551 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
|
552 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
|
553 |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
554 def __add__(self, interval): |
|
68cef2bb929d
fixed the date module so that Date(". - 2d") works
Richard Jones <richard@users.sourceforge.net>
parents:
720
diff
changeset
|
555 """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
|
556 """ |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
557 return Date(self.addInterval(interval), translator=self.translator) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
558 |
|
1103
db787cef1385
handled some XXXs
Richard Jones <richard@users.sourceforge.net>
parents:
1094
diff
changeset
|
559 # deviates from spec to allow subtraction of dates as well |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
560 def __sub__(self, other): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
561 """ Subtract: |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
562 1. an interval from this date to produce another date. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
563 2. a date from this date to produce an interval. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
564 """ |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
565 if isinstance(other, Interval): |
|
1094
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
566 other = Interval(other.get_tuple()) |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
567 other.sign *= -1 |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
568 return self.__add__(other) |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
569 |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
570 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
|
571 |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
572 return self.dateDelta(other) |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
573 |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
574 def dateDelta(self, other): |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
575 """ Produce an Interval of the difference between this date |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
576 and another date. Only returns days:hours:minutes:seconds. |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
577 """ |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
578 # Returning intervals larger than a day is almost |
|
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
579 # 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
|
580 a = calendar.timegm((self.year, self.month, self.day, self.hour, |
| 6193 | 581 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
|
582 b = calendar.timegm((other.year, other.month, other.day, |
| 6193 | 583 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
|
584 # 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
|
585 diff = int(a - b) |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
586 if diff > 0: |
|
641
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
587 sign = 1 |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
588 else: |
|
cfa460943d4d
Oops, there's 24 hours in a day...
Richard Jones <richard@users.sourceforge.net>
parents:
640
diff
changeset
|
589 sign = -1 |
|
1894
41ad8b781232
Date - Date works again.
Anthony Baxter <anthonybaxter@users.sourceforge.net>
parents:
1885
diff
changeset
|
590 diff = -diff |
| 6193 | 591 S = diff % 60 |
| 592 M = (diff//60) % 60 | |
| 593 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
|
594 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
|
595 return Interval((0, 0, d, H, M, S), sign=sign, |
| 6193 | 596 translator=self.translator) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
597 |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
598 def __cmp__(self, other, int_seconds=0): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
599 """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
|
600 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
|
601 return 1 |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
602 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
|
603 if not hasattr(other, attr): |
|
a568596dbea7
Unit tests and a few fixes.
Richard Jones <richard@users.sourceforge.net>
parents:
861
diff
changeset
|
604 return 1 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
605 r = cmp(getattr(self, attr), getattr(other, attr)) |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
606 if r: return r |
|
2102
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
607 if not hasattr(other, 'second'): |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
608 return 1 |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
609 if int_seconds: |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
610 return cmp(int(self.second), int(other.second)) |
|
666402433998
Fix some tests.
Richard Jones <richard@users.sourceforge.net>
parents:
2100
diff
changeset
|
611 return cmp(self.second, other.second) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
612 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
613 def __lt__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
614 return self.__cmp__(other) < 0 |
| 6193 | 615 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
616 def __le__(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 __eq__(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 __ne__(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 __gt__(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 __ge__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
629 return self.__cmp__(other) >= 0 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
630 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
631 def __str__(self): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
632 """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
|
633 return self.formal() |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
634 |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
635 def formal(self, sep='.', sec='%02d'): |
| 6193 | 636 f = '%%04d-%%02d-%%02d%s%%02d:%%02d:%s' % (sep, sec) |
| 637 return f % (self.year, self.month, self.day, self.hour, self.minute, | |
| 638 self.second) | |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
639 |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
640 def isoformat(self): |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
641 ''' 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
|
642 |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
643 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
|
644 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
|
645 ''' |
| 6193 | 646 f = '%%04d-%%02d-%%02d%s%%02d:%%02d:%s' % ("T", "%02.6d") |
| 647 return f % (self.year, self.month, self.day, self.hour, self.minute, | |
| 648 self.second) | |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
649 |
|
1272
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
650 def pretty(self, format='%d %B %Y'): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
651 ''' 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
|
652 |
|
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
653 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
|
654 format, then the day number will be removed from output. |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
655 ''' |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
656 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
|
657 self.minute, int(self.second), |
| 6193 | 658 int((self.second - int(self.second)) * 1000000.)) |
|
3822
2a60b68985db
Fix arbitrary limit on dates.
Ralf Schlatterbeck <schlatterbeck@users.sourceforge.net>
parents:
3798
diff
changeset
|
659 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
|
660 |
|
1272
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
661 # handle zero day by removing it |
|
cae50587fb96
expose the Date.pretty method to templating
Richard Jones <richard@users.sourceforge.net>
parents:
1268
diff
changeset
|
662 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
|
663 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
|
664 return str |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
665 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
666 def __repr__(self): |
| 6193 | 667 return '<Date %s>' % self.formal(sec='%06.3f') |
|
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 local(self, offset): |
|
1318
46a1951fdb14
fix Date.local()
Richard Jones <richard@users.sourceforge.net>
parents:
1272
diff
changeset
|
670 """ 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
|
671 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
|
672 """ |
| 6193 | 673 y,m,d,H,M,S = _utc_to_local(self.year, self.month, self.day, |
| 674 self.hour, self.minute, self.second, offset) | |
| 675 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
|
676 |
|
2359
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
677 def __deepcopy__(self, memo): |
|
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
678 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
|
679 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
|
680 |
|
47
34491d9b91a0
Storing only marshallable data in the db - no nasty pickled class references.
Richard Jones <richard@users.sourceforge.net>
parents:
26
diff
changeset
|
681 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
|
682 return (self.year, self.month, self.day, self.hour, self.minute, |
| 6193 | 683 self.second, 0, 0, 0) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
684 |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
685 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
|
686 """ 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
|
687 |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
688 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
|
689 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
|
690 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
|
691 |
|
8ab5d7d63191
Fixed issue2550802: Fixed date so second fraction can't cause rounding to
Bernhard Reiter <bernhard@intevation.de>
parents:
4570
diff
changeset
|
692 """ |
| 6193 | 693 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
|
694 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
|
695 |
|
1946
c538a64b94a7
Refactored CGI file serving so that FileClass contents are...
Richard Jones <richard@users.sourceforge.net>
parents:
1941
diff
changeset
|
696 def timestamp(self): |
|
c538a64b94a7
Refactored CGI file serving so that FileClass contents are...
Richard Jones <richard@users.sourceforge.net>
parents:
1941
diff
changeset
|
697 ''' 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
|
698 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
|
699 ts = calendar.timegm((self.year, self.month, self.day, self.hour, |
| 6193 | 700 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
|
701 # we lose the fractional part |
|
18addf2a8596
Implemented proper datatypes in mysql and postgresql backends...
Richard Jones <richard@users.sourceforge.net>
parents:
2005
diff
changeset
|
702 return ts + frac |
|
1946
c538a64b94a7
Refactored CGI file serving so that FileClass contents are...
Richard Jones <richard@users.sourceforge.net>
parents:
1941
diff
changeset
|
703 |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
704 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
|
705 """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
|
706 |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
707 'translator' |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
708 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
|
709 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
|
710 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
|
711 """ |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
712 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
|
713 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
|
714 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
|
715 |
|
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
|
716 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
|
717 """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
|
718 |
|
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 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
|
720 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
|
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 usec = int((ts - int(ts)) * 1000000.) |
| 6193 | 723 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
|
724 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
|
725 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
|
726 |
| 6193 | 727 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
728 class Interval: |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
729 ''' |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
730 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
|
731 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
|
732 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
|
733 may not). |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
734 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
735 "3y" means three years |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
736 "2y 1m" means two years and one month |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
737 "1m 25d" means one month and 25 days |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
738 "2w 3d" means two weeks and three days |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
739 "1d 2:50" means one day, two hours, and 50 minutes |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
740 "14:00" means 14 hours |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
741 "0:04:33" means four minutes and 33 seconds |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
742 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
743 Example usage: |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
744 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
|
745 >>> 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
|
746 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
747 >>> Interval(" 3w 1 d 2:00") |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
748 <Interval + 22d 2:00> |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
749 >>> Date(". + 2d") + Interval("- 3w") |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
750 <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
|
751 >>> Interval('1:59:59') + Interval('00:00:01') |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
752 <Interval + 2:00> |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
753 >>> Interval('2:00') + Interval('- 00:00:01') |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
754 <Interval + 1:59:59> |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
755 >>> Interval('1y')/2 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
756 <Interval + 6m> |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
757 >>> Interval('1:00')/2 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
758 <Interval + 0:30> |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
759 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
760 [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
|
761 >>> Interval('2003-03-18') |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
762 <Interval - 995d> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
763 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
764 [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
|
765 >>> Interval('-4d 2003-03-18') |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
766 <Interval - 991d> |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
767 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
768 >>> test_fin(u) |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
769 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
770 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
|
771 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
|
772 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
|
773 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
|
774 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
|
775 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
776 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
|
777 seconds, minutes, hours, years, months, days |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
778 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
779 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
|
780 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
|
781 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
|
782 |
|
964
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
783 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
|
784 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
|
785 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
|
786 |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
787 TODO: more examples, showing the order of addition operation |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
788 ''' |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
789 def __init__(self, spec, sign=1, allowdate=1, add_granularity=False, |
| 6193 | 790 translator=i18n): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
791 """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
|
792 self.setTranslator(translator) |
|
5394
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
793 try: |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
794 # Python 2. |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
795 arith_types = (int, float, long) |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
796 except NameError: |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
797 # Python 3. |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
798 arith_types = (int, float) |
|
c26d88ec071e
Python 3 preparation: handle absence of "long" type.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5380
diff
changeset
|
799 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
|
800 self.from_seconds(spec) |
|
5438
e2382945d302
Python 3 preparation: avoid basestring.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5414
diff
changeset
|
801 elif is_us(spec): |
| 6193 | 802 self.set(spec, allowdate=allowdate, |
| 803 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
|
804 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
|
805 (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
|
806 self.minute, self.second) = spec.get_tuple() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
807 else: |
|
1094
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
808 if len(spec) == 7: |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
809 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
|
810 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
|
811 self.second = int(self.second) |
|
1094
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
812 else: |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
813 # old, buggy spec form |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
814 self.sign = sign |
|
854d45f8b745
fixes to tests and Interval unmarshalling
Richard Jones <richard@users.sourceforge.net>
parents:
1090
diff
changeset
|
815 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
|
816 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
|
817 self.second = int(self.second) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
818 |
|
2359
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
819 def __deepcopy__(self, memo): |
|
8611bf29baec
deepcopy was broken for date stuff
Richard Jones <richard@users.sourceforge.net>
parents:
2339
diff
changeset
|
820 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
|
821 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
|
822 |
| 5809 | 823 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
|
824 \s*(?P<s>[-+])? # + or - |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
825 \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
|
826 \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
|
827 \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
|
828 \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
|
829 \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
|
830 \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
|
831 (\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
|
832 \.? # . |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
833 (\d?\d:\d\d)?(:\d\d)? # hh:mm:ss |
|
5811
7d276bb8b46d
More invalid escape sequence fixes.
John Rouillard <rouilj@ieee.org>
parents:
5809
diff
changeset
|
834 )?''', 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
|
835 (?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
|
836 (?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
|
837 add_granularity=False): |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
838 ''' 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
|
839 ''' |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
840 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
|
841 self.minute = self.second = 0 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
842 self.sign = 1 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
843 m = serialised_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
844 if not m: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
845 m = interval_re.match(spec) |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
846 if not m: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
847 raise ValueError(self._('Not an interval spec: "%s"' |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
848 ' ([+-] [#y] [#m] [#w] [#d] [[[H]H:MM]:SS] [date spec])' |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
849 % spec)) |
|
1542
3faabaeab6aa
fixed date unserialisation for *dbm backends
Richard Jones <richard@users.sourceforge.net>
parents:
1537
diff
changeset
|
850 else: |
|
3faabaeab6aa
fixed date unserialisation for *dbm backends
Richard Jones <richard@users.sourceforge.net>
parents:
1537
diff
changeset
|
851 allowdate = 0 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
852 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
853 # 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
|
854 info = m.groupdict() |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
855 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
|
856 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
|
857 if info[gran] is not None: |
| 6193 | 858 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
|
859 break |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
860 |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
861 valid = 0 |
| 6193 | 862 for group, attr in {'y': 'year', 'm': 'month', 'w': 'week', 'd': 'day', |
| 863 '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
|
864 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
|
865 valid = 1 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
866 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
|
867 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
868 # 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
|
869 if not valid and not info['D']: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
870 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
|
871 ' ([+-] [#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
|
872 % spec)) |
|
1535
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
873 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
874 if self.week: |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
875 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
|
876 |
|
26f29449c494
fixed detection of bad date specs [SF#691439]
Richard Jones <richard@users.sourceforge.net>
parents:
1509
diff
changeset
|
877 if info['s'] is not None: |
| 6193 | 878 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
|
879 |
|
1537
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
880 # 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
|
881 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
|
882 now = Date('.') |
|
0e36c9b23aa6
more lenient date input and addition Interval input support [SF#677764]
Richard Jones <richard@users.sourceforge.net>
parents:
1535
diff
changeset
|
883 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
|
884 # 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
|
885 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
|
886 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
|
887 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
|
888 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
|
889 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
|
890 |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
891 def __lt__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
892 """Compare this interval to another interval.""" |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
893 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
894 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
895 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
896 return False |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
897 return self.as_seconds() < other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
898 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
899 def __le__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
900 """Compare this interval to another interval.""" |
|
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 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
903 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
904 return False |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
905 return self.as_seconds() <= other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
906 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
907 def __eq__(self, other): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
908 """Compare this interval to another interval.""" |
|
4461
5f82173d1684
Simplify and fix interval comparison.
Stefan Seefeld <stefan@seefeld.name>
parents:
4422
diff
changeset
|
909 |
|
4465
605f31a596b8
Simplify and fix interval comparison.
Stefan Seefeld <stefan@seefeld.name>
parents:
4461
diff
changeset
|
910 if other is None: |
|
605f31a596b8
Simplify and fix interval comparison.
Stefan Seefeld <stefan@seefeld.name>
parents:
4461
diff
changeset
|
911 # we are always larger than None |
|
5414
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
912 return False |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
913 return self.as_seconds() == other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
914 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
915 def __ne__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
916 """Compare this interval to another interval.""" |
|
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 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
919 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
920 return True |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
921 return self.as_seconds() != other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
922 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
923 def __gt__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
924 """Compare this interval to another interval.""" |
|
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 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
927 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
928 return True |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
929 return self.as_seconds() > other.as_seconds() |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
930 |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
931 def __ge__(self, other): |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
932 """Compare this interval to another interval.""" |
|
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 if other is None: |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
935 # we are always larger than None |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
936 return True |
|
3fa026621f69
Python 3 preparation: comparisons.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5400
diff
changeset
|
937 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
|
938 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
939 def __str__(self): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
940 """Return this interval as a string.""" |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
941 l = [] |
| 6193 | 942 if self.year: l.append('%sy' % self.year) |
| 943 if self.month: l.append('%sm' % self.month) | |
| 944 if self.day: l.append('%sd' % self.day) | |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
945 if self.second: |
| 6193 | 946 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
|
947 elif self.hour or self.minute: |
| 6193 | 948 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
|
949 if l: |
| 6193 | 950 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
|
951 else: |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
952 l.append('00:00') |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
953 return ' '.join(l) |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
954 |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
955 def __add__(self, other): |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
956 if isinstance(other, Date): |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
957 # 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
|
958 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
|
959 elif isinstance(other, Interval): |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
960 # add the other Interval to this one |
|
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
961 a = self.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
962 asgn = a[0] |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
963 b = other.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
964 bsgn = b[0] |
| 6193 | 965 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
|
966 i.insert(0, 1) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
967 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
|
968 return Interval(i, translator=self.translator) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
969 # nope, no idea what to do with this other... |
| 6193 | 970 raise TypeError("Can't add %r" % other) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
971 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
972 def __sub__(self, other): |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
973 if isinstance(other, Date): |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
974 # the other is a Date - produce a Date |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
975 interval = Interval(self.get_tuple()) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
976 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
|
977 return Date(other.addInterval(interval), |
| 6193 | 978 translator=self.translator) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
979 elif isinstance(other, Interval): |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
980 # add the other Interval to this one |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
981 a = self.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
982 asgn = a[0] |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
983 b = other.get_tuple() |
|
3702
06d7816976bc
python 2.6 compatibility
Richard Jones <richard@users.sourceforge.net>
parents:
3619
diff
changeset
|
984 bsgn = b[0] |
| 6193 | 985 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
|
986 i.insert(0, 1) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
987 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
|
988 return Interval(i, translator=self.translator) |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
989 # nope, no idea what to do with this other... |
| 6193 | 990 raise TypeError("Can't add %r" % other) |
|
1268
b34adc9bcaf2
implemented the missing Interval.__add__
Richard Jones <richard@users.sourceforge.net>
parents:
1266
diff
changeset
|
991 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
992 def __truediv__(self, other): |
|
1930
71056b09f2bf
Add tests for Interval.pretty().
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1894
diff
changeset
|
993 """ Divide this interval by an int value. |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
994 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
995 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
|
996 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
|
997 """ |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
998 try: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
999 other = float(other) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1000 except TypeError: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
1001 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
|
1002 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1003 y, m, d, H, M, S = (self.year, self.month, self.day, |
| 6193 | 1004 self.hour, self.minute, self.second) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1005 if y or m: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1006 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
|
1007 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
|
1008 months = self.year*12 + self.month |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1009 months *= self.sign |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1010 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1011 months = int(months/other) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1012 |
| 6193 | 1013 sign = months < 0 and -1 or 1 |
| 1014 m = months % 12 | |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1015 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
|
1016 return Interval((sign, y, m, 0, 0, 0, 0), |
| 6193 | 1017 translator=self.translator) |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1018 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1019 else: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1020 # handle a day/time division |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1021 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
|
1022 seconds *= self.sign |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1023 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1024 seconds = int(seconds/other) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1025 |
| 6193 | 1026 sign = seconds < 0 and -1 or 1 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1027 seconds *= sign |
| 6193 | 1028 S = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1029 seconds //= 60 |
| 6193 | 1030 M = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1031 seconds //= 60 |
| 6193 | 1032 H = seconds % 24 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1033 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
|
1034 return Interval((sign, 0, 0, d, H, M, S), |
| 6193 | 1035 translator=self.translator) |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1036 # Python 2 compatibility: |
|
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1037 __div__ = __truediv__ |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1038 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1039 def __repr__(self): |
| 6193 | 1040 return '<Interval %s>' % self.__str__() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1041 |
|
487
2f4c37d28108
I18N'ification
Richard Jones <richard@users.sourceforge.net>
parents:
406
diff
changeset
|
1042 def pretty(self): |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1043 ''' print up the date date using one of these nice formats.. |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1044 ''' |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1045 _quarters = self.minute // 15 |
|
720
5e0a75bfdd90
reverting to dates for intervals > 2 months sucks
Richard Jones <richard@users.sourceforge.net>
parents:
641
diff
changeset
|
1046 if self.year: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1047 s = self.ngettext("%(number)s year", "%(number)s years", |
| 6193 | 1048 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
|
1049 elif self.month or self.day > 28: |
| 2416 | 1050 _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
|
1051 s = self.ngettext("%(number)s month", "%(number)s months", |
| 6193 | 1052 _months) % {'number': _months} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1053 elif self.day > 7: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1054 _weeks = int(self.day / 7) |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1055 s = self.ngettext("%(number)s week", "%(number)s weeks", |
| 6193 | 1056 _weeks) % {'number': _weeks} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1057 elif self.day > 1: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1058 # 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
|
1059 s = self.ngettext('%(number)s day', '%(number)s days', |
| 6193 | 1060 self.day) % {'number': self.day} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1061 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
|
1062 if self.sign > 0: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1063 return self._('tomorrow') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1064 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1065 return self._('yesterday') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1066 elif self.hour > 1: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1067 # 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
|
1068 s = self.ngettext('%(number)s hour', '%(number)s hours', |
| 6193 | 1069 self.hour) % {'number': self.hour} |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1070 elif self.hour == 1: |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1071 if self.minute < 15: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1072 s = self._('an hour') |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1073 elif _quarters == 2: |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1074 s = self._('1 1/2 hours') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1075 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1076 s = self.ngettext('1 %(number)s/4 hours', |
| 6193 | 1077 '1 %(number)s/4 hours', |
| 1078 _quarters) % {'number': _quarters} | |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1079 elif self.minute < 1: |
|
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1080 if self.sign > 0: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1081 return self._('in a moment') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1082 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1083 return self._('just now') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1084 elif self.minute == 1: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1085 # 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
|
1086 s = self._('1 minute') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1087 elif self.minute < 15: |
|
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 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
|
1089 s = self.ngettext('%(number)s minute', '%(number)s minutes', |
| 6193 | 1090 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
|
1091 elif _quarters == 2: |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1092 s = self._('1/2 an hour') |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1093 else: |
| 2416 | 1094 s = self.ngettext('%(number)s/4 hour', '%(number)s/4 hours', |
| 6193 | 1095 _quarters) % {'number': _quarters} |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1096 # XXX this is internationally broken |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1097 if self.sign < 0: |
|
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1098 s = self._('%s ago') % s |
|
1413
4ae9d725bec4
make future intervals obvious too
Richard Jones <richard@users.sourceforge.net>
parents:
1412
diff
changeset
|
1099 else: |
|
2339
6ba57546d212
fix i18n - mainly plural forms in Interval.pretty()
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2262
diff
changeset
|
1100 s = self._('in %s') % s |
|
640
7dd13fd5d8ea
fixed some problems in date calculations
Richard Jones <richard@users.sourceforge.net>
parents:
585
diff
changeset
|
1101 return s |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1102 |
|
47
34491d9b91a0
Storing only marshallable data in the db - no nasty pickled class references.
Richard Jones <richard@users.sourceforge.net>
parents:
26
diff
changeset
|
1103 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
|
1104 return (self.sign, self.year, self.month, self.day, self.hour, |
| 6193 | 1105 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
|
1106 |
|
832d1209aaa2
Preparing to turn back on link/unlink journal events.
Richard Jones <richard@users.sourceforge.net>
parents:
890
diff
changeset
|
1107 def serialise(self): |
|
1261
85d71588a1cf
fixed bug in Interval serialisation
Richard Jones <richard@users.sourceforge.net>
parents:
1192
diff
changeset
|
1108 sign = self.sign > 0 and '+' or '-' |
| 6193 | 1109 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
|
1110 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
|
1111 |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1112 def isoformat(self): |
|
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1113 '''Represent interval as an ISO 8061 duration (absolute value) |
| 6193 | 1114 |
|
5637
43970d92323e
Add two isoformat() methods. One for Date and one for Interval.
John Rouillard <rouilj@ieee.org>
parents:
5438
diff
changeset
|
1115 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
|
1116 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
|
1117 ''' |
| 6193 | 1118 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
|
1119 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
|
1120 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1121 def as_seconds(self): |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1122 '''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
|
1123 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1124 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
|
1125 int. |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1126 ''' |
|
5380
64c4e43fbb84
Python 3 preparation: numeric literal syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5378
diff
changeset
|
1127 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
|
1128 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
|
1129 n = n + self.day |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1130 n = n * 24 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1131 n = n + self.hour |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1132 n = n * 60 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1133 n = n + self.minute |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1134 n = n * 60 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1135 n = n + self.second |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1136 return n * self.sign |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1137 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1138 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
|
1139 '''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
|
1140 value. |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1141 ''' |
|
2981
ad04cb95e2b0
fix Interval.from_seconds() with float argument
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2729
diff
changeset
|
1142 val = int(val) |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1143 if val < 0: |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1144 self.sign = -1 |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1145 val = -val |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1146 else: |
|
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 self.second = val % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1149 val = val // 60 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1150 self.minute = val % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1151 val = val // 60 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1152 self.hour = val % 24 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1153 val = val // 24 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1154 self.day = val |
|
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1155 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
|
1156 |
|
2388
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1157 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
|
1158 """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
|
1159 |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1160 'translator' |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1161 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
|
1162 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
|
1163 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
|
1164 """ |
|
8a9af227a557
Date and Interval objects allow to change translator after instance creation...
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
2359
diff
changeset
|
1165 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
|
1166 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
|
1167 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
|
1168 |
|
2212
75e46660345c
stop Interval from displaying an empty string [SF#934022]
Richard Jones <richard@users.sourceforge.net>
parents:
2189
diff
changeset
|
1169 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1170 def fixTimeOverflow(time): |
|
1930
71056b09f2bf
Add tests for Interval.pretty().
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1894
diff
changeset
|
1171 """ 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
|
1172 (sign, y,m,d,H,M,S) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1173 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1174 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
|
1175 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
|
1176 to, generally. |
|
1930
71056b09f2bf
Add tests for Interval.pretty().
Johannes Gijsbers <jlgijsbers@users.sourceforge.net>
parents:
1894
diff
changeset
|
1177 """ |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1178 # 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
|
1179 # 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
|
1180 # XXX overflow... |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1181 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1182 sign, y, m, d, H, M, S = time |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1183 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
|
1184 if seconds: |
| 6193 | 1185 sign = seconds < 0 and -1 or 1 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1186 seconds *= sign |
| 6193 | 1187 S = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1188 seconds //= 60 |
| 6193 | 1189 M = seconds % 60 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1190 seconds //= 60 |
| 6193 | 1191 H = seconds % 24 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1192 d = seconds // 24 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1193 else: |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1194 months = y*12 + m |
| 6193 | 1195 sign = months < 0 and -1 or 1 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1196 months *= sign |
| 6193 | 1197 m = months % 12 |
|
5400
2120f77554d5
Python 3 preparation: use // and __truediv__ as needed.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5394
diff
changeset
|
1198 y = months//12 |
|
1487
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1199 |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1200 return (sign, y, m, d, H, M, S) |
|
37a1906f4454
fixed Interval maths [SF#665357]
Richard Jones <richard@users.sourceforge.net>
parents:
1463
diff
changeset
|
1201 |
| 6193 | 1202 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1203 class Range: |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1204 """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
|
1205 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
|
1206 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1207 1. Native english syntax:: |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1208 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1209 [[From] <value>][ To <value>] |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1210 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1211 Keywords "From" and "To" are case insensitive. Keyword "From" is |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1212 optional. |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1213 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1214 2. "Geek" syntax:: |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1215 |
|
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1216 [<value>][; <value>] |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1217 |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1218 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
|
1219 |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1946
diff
changeset
|
1220 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
|
1221 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1222 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
|
1223 >>> 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
|
1224 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1225 >>> 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
|
1226 <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
|
1227 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1228 >>> 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
|
1229 <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
|
1230 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1231 >>> Range("tO +3d", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1232 <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
|
1233 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1234 >>> 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
|
1235 <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
|
1236 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1237 >>> Range("12:00;", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
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("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
|
1241 <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
|
1242 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1243 >>> 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
|
1244 <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
|
1245 |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1246 >>> Range("from 2003-02-16", Date) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1247 <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
|
1248 |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1249 >>> Range("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 |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1252 Granularity tests: |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1253 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1254 >>> Range("12:00", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1255 <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
|
1256 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1257 >>> Range("2003-03-08", Date) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1258 <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
|
1259 |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1260 >>> test_fin(u) |
|
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1261 |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1262 Range of Interval tests |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1263 |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1264 >>> 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
|
1265 <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
|
1266 >>> 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
|
1267 <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
|
1268 >>> Range ("from 5:50", Interval) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1269 <Range from + 5:50 to None> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1270 >>> Range ("to 0:05", Interval) |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1271 <Range from None to + 0:05> |
|
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1272 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1273 """ |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1274 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
|
1275 """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
|
1276 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1277 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
|
1278 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
|
1279 +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
|
1280 |
|
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1281 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
|
1282 class instance. |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1283 """ |
|
1509
1d4ebe2a88fc
hope this will make Range class a little bit clearer
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1505
diff
changeset
|
1284 self.range_type = Type |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1285 re_range = r'^(?:from)?(.+?)?to(.+?)?$' |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1286 re_range_no_to = r'^from(.+)(.)?$' |
|
4985
07c59221f363
Make doctests run in date module
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4856
diff
changeset
|
1287 re_geek_range = r'^(.+?)?;(.+?)?$' |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1288 # 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
|
1289 if ';' in spec: |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1290 # Geek |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1291 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
|
1292 else: |
|
3954
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1293 # Native english |
|
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_range, spec.strip(), re.IGNORECASE) |
| 6193 | 1295 if not m: |
|
4991
03505579abef
Fix bug in (Date- or Interval-) Range parsing
Ralf Schlatterbeck <rsc@runtux.com>
parents:
4986
diff
changeset
|
1296 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
|
1297 if m: |
|
3d5a0a949107
Fix granularity stuff so it handles wrapping a lot better.
Richard Jones <richard@users.sourceforge.net>
parents:
3953
diff
changeset
|
1298 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
|
1299 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
|
1300 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
|
1301 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
|
1302 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
|
1303 else: |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1304 if allow_granularity: |
|
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1305 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
|
1306 self.to_value = Type(spec, add_granularity=True, **params) |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1307 else: |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5377
diff
changeset
|
1308 raise ValueError("Invalid range") |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1309 |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1310 def __str__(self): |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1311 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
|
1312 |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1313 def __repr__(self): |
|
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1314 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
|
1315 |
| 6193 | 1316 |
|
1499
8ee69708da0c
added support for searching on ranges of dates
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1494
diff
changeset
|
1317 def test_range(): |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1318 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
|
1319 "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
|
1320 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
|
1321 for rspec in rspecs: |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4991
diff
changeset
|
1322 print('>>> Range("%s")' % rspec) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1323 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
|
1324 print() |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1325 for rspec in rispecs: |
|
5376
64b05e24dbd8
Python 3 preparation: convert print to a function.
Joseph Myers <jsm@polyomino.org.uk>
parents:
4991
diff
changeset
|
1326 print('>>> Range("%s")' % rspec) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1327 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
|
1328 print() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1329 |
| 6193 | 1330 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1331 def test(): |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1332 intervals = (" 3w 1 d 2:00", " + 2d", "3w") |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1333 for interval in intervals: |
| 6193 | 1334 print('>>> Interval("%s")' % interval) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1335 print(repr(Interval(interval))) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1336 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1337 dates = (".", "2000-06-25.19:34:02", ". + 2d", "1997-04-17", "01-25", |
| 6193 | 1338 "08-13.22:13", "14:25", '2002-12') |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1339 for date in dates: |
| 6193 | 1340 print('>>> Date("%s")' % date) |
|
5377
12fe83f90f0d
Python 3 preparation: use repr() instead of ``.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5376
diff
changeset
|
1341 print(repr(Date(date))) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1342 |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1343 sums = ((". + 2d", "3w"), (".", " 3w 1 d 2:00")) |
|
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1344 for date, interval in sums: |
| 6193 | 1345 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
|
1346 print(repr(Date(date) + Interval(interval))) |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1347 |
| 6193 | 1348 |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1349 if __name__ == '__main__': |
|
1597
4d55f90d4af1
granularity based ranges
Andrey Lebedev <kedder@users.sourceforge.net>
parents:
1542
diff
changeset
|
1350 test() |
|
25
4cf1daf2f2eb
More Grande Splite
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1351 |
|
3210
ea29d69f7415
fix error message formatting in Date.__init__;
Alexander Smishlajev <a1s@users.sourceforge.net>
parents:
3151
diff
changeset
|
1352 # vim: set filetype=python sts=4 sw=4 et si : |
