9
9
from logging import getLogger
10
10
11
11
from .compat import TO_UNICODE
12
+ from .constants import is_timestamp_type_name
12
13
from .converter import (SnowflakeConverter , ZERO_EPOCH )
13
14
from .sfbinaryformat import (binary_to_python , SnowflakeBinaryFormat )
14
15
from .sfdatetime import (SnowflakeDateTimeFormat , SnowflakeDateTime )
17
18
18
19
19
20
def _format_sftimestamp (fmt , value , franction_of_nanoseconds ):
20
- if fmt :
21
- return SnowflakeDateTimeFormat (fmt ).format (
22
- SnowflakeDateTime (
23
- value , nanosecond = franction_of_nanoseconds ))
24
- return TO_UNICODE (SnowflakeDateTime (
25
- value , nanosecond = franction_of_nanoseconds ))
21
+ sf_datetime = SnowflakeDateTime (value , nanosecond = franction_of_nanoseconds )
22
+ return fmt .format (sf_datetime ) if fmt else TO_UNICODE (sf_datetime )
26
23
27
24
28
25
class SnowflakeConverterSnowSQL (SnowflakeConverter ):
@@ -60,8 +57,13 @@ def _get_format(self, type_name):
60
57
#
61
58
def to_python_method (self , type_name , row_type ):
62
59
try :
60
+ fmt = None
61
+ if is_timestamp_type_name (type_name ):
62
+ fmt = SnowflakeDateTimeFormat (self ._get_format (type_name ))
63
+ elif type_name == u'BINARY' :
64
+ fmt = SnowflakeBinaryFormat (self ._get_format (type_name ))
63
65
return getattr (self , u'_{type_name}_to_python' .format (
64
- type_name = type_name )), self . _get_format ( type_name )
66
+ type_name = type_name )), fmt
65
67
except KeyError :
66
68
# no type is defined, pass through it
67
69
return self ._TEXT_to_python , None
@@ -88,8 +90,7 @@ def _BINARY_to_python(self, value, _, fmt):
88
90
"""
89
91
BINARY to a string formatted by BINARY_OUTPUT_FORMAT
90
92
"""
91
- bytes_value = binary_to_python (value )
92
- return SnowflakeBinaryFormat (fmt ).format (bytes_value )
93
+ return fmt .format (binary_to_python (value ))
93
94
94
95
def _DATE_to_python (self , value , _ , fmt ):
95
96
"""
@@ -100,7 +101,7 @@ def _DATE_to_python(self, value, _, fmt):
100
101
try :
101
102
t = ZERO_EPOCH + timedelta (seconds = int (value ) * (24 * 60 * 60 ))
102
103
if fmt :
103
- return SnowflakeDateTimeFormat ( fmt ) .format (t )
104
+ return fmt .format (t )
104
105
return TO_UNICODE (date (t .year , t .month , t .day ))
105
106
except OverflowError :
106
107
self .logger .debug (
@@ -109,7 +110,7 @@ def _DATE_to_python(self, value, _, fmt):
109
110
value )
110
111
t = time .gmtime (value )
111
112
if fmt :
112
- return SnowflakeDateTimeFormat ( fmt ) .format (
113
+ return fmt .format (
113
114
SnowflakeDateTime (t , nanosecond = 0 )
114
115
)
115
116
return u'{year:d}-{month:02d}-{day:02d}' .format (
0 commit comments