python数据结构学习笔记-2016-10-07-01-完善后的mydate.py

        这是经过完善后的date.py,重命名为mydate.py。

#-*-coding: utf-8-*-

# 通过儒略日格式实现公历日期

from time import localtime

class Date(object):
    # 创建公历日期实例对象
    def __init__(self, month=localtime()[1], day=localtime()[2], year=localtime()[0]):
        self._julianDay = 0
        assert self._isValidGregorian(month, day, year), "Invalid Gregorian date." # 判断输入日期是否晚于公元前4713年11月24日(公历表示)
    
        tmp = 0
        if month < 3:
            tmp = -1
        self._julianDay = day - 32075 + (1461 * (year + 4800 + tmp) / 4) + (367 * (month - 2 - tmp * 12) / 12) - (3 * ((year + 4900 + tmp) / 100) / 4)

    # 提取公历日期的组件
    def month(self):
        return (self._toGregorian())[0] # 从(M, d, y)返回M

    def day(self):
        return (self._toGregorian())[1] # 从(m, D, y)返回D

    def year(self):
        return (self._toGregorian())[2] # 从(m, d, Y)返回Y

    # 返回星期几(数字形式)
    def dayOfWeek(self):
        month, day, year = self._toGregorian()
        if month < 3:
            month += 12
            year -= 1
        return ((13 * month + 3) / 5 + day + year + year / 4 - year / 100 + year / 400) % 7

    # 返回星期几(字符串形式)
    def dayOfWeekName(self):
        return {0: 'Mon', 1: 'Tue', 2: 'Wed', 3: 'Thu', 4: 'Fri', 5: 'Sat', 6: 'Sun'}[self.dayOfWeek()]

    # 判断输入的日期是否是工作日
    def isWeekday(self):
        if self.dayOfWeek() in range(0, 5):
            return True
        else:
            return False

    # 以通用日期格式返回日期
    def __str__(self):
        month, day, year = self._toGregorian()
        return "%02d/%02d/%04d" % (month, day, year)

    # 以不同的分隔符分隔年月日
    def asGregorian(self, divchar='/'):
        month, day, year = self._toGregorian()
        return "%02d%s%02d%s%04d" % (month, divchar, day, divchar, year)

    # 比较两个日期
    def __eq__(self, otherDate):
        return self._julianDay == otherDate._julianDay

    def __lt__(self, otherDate):
        return self._julianDay < otherDate._julianDay

    def __le__(self, otherDate):
        return self._julianDay <= otherDate._julianDay

    # 返回月份名称
    def monthName(self):
        return {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun', 7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov', 12: 'Dec'}[self.month()]
    
    # 判断输入的年份是否为闰年
    def isLeapYear(self):
        yr = self.year()
        if yr > 0:
            if yr % 4 != 0:
                return False
            elif yr % 4 == 0 and yr % 100 != 0:
                return True
            elif yr % 400 != 0:
                return False
            else:
                return True
        else:
            if yr % 4 != 1:
                return False
            elif yr % 4 == 1 and yr % 100 != 1:
                return True
            elif yr % 400 != 1:
                return False
            else:
                return True

    # 与other相差的天数(正整数)
    def numDays(self, other):
        return abs(self._julianDay - other._julianDay)

    # 返回当年的第几天
    def dayOfYear(self):
        reture numDays(self, Date(12, 31, self.year()-1))

    # 返回当前日期增加days天的日期(days可为负数)
    def advanceDay(self, days):
        self._julianDay += days
        if self._julianDay >= 0:
            return self._toGregorian()
        else:
            print "The date is capped to November 24, 4714 BC."
            self._julianDay -= days
            
    # 判断用户输入的日期是否晚于公元前4713年11月24日
    def _isValidGregorian(self, month, day, year):
        if year > -4713:
            return True
        elif year == -4713 and month > 11:
            return True
        elif year == -4713 and month == 11 and day >= 24:
            return True
        else:
            return False

    # 以元组形式返回公历日期
    def _toGregorian(self):
        A = self._julianDay + 68569
        B = 4 * A / 146097
        A = A - (146097 * B + 3) / 4
        year = 4000 * (A + 1) / 1461001
        A = A - (1461 * year / 4) + 31
        month = 80 * A / 2447
        day = A - (2447 * month / 80)
        A = month / 11
        month = month + 2 - (12 * A)
        year = 100 * (B - 49) + year + A
        return month, day, year

    # 打印输入日期当前月份的日历
    def printCalender(self):
        months = {1: 'January', 2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June', 7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'}
        week = {0: 'Mo', 1: 'Tu', 2: 'We', 3: 'Th', 4: 'Fr', 5: 'Sa', 6: 'Su'}
        month, day, year = self._toGregorian()
        print "{0^26}".format('{0} {1}'.format(months[month], year))
        print "{0}  {1}  {2}  {3}  {4}  {5}  {6}".format(week[6], week[0], week[1], week[2], week[3], week[4], week[5])
        calend = Date(month, 1, year).daysOfWeek()
        line = '    ' * ((calend + 1) % 7)
        if month in [1, 3, 5, 7, 8, 10, 12]:
            end = 31
        elif month in [4, 6, 9, 11]:
            end = 30
        else:
            if self.isLeapYear():
                end = 29
            else:
                end = 28

        for i in range(1, end):
            line += '{:>2}'.format(i)
            if len(line) == 38:
                print line
                line = ''
            else:
                line += '  '
        print line


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值