Closes #582: Allow partial minutes for python 3.6 or newer#763
Closes #582: Allow partial minutes for python 3.6 or newer#763pganssle merged 2 commits intodateutil:masterfrom
Conversation
630a6fe to
091c496
Compare
8d88191 to
d2600d8
Compare
| return (dt.replace(tzinfo=None) - EPOCH).total_seconds() | ||
|
|
||
|
|
||
| def supports_seconds(): |
There was a problem hiding this comment.
I would not like to change the public API at all with this, there should be no way to query this.
Also, this has a runtime cost when I would prefer an import-time cost.
There was a problem hiding this comment.
fixed, made private and calculate correct function at import
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), | ||
| "1900-01-01 00:00:00+01:40") | ||
| # For python version pre-3.6, this will be rounded | ||
| set_as_old_python() |
There was a problem hiding this comment.
There's no need to mock out the old behavior - we don't need to verify that this behavior is accurate on all versions of Python. It's best to split this into two tests, one for pre-3.6 and one for 3.6+, and then use pytest.skipif to run them selectively based on version.
b81d332 to
6413db4
Compare
| def testPartialMinuteSupport(self): | ||
| # If user running python 3.6 or newer, exact offset is used | ||
| tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI))) | ||
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), |
There was a problem hiding this comment.
I think I would prefer a test that looks like this:
assert datetime(1900, 1, 1, 0, 0, tzinfo=tzc).utcoffset() == timedelta(hours=1, minutes=39, seconds=52)Though I am a bit torn because I will admit that the "compare the string" version sure is easy to read.
There was a problem hiding this comment.
The original was with string (see testRoundNonFullMinutes), but I can change them both to timedelta.
However, note that the two would test slightly different things (assert datetime(1900, 1, 1, 0, 0, tzinfo=tzc).utcoffset() == timedelta(hours=1, minutes=39, seconds=52) tests the date offset through utcoffset function, while the current version tests just the date formation from specified offset)
There was a problem hiding this comment.
I'll make the change, but in separate commit for easy undo in case we decide to go with original method
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), | ||
| "1900-01-01 00:00:00+01:39:52") | ||
|
|
||
| delta = timedelta(hours=12, seconds=30) |
There was a problem hiding this comment.
I would prefer this to be a separate test (or better yet, a parametrized test).
There was a problem hiding this comment.
sure, made this and second part of testRoundNonFullMinutes separate tests
dbc0a17 to
29605a0
Compare
cssherry
left a comment
There was a problem hiding this comment.
I noticed there's also offset in tzlocal class and tzfile.fromutc function. Not sure where/if that needs to get trimmed, but haven't touched them
| assert dt_r == dt_exp | ||
| assert dt_r.tzname() == dt_exp.tzname() | ||
| assert dt_r.utcoffset() == dt_exp.utcoffset() | ||
| if tz.supports_seconds() or tzi != tz.gettz('Africa/Monrovia'): |
There was a problem hiding this comment.
Alternatively, just remove Africa/Monrovia test from this set of tests
| sys.version_info[0] > 3 | ||
|
|
||
|
|
||
| def get_supported_offset(offset): |
There was a problem hiding this comment.
There's also offset in tzlocal class. Not sure where/if that needs to get trimmed
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), | ||
| "1900-01-01 00:00:00+01:40") | ||
| # For python version pre-3.6, this will be rounded | ||
| set_as_old_python() |
| return (dt.replace(tzinfo=None) - EPOCH).total_seconds() | ||
|
|
||
|
|
||
| def supports_seconds(): |
There was a problem hiding this comment.
fixed, made private and calculate correct function at import
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), | ||
| "1900-01-01 00:00:00+01:39:52") | ||
|
|
||
| delta = timedelta(hours=12, seconds=30) |
There was a problem hiding this comment.
sure, made this and second part of testRoundNonFullMinutes separate tests
| def testPartialMinuteSupport(self): | ||
| # If user running python 3.6 or newer, exact offset is used | ||
| tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI))) | ||
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), |
There was a problem hiding this comment.
The original was with string (see testRoundNonFullMinutes), but I can change them both to timedelta.
However, note that the two would test slightly different things (assert datetime(1900, 1, 1, 0, 0, tzinfo=tzc).utcoffset() == timedelta(hours=1, minutes=39, seconds=52) tests the date offset through utcoffset function, while the current version tests just the date formation from specified offset)
| def testPartialMinuteSupport(self): | ||
| # If user running python 3.6 or newer, exact offset is used | ||
| tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI))) | ||
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), |
There was a problem hiding this comment.
I'll make the change, but in separate commit for easy undo in case we decide to go with original method
| tzc = tz.tzfile(BytesIO(base64.b64decode(EUROPE_HELSINKI))) | ||
| self.assertEqual(str(datetime(1900, 1, 1, 0, 0, tzinfo=tzc)), | ||
| "1900-01-01 00:00:00+01:40") | ||
| assert datetime(1900, 1, 1, 0, 0, tzinfo=tzc).utcoffset(), timedelta(hours=1, minutes=40) |
There was a problem hiding this comment.
I think this needs to be:
assert datetime(1900, 1, 1, 0, 0, tzinfo=tzc).utcoffset() == timedelta(hours=1, minutes=40)0ed3eb6 to
d676718
Compare
d676718 to
44e4a6c
Compare
- Add "Africa/Monrovia" to test_resolve_imaginary test for python 3.6 or newer - Test that partial minutes is supported in python 3.6 or newer for tz.tzfile and datetime.utcoffset - Enable partial minutes in both tz.tzoffset and tz.tzfile - For now, don't warn user when rounding to nearest minute for python pre-3.6
44e4a6c to
41e5176
Compare
Summary of changes
As described in python bug tracker (https://bugs.python.org/issue5288), datetime now allows offsets that are not an integer number of minutes. Dateutil now accounts for this when running in newer versions of python
Closes #582
Pull Request Checklist