Mercurial > p > mysql-python > mysqldb-2
annotate MySQLdb/times.py @ 42:fdf0cabb27be MySQLdb
Member stuff is stable post py2.2, so remove the MyMember* workarounds
author | kylev |
---|---|
date | Tue, 17 Feb 2009 05:55:24 +0000 |
parents | 7773efbe9b30 |
children | 98d968f5af11 |
rev | line source |
---|---|
13 | 1 """ |
2 times module | |
3 ------------ | |
0 | 4 |
13 | 5 This module provides some help functions for dealing with MySQL data. |
6 Most of these you will not have to use directly. | |
0 | 7 |
13 | 8 Uses Python datetime module to handle time-releated columns.""" |
0 | 9 |
14 | 10 __revision__ = "$Revision$"[11:-2] |
11 __author__ = "$Author$"[9:-2] | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
12 |
0 | 13 from time import localtime |
14 from datetime import date, datetime, time, timedelta | |
15 from _mysql import string_literal | |
16 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
17 # These are required for DB-API (PEP-249) |
0 | 18 Date = date |
19 Time = time | |
20 TimeDelta = timedelta | |
21 Timestamp = datetime | |
22 | |
23 def DateFromTicks(ticks): | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
24 """Convert UNIX ticks into a date instance. |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
25 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
26 >>> DateFromTicks(1172466380) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
27 datetime.date(2007, 2, 25) |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
28 >>> DateFromTicks(0) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
29 datetime.date(1969, 12, 31) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
30 >>> DateFromTicks(2**31-1) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
31 datetime.date(2038, 1, 18) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
32 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
33 This is a standard DB-API constructor. |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
34 """ |
0 | 35 return date(*localtime(ticks)[:3]) |
36 | |
37 def TimeFromTicks(ticks): | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
38 """Convert UNIX ticks into a time instance. |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
39 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
40 >>> TimeFromTicks(1172466380) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
41 datetime.time(23, 6, 20) |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
42 >>> TimeFromTicks(0) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
43 datetime.time(18, 0) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
44 >>> TimeFromTicks(2**31-1) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
45 datetime.time(21, 14, 7) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
46 |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
47 This is a standard DB-API constructor. |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
48 """ |
0 | 49 return time(*localtime(ticks)[3:6]) |
50 | |
51 def TimestampFromTicks(ticks): | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
52 """Convert UNIX ticks into a datetime instance. |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
53 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
54 >>> TimestampFromTicks(1172466380) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
55 datetime.datetime(2007, 2, 25, 23, 6, 20) |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
56 >>> TimestampFromTicks(0) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
57 datetime.datetime(1969, 12, 31, 18, 0) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
58 >>> TimestampFromTicks(2**31-1) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
59 datetime.datetime(2038, 1, 18, 21, 14, 7) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
60 |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
61 This is a standard DB-API constructor. |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
62 """ |
0 | 63 return datetime(*localtime(ticks)[:6]) |
64 | |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
65 def timedelta_to_str(obj): |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
66 """Format a timedelta as a string. |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
67 |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
68 >>> timedelta_to_str(timedelta(seconds=-86400)) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
69 '-1 00:00:00' |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
70 >>> timedelta_to_str(timedelta(hours=73, minutes=15, seconds=32)) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
71 '3 01:15:32' |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
72 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
73 """ |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
74 seconds = int(obj.seconds) % 60 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
75 minutes = int(obj.seconds / 60) % 60 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
76 hours = int(obj.seconds / 3600) % 24 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
77 return '%d %02d:%02d:%02d' % (obj.days, hours, minutes, seconds) |
4 | 78 |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
79 def datetime_to_str(obj): |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
80 """Convert a datetime to an ISO-format string. |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
81 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
82 >>> datetime_to_str(datetime(2007, 2, 25, 23, 6, 20)) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
83 '2007-02-25 23:06:20' |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
84 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
85 """ |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
86 return obj.strftime("%Y-%m-%d %H:%M:%S") |
0 | 87 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
88 def datetime_or_None(obj): |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
89 """Returns a DATETIME or TIMESTAMP column value as a datetime object: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
90 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
91 >>> datetime_or_None('2007-02-25 23:06:20') |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
92 datetime.datetime(2007, 2, 25, 23, 6, 20) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
93 >>> datetime_or_None('2007-02-25T23:06:20') |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
94 datetime.datetime(2007, 2, 25, 23, 6, 20) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
95 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
96 Illegal values are returned as None: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
97 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
98 >>> datetime_or_None('2007-02-31T23:06:20') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
99 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
100 >>> datetime_or_None('0000-00-00 00:00:00') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
101 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
102 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
103 """ |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
104 if ' ' in obj: |
0 | 105 sep = ' ' |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
106 elif 'T' in obj: |
0 | 107 sep = 'T' |
108 else: | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
109 return date_or_None(obj) |
0 | 110 |
111 try: | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
112 ymd, hms = obj.split(sep, 1) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
113 return datetime(*[ int(x) for x in ymd.split('-')+hms.split(':') ]) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
114 except ValueError: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
115 return date_or_None(obj) |
0 | 116 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
117 def timedelta_or_None(obj): |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
118 """Returns a TIME column as a timedelta object: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
119 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
120 >>> timedelta_or_None('25:06:17') |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
121 datetime.timedelta(1, 3977) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
122 >>> timedelta_or_None('-25:06:17') |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
123 datetime.timedelta(-2, 83177) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
124 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
125 Illegal values are returned as None: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
126 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
127 >>> timedelta_or_None('random crap') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
128 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
129 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
130 Note that MySQL always returns TIME columns as (+|-)HH:MM:SS, but |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
131 can accept values as (+|-)DD HH:MM:SS. The latter format will not |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
132 be parsed correctly by this function. |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
133 """ |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
134 from math import modf |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
135 try: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
136 hours, minutes, seconds = obj.split(':') |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
137 tdelta = timedelta( |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
138 hours = int(hours), |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
139 minutes = int(minutes), |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
140 seconds = int(seconds), |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
141 microseconds = int(modf(float(seconds))[0]*1000000), |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
142 ) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
143 if hours < 0: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
144 return -tdelta |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
145 else: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
146 return tdelta |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
147 except ValueError: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
148 return None |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
149 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
150 def time_or_None(obj): |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
151 """Returns a TIME column as a time object: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
152 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
153 >>> time_or_None('15:06:17') |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
154 datetime.time(15, 6, 17) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
155 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
156 Illegal values are returned as None: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
157 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
158 >>> time_or_None('-25:06:17') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
159 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
160 >>> time_or_None('random crap') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
161 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
162 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
163 Note that MySQL always returns TIME columns as (+|-)HH:MM:SS, but |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
164 can accept values as (+|-)DD HH:MM:SS. The latter format will not |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
165 be parsed correctly by this function. |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
166 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
167 Also note that MySQL's TIME column corresponds more closely to |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
168 Python's timedelta and not time. However if you want TIME columns |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
169 to be treated as time-of-day and not a time offset, then you can |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
170 use set this function as the converter for FIELD_TYPE.TIME. |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
171 """ |
0 | 172 from math import modf |
173 try: | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
174 hour, minute, second = obj.split(':') |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
175 return time(hour=int(hour), minute=int(minute), second=int(second), |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
176 microsecond=int(modf(float(second))[0]*1000000)) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
177 except ValueError: |
0 | 178 return None |
179 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
180 def date_or_None(obj): |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
181 """Returns a DATE column as a date object: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
182 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
183 >>> date_or_None('2007-02-26') |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
184 datetime.date(2007, 2, 26) |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
185 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
186 Illegal values are returned as None: |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
187 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
188 >>> date_or_None('2007-02-31') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
189 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
190 >>> date_or_None('0000-00-00') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
191 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
192 |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
193 """ |
0 | 194 try: |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
195 return date(*[ int(x) for x in obj.split('-', 2) ]) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
196 except ValueError: |
0 | 197 return None |
198 | |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
199 def datetime_to_sql(obj, conv): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
200 """Format a DateTime object as an ISO timestamp.""" |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
201 return string_literal(datetime_to_str(obj), conv) |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
202 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
203 def timedelta_to_sql(obj, conv): |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
204 """Format a timedelta as an SQL literal.""" |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
205 return string_literal(timedelta_to_str(obj), conv) |
0 | 206 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
207 def mysql_timestamp_converter(timestamp): |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
208 """Convert a MySQL TIMESTAMP to a Timestamp object. |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
209 |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
210 MySQL >= 4.1 returns TIMESTAMP in the same format as DATETIME: |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
211 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
212 >>> mysql_timestamp_converter('2007-02-25 22:32:17') |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
213 datetime.datetime(2007, 2, 25, 22, 32, 17) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
214 |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
215 MySQL < 4.1 uses a big string of numbers: |
0 | 216 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
217 >>> mysql_timestamp_converter('20070225223217') |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
218 datetime.datetime(2007, 2, 25, 22, 32, 17) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
219 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
220 Illegal values are returned as None: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
221 |
12
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
222 >>> mysql_timestamp_converter('2007-02-31 22:32:17') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
223 True |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
224 >>> mysql_timestamp_converter('00000000000000') is None |
d68fe80ce1c3
More docstrings and doctests for times and other cleanups.
adustman
parents:
11
diff
changeset
|
225 True |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
226 |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
227 """ |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
228 if timestamp[4] == '-': |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
229 return datetime_or_None(timestamp) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
230 timestamp += "0"*(14-len(timestamp)) # padding |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
231 year, month, day, hour, minute, second = \ |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
232 int(timestamp[:4]), int(timestamp[4:6]), int(timestamp[6:8]), \ |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
233 int(timestamp[8:10]), int(timestamp[10:12]), int(timestamp[12:14]) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
234 try: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
235 return datetime(year, month, day, hour, minute, second) |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
236 except ValueError: |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
237 return None |
0 | 238 |
11
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
239 if __name__ == "__main__": |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
240 import doctest |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
241 doctest.testmod() |
aef6ea6a9737
More PyLint-inspired cleanups. Added a bunch of doctests in times.
adustman
parents:
4
diff
changeset
|
242 |