-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adjust usage of utcnow()
and utcfromtimestamp()
for Python 3
#440
Changes from all commits
5a94e36
70af9c0
0c29608
10a2c7b
302984c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,10 +65,10 @@ Retrieve the location from the database:: | |
>>> location.name | ||
'Earth' | ||
|
||
Date should have been set at the insert due to default value via python method:: | ||
Date should have been set at the insert due to default value via Python method:: | ||
|
||
>>> from datetime import datetime | ||
>>> now = datetime.utcnow() | ||
>>> from datetime import datetime, timezone | ||
>>> now = datetime.now(timezone.utc).replace(tzinfo=None) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand the change here. Isn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is really sad. In situations when the system time zone setting, defined through the With c1f183e, I've tried to improve the situation to outline the problem, on behalf of some test cases. 5894c2e quotes and references all important information about this topic, from both the Python documentation and articles by other capacities. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the case.
Yes, it is a naive datetime without timezone info attached, but it is using utc.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also as the articles point out, the problem is the use of the naive datetime (lack of tzinfo) in subsequent operations. If we remove the tzinfo anyways via And I'm not sure if attaching a timezone is even the correct thing to do, because the timestamp stored in CrateDB also doesn't have one attached. Assuming that timestamp values returned by the server are UTC may not be safe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. You convinced me that this goes too far. I've submitted #441 instead, which solely focuses on mitigating a fluke I |
||
>>> dt = location.date | ||
|
||
>>> dt.year == now.year | ||
|
@@ -80,9 +80,6 @@ Date should have been set at the insert due to default value via python method:: | |
>>> dt.day == now.day | ||
True | ||
|
||
>>> (now - location.datetime_tz).seconds < 4 | ||
True | ||
|
||
Verify the return type of date and datetime:: | ||
|
||
>>> type(location.date) | ||
|
@@ -103,10 +100,10 @@ aren't set when the row is inserted as there is no default method:: | |
>>> location.nullable_date is None | ||
True | ||
|
||
The datetime and date can be set using a update statement:: | ||
The datetime and date can be set using an update statement:: | ||
|
||
>>> location.nullable_date = datetime.today() | ||
>>> location.nullable_datetime = datetime.utcnow() | ||
>>> location.nullable_date = datetime.now(timezone.utc).replace(tzinfo=None).date() | ||
>>> location.nullable_datetime = datetime.now(timezone.utc).replace(tzinfo=None) | ||
>>> session.flush() | ||
|
||
Refresh "locations" table: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,6 @@ | |
import unittest | ||
import doctest | ||
from pprint import pprint | ||
from datetime import datetime, date | ||
from http.server import HTTPServer, BaseHTTPRequestHandler | ||
import ssl | ||
import time | ||
|
@@ -56,6 +55,7 @@ | |
) | ||
from .sqlalchemy.tests import test_suite as sqlalchemy_test_suite | ||
from .sqlalchemy.types import ObjectArray | ||
from ..testing.util import datetime_now_utc_naive, date_now_utc_naive | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this is relative? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PyCharm created that import line for me. The import statements above are also relative. |
||
|
||
log = logging.getLogger('crate.testing.layer') | ||
ch = logging.StreamHandler() | ||
|
@@ -213,9 +213,9 @@ class Location(Base): | |
__tablename__ = 'locations' | ||
name = sa.Column(sa.String, primary_key=True) | ||
kind = sa.Column(sa.String) | ||
date = sa.Column(sa.Date, default=date.today) | ||
datetime_tz = sa.Column(sa.DateTime, default=datetime.utcnow) | ||
datetime_notz = sa.Column(sa.DateTime, default=datetime.utcnow) | ||
date = sa.Column(sa.Date, default=date_now_utc_naive) | ||
datetime_tz = sa.Column(sa.DateTime, default=datetime_now_utc_naive) | ||
datetime_notz = sa.Column(sa.DateTime, default=datetime_now_utc_naive) | ||
nullable_datetime = sa.Column(sa.DateTime) | ||
nullable_date = sa.Column(sa.Date) | ||
flag = sa.Column(sa.Boolean) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we throw away the tzinfo why not keep using utcfromtimestamp?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the idea is to be a preparation step to actually allow the user to pass a tzinfo here, later on?