diff options
author | Michael Twomey <michael.twomey@fieldaware.com> | 2013-10-17 18:11:14 +0100 |
---|---|---|
committer | Michael Twomey <michael.twomey@fieldaware.com> | 2013-10-17 18:11:14 +0100 |
commit | 9c3f2b8518d5641973cd5ad45e26c0a980506bf9 (patch) | |
tree | 66965fab0c0ff8f1d18fe3fedf2d293f1afa3c6c | |
parent | 0f9f3ea4a92619cccd35481c0fdb112f29a1b570 (diff) | |
download | pyiso8601-9c3f2b8518d5641973cd5ad45e26c0a980506bf9.tar.gz |
Fix microsecond rounding issues
Thanks to nielsenb@jetfuse.net
Fixes #2
-rw-r--r-- | README.rst | 1 | ||||
-rw-r--r-- | iso8601/iso8601.py | 6 | ||||
-rw-r--r-- | iso8601/test_iso8601.py | 1 |
3 files changed, 6 insertions, 2 deletions
@@ -71,6 +71,7 @@ Changes * Make seconds optional in date format ("1997-07-16T19:20+01:00" now valid). https://bitbucket.org/micktwomey/pyiso8601/pull-request/1/make-the-inclusion-of-seconds-optional-in/diff (thanks to Chris Down) * Correctly raise ParseError for more invalid inputs (https://bitbucket.org/micktwomey/pyiso8601/issue/1/raise-parseerror-for-invalid-input) (thanks to manish.tomar) * Support more variations of ISO 8601 dates, times and time zone specs. +* Fix microsecond rounding issues (https://bitbucket.org/micktwomey/pyiso8601/issue/2/roundoff-issues-when-parsing-decimal) (thanks to nielsenb@jetfuse.net) 0.1.4 ----- diff --git a/iso8601/iso8601.py b/iso8601/iso8601.py index a121e2e..b979f02 100644 --- a/iso8601/iso8601.py +++ b/iso8601/iso8601.py @@ -13,6 +13,7 @@ from datetime import ( timedelta, tzinfo ) +from decimal import Decimal import logging import sys import re @@ -28,7 +29,8 @@ else: # Adapted from http://delete.me.uk/2005/03/iso8601.html -ISO8601_REGEX = re.compile(r""" +ISO8601_REGEX = re.compile( + r""" (?P<year>[0-9]{4}) (-{0,1}(?P<month>[0-9]{1,2})){1} (-{0,1}(?P<day>[0-9]{1,2})){1} @@ -159,7 +161,7 @@ def parse_date(datestring, default_timezone=UTC): tz = parse_timezone(groups, default_timezone=default_timezone) - groups["second_fraction"] = int(float("0.%s" % to_int(groups, "second_fraction", default_to_zero=True)) * 1e6) + groups["second_fraction"] = int(Decimal("0.%s" % to_int(groups, "second_fraction", default_to_zero=True)) * Decimal("1000000.0")) try: return datetime( diff --git a/iso8601/test_iso8601.py b/iso8601/test_iso8601.py index 5b5e760..4a0b05c 100644 --- a/iso8601/test_iso8601.py +++ b/iso8601/test_iso8601.py @@ -46,6 +46,7 @@ def test_parse_invalid_date(invalid_date): ("2013-10-15T1831Z", datetime.datetime(2013, 10, 15, 18, 31, 0, 0, iso8601.UTC)), # hhmm ("2013-10-15T18Z", datetime.datetime(2013, 10, 15, 18, 0, 0, 0, iso8601.UTC)), # hh ("20131015T18:30Z", datetime.datetime(2013, 10, 15, 18, 30, 0, 0, iso8601.UTC)), # YYYYMMDD + ("2012-12-19T23:21:28.512400+00:00", datetime.datetime(2012, 12, 19, 23, 21, 28, 512400, iso8601.FixedOffset(0, 0, "+00:00"))), # https://code.google.com/p/pyiso8601/issues/detail?id=21 ]) def test_parse_valid_date(valid_date, expected_datetime): parsed = iso8601.parse_date(valid_date) |