# Implements a proleptic Gregorian calendar date as a Julian day number.class Date :# Creates an object instance for the specified Gregorian date.def __init__( self, month, day, year ):self._julianDay = 0assert self._isValidGregorian( month, day, year ), \"Invalid Gregorian date."# The first line of the equation, T = (M - 14) / 12, has to be changed# since Python's implementation of integer division is not the same# as the mathematical definition.tmp = 0if month < 3 :tmp = -1self._julianDay = day - 32075 + \(1461 * (year + 4800 + tmp) // 4) + \(367 * (month - 2 - tmp * 12) // 12) - \(3 * ((year + 4900 + tmp) // 100) // 4)# Extracts the appropriate Gregorian date component.def month( self ):return (self._toGregorian())[0] # returning M from (M, d, y)def day( self ):return (self._toGregorian())[1] # returning D from (m, D, y)def year( self ):return (self._toGregorian())[2] # returning Y from (m, d, Y)# Returns day of the week as an int between 0 (Mon) and 6 (Sun).def dayOfWeek( self ):month, day, year = self._toGregorian()if month < 3 :month = month + 12year = year - 1return ((13 * month + 3) // 5 + day + \year + year // 4 - year // 100 + year // 400) % 7# Returns the date as a string in Gregorian format.def __str__( self ):month, day, year = self._toGregorian()return "%02d/%02d/%04d" % (month, day, year)# Logically compares the two dates.def __eq__( self, otherDate ):return self._julianDay == otherDate._julianDaydef __lt__( self, otherDate ):return self._julianDay < otherDate._julianDaydef __le__( self, otherDate ):return self._julianDay <= otherDate._julianDay# The remaining methods are to be included at this point.# ......# Returns the Gregorian date as a tuple: (month, day, year).def _toGregorian( self ):A = self._julianDay + 68569B = 4 * A // 146097A = A - (146097 * B + 3) // 4year = 4000 * (A + 1) // 1461001A = A - (1461 * year // 4) + 31month = 80 * A // 2447day = A - (2447 * month // 80)A = month // 11month = month + 2 - (12 * A)year = 100 * (B - 49) + year + Areturn month, day, year
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。