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

switch only_use_cftime_datetimes to True by default #135

Merged
merged 5 commits into from
Nov 26, 2019
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
16 changes: 16 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version 1.1.0 (not yet released)
================================

* improved exceptions for time differences (issue #128, PR #131).

* fix intersphinx entries (issue #133, PR #133)

* make use_only_cftime_datetimes=True by default, so cftime datetime
instances are returned by default by num2date (instead of returning python
datetime instances where possible). Issue #136, PR #135.

version 1.0.4.2 release
=======================

* fix for issue #126 (date2num error when converting a DatetimeProlepticGregorian
object). PR #127.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Time-handling functionality from netcdf4-python
[![Commits Status](https://img.shields.io/github/commits-since/UniData/cftime/latest.svg)](https://github.com/UniData/cftime/commits/master)

## News
For details on the latest updates, see the [Changelog](https://github.com/cftime/blob/master/Changelog).

10/25/2019: version 1.0.4.2 released (fix for [issue #126](https://github.com/Unidata/cftime/issues/126)).

10/21/2019: version 1.0.4 released.
Expand Down
16 changes: 8 additions & 8 deletions cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ cdef int[13] _spm_366day = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 33
_rop_lookup = {Py_LT: '__gt__', Py_LE: '__ge__', Py_EQ: '__eq__',
Py_GT: '__lt__', Py_GE: '__le__', Py_NE: '__ne__'}

__version__ = '1.0.4.2'
__version__ = '1.1.0'

# 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 @@ -215,7 +215,7 @@ def date2num(dates,units,calendar='standard'):
return cdftime.date2num(dates)


def num2date(times,units,calendar='standard',only_use_cftime_datetimes=False):
def num2date(times,units,calendar='standard',only_use_cftime_datetimes=True):
"""num2date(times,units,calendar='standard')

Return datetime objects given numeric time values. The units
Expand All @@ -238,9 +238,9 @@ def num2date(times,units,calendar='standard',only_use_cftime_datetimes=False):
'noleap', '365_day', '360_day', 'julian', 'all_leap', '366_day'`.
Default is `'standard'`, which is a mixed Julian/Gregorian calendar.

**`only_use_cftime_datetimes`**: if False (default), datetime.datetime
**`only_use_cftime_datetimes`**: if False, datetime.datetime
objects are returned from num2date where possible; if True dates which
subclass cftime.datetime are returned for all calendars.
subclass cftime.datetime are returned for all calendars. Default is True.

returns a datetime instance, or an array of datetime instances with
approximately 100 microsecond accuracy.
Expand Down Expand Up @@ -435,7 +435,7 @@ def JulianDayFromDate(date, calendar='standard'):
else:
return jd

def DateFromJulianDay(JD, calendar='standard', only_use_cftime_datetimes=False,
def DateFromJulianDay(JD, calendar='standard', only_use_cftime_datetimes=True,
return_tuple=False):
"""

Expand Down Expand Up @@ -690,7 +690,7 @@ it should be noted that udunits treats 0 AD as identical to 1 AD."
"""

def __init__(self, unit_string, calendar='standard',
only_use_cftime_datetimes=False):
only_use_cftime_datetimes=True):
"""
@param unit_string: a string of the form
C{'time-units since <time-origin>'} defining the time units.
Expand Down Expand Up @@ -723,9 +723,9 @@ are:
Proleptic Julian calendar, extended to dates after 1582-10-5. A year is a
leap year if it is divisible by 4.

@keyword only_use_cftime_datetimes: if False (default), datetime.datetime
@keyword only_use_cftime_datetimes: if False, datetime.datetime
objects are returned from num2date where possible; if True dates which subclass
cftime.datetime are returned for all calendars.
cftime.datetime are returned for all calendars. Default True.

@returns: A class instance which may be used for converting times from netCDF
units to datetime objects.
Expand Down
14 changes: 9 additions & 5 deletions test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ def test_tz_naive(self):
err = np.abs(mins1 - mins2)
maxerr = max(err,maxerr)
assert(err < eps)
assert(date1.strftime(dateformat) == date2.strftime(dateformat))
diff = abs(date1-date2)
assert(diff.microseconds < 100)
if verbose:
print('calendar = %s max abs err (mins) = %s eps = %s' % \
(calendar,maxerr,eps))
Expand All @@ -456,7 +457,8 @@ def test_tz_naive(self):
err = np.abs(hrs1 - hrs2)
maxerr = max(err,maxerr)
assert(err < eps)
assert(date1.strftime(dateformat) == date2.strftime(dateformat))
diff = abs(date1-date2)
assert(diff.microseconds < 100)
if verbose:
print('calendar = %s max abs err (hours) = %s eps = %s' % \
(calendar,maxerr,eps))
Expand All @@ -472,7 +474,8 @@ def test_tz_naive(self):
err = np.abs(days1 - days2)
maxerr = max(err,maxerr)
assert(err < eps)
assert(date1.strftime(dateformat) == date2.strftime(dateformat))
diff = abs(date1-date2)
assert(diff.microseconds < 100)
if verbose:
print('calendar = %s max abs err (days) = %s eps = %s' % \
(calendar,maxerr,eps))
Expand Down Expand Up @@ -895,8 +898,9 @@ def test_select_nc(self):
datetime(1995, 11, 25, 18, 7, 59, 999999)]
times2 = date2num(dates, units)
dates2 = num2date(times2, units)
for date, date2 in zip(dates, dates2):
assert_equal(date, date2)
datediff = abs(dates-dates2)
for diff in datediff:
assert(diff.microseconds < 100) # tolerance of 100 ms

def test_issue444(self):
# make sure integer overflow not causing error in
Expand Down