本文整理汇总了Python中calendar.leapdays方法的典型用法代码示例。如果您正苦于以下问题:Python calendar.leapdays方法的具体用法?Python calendar.leapdays怎么用?Python calendar.leapdays使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类calendar
的用法示例。
在下文中一共展示了calendar.leapdays方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: timegm
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def timegm(year, month, day, hour, minute, second):
"""
Convert time tuple in GMT to seconds since epoch, GMT
"""
EPOCH = 1970
if year < EPOCH:
raise ValueError("Years prior to %d not supported" % (EPOCH,))
assert 1 <= month <= 12
days = 365*(year-EPOCH) + calendar.leapdays(EPOCH, year)
for i in range(1, month):
days = days + calendar.mdays[i]
if month > 2 and calendar.isleap(year):
days = days + 1
days = days + day - 1
hours = days*24 + hour
minutes = hours*60 + minute
seconds = minutes*60 + second
return seconds
示例2: xyearfrac
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def xyearfrac(start_date, end_date, basis=0):
raise_errors(basis, start_date, end_date)
basis = tuple(flatten(basis, None))
if len(basis) != 1 or isinstance(basis[0], bool):
return Error.errors['#VALUE!']
basis = 0 if basis[0] is sh.EMPTY else basis[0]
if not is_number(basis) or int(basis) not in (0, 1, 2, 3, 4):
return Error.errors['#NUM!']
dates = [tuple(flatten(d, None)) for d in (start_date, end_date)]
if any(isinstance(d[0], bool) for d in dates):
return Error.errors['#VALUE!']
# noinspection PyTypeChecker
basis, dates = int(basis), [xday(*d, slice(0, 3)) for d in dates]
err = get_error(*dates)
if err:
return err
(y1, m1, d1), (y2, m2, d2) = sorted(dates)
denom = 360
if basis in (0, 4): # US 30/360 & Eurobond 30/360
d1 = min(d1, 30)
if basis == 4:
d2 = min(d2, 30)
elif d1 == 30:
d2 = max(d2, 30)
n_days = 360 * (y2 - y1) + 30 * (m2 - m1) + (d2 - d1)
else: # Actual/actual & Actual/360 & Actual/365
n_days = xdate(y2, m2, d2) - xdate(y1, m1, d1)
if basis == 3:
denom = 365
elif basis == 1:
denom = 365 + calendar.leapdays(y1, y2 + 1) / (y2 - y1 + 1)
return n_days / denom
示例3: test_no_range
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def test_no_range(self):
# test when no range i.e. two identical years as args
self.assertEqual(calendar.leapdays(2010,2010), 0)
示例4: test_no_leapdays
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def test_no_leapdays(self):
# test when no leap years in range
self.assertEqual(calendar.leapdays(2010,2011), 0)
示例5: test_no_leapdays_upper_boundary
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def test_no_leapdays_upper_boundary(self):
# test no leap years in range, when upper boundary is a leap year
self.assertEqual(calendar.leapdays(2010,2012), 0)
示例6: test_one_leapday_lower_boundary
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def test_one_leapday_lower_boundary(self):
# test when one leap year in range, lower boundary is leap year
self.assertEqual(calendar.leapdays(2012,2013), 1)
示例7: test_several_leapyears_in_range
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def test_several_leapyears_in_range(self):
self.assertEqual(calendar.leapdays(1997,2020), 5)
示例8: builtin_date
# 需要导入模块: import calendar [as 别名]
# 或者: from calendar import leapdays [as 别名]
def builtin_date(name, args, pos):
"""
date(year, month, day) builtin function.
@return Days since 1 jan 1 of the given date.
"""
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if len(args) != 3:
raise generic.ScriptError("date() requires exactly 3 arguments", pos)
identifier.ignore_all_invalid_ids = True
year = args[0].reduce(global_constants.const_list)
identifier.ignore_all_invalid_ids = False
try:
month = args[1].reduce_constant().value
day = args[2].reduce_constant().value
except generic.ConstError:
raise generic.ScriptError("Month and day parameters of date() should be compile-time constants", pos)
generic.check_range(month, 1, 12, "month", args[1].pos)
generic.check_range(day, 1, days_in_month[month-1], "day", args[2].pos)
if not isinstance(year, ConstantNumeric):
if month != 1 or day != 1:
raise generic.ScriptError("when the year parameter of date() is not a compile time constant month and day should be 1", pos)
#num_days = year*365 + year/4 - year/100 + year/400
part1 = BinOp(nmlop.MUL, year, ConstantNumeric(365))
part2 = BinOp(nmlop.DIV, year, ConstantNumeric(4))
part3 = BinOp(nmlop.DIV, year, ConstantNumeric(100))
part4 = BinOp(nmlop.DIV, year, ConstantNumeric(400))
res = BinOp(nmlop.ADD, part1, part2)
res = BinOp(nmlop.SUB, res, part3)
res = BinOp(nmlop.ADD, res, part4)
return res
generic.check_range(year.value, 0, 5000000, "year", year.pos)
day_in_year = 0
for i in range(month - 1):
day_in_year += days_in_month[i]
day_in_year += day
if month >= 3 and (year.value % 4 == 0) and ((not year.value % 100 == 0) or (year.value % 400 == 0)):
day_in_year += 1
return ConstantNumeric(year.value * 365 + calendar.leapdays(0, year.value) + day_in_year - 1, pos)