Skip to content

Commit 99d9d9c

Browse files
author
Davies Liu
committed
use int for timestamp
1 parent 10aa7ca commit 99d9d9c

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

python/pyspark/sql/tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,10 @@ def test_time_with_timezone(self):
612612
utcnow = datetime.datetime.fromtimestamp(ts, utc)
613613
df = self.sqlCtx.createDataFrame([(now, utcnow)])
614614
now1, utcnow1 = df.first()
615-
# Spark SQL does not support microsecond, the error should be
615+
# Pyrolite does not support microsecond, the error should be
616616
# less than 1 millisecond
617-
self.assertTrue(now1 - now < datetime.timedelta(0.001))
618-
self.assertTrue(utcnow1 - now < datetime.timedelta(0.001))
617+
self.assertTrue(now - now1 < datetime.timedelta(0.001))
618+
self.assertTrue(now - utcnow1 < datetime.timedelta(0.001))
619619

620620
def test_dropna(self):
621621
schema = StructType([

python/pyspark/sql/types.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,6 @@ def _need_python_to_sql_conversion(dataType):
653653
elif isinstance(dataType, MapType):
654654
return _need_python_to_sql_conversion(dataType.keyType) or \
655655
_need_python_to_sql_conversion(dataType.valueType)
656-
elif isinstance(dataType, TimestampType):
657-
return True
658656
elif isinstance(dataType, UserDefinedType):
659657
return True
660658
elif isinstance(dataType, TimestampType):
@@ -711,25 +709,17 @@ def converter(obj):
711709
value_converter = _python_to_sql_converter(dataType.valueType)
712710
return lambda m: dict([(key_converter(k), value_converter(v)) for k, v in m.items()])
713711

714-
elif isinstance(dataType, TimestampType):
715-
716-
def to_posix_timstamp(dt):
717-
if dt.tzinfo is None:
718-
return time.mktime(dt.timetuple()) + dt.microsecond / 1e6
719-
else:
720-
return calendar.timegm(dt.utctimetuple()) + dt.microsecond / 1e6
721-
return to_posix_timstamp
722-
723712
elif isinstance(dataType, UserDefinedType):
724713
return lambda obj: dataType.serialize(obj)
714+
725715
elif isinstance(dataType, TimestampType):
726716

727717
def to_posix_timstamp(dt):
728-
if dt.tzinfo is None:
729-
return int(time.mktime(dt.timetuple()) * 1e7 + dt.microsecond * 10)
730-
else:
731-
return int(calendar.timegm(dt.utctimetuple()) * 1e7 + dt.microsecond * 10)
718+
seconds = (calendar.timegm(dt.utctimetuple()) if dt.tzinfo
719+
else time.mktime(dt.timetuple()))
720+
return int(seconds * 1e7 + dt.microsecond * 10)
732721
return to_posix_timstamp
722+
733723
else:
734724
raise ValueError("Unexpected type %r" % dataType)
735725

0 commit comments

Comments
 (0)