Skip to content

Commit

Permalink
fix for issue #354
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit committed Jan 9, 2025
1 parent 06624d3 commit 19b6775
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cftime/_cftime.pyx
Original file line number Diff line number Diff line change
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 19b6775

Please sign in to comment.