From 89f80896e074c072e6014b6196cd768d9e75d0b0 Mon Sep 17 00:00:00 2001 From: Gerhard Weis Date: Mon, 13 Dec 2021 10:05:41 +1000 Subject: - round down microseconds (in case precision is higher) --- CHANGES.txt | 1 + src/isodate/isotime.py | 11 +++++++---- src/isodate/tests/test_datetime.py | 4 ++-- src/isodate/tests/test_time.py | 7 ++++++- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 1bac7d5..3f4191e 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -7,6 +7,7 @@ CHANGES - drop end of life python versions - Don't match garbage characters at the end of parsed strings #16 (Gabriel de Perthuis) +- Breaking: fractional seconds are cut off to microseconds (round down) 0.6.1 (2021-12-13) diff --git a/src/isodate/isotime.py b/src/isodate/isotime.py index 3e2bb46..470c5b6 100644 --- a/src/isodate/isotime.py +++ b/src/isodate/isotime.py @@ -6,7 +6,7 @@ It supports all basic and extended formats including time zone specifications as described in the ISO standard. """ import re -from decimal import Decimal +from decimal import Decimal, ROUND_FLOOR from datetime import time from isodate.isostrf import strftime, TIME_EXT_COMPLETE, TZ_EXT @@ -102,8 +102,9 @@ def parse_time(timestring): int(groups["tzmin"] or 0), ) if "second" in groups: - # round to microseconds if fractional seconds are more precise - second = Decimal(groups["second"]).quantize(Decimal(".000001")) + second = Decimal(groups["second"]).quantize( + Decimal(".000001"), rounding=ROUND_FLOOR + ) microsecond = (second - int(second)) * int(1e6) # int(...) ... no rounding # to_integral() ... rounding @@ -116,7 +117,9 @@ def parse_time(timestring): ) if "minute" in groups: minute = Decimal(groups["minute"]) - second = (minute - int(minute)) * 60 + second = Decimal((minute - int(minute)) * 60).quantize( + Decimal(".000001"), rounding=ROUND_FLOOR + ) microsecond = (second - int(second)) * int(1e6) return time( int(groups["hour"]), diff --git a/src/isodate/tests/test_datetime.py b/src/isodate/tests/test_datetime.py index 465654e..19752d3 100644 --- a/src/isodate/tests/test_datetime.py +++ b/src/isodate/tests/test_datetime.py @@ -105,9 +105,9 @@ TEST_CASES = [ ), ( "2012-10-30T08:55:22.1234567Z", - datetime(2012, 10, 30, 8, 55, 22, 123457, tzinfo=UTC), + datetime(2012, 10, 30, 8, 55, 22, 123456, tzinfo=UTC), DATE_EXT_COMPLETE + "T" + TIME_EXT_COMPLETE + ".%f" + TZ_BAS, - "2012-10-30T08:55:22.123457Z", + "2012-10-30T08:55:22.123456Z", ), ( "2012-10-30T08:55:22.1234561Z", diff --git a/src/isodate/tests/test_time.py b/src/isodate/tests/test_time.py index d369714..c843bc9 100644 --- a/src/isodate/tests/test_time.py +++ b/src/isodate/tests/test_time.py @@ -24,7 +24,12 @@ TEST_CASES = [ # test precision ("15:33:42.123456", time(15, 33, 42, 123456), None), ("15:33:42.1234564", time(15, 33, 42, 123456), None), - ("15:33:42.1234557", time(15, 33, 42, 123456), None), + ("15:33:42.1234557", time(15, 33, 42, 123455), None), + ( + "10:59:59.9999999Z", + time(10, 59, 59, 999999, tzinfo=UTC), + None, + ), # TIME_EXT_COMPLETE + TZ_EXT), ("2320,8", time(23, 20, 48), None), ("23:20,8", time(23, 20, 48), None), ("23,3", time(23, 18), None), -- cgit v1.2.1