Skip to content

Commit

Permalink
Merge pull request #355 from Unidata/issue354
Browse files Browse the repository at this point in the history
fix for issue #354
  • Loading branch information
jswhit authored Jan 9, 2025
2 parents 06624d3 + e8f0e6d commit 7865dfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
4 changes: 2 additions & 2 deletions src/cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 7865dfe

Please sign in to comment.