File tree Expand file tree Collapse file tree 2 files changed +34
-20
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +34
-20
lines changed
Original file line number Diff line number Diff line change @@ -48,25 +48,32 @@ def format_TIMESTAMP(d):
48
48
49
49
50
50
def DateTime_or_None (s ):
51
- if ' ' in s :
52
- sep = ' '
53
- elif 'T' in s :
54
- sep = 'T'
55
- else :
56
- return Date_or_None (s )
57
-
58
51
try :
59
- d , t = s .split (sep , 1 )
60
- if '.' in t :
61
- t , ms = t .split ('.' ,1 )
62
- ms = ms .ljust (6 , '0' )
52
+ if len (s ) < 11 :
53
+ return Date_or_None (s )
54
+
55
+ micros = s [20 :]
56
+
57
+ if len (micros ) == 0 :
58
+ # 12:00:00
59
+ micros = 0
60
+ elif len (micros ) < 7 :
61
+ # 12:00:00.123456
62
+ micros = int (micros ) * 10 ** (6 - len (micros ))
63
63
else :
64
- ms = 0
65
- return datetime (* [ int (x ) for x in d .split ('-' )+ t .split (':' )+ [ms ] ])
66
- except (SystemExit , KeyboardInterrupt ):
67
- raise # pragma: no cover
68
- except :
69
- return Date_or_None (s )
64
+ return None
65
+
66
+ return datetime (
67
+ int (s [:4 ]), # year
68
+ int (s [5 :7 ]), # month
69
+ int (s [8 :10 ]), # day
70
+ int (s [11 :13 ] or 0 ), # hour
71
+ int (s [14 :16 ] or 0 ), # minute
72
+ int (s [17 :19 ] or 0 ), # second
73
+ micros , # microsecond
74
+ )
75
+ except ValueError :
76
+ return None
70
77
71
78
def TimeDelta_or_None (s ):
72
79
try :
@@ -107,14 +114,18 @@ def Time_or_None(s):
107
114
108
115
def Date_or_None (s ):
109
116
try :
110
- return date (* [ int (x ) for x in s .split ('-' ,2 )])
111
- except (TypeError , ValueError ):
117
+ return date (
118
+ int (s [:4 ]), # year
119
+ int (s [5 :7 ]), # month
120
+ int (s [8 :10 ]), # day
121
+ )
122
+ except ValueError :
112
123
return None
113
124
114
125
def DateTime2literal (d , c ):
115
126
"""Format a DateTime object as an ISO timestamp."""
116
127
return string_literal (format_TIMESTAMP (d ), c )
117
-
128
+
118
129
def DateTimeDelta2literal (d , c ):
119
130
"""Format a DateTimeDelta object as a time."""
120
131
return string_literal (format_TIMEDELTA (d ),c )
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ def test_date_or_none(self):
19
19
assert times .Date_or_None ('fail' ) is None
20
20
assert times .Date_or_None ('2015-12' ) is None
21
21
assert times .Date_or_None ('2015-12-40' ) is None
22
+ assert times .Date_or_None ('0000-00-00' ) is None
22
23
23
24
def test_time_or_none (self ):
24
25
assert times .Time_or_None ('00:00:00' ) == time (0 , 0 )
@@ -44,6 +45,8 @@ def test_datetime_or_none(self):
44
45
45
46
assert times .DateTime_or_None ('' ) is None
46
47
assert times .DateTime_or_None ('fail' ) is None
48
+ assert times .DateTime_or_None ('0000-00-00 00:00:00' ) is None
49
+ assert times .DateTime_or_None ('0000-00-00 00:00:00.000000' ) is None
47
50
assert times .DateTime_or_None ('2015-12-13T01:02:03.123456789' ) is None
48
51
49
52
def test_timedelta_or_none (self ):
You can’t perform that action at this time.
0 commit comments