@@ -109,12 +109,56 @@ def _extract_timestamp(value, ctx):
109109 scale = ctx ['scale' ]
110110 microseconds = float (
111111 value [0 :- scale + 6 ]) if scale > 6 else float (value )
112- fraction_of_nanoseconds = SnowflakeConverter . _adjust_fraction_of_nanoseconds (
112+ fraction_of_nanoseconds = _adjust_fraction_of_nanoseconds (
113113 value , ctx ['max_fraction' ], scale )
114114
115115 return microseconds , fraction_of_nanoseconds
116116
117117
118+ def _adjust_fraction_of_nanoseconds (value , max_fraction , scale ):
119+ if scale == 0 :
120+ return 0
121+ if value [0 ] != '-' :
122+ return int ((value [- scale :] + ZERO_FILL [:9 - scale ]))
123+
124+ frac = int (value [- scale :])
125+ if frac == 0 :
126+ return 0
127+ else :
128+ return int (TO_UNICODE (max_fraction - frac ) + ZERO_FILL [:9 - scale ])
129+
130+
131+ def _generate_tzinfo_from_tzoffset (tzoffset_minutes ):
132+ """
133+ Generates tzinfo object from tzoffset.
134+ """
135+ try :
136+ return _TZINFO_CLASS_CACHE [tzoffset_minutes ]
137+ except KeyError :
138+ pass
139+ sign = u'P' if tzoffset_minutes >= 0 else u'N'
140+ abs_tzoffset_minutes = abs (tzoffset_minutes )
141+ hour , minute = divmod (abs_tzoffset_minutes , 60 )
142+ name = u'GMT{sign:s}{hour:02d}{minute:02d}' .format (
143+ sign = sign ,
144+ hour = hour ,
145+ minute = minute )
146+ tzinfo_class_type = type (
147+ str (name ), # str() for both Python 2 and 3
148+ (tzinfo ,),
149+ dict (
150+ utcoffset = lambda self0 , dt , is_dst = False : timedelta (
151+ minutes = tzoffset_minutes ),
152+ tzname = lambda self0 , dt : name ,
153+ dst = lambda self0 , dt : ZERO_TIMEDELTA ,
154+ __repr__ = lambda _ : name
155+ )
156+ )
157+ tzinfo_cls = tzinfo_class_type ()
158+ _TZINFO_CLASS_CACHE [tzoffset_minutes ] = tzinfo_cls
159+ return tzinfo_cls
160+
161+
118162class SnowflakeConverter (object ):
119163 def __init__ (self , ** kwargs ):
120164 self ._parameters = {}
@@ -136,47 +180,6 @@ def get_parameters(self):
136180 def get_parameter (self , param ):
137181 return self ._parameters [param ] if param in self ._parameters else None
138182
139- @staticmethod
140- def _adjust_fraction_of_nanoseconds (value , max_fraction , scale ):
141- if scale == 0 :
142- return 0
143- if value [0 ] != '-' :
144- return int ((value [- scale :] + ZERO_FILL )[:9 ])
145-
146- return int ((TO_UNICODE (max_fraction - int (value [- scale :])) +
147- ZERO_FILL )[:9 ])
148-
149- @staticmethod
150- def _generate_tzinfo_from_tzoffset (tzoffset_minutes ):
151- """
152- Generates tzinfo object from tzoffset.
153- """
154- try :
155- return _TZINFO_CLASS_CACHE [tzoffset_minutes ]
156- except KeyError :
157- pass
158- sign = u'P' if tzoffset_minutes >= 0 else u'N'
159- abs_tzoffset_minutes = abs (tzoffset_minutes )
160- hour , minute = divmod (abs_tzoffset_minutes , 60 )
161- name = u'GMT{sign:s}{hour:02d}{minute:02d}' .format (
162- sign = sign ,
163- hour = hour ,
164- minute = minute )
165- tzinfo_class_type = type (
166- str (name ), # str() for both Python 2 and 3
167- (tzinfo ,),
168- dict (
169- utcoffset = lambda self0 , dt , is_dst = False : timedelta (
170- minutes = tzoffset_minutes ),
171- tzname = lambda self0 , dt : name ,
172- dst = lambda self0 , dt : ZERO_TIMEDELTA ,
173- __repr__ = lambda _ : name
174- )
175- )
176- tzinfo_cls = tzinfo_class_type ()
177- _TZINFO_CLASS_CACHE [tzoffset_minutes ] = tzinfo_cls
178- return tzinfo_cls
179-
180183 #
181184 # FROM Snowflake to Python Objects
182185 #
@@ -263,15 +266,13 @@ def _TIMESTAMP_TZ_to_python(self, ctx):
263266
264267 def conv0 (encoded_value ):
265268 value , tz = encoded_value .split ()
266- tzinfo = SnowflakeConverter ._generate_tzinfo_from_tzoffset (
267- int (tz ) - 1440 )
269+ tzinfo = _generate_tzinfo_from_tzoffset (int (tz ) - 1440 )
268270 return datetime .fromtimestamp (float (value ), tz = tzinfo )
269271
270272 def conv (encoded_value ):
271273 value , tz = encoded_value .split ()
272274 microseconds = float (value [0 :- scale + 6 ])
273- tzinfo = SnowflakeConverter ._generate_tzinfo_from_tzoffset (
274- int (tz ) - 1440 )
275+ tzinfo = _generate_tzinfo_from_tzoffset (int (tz ) - 1440 )
275276 return datetime .fromtimestamp (microseconds , tz = tzinfo )
276277
277278 return conv if scale > 6 else conv0
@@ -592,8 +593,7 @@ def _time_to_snowflake(self, value):
592593 return value .strftime (u'%H:%M:%S' )
593594
594595 def _struct_time_to_snowflake (self , value ):
595- tzinfo_value = SnowflakeConverter ._generate_tzinfo_from_tzoffset (
596- - time .timezone // 60 )
596+ tzinfo_value = _generate_tzinfo_from_tzoffset (time .timezone // 60 )
597597 t = datetime .fromtimestamp (time .mktime (value ))
598598 if pytz .utc != tzinfo_value :
599599 t += tzinfo_value .utcoffset (t )
0 commit comments