Skip to content

Commit

Permalink
fix for issue #165
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit committed Apr 17, 2020
1 parent 9535d9f commit 07999d1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 12 additions & 6 deletions cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,13 @@ def num2date(times,units,calendar='standard',\
msg='zero not allowed as a reference year, does not exist in Julian or Gregorian calendars'
raise ValueError(msg)

if only_use_cftime_datetimes or not \
(only_use_python_datetimes and can_use_python_datetime):
cdftime = utime(units, calendar=calendar,
only_use_cftime_datetimes=only_use_cftime_datetimes)
return cdftime.num2date(times)
else: # use python datetime module
use_python_datetime = False
if only_use_python_datetimes and not only_use_cftime_datetimes:
use_python_datetime = True
if not only_use_python_datetimes and not only_use_cftime_datetimes and can_use_python_datetime:
use_python_datetime = True

if use_python_datetime: # use python datetime module
isscalar = False
try:
times[0]
Expand Down Expand Up @@ -386,6 +387,11 @@ OverflowError in python datetime, probably because year < datetime.MINYEAR"""
return dates[0]
else:
return np.reshape(np.array(dates), shape)
else: # use cftime datetime
cdftime = utime(units, calendar=calendar,
only_use_cftime_datetimes=only_use_cftime_datetimes)
return cdftime.num2date(times)



def date2index(dates, nctime, calendar=None, select='exact'):
Expand Down
3 changes: 3 additions & 0 deletions test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,9 @@ def test_tz_naive(self):
d = cftime.num2date(1261440000.015625,units)
# on windows only 100 ms precision
assert(str(d)[0:24] == '2009-12-22 00:00:00.0156')
# issue #165: make sure python datetime returned
d=num2date(0,units="seconds since 2000-01-01 00:00:00",only_use_cftime_datetimes=False)
assert isinstance(d, datetime)

class TestDate2index(unittest.TestCase):

Expand Down

0 comments on commit 07999d1

Please sign in to comment.