Skip to content
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
2 changes: 1 addition & 1 deletion src/cftime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ._cftime import CFWarning
# these will be removed in a future release
from ._cftime import (DatetimeNoLeap, DatetimeAllLeap, Datetime360Day,
Datetime360Day, DatetimeJulian,
Datetime360Day, DatetimeJulian, DatetimeTAI,
DatetimeGregorian, DatetimeProlepticGregorian)


Expand Down
4 changes: 2 additions & 2 deletions src/cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ _units = microsec_units+millisec_units+sec_units+min_units+hr_units+day_units
# '366_day'=='all_leap','365_day'=='noleap')
# see http://cfconventions.org/cf-conventions/cf-conventions#calendar
# for definitions.
_calendars = ['standard', 'gregorian', 'proleptic_gregorian','tai',
_calendars = ['standard', 'gregorian', 'proleptic_gregorian', 'tai',
'noleap', 'julian', 'all_leap', '365_day', '366_day', '360_day']
_idealized_calendars= ['all_leap','noleap','366_day','365_day','360_day']
# Following are number of days per month
Expand Down Expand Up @@ -83,7 +83,7 @@ def _datesplit(timestr):

return units.lower(), remainder

def _dateparse(timestr,calendar,has_year_zero=None):
def _dateparse(timestr, calendar, has_year_zero=None):
"""parse a string of the form time-units since yyyy-mm-dd hh:mm:ss,
return a datetime instance"""
# same as version in cftime, but returns a timezone naive
Expand Down
73 changes: 72 additions & 1 deletion test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from cftime import real_datetime
from cftime import (Datetime360Day, DatetimeAllLeap,
DatetimeGregorian, DatetimeJulian, DatetimeNoLeap,
DatetimeProlepticGregorian, _parse_date,
DatetimeProlepticGregorian, DatetimeTAI, _parse_date,
date2index, date2num, num2date, UNIT_CONVERSION_FACTORS)
import copy
import unittest
Expand Down Expand Up @@ -2248,6 +2248,77 @@ def test_num2date_precision():
assert np.ma.is_masked(date2[0])
assert date[1] == date2[1]

# NOTE: using pytest style tests -- these won't run without pytest
# but it looks like you're using pytest, so this is cleaner and easier

# There's really no reason to put these in a class, but it does organize things
class Test_tai:
"""
tests specific to the tai calendar
"""
def test_dateparse_valid(self):
"""
It should raise for an epoch before 1958
"""
# This is directly testing the _dateparse function
# Which is a "proper" unit test, but also testing an internal function.
timestring = "seconds since 1958-01-01T00:00:00"
basedate = cftime._dateparse(timestring, 'tai')

print(repr(basedate))

assert basedate == cftime.datetime(1958, 1, 1, 0, 0, 0, 0, calendar='tai', has_year_zero=False)

def test_dateparse_before_1958(self):
"""
It should raise for an epoch before 1958
"""
# This is directly testing the _dateparse function
# Which is a "proper" unit test, but also testing an internal function.
timestring = "seconds since 1957-01-01T00:00:00"
with pytest.raises(ValueError):
basedate = cftime._dateparse(timestring, 'tai')
# cftime._dateparse(timestring, 'tai', has_year_zero=None)
print(basedate)

def test_dateparse_with_offset(self):
"""
It should raise if there's an offset
"""
# This is directly testing the _dateparse function
# Which is a "proper" unit test, but also testing an internal function.
timestring = "seconds since 1965-01-01T00:00:00+08:00"
with pytest.raises(ValueError):
basedate = cftime._dateparse(timestring, 'tai')
# cftime._dateparse(timestring, 'tai', has_year_zero=None)
print(basedate)

def test_year_zero(self):
"""
tai does not have a year zero -- not sure this is worth testing, but for full coverage.
"""
# This is directly testing the _dateparse function
# Which is a "proper" unit test, but also testing an internal function.
timestring = "seconds since 1965-01-01T00:00:00"
with pytest.raises(ValueError):
basedate = cftime._dateparse(timestring, 'tai', has_year_zero=True)
# cftime._dateparse(timestring, 'tai', has_year_zero=None)
print(basedate)

def test_creation_valid(self):
dt = DatetimeTAI(2025, 1, 9, 14, 18)

assert dt.calendar == 'tai'
assert dt.has_year_zero is False

print(repr(dt))

def test_creation_before_1958(self):
with pytest.raises(ValueError):
dt = DatetimeTAI(1957, 1, 9, 14, 18)
print(repr(dt))



if __name__ == '__main__':
unittest.main()