forked from snowflakedb/snowflake-connector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfdatetime.py
423 lines (383 loc) · 16.6 KB
/
sfdatetime.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2017 Snowflake Computing Inc. All right reserved.
#
import time
from datetime import datetime, timedelta
import pytz
from . import errors
from .compat import TO_UNICODE
from .constants import UTF8
from .mixin import UnicodeMixin
ZERO_TIMEDELTA = timedelta(0)
ElementType = {
u'Year2digit_ElementType': [u"YY", u"%y"],
u'Year_ElementType': [u"YYYY", u"%Y"],
u'Month_ElementType': [u"MM", u"%m"],
u'MonthAbbrev_ElementType': [u"MON", u"%b"],
u'DayOfMonth_ElementType': [u"DD", u"%d"],
u'DayOfWeekAbbrev_ElementType': [u"DY", u"%a"],
u'Hour24_ElementType': [u"HH24", u"%H"],
u'Hour12_ElementType': [u"HH12", u"%I"],
u'Hour_ElementType': [u"HH", u"%H"],
u'Ante_Meridiem_ElementType': [u"AM", u"%p"],
u'Post_Meridiem_ElementType': [u"PM", u"%p"],
u'Minute_ElementType': [u"MI", u"%M"],
u'Second_ElementType': [u"SS", u"%S"],
u'MilliSecond_ElementType': [u"FF", u""],
# special code for parsing fractions
u'TZOffsetHourColonMin_ElementType': [u"TZH:TZM", u"%z"],
u'TZOffsetHourMin_ElementType': [u"TZHTZM", u"%z"],
u'TZOffsetHourOnly_ElementType': [u"TZH", u"%z"],
u'TZAbbr_ElementType': [u"TZD", u"%Z"],
}
def sfdatetime_total_seconds_from_timedelta(td):
return (td.microseconds + (
td.seconds + td.days * 24 * 3600) * 10 ** 6) // 10 ** 6
def sfdatetime_to_snowflake(value):
dt = value.datetime
nanosecond = value.nanosecond
if isinstance(dt, time.struct_time):
if nanosecond:
return (
u'{year:d}-{month:02d}-{day:02d} '
u'{hour:02d}:{minute:02d}:{second:02d}.'
u'{nanosecond:d}').format(
year=dt.tm_year, month=dt.tm_mon, day=dt.tm_mday,
hour=dt.tm_hour, minute=dt.tm_min, second=dt.tm_sec,
nanosecond=nanosecond
)
return (
u'{year:d}-{month:02d}-{day:02d} '
u'{hour:02d}:{minute:02d}:{second:02d}').format(
year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute, second=dt.second
)
else:
tzinfo = dt.tzinfo
if tzinfo:
if pytz.utc != tzinfo:
td = tzinfo.utcoffset(dt, is_dst=False)
else:
td = ZERO_TIMEDELTA
sign = u'+' if td >= ZERO_TIMEDELTA else u'-'
td_secs = sfdatetime_total_seconds_from_timedelta(td)
h, m = divmod(abs(td_secs // 60), 60)
if nanosecond:
return (u'{year:d}-{month:02d}-{day:02d} '
u'{hour:02d}:{minute:02d}:{second:02d}.'
u'{nanosecond:d}{sign}{tzh:02d}:{tzm:02d}').format(
year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute, second=dt.second,
nanosecond=nanosecond, sign=sign, tzh=h, tzm=m
)
return (
u'{year:d}-{month:02d}-{day:02d} '
u'{hour:02d}:{minute:02d}:{second:02d}'
u'{sign}{tzh:02d}:{tzm:02d}').format(
year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute, second=dt.second, sign=sign,
tzh=h,
tzm=m
)
else:
if nanosecond:
return (
u'{year:d}-{month:02d}-{day:02d} '
u'{hour:02d}:{minute:02d}:{second:02d}.'
u'{nanosecond:d}').format(
year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute, second=dt.second,
nanosecond=nanosecond
)
return (
u'{year:d}-{month:02d}-{day:02d} '
u'{hour:02d}:{minute:02d}:{second:02d}').format(
year=dt.year, month=dt.month, day=dt.day,
hour=dt.hour, minute=dt.minute, second=dt.second
)
class SnowflakeDateTime(UnicodeMixin):
"""
Snowflake DateTime class.
The differene to the native datetime class is Snowflake supports up to
nanoseconds precision.
"""
def __init__(self, ts, nanosecond, scale):
self._datetime = ts
self._nanosecond = nanosecond
self._scale = scale
@property
def datetime(self):
return self._datetime
@property
def nanosecond(self):
return self._nanosecond
def __repr__(self):
return self.__str__()
def __unicode__(self):
return sfdatetime_to_snowflake(self)
def __bytes__(self):
return self.__unicode__().encode(UTF8)
class SnowflakeDateTimeFormat(object):
"""
Snowflake DateTime Formatter
"""
def __init__(self, sql_format, datetime_class=datetime):
self._sql_format = sql_format
self._fragments = []
self._compile()
if len(self._fragments) != 1:
raise errors.InternalError(
u'Only one fragment is allowed {0}'.format(
u','.join(self._fragments)))
self._simple_datetime_pattern = self._fragments[0][u'python_format']
self._nano_str = u'{:09d}'
if self._fractions_pos >= 0 and self._fractions_with_dot:
self._nano_str = u'.{:09d}'
self.format = getattr(self, u'_format_{type_name}'.format(
type_name=datetime_class.__name__))
def python_format(self):
return self._python_format
def _pre_format(self, value):
updated_format = self._simple_datetime_pattern
if self._fractions_pos >= 0:
# if FF is included
if hasattr(value, 'microsecond'):
fraction = value.microsecond
self._nano_str = u'{:06d}'
if self._fractions_with_dot:
self._nano_str = u'.{:06d}'
elif hasattr(value, 'nanosecond'):
fraction = value.nanosecond
else:
self._nano_str = u'{:01d}'
if self._fractions_with_dot:
self._nano_str = u'.{:01d}'
fraction = 0 # struct_time. no fraction of second
if self._fractions_len > 0:
# truncate up to the specified length of FF
nano_value = self._nano_str.format(fraction)[
:self._fractions_len + 1]
else:
# no length of FF is specified
nano_value = self._nano_str.format(fraction)
if hasattr(value, '_scale'):
nano_value = nano_value[:value._scale + 1]
updated_format = \
updated_format[:self._fractions_pos] + nano_value + \
updated_format[self._fractions_pos:]
return updated_format
def _format_SnowflakeDateTime(self, value):
"""
Formats SnowflakeDateTime object
"""
updated_format = self._pre_format(value)
if isinstance(value.datetime, time.struct_time):
return TO_UNICODE(time.strftime(
updated_format, value.datetime))
if value.datetime.year < 1000:
# NOTE: still not supported
return value.datetime.isoformat()
return value.datetime.strftime(updated_format)
def _format_datetime(self, value):
"""
Formats datetime object
"""
updated_format = self._pre_format(value)
if isinstance(value, time.struct_time):
return TO_UNICODE(time.strftime(updated_format, value))
if value.year < 1000:
# NOTE: still not supported.
return value.isoformat()
return value.strftime(updated_format)
def _create_new_fragment(self, element_types):
self._fragments.append({
u'python_format': self._python_format,
u'element_types': element_types,
})
def _add_raw_char(self, sql_format, ch):
sql_format += u'%%' if ch == u'%' else ch
return sql_format
def _add_element(self, element, element_types):
self._python_format += element[1] # python format
element_types.append(element)
return len(element[0]) # sql format
def _compile(self):
u"""Converts the date time/timestamp format to Python"""
self._python_format = u""
self._fractions_with_dot = False
self._fractions_pre_formatter = None
self._fractions_pos = -1
self._fractions_len = -1
element_types = []
idx = 0
u_sql_format = self._sql_format.upper()
while idx < len(u_sql_format):
ch = u_sql_format[idx]
if ch == u'A':
if u_sql_format[idx:].startswith(
ElementType[u'Ante_Meridiem_ElementType'][0]):
idx += self._add_element(
ElementType[u'Ante_Meridiem_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'D':
if u_sql_format[idx:].startswith(
ElementType[u'DayOfMonth_ElementType'][0]):
idx += self._add_element(
ElementType[u'DayOfMonth_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'DayOfWeekAbbrev_ElementType'][0]):
idx += self._add_element(
ElementType[u'DayOfWeekAbbrev_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'H':
if u_sql_format[idx:].startswith(
ElementType[u'Hour24_ElementType'][0]):
idx += self._add_element(
ElementType[u'Hour24_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'Hour12_ElementType'][0]):
idx += self._add_element(
ElementType[u'Hour12_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'Hour_ElementType'][0]):
idx += self._add_element(ElementType[u'Hour_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'M':
if u_sql_format[idx:].startswith(
ElementType[u'MonthAbbrev_ElementType'][0]):
idx += self._add_element(
ElementType[
u'MonthAbbrev_ElementType'], element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'Month_ElementType'][0]):
idx += self._add_element(
ElementType[u'Month_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'Minute_ElementType'][0]):
idx += self._add_element(
ElementType[u'Minute_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'P':
if u_sql_format[idx:].startswith(
ElementType[u'Post_Meridiem_ElementType'][0]):
idx += self._add_element(
ElementType[u'Post_Meridiem_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'S':
if u_sql_format[idx:].startswith(
ElementType[u'Second_ElementType'][0]):
idx += self._add_element(
ElementType[u'Second_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'T':
if u_sql_format[idx:].startswith(
ElementType[u'TZOffsetHourColonMin_ElementType'][0]):
idx += self._add_element(
ElementType[u'TZOffsetHourColonMin_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'TZOffsetHourMin_ElementType'][0]):
idx += self._add_element(
ElementType[u'TZOffsetHourMin_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'TZOffsetHourOnly_ElementType'][0]):
idx += self._add_element(
ElementType[u'TZOffsetHourOnly_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'TZAbbr_ElementType'][0]):
idx += self._add_element(
ElementType[u'TZAbbr_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'Y':
if u_sql_format[idx:].startswith(
ElementType[u'Year_ElementType'][0]):
idx += self._add_element(ElementType[u'Year_ElementType'],
element_types)
elif u_sql_format[idx:].startswith(
ElementType[u'Year2digit_ElementType'][0]):
idx += self._add_element(
ElementType[u'Year2digit_ElementType'],
element_types)
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'.':
if idx + 1 < len(u_sql_format) and \
u_sql_format[idx + 1:].startswith(
ElementType[u'MilliSecond_ElementType'][0]):
# Will be FF, just mark that there's a dot before FF
self._fractions_with_dot = True
idx += 1
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'F':
if u_sql_format[idx:].startswith(
ElementType[u'MilliSecond_ElementType'][0]):
idx += len(ElementType[u'MilliSecond_ElementType'][0])
# @TODO Handle multiple occurrences?
# Construct formatter to find fractions position.
self._fractions_pre_formatter = self._python_format
self._fractions_pos = len(self._python_format)
self._fractions_len = -1
if idx < len(u_sql_format) and u_sql_format[idx].isdigit():
self._fractions_len = int(u_sql_format[idx])
idx += 1
else:
self._python_format = self._add_raw_char(
self._python_format, ch)
idx += 1
elif ch == u'"':
# copy a double quoted string to the python format
idx += 1
while idx < len(self._sql_format) and \
self._sql_format[idx] != u'"':
self._python_format += self._sql_format[idx]
idx += 1
if idx < len(self._sql_format):
idx += 1
else:
self._python_format = self._add_raw_char(self._python_format,
ch)
idx += 1
if len(element_types) > 0 or len(
self._python_format) > 0 or self._fractions_len > 0:
self._create_new_fragment(element_types)