Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue #354 #355

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading