Skip to content

Commit

Permalink
Merge branch 'master' into issue187
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit committed Jul 18, 2020
2 parents 17f0c47 + c970ec9 commit ab10bbf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Changelog
Expand Up @@ -2,11 +2,11 @@ version 1.2.1 (not yet released)
=================================
* num2date uses 'proleptic_gregorian' scheme when basedate is post-Gregorian but date is pre-Gregorian
(issue #182).
* fix 1.2.0 regression (date2num no longer works with numpy scalar array inputs, issue #185).
* Fix for issue #187 (have date2num round to the nearest second when within 1
microsecond).
* Fix for issue #188 (leap years calculated incorrectly for negative years in
proleptic_gregorian calendar).


version 1.2.0 (release tag v1.2.0rel)
=====================================
Expand Down
7 changes: 2 additions & 5 deletions cftime/_cftime.pyx
Expand Up @@ -220,11 +220,8 @@ def date2num(dates,units,calendar='standard'):
if np.ma.isMA(dates) and np.ma.is_masked(dates):
mask = dates.mask
ismasked = True
if isscalar:
dates = np.array([dates])
else:
dates = np.array(dates)
shape = dates.shape
dates = np.asanyarray(dates)
shape = dates.shape
# are all dates python datetime instances?
all_python_datetimes = True
for date in dates.flat:
Expand Down
22 changes: 13 additions & 9 deletions test/test_cftime.py
Expand Up @@ -805,17 +805,21 @@ def test_tz_naive(self):
time2 = date2num(date,units,calendar=calendar)
date2 = num2date(time2,units,calendar=calendar)
assert(date2 == refdate)
# issue #185: date2num should work the numpy scalar array of dates (1.2.0 regression)
dates = np.array(datetime(2010, 2, 2, 0, 0))
assert (date2num(dates, units="hours since 2010-02-01 00:00:00") == 24.)
# issue #187 - roundtrip near second boundary
dt1 = datetime(1810, 4, 24, 16, 15, 10)
units = 'days since -4713-01-01 12:00'
dt2 = num2date(date2num(dt1, units), units)
assert(dt1 == dt2)
dt1 = datetime(1810, 4, 24, 16, 15, 10)
units = 'days since -4713-01-01 12:00'
dt2 = num2date(date2num(dt1, units), units)
assert(dt1 == dt2)
# issue #188 - leap years calculated incorrectly for negative years in proleptic_gregorian calendar
dt1 = datetime(2020, 4, 24, 16, 15, 10)
units = 'days since -4713-01-01 12:00'
cal = 'proleptic_gregorian'
dt2 = num2date(date2num(dt1, units, cal), units, cal)
assert(dt1 == dt2)
dt1 = datetime(2020, 4, 24, 16, 15, 10)
units = 'days since -4713-01-01 12:00'
cal = 'proleptic_gregorian'
dt2 = num2date(date2num(dt1, units, cal), units, cal)
assert(dt1 == dt2)


class TestDate2index(unittest.TestCase):

Expand Down

0 comments on commit ab10bbf

Please sign in to comment.