diff options
author | Gerhard Weis <gerhard.weis@gmail.com> | 2012-10-30 10:38:22 +1000 |
---|---|---|
committer | Gerhard Weis <gerhard.weis@gmail.com> | 2012-10-30 10:38:22 +1000 |
commit | f16071ab3f7d16155e4b1a4c58ddb9016bd71dbe (patch) | |
tree | bce8406f74569f309287cd8f6a4057e35c17e75f | |
parent | df6341af6ceeef11c0a13bc60f7b1544cd0f3c03 (diff) | |
download | isodate-f16071ab3f7d16155e4b1a4c58ddb9016bd71dbe.tar.gz |
- round to microseconds in case provided iso date string uses higher precision
-rw-r--r-- | CHANGES.txt | 1 | ||||
-rw-r--r-- | src/isodate/isotime.py | 3 | ||||
-rw-r--r-- | src/isodate/tests/test_datetime.py | 46 |
3 files changed, 34 insertions, 16 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index af5249c..f339a94 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -5,6 +5,7 @@ CHANGES 0.4.9 (unreleased) ------------------ +- make sure parsed fractional seconds are in microseconds - add leading zeros when formattig microseconds (Jarom Loveridge) 0.4.8 (2012-05-04) diff --git a/src/isodate/isotime.py b/src/isodate/isotime.py index f0003d6..091ae18 100644 --- a/src/isodate/isotime.py +++ b/src/isodate/isotime.py @@ -123,7 +123,8 @@ def parse_time(timestring): int(groups['tzhour'] or 0), int(groups['tzmin'] or 0)) if 'second' in groups: - second = Decimal(groups['second']) + # round to microseconds if fractional seconds are more precise + second = Decimal(groups['second']).quantize(Decimal('.000001')) microsecond = (second - int(second)) * long(1e6) # int(...) ... no rounding # to_integral() ... rounding diff --git a/src/isodate/tests/test_datetime.py b/src/isodate/tests/test_datetime.py index 339d40a..bbfafd5 100644 --- a/src/isodate/tests/test_datetime.py +++ b/src/isodate/tests/test_datetime.py @@ -41,35 +41,51 @@ from isodate import DATE_BAS_WEEK_COMPLETE, DATE_EXT_WEEK_COMPLETE # result from the parse_datetime method. A result of None means an ISO8601Error # is expected. TEST_CASES = [('19850412T1015', datetime(1985, 4, 12, 10, 15), - DATE_BAS_COMPLETE + 'T' + TIME_BAS_MINUTE), + DATE_BAS_COMPLETE + 'T' + TIME_BAS_MINUTE, + '19850412T1015'), ('1985-04-12T10:15', datetime(1985, 4, 12, 10, 15), - DATE_EXT_COMPLETE + 'T' + TIME_EXT_MINUTE), + DATE_EXT_COMPLETE + 'T' + TIME_EXT_MINUTE, + '1985-04-12T10:15'), ('1985102T1015Z', datetime(1985, 4, 12, 10, 15, tzinfo=UTC), - DATE_BAS_ORD_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS), + DATE_BAS_ORD_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS, + '1985102T1015Z'), ('1985-102T10:15Z', datetime(1985, 4, 12, 10, 15, tzinfo=UTC), - DATE_EXT_ORD_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_EXT), + DATE_EXT_ORD_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_EXT, + '1985-102T10:15Z'), ('1985W155T1015+0400', datetime(1985, 4, 12, 10, 15, tzinfo=FixedOffset(4, 0, '+0400')), - DATE_BAS_WEEK_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS), + DATE_BAS_WEEK_COMPLETE + 'T' + TIME_BAS_MINUTE + TZ_BAS, + '1985W155T1015+0400'), ('1985-W15-5T10:15+04', datetime(1985, 4, 12, 10, 15, tzinfo=FixedOffset(4, 0, - '+0400')), - DATE_EXT_WEEK_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_HOUR), + '+0400'),), + DATE_EXT_WEEK_COMPLETE + 'T' + TIME_EXT_MINUTE + TZ_HOUR, + '1985-W15-5T10:15+04'), ('20110410T101225.123000Z', datetime(2011, 4, 10, 10, 12, 25, 123000, tzinfo=UTC), - DATE_BAS_COMPLETE + 'T' + TIME_BAS_COMPLETE + ".%f" + TZ_BAS), + DATE_BAS_COMPLETE + 'T' + TIME_BAS_COMPLETE + ".%f" + TZ_BAS, + '20110410T101225.123000Z'), ('2012-10-12T08:29:46.069178Z', datetime(2012, 10, 12, 8, 29, 46, 69178, tzinfo=UTC), - DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + ".%f" + TZ_BAS), + DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS, + '2012-10-12T08:29:46.069178Z'), ('2012-10-12T08:29:46.691780Z', datetime(2012, 10, 12, 8, 29, 46, 691780, tzinfo=UTC), - DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + ".%f" + TZ_BAS), + DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS, + '2012-10-12T08:29:46.691780Z'), + ('2012-10-30T08:55:22.1234567Z', + datetime(2012, 10, 30, 8, 55, 22, 123457, tzinfo=UTC), + DATE_EXT_COMPLETE + 'T' + TIME_EXT_COMPLETE + '.%f' + TZ_BAS, + '2012-10-30T08:55:22.123457Z'), + ('2012-10-30T08:55:22.1234561Z', + 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.123456Z') ] - -def create_testcase(datetimestring, expectation, format): +def create_testcase(datetimestring, expectation, format, output): """ Create a TestCase class for a specific test. @@ -100,7 +116,7 @@ def create_testcase(datetimestring, expectation, format): datetime_isoformat, expectation, format) else: self.assertEqual(datetime_isoformat(expectation, format), - datetimestring) + output) return unittest.TestLoader().loadTestsFromTestCase(TestDateTime) @@ -110,8 +126,8 @@ def test_suite(): Construct a TestSuite instance for all test cases. ''' suite = unittest.TestSuite() - for datetimestring, expectation, format in TEST_CASES: - suite.addTest(create_testcase(datetimestring, expectation, format)) + for datetimestring, expectation, format, output in TEST_CASES: + suite.addTest(create_testcase(datetimestring, expectation, format, output)) return suite if __name__ == '__main__': |