From 6088d873f818480f01b56676dd11dbbcccd9bb77 Mon Sep 17 00:00:00 2001 From: ricpol Date: Sat, 25 May 2019 18:53:04 +0200 Subject: [PATCH] fixes and tests for CDate.py --- unittests/test_lib_cdate.py | 23 ++++++++++++++--------- wx/lib/CDate.py | 23 ++++++++++++----------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/unittests/test_lib_cdate.py b/unittests/test_lib_cdate.py index ddced3d7..02acd124 100644 --- a/unittests/test_lib_cdate.py +++ b/unittests/test_lib_cdate.py @@ -2,7 +2,7 @@ import unittest from unittests import wtc import wx.lib.CDate as cdate import six - +import datetime class lib_cdate_Tests(wtc.WidgetTestCase): @@ -21,16 +21,21 @@ class lib_cdate_Tests(wtc.WidgetTestCase): self.assertFalse(l2, msg='Expected a non leap year') def test_lib_cdate_Julianday(self): - bd = cdate.Date(2014, 1, 10) - jd = cdate.julianDay(bd.year, bd.month, bd.day) - - self.assertTrue(jd == bd.julian, - msg='Expected them to be equal') + for m in range(3, 6): + for d in range(10, 20): + j = cdate.julianDay(2020, m, d) + jy, jm, jd = cdate.FromJulian(j) + self.assertEqual((2020, m, d), (jy, jm, jd), + msg='Julian/Gregorian round-trip failed for 2020-%i-%i' % (m, d)) def test_lib_cdate_Dayofweek(self): - jd = cdate.julianDay(2014, 1, 10) - dw = cdate.dayOfWeek(jd) - self.assertTrue(dw == 4, msg='Expected "4" assuming Monday is 1, got %s' % dw) + # this also validates cdate.julianDay, since Date.day_of_week depends on it + for m in range(3, 6): + for d in range(10, 20): + realwd = datetime.date(2020, m, d).weekday() + testwd = cdate.Date(2020, m, d).day_of_week + self.assertEqual(realwd, testwd, + msg="Expected weekday to be %i for date 2020-%i-%i, got %i" % (realwd, m, d, testwd)) #--------------------------------------------------------------------------- diff --git a/wx/lib/CDate.py b/wx/lib/CDate.py index 6a2ffa64..d6d2068b 100644 --- a/wx/lib/CDate.py +++ b/wx/lib/CDate.py @@ -15,6 +15,7 @@ # in a string format, then an error was raised. # """Date and calendar classes and date utitility methods.""" +from __future__ import division import time # I18N @@ -43,7 +44,7 @@ def leapdays(y1, y2): Return number of leap years in range [y1, y2] Assume y1 <= y2 and no funny (non-leap century) years """ - return (y2 + 3) / 4 - (y1 + 3) / 4 + return (y2 + 3) // 4 - (y1 + 3) // 4 def isleap(year): @@ -75,11 +76,11 @@ def julianDay(year, month, day): """ b = 0 if month > 12: - year = year + month / 12 + year = year + month // 12 month = month % 12 elif month < 1: month = -month - year = year - month / 12 - 1 + year = year - month // 12 - 1 month = 12 - month % 12 if year > 0: yearCorr = 0 @@ -89,8 +90,8 @@ def julianDay(year, month, day): year = year - 1 month = month + 12 if year * 10000 + month * 100 + day > 15821014: - b = 2 - year / 100 + year / 400 - return (1461 * year - yearCorr) / 4 + 306001 * (month + 1) / 10000 + day + 1720994 + b + b = 2 - year // 100 + year // 400 + return (1461 * year - yearCorr) // 4 + 306001 * (month + 1) // 10000 + day + 1720994 + b def TodayDay(): @@ -122,12 +123,12 @@ def FromJulian(julian): if (julian < 2299160): b = julian + 1525 else: - alpha = (4 * julian - 7468861) / 146097 - b = julian + 1526 + alpha - alpha / 4 - c = (20 * b - 2442) / 7305 - d = 1461 * c / 4 - e = 10000 * (b - d) / 306001 - day = int(b - d - 306001 * e / 10000) + alpha = (4 * julian - 7468861) // 146097 + b = julian + 1526 + alpha - alpha // 4 + c = (20 * b - 2442) // 7305 + d = 1461 * c // 4 + e = 10000 * (b - d) // 306001 + day = int(b - d - 306001 * e // 10000) if e < 14: month = int(e - 1) else: