Skip to content

TINKERPOP-2264 Fixed g:Date serialization for python.#1165

Merged
spmallette merged 2 commits intotp33from
TINKERPOP-2264
Jul 30, 2019
Merged

TINKERPOP-2264 Fixed g:Date serialization for python.#1165
spmallette merged 2 commits intotp33from
TINKERPOP-2264

Conversation

@spmallette
Copy link
Copy Markdown
Contributor

https://issues.apache.org/jira/browse/TINKERPOP-2264

Got timezone of the local system out of the derser mix and standardized around what's always been expected in Java's Date.getTime().

Builds with mvn clean install :pl gremlin-python

VOTE +1

Got timezone of the local system out of the derser mix and standardized around what's always been expected in Java's Date.getTime().
-1, -1, -1)) + obj.microsecond / 1e6
# Hack for legacy Python - timestamp() in Python 3.3
pts = (time.mktime(obj.timetuple()) + obj.microsecond / 1e6) - \
(time.mktime(cls.epoch.timetuple()))
Copy link
Copy Markdown

@TheRealFalcon TheRealFalcon Jul 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm understanding this right, it's taking the epoch offset and removing it from our current time.
So for example, obj.timetuple() will return current UTC-5 for me, and cls.epoch.timetuple() should return epoch-5, so we if we subtract them, we get the UTC time as unix time.

But according to the end of https://stackoverflow.com/a/5499906/37205, the epoch offset can be different for DST timezones. I don't really understand how that can be, but when I'm not in daylight time, I'm UTC-6. I think this answer is saying that if my timezone was affected, it would return epoch as epoch-6 when current time would be UTC-5.

# Java timestamp expects milliseconds.
if six.PY3:
pts = obj.timestamp()
pts = (obj - cls.epoch) / datetime.timedelta(seconds=1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me this is a bit of a confusing way to get to a timestamp. I might suggest having a look at datetime handling in the Cassandra driver: https://github.com/datastax/python-driver/blob/master/cassandra/cqltypes.py#L577-L579
This technique has a few advantages:

  • No special cases per python runtime
  • Handles datetime inputs with timezones attached, normalizing back to UTC
  • More readable (imo)

-1, -1, -1)) + obj.microsecond / 1e6
# Hack for legacy Python - timestamp() in Python 3.3
pts = (time.mktime(obj.timetuple()) + obj.microsecond / 1e6) - \
(time.mktime(cls.epoch.timetuple()))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was not introduced in this change, but a "hack" should not be needed if an alternative conversion technique is used (see above).

# Java timestamp expects milliseconds.
if six.PY3:
pts = obj.timestamp()
pts = (obj - cls.epoch) / datetime.timedelta(seconds=1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comments as V2d0 file.

This approach shouldn't hose up with daylight savings time.
@spmallette
Copy link
Copy Markdown
Contributor Author

thanks @aholmberg and @TheRealFalcon - i got rid of hacks (and the "old" way in general) in exchange for calendar . I didn't use the full DS driver code in terms of error handling because this code should only get triggered for actual datetime sorts of objects. if the timestamp was a numeric i think it believe it would have ended up being handled by that serializer.

Copy link
Copy Markdown
Contributor

@aholmberg aholmberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree that you only needed the datetime leg of that code.

@robertdale
Copy link
Copy Markdown
Member

VOTE +1

@dkuppitz
Copy link
Copy Markdown
Contributor

Not an expert in Python, but the code appears to be good.

VOTE +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants