Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.date1_365_day = DatetimeNoLeap(-5000, 1, 2, 12)
self.date2_365_day = DatetimeNoLeap(-5000, 1, 3, 12)
self.date3_gregorian = DatetimeGregorian(1969, 7, 20, 12)
# last day of the Julian calendar in the mixed Julian/Gregorian calendar
self.date4_gregorian = DatetimeGregorian(1582, 10, 4)
# first day of the Gregorian calendar in the mixed Julian/Gregorian calendar
self.date5_gregorian = DatetimeGregorian(1582, 10, 15)
self.date6_proleptic_gregorian = DatetimeProlepticGregorian(1582, 10, 15)
self.date7_360_day = Datetime360Day(2000, 1, 1)
self.date8_julian = DatetimeJulian(1582, 10, 4)
# a datetime.datetime instance (proleptic Gregorian calendar)
self.datetime_date1 = datetime(1969, 7, 21, 12)
self.delta = timedelta(hours=25)
def setUp(self):
self.date1_365_day = DatetimeNoLeap(-5000, 1, 2, 12)
self.date2_365_day = DatetimeNoLeap(-5000, 1, 3, 12)
self.date3_gregorian = DatetimeGregorian(1969, 7, 20, 12)
# last day of the Julian calendar in the mixed Julian/Gregorian calendar
self.date4_gregorian = DatetimeGregorian(1582, 10, 4)
# first day of the Gregorian calendar in the mixed Julian/Gregorian calendar
self.date5_gregorian = DatetimeGregorian(1582, 10, 15)
self.date6_proleptic_gregorian = DatetimeProlepticGregorian(1582, 10, 15)
self.date7_360_day = Datetime360Day(2000, 1, 1)
self.date8_julian = DatetimeJulian(1582, 10, 4)
# a datetime.datetime instance (proleptic Gregorian calendar)
self.datetime_date1 = datetime(1969, 7, 21, 12)
self.delta = timedelta(hours=25)
delta = self.datetime_date1 - self.date3_gregorian
# real_date2 and real_date1 are exactly one day apart
self.assertEqual(total_seconds(delta), 86400)
# subtracting datetime.datetime from netcdftime.datetime
delta = self.date3_gregorian - self.datetime_date1
# real_date2 and real_date1 are exactly one day apart
self.assertEqual(total_seconds(delta), -86400)
# Test the Julian/Gregorian transition.
self.assertEqual(self.date5_gregorian - self.delta,
DatetimeGregorian(1582, 10, 3, 23))
# The proleptic Gregorian calendar does not have invalid dates.
self.assertEqual(self.date6_proleptic_gregorian - self.delta,
DatetimeProlepticGregorian(1582, 10, 13, 23))
# The Gregorian calendar has no year zero.
self.assertEqual(DatetimeGregorian(1, 1, 1) - self.delta,
DatetimeGregorian(-1, 12, 30, 23))
# The 360_day calendar has year zero.
self.assertEqual(self.date7_360_day - timedelta(days=2000 * 360),
Datetime360Day(0, 1, 1))
# Test going over the year boundary.
self.assertEqual(DatetimeGregorian(2000, 3, 1) - timedelta(days=29 + 31 + 31),
DatetimeGregorian(1999, 12, 1))
# Year 2000 is a leap year.
self.assertEqual(DatetimeGregorian(2000, 3, 1) - self.delta,
d = num2date(1, 'months since 0000-01-01 00:00:00', calendar='360_day')
self.assertEqual(d, Datetime360Day(0,2,1))
t = date2num(d, 'months since 0000-01-01 00:00:00', calendar='360_day')
self.assertEqual(t, 1)
# check that exception is raised if 'months since' used with
# anything but the 360_day calendar.
self.assertRaises(ValueError, num2date, \
1, 'months since 01-01-01',calendar='standard')
self.assertRaises(ValueError, utime, \
'months since 01-01-01', calendar='standard')
# issue #78 - extra digits due to roundoff
assert(cftime.date2num(cftime.datetime(1, 12, 1, 0, 0, 0, 0, -1, 1), units='days since 01-01-01',calendar='noleap') == 334.0)
assert(cftime.date2num(cftime.num2date(1.0,units='days since 01-01-01',calendar='noleap'),units='days since 01-01-01',calendar='noleap') == 1.0)
assert(cftime.date2num(cftime.DatetimeNoLeap(1980, 1, 1, 0, 0, 0, 0, 6, 1),'days since 1970-01-01','noleap') == 3650.0)
# issue #126
d = cftime.DatetimeProlepticGregorian(1, 1, 1)
assert(cftime.date2num(d, 'days since 0001-01-01',\
'proleptic_gregorian') == 0.0)
# issue #140 (fractional seconds in reference date)
d = datetime.strptime('2018-01-23 09:27:10.950000',"%Y-%m-%d %H:%M:%S.%f")
units = 'seconds since 2018-01-23 09:31:42.94'
assert(cftime.date2num(d, units) == -271.99)
# issue 143 - same answer for arrays vs scalars.
units = 'seconds since 1970-01-01 00:00:00'
times_in = [1261440000.0, 1261440001.0, 1261440002.0, 1261440003.0,
1261440004.0, 1261440005.0]
times_out1 = cftime.num2date(times_in, units)
times_out2 = []
for time_in in times_in:
times_out2.append(cftime.num2date(time_in, units))
dates1 = [str(d) for d in times_out1]
dates2 = [str(d) for d in times_out2]
def setUp(self):
self.date1_365_day = DatetimeNoLeap(-5000, 1, 2, 12)
self.date2_365_day = DatetimeNoLeap(-5000, 1, 3, 12)
self.date3_gregorian = DatetimeGregorian(1969, 7, 20, 12)
# last day of the Julian calendar in the mixed Julian/Gregorian calendar
self.date4_gregorian = DatetimeGregorian(1582, 10, 4)
# first day of the Gregorian calendar in the mixed Julian/Gregorian calendar
self.date5_gregorian = DatetimeGregorian(1582, 10, 15)
self.date6_proleptic_gregorian = DatetimeProlepticGregorian(1582, 10, 15)
self.date7_360_day = Datetime360Day(2000, 1, 1)
self.date8_julian = DatetimeJulian(1582, 10, 4)
# a datetime.datetime instance (proleptic Gregorian calendar)
self.datetime_date1 = datetime(1969, 7, 21, 12)
self.delta = timedelta(hours=25)
delta = self.datetime_date1 - self.date3_gregorian
# real_date2 and real_date1 are exactly one day apart
self.assertEqual(total_seconds(delta), 86400)
# subtracting datetime.datetime from cftime.datetime
delta = self.date3_gregorian - self.datetime_date1
# real_date2 and real_date1 are exactly one day apart
self.assertEqual(total_seconds(delta), -86400)
# Test the Julian/Gregorian transition.
self.assertEqual(self.date5_gregorian - self.delta,
DatetimeGregorian(1582, 10, 3, 23))
# The proleptic Gregorian calendar does not have invalid dates.
self.assertEqual(self.date6_proleptic_gregorian - self.delta,
DatetimeProlepticGregorian(1582, 10, 13, 23))
# The Gregorian calendar has no year zero.
self.assertEqual(DatetimeGregorian(1, 1, 1) - self.delta,
DatetimeGregorian(-1, 12, 30, 23))
# The 360_day calendar has year zero.
self.assertEqual(self.date7_360_day - timedelta(days=2000 * 360),
Datetime360Day(0, 1, 1))
# Test going over the year boundary.
self.assertEqual(DatetimeGregorian(2000, 3, 1) - timedelta(days=29 + 31 + 31),
DatetimeGregorian(1999, 12, 1))
# Year 2000 is a leap year.
self.assertEqual(DatetimeGregorian(2000, 3, 1) - self.delta,
assert (d1.dayofyr == d2.dayofyr == 60)
d1 = DatetimeGregorian(2020,2,28,23,59,59)
d2 = real_datetime(2020,2,28,23,59,59)
assert (d1.dayofwk == d2.dayofwk == 4)
assert (d1.dayofyr == d2.dayofyr == 59)
d1 = DatetimeGregorian(1700,1,1)
d2 = real_datetime(1700,1,1)
assert (d1.dayofwk == d2.dayofwk == 4)
assert (d1.dayofyr == d2.dayofyr == 1)
# last day of Julian Calendar (Thursday)
d1 = DatetimeJulian(1582, 10, 4, 12)
d2 = DatetimeGregorian(1582, 10, 4, 12)
assert (d1.dayofwk == d2.dayofwk == 3)
assert (d1.dayofyr == d2.dayofyr == 277)
# Monday in proleptic gregorian calendar
d1 = DatetimeProlepticGregorian(1582, 10, 4, 12)
d2 = real_datetime(1582,10,4,12)
assert (d1.dayofwk == d2.dayofwk == 0)
assert (d1.dayofyr == d2.dayofyr == 277)
# issue 71: negative reference years
# https://coastwatch.pfeg.noaa.gov/erddap/convert/time.html
# gives 2446433 (365 days more - is it counting year 0?)
# however http://aa.usno.navy.mil/data/docs/JulianDate.php gives
# 2446068, which agrees with us
units = "days since -4713-01-01T00:00:00Z"
t = date2num(datetime(1985,1,2), units, calendar="standard")
assert_almost_equal(t, 2446068)
# issue #68: allow months since for 360_day calendar
d = num2date(1, 'months since 0000-01-01 00:00:00', calendar='360_day')
self.assertEqual(d, Datetime360Day(0,2,1))
t = date2num(d, 'months since 0000-01-01 00:00:00', calendar='360_day')
def days_per_month_leap_year(date_type, month):
if date_type is Datetime360Day:
return [-1, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30][month]
if date_type in [DatetimeGregorian, DatetimeProlepticGregorian,
DatetimeJulian, DatetimeAllLeap]:
return [-1, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
else:
return [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month]
else:
result = ut.date2num(value)
if shape is not None:
result = result.reshape(shape)
return result
# Automatically register NetCDFTimeConverter with matplotlib.unit's converter
# dictionary.
if CalendarDateTime not in munits.registry:
munits.registry[CalendarDateTime] = NetCDFTimeConverter()
CFTIME_TYPES = [cftime.DatetimeNoLeap, cftime.DatetimeAllLeap,
cftime.DatetimeProlepticGregorian, cftime.DatetimeGregorian,
cftime.Datetime360Day, cftime.DatetimeJulian]
for date_type in CFTIME_TYPES:
if date_type not in munits.registry:
munits.registry[date_type] = NetCDFTimeConverter()
def get_date_type(calendar):
"""Return the cftime date type for a given calendar name."""
try:
import cftime
except ImportError:
raise ImportError("cftime is required for dates with non-standard calendars")
else:
calendars = {
"noleap": cftime.DatetimeNoLeap,
"360_day": cftime.Datetime360Day,
"365_day": cftime.DatetimeNoLeap,
"366_day": cftime.DatetimeAllLeap,
"gregorian": cftime.DatetimeGregorian,
"proleptic_gregorian": cftime.DatetimeProlepticGregorian,
"julian": cftime.DatetimeJulian,
"all_leap": cftime.DatetimeAllLeap,
"standard": cftime.DatetimeGregorian,
}
return calendars[calendar]