Skip to content

Commit

Permalink
Fix issue 203 (#205)
Browse files Browse the repository at this point in the history
* Map gregorian calendar to standard calendar (not the other way around)

* Updated existing unit tests

* fixed 2 unit tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
larsbarring and pre-commit-ci[bot] authored Feb 16, 2022
1 parent 1d46964 commit f6ee3e3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
9 changes: 3 additions & 6 deletions cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
#: Where calendars have multiple names, we map the alias to the
#: definitive form.
CALENDAR_ALIASES = {
CALENDAR_STANDARD: CALENDAR_GREGORIAN,
CALENDAR_GREGORIAN: CALENDAR_STANDARD,
CALENDAR_NO_LEAP: CALENDAR_365_DAY,
CALENDAR_ALL_LEAP: CALENDAR_366_DAY,
}
Expand Down Expand Up @@ -830,7 +830,7 @@ def __init__(self, unit, calendar=None):
raise value_error from None
if _OP_SINCE in unit.lower():
if calendar is None:
calendar_ = CALENDAR_GREGORIAN
calendar_ = CALENDAR_STANDARD
elif isinstance(calendar, (str,)):
calendar_ = calendar.lower()
if calendar_ in CALENDAR_ALIASES:
Expand Down Expand Up @@ -1813,10 +1813,7 @@ def convert(self, value, other, ctype=FLOAT64, inplace=False):
result = copy.deepcopy(value)
# Use cftime for converting reference times that are not using a
# gregorian calendar as it handles these and udunits does not.
if (
self.is_time_reference()
and self.calendar != CALENDAR_GREGORIAN
):
if self.is_time_reference() and self.calendar != CALENDAR_STANDARD:
result_datetimes = cftime.num2date(
result, self.cftime_unit, self.calendar
)
Expand Down
10 changes: 5 additions & 5 deletions cf_units/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_calendar_alias(self):

def test_no_calendar(self):
u = Unit("hours since 1970-01-01 00:00:00")
self.assertEqual(u.calendar, unit.CALENDAR_GREGORIAN)
self.assertEqual(u.calendar, unit.CALENDAR_STANDARD)

def test_unsupported_calendar(self):
with self.assertRaisesRegex(ValueError, "unsupported calendar"):
Expand Down Expand Up @@ -576,9 +576,9 @@ def test___repr___basic(self):

def test___repr___time_unit(self):
u = Unit(
"hours since 2007-01-15 12:06:00", calendar=unit.CALENDAR_STANDARD
"hours since 2007-01-15 12:06:00", calendar=unit.CALENDAR_GREGORIAN
)
exp = "Unit('hours since 2007-01-15 12:06:00', calendar='gregorian')"
exp = "Unit('hours since 2007-01-15 12:06:00', calendar='standard')"
self.assertEqual(repr(u), exp)


Expand Down Expand Up @@ -949,11 +949,11 @@ def test_decode_time(self):
class TestNumsAndDates(unittest.TestCase):
def test_num2date(self):
u = Unit(
"hours since 2010-11-02 12:00:00", calendar=unit.CALENDAR_STANDARD
"hours since 2010-11-02 12:00:00", calendar=unit.CALENDAR_GREGORIAN
)
res = u.num2date(1)
self.assertEqual(str(res), "2010-11-02 13:00:00")
self.assertEqual(res.calendar, "gregorian")
self.assertEqual(res.calendar, "standard")
self.assertIsInstance(res, cftime.datetime)

def test_num2date_py_datetime_type(self):
Expand Down
4 changes: 2 additions & 2 deletions cf_units/tests/unit/unit/test_Unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

class Test___init__(unittest.TestCase):
def test_capitalised_calendar(self):
calendar = "GrEgoRian"
expected = cf_units.CALENDAR_GREGORIAN
calendar = "StAnDaRd"
expected = cf_units.CALENDAR_STANDARD
u = Unit("hours since 1970-01-01 00:00:00", calendar=calendar)
self.assertEqual(u.calendar, expected)

Expand Down
4 changes: 2 additions & 2 deletions cf_units/tests/unit/unit/test_as_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def test_cf_unit(self):
def test_non_cf_unit_no_calendar(self):
# On passing as_unit a cf_unit.Unit-like object (not a cf_unit.Unit
# object) with no calendar, ensure that a cf_unit.Unit is returned
# with default calendar (Gregorian).
# with default calendar (standard).
unit = StubUnit()
result = as_unit(unit)

self.assertEqual(result.calendar, "gregorian")
self.assertEqual(result.calendar, "standard")
self.assertIsInstance(result, Unit)

def test_non_cf_unit_with_calendar(self):
Expand Down

0 comments on commit f6ee3e3

Please sign in to comment.