From 19b67757a5f19936d2bdf094f5121318757398db Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Thu, 9 Jan 2025 14:17:42 -0700 Subject: [PATCH 1/2] fix for issue #354 --- src/cftime/_cftime.pyx | 2 +- test/test_cftime.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/cftime/_cftime.pyx b/src/cftime/_cftime.pyx index f3906afb..34b6209c 100644 --- a/src/cftime/_cftime.pyx +++ b/src/cftime/_cftime.pyx @@ -261,7 +261,7 @@ def date2num(dates, units, calendar=None, has_year_zero=None, longdouble=False): if unit in ["months", "month"] and calendar != "360_day": raise ValueError("Units of months only valid for 360_day calendar.") unit_timedelta = timedelta(microseconds=UNIT_CONVERSION_FACTORS[unit]) - can_use_python_basedatetime = _can_use_python_datetime(basedate,calendar) + can_use_python_basedatetime = calendar=='proleptic_gregorian' and _can_use_python_datetime(basedate,calendar) if can_use_python_basedatetime and all_python_datetimes: use_python_datetime = True diff --git a/test/test_cftime.py b/test/test_cftime.py index 07d7c6e9..778e9597 100644 --- a/test/test_cftime.py +++ b/test/test_cftime.py @@ -943,6 +943,16 @@ def roundtrip(delta,eps,units): times = np.array([1,2,3,np.inf],dtype=np.float64) result = cftime.num2date(times, 'days since 2000-01-01', 'standard') np.testing.assert_equal(result, expected) + # issue #354: roundtrip not correct when dates are all python datetime + # instances and calendar not proleptic_gregorian. + datesin = np.array(["0002"], + dtype="datetime64[s]").astype("M8[us]").astype(datetime) + datein = datesin.item() + num = cftime.date2num(datein, "seconds since 2000-01-01", calendar='standard') + dateout = cftime.num2date(num, "seconds since 2000-01-01", calendar='standard') + dateout2 = cftime.datetime(datein.year, datein.month, datein.day, + calendar='standard') + assert(dateout==dateout2) class TestDate2index(unittest.TestCase): From e8f0e6d2f2b27b46ea8103c6bc9d6636072f340e Mon Sep 17 00:00:00 2001 From: Jeff Whitaker Date: Thu, 9 Jan 2025 14:20:17 -0700 Subject: [PATCH 2/2] bump version number, add Changelog entry --- Changelog | 5 +++++ src/cftime/_cftime.pyx | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Changelog b/Changelog index 9dcafbdb..3402800b 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,8 @@ +since version 1.6.4 release +=========================== +* roundtrip not correct when dates are all python datetime + instances and calendar not proleptic_gregorian (issue #354). + version 1.6.4 (release tag v1.6.4rel) ===================================== * build aarch64 linux wheels (issue #333). diff --git a/src/cftime/_cftime.pyx b/src/cftime/_cftime.pyx index 34b6209c..cfa3d6ab 100644 --- a/src/cftime/_cftime.pyx +++ b/src/cftime/_cftime.pyx @@ -40,7 +40,7 @@ cdef int[12] _dayspermonth_leap = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 3 cdef int[13] _cumdayspermonth = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365] cdef int[13] _cumdayspermonth_leap = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366] -__version__ = '1.6.4.post1' +__version__ = '1.6.5' # Adapted from http://delete.me.uk/2005/03/iso8601.html # Note: This regex ensures that all ISO8601 timezone formats are accepted - but, due to legacy support for other timestrings, not all incorrect formats can be rejected.