From 30bb1a380c5d75b9c9dbc4e1b381405517480b70 Mon Sep 17 00:00:00 2001 From: xi Date: Wed, 4 Oct 2006 07:42:50 +0000 Subject: [PATCH] Fix loss of microsecond precision in datetime.datetime constructor (fix #30). Thanks to edemaine@mit.edu for the bug report and the patch. git-svn-id: http://svn.pyyaml.org/pyyaml/trunk@234 18f92427-320e-0410-9341-c67f048884a3 --- lib/yaml/constructor.py | 4 ++-- tests/data/timestamp-bugs.code | 1 + tests/data/timestamp-bugs.data | 1 + tests/test_constructor.py | 4 ++++ tests/test_representer.py | 4 ++++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/yaml/constructor.py b/lib/yaml/constructor.py index d1d4f6a..c31c867 100644 --- a/lib/yaml/constructor.py +++ b/lib/yaml/constructor.py @@ -300,7 +300,7 @@ class SafeConstructor(BaseConstructor): (?P[0-9][0-9]?) :(?P[0-9][0-9]) :(?P[0-9][0-9]) - (?:(?P\.[0-9]*))? + (?:\.(?P[0-9]*))? (?:[ \t]*(?PZ|(?P[-+])(?P[0-9][0-9]?) (?::(?P[0-9][0-9]))?))?)?$''', re.X) @@ -318,7 +318,7 @@ class SafeConstructor(BaseConstructor): second = int(values['second']) fraction = 0 if values['fraction']: - fraction = int(float(values['fraction'])*1000000) + fraction = int(values['fraction'][:6].ljust(6, '0')) delta = None if values['tz_sign']: tz_hour = int(values['tz_hour']) diff --git a/tests/data/timestamp-bugs.code b/tests/data/timestamp-bugs.code index 47ac491..b1d6e9c 100644 --- a/tests/data/timestamp-bugs.code +++ b/tests/data/timestamp-bugs.code @@ -4,4 +4,5 @@ datetime.datetime(2001, 12, 14, 21, 59, 43, 1010), datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(60, "+1")), datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(-90, "-1:30")), + datetime.datetime(2005, 7, 8, 17, 35, 4, 517600), ] diff --git a/tests/data/timestamp-bugs.data b/tests/data/timestamp-bugs.data index bc3223f..721d290 100644 --- a/tests/data/timestamp-bugs.data +++ b/tests/data/timestamp-bugs.data @@ -3,3 +3,4 @@ - 2001-12-14 21:59:43.00101 - 2001-12-14 21:59:43+1 - 2001-12-14 21:59:43-1:30 +- 2005-07-08 17:35:04.517600 diff --git a/tests/test_constructor.py b/tests/test_constructor.py index 0c873e9..04f54ef 100644 --- a/tests/test_constructor.py +++ b/tests/test_constructor.py @@ -288,6 +288,10 @@ class TestConstructorTypes(test_appliance.TestAppliance): if (item1 != item1 or (item1 == 0.0 and item1 == 1.0)) and \ (item2 != item2 or (item2 == 0.0 and item2 == 1.0)): continue + if isinstance(item1, datetime.datetime) \ + and isinstance(item2, datetime.datetime): + self.failUnlessEqual(item1.microsecond, + item2.microsecond) if isinstance(item1, datetime.datetime): item1 = item1.utctimetuple() if isinstance(item2, datetime.datetime): diff --git a/tests/test_representer.py b/tests/test_representer.py index 961b477..6746146 100644 --- a/tests/test_representer.py +++ b/tests/test_representer.py @@ -36,6 +36,10 @@ class TestRepresenterTypes(test_appliance.TestAppliance): if (item1 != item1 or (item1 == 0.0 and item1 == 1.0)) and \ (item2 != item2 or (item2 == 0.0 and item2 == 1.0)): continue + if isinstance(item1, datetime.datetime) \ + and isinstance(item2, datetime.datetime): + self.failUnlessEqual(item1.microsecond, + item2.microsecond) if isinstance(item1, datetime.datetime): item1 = item1.utctimetuple() if isinstance(item2, datetime.datetime): -- 2.11.4.GIT