zoukankan      html  css  js  c++  java
  • Arrow模块生成时间

    import arrow
    def isLeapYear(years):
        '''
        通过判断闰年,获取年份years下一年的总天数
        :param years: 年份,int
        :return:days_sum,一年的总天数
        '''
        # 断言:年份不为整数时,抛出异常。
        assert isinstance(years, int), "请输入整数年,如 2018"
     
        if ((years % 4 == 0 and years % 100 != 0) or (years % 400 == 0)):  # 判断是否是闰年
            # print(years, "是闰年")
            days_sum = 366
            return days_sum
        else:
            # print(years, '不是闰年')
            days_sum = 365
            return days_sum
     
     
    def getAllDayPerYear(years):
        '''
        获取一年的所有日期
        :param years:年份
        :return:全部日期列表
        '''
        start_date = '%s-1-1' % years
        a = 0
        all_date_list = []
        days_sum = isLeapYear(int(years))
        print()
        while a < days_sum:
            b = arrow.get(start_date).shift(days=a).format("YYYY-MM-DD")
            a += 1
            all_date_list.append(b)
        # print(all_date_list)
        return all_date_list
     
     
    if __name__ == '__main__':
        # years = "2001"
        # years = int(years)
        # # 通过判断闰年,获取一年的总天数
        # days_sum = isLeapYear(years)
     
        # 获取一年的所有日期
        all_date_list = getAllDayPerYear(years)
    tzinfo

    获取tzinfo的的Arrow对象。

    >>> arw=arrow.utcnow()
    >>> arw.tzinfo
    tzutc()
    
    datetime

    返回Arrow对象的日期时间表示形式

    >>> arw=arrow.utcnow()
    >>> arw.datetime
    datetime.datetime(2019, 1, 24, 16, 35, 27, 276649, tzinfo=tzutc())
    
    naive

    返回Arrow 对象的简单日期时间表示

    >>> nairobi = arrow.now('Africa/Nairobi')
    >>> nairobi
    <Arrow [2019-01-23T19:27:12.297999+03:00]>
    >>> nairobi.naive
    datetime.datetime(2019, 1, 23, 19, 27, 12, 297999)
    
    timestamp

    Arrow以UTC时间形式返回对象的时间戳表示形式

    >>> arrow.utcnow().timestamp
    1548260567
    
    float_timestamp

    Arrow 以UTC时间形式返回对象的浮点表示形式

    >>> arrow.utcnow().float_timestamp
    1548260516.830896
    
    clone()

    返回Arrow从当前对象克隆的新对象。

    >>> arw = arrow.utcnow()
    >>> cloned = arw.clone()
    
    replace(**kwargs)

    返回具有Arrow根据输入更新的属性的新对象。

    使用属性名称绝对设置其值:

    >>> import arrow
    >>> arw = arrow.utcnow()
    >>> arw
    <Arrow [2013-05-11T22:27:34.787885+00:00]>
    >>> arw.replace(year=2014, month=6)
    <Arrow [2014-06-11T22:27:34.787885+00:00]>
    

    您还可以使用时区表达式替换时区而不进行转换 

    >>> arw.replace(tzinfo=tz.tzlocal())
    <Arrow [2013-05-11T22:27:34.787885-07:00]>
    
    shift(**kwargs)

    返回具有Arrow根据输入更新的属性的新对象。

    使用多个属性名称相对地移动它们的当前值:

    >>> import arrow
    >>> arw = arrow.utcnow()
    >>> arw
    <Arrow [2013-05-11T22:27:34.787885+00:00]>
    >>> arw.shift(years=1, months=-1)
    <Arrow [2014-04-11T22:27:34.787885+00:00]>
    

    星期几相对移位可以使用Python的工作日数字(星期一= 0,星期二= 1 ...星期日= 6)或使用dateutil.relativedelta的日期实例(MO,TU ... SU)。使用工作日数字时,返回的日期将始终大于或等于开始日期。

    使用上面的代码(这是一个星期六)并要求它转移到星期六:

    >>> arw.shift(weekday=5)
    <Arrow [2013-05-11T22:27:34.787885+00:00]>
    

    在要求周一时:

    >>> arw.shift(weekday=0)
    <Arrow [2013-05-13T22:27:34.787885+00:00]>
    
    to(tz)

    返回一个Arrow转换为目标时区的新对象。


    >>>
    utc = arrow.utcnow()Usage:

    >>> utc
    <Arrow [2013-05-09T03:49:12.311072+00:00]>
    
    >>> utc.to('US/Pacific')
    <Arrow [2013-05-08T20:49:12.311072-07:00]>
    
    >>> utc.to(tz.tzlocal())
    <Arrow [2013-05-08T20:49:12.311072-07:00]>
    
    >>> utc.to('-07:00')
    <Arrow [2013-05-08T20:49:12.311072-07:00]>
    
    >>> utc.to('local')
    <Arrow [2013-05-08T20:49:12.311072-07:00]>
    
    >>> utc.to('local').to('utc')
    <Arrow [2013-05-09T03:49:12.311072+00:00]>
    
    span(framecount=1)

    返回两个新Arrow对象,表示Arrow给定时间范围内对象的时间跨度

    支持的帧值:年,季度,月,周,日,小时,分钟,秒。

    >>> arrow.utcnow()
    <Arrow [2013-05-09T03:32:36.186203+00:00]>
    
    >>> arrow.utcnow().span('hour')
    (<Arrow [2013-05-09T03:00:00+00:00]>, <Arrow [2013-05-09T03:59:59.999999+00:00]>)
    
    >>> arrow.utcnow().span('day')
    (<Arrow [2013-05-09T00:00:00+00:00]>, <Arrow [2013-05-09T23:59:59.999999+00:00]>)
    
    >>> arrow.utcnow().span('day', count=2)
    (<Arrow [2013-05-09T00:00:00+00:00]>, <Arrow [2013-05-10T23:59:59.999999+00:00]>)
    
    floor(frame)

    返回一个新Arrow对象,表示Arrow给定时间范围内对象的时间跨度的“下限” 相当于返回的2元组中的第一个元素 span


    >>>
    arrow.utcnow().floor('hour')Usage:

    <Arrow [2013-05-09T03:00:00+00:00]>
    
    ceil(frame)

    返回一个新Arrow对象,表示Arrow给定时间范围内对象的时间跨度的“上限” 相当于返回的2元组中的第二个元素 span


    >>>
    arrow.utcnow().ceil('hour')Usage:

    <Arrow [2013-05-09T03:59:59.999999+00:00]>
    
    format(fmt='YYYY-MM-DD HH:mm:ssZZ'locale='en_us')

    返回Arrow对象的字符串表示形式,根据格式字符串进行格式化。


    >>>
    arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ')Usage:

    '2013-05-09 03:56:47 -00:00'
    
    >>> arrow.utcnow().format('X')
    '1368071882'
    
    >>> arrow.utcnow().format('MMMM DD, YYYY')
    'May 09, 2013'
    
    >>> arrow.utcnow().format()
    '2013-05-09 03:56:47 -00:00'
    
    humanize(other=Nonelocale='en_us'only_distance=Falsegranularity='auto')

    返回相对时间差异的本地化人性化表示。


    >>>
    earlier = arrow.utcnow().shift(hours=-2)Usage:

    >>> earlier.humanize()
    '2 hours ago'
    
    >>> later = earlier.shift(hours=4)
    >>> later.humanize(earlier)
    'in 4 hours'
    
    date()

    返回date具有相同年,月和日的对象。

    >>> arrow.utcnow().date()
    datetime.date(2019, 1, 23)
    
    time()

    返回time具有相同小时,分钟,秒,微秒的对象。

    >>> arrow.utcnow().time()
    datetime.time(12, 15, 34, 68352)
    
    timetz()

    返回time具有相同小时,分钟,秒,微秒和tzinfo的对象。

    >>> arrow.utcnow().timetz()
    datetime.time(12, 5, 18, 298893, tzinfo=tzutc())
    
    astimezone(tz)

    返回一个datetime转换为指定时区对象。


    >>>
    pacific=arrow.now('US/Pacific')Usage:

    >>> nyc=arrow.now('America/New_York').tzinfo
    >>> pacific.astimezone(nyc)
    datetime.datetime(2019, 1, 20, 10, 24, 22, 328172, tzinfo=tzfile('/usr/share/zoneinfo/America/New_York'))
    
    utcoffset()

    返回timedelta表示与UTC时间的整数分钟差异对象。

    >>> arrow.now('US/Pacific').utcoffset()
    datetime.timedelta(-1, 57600)
    
    dst()

    返回夏令时调整。

    >>> arrow.utcnow().dst()
    datetime.timedelta(0)
    
    timetuple()

    在当前时区中返回time.struct_time

    >>> arrow.utcnow().timetuple()
    time.struct_time(tm_year=2019, tm_mon=1, tm_mday=20, tm_hour=15, tm_min=17, tm_sec=8, tm_wday=6, tm_yday=20, tm_isdst=0)
    
    utctimetuple()

    以UTC时间返回 time.struct_time

    >>> arrow.utcnow().utctimetuple()
    time.struct_time(tm_year=2019, tm_mon=1, tm_mday=19, tm_hour=21, tm_min=41, tm_sec=7, tm_wday=5, tm_yday=19, tm_isdst=0)
    
    toordinal()

    返回日期的公历格里高利序数。

    >>> arrow.utcnow().toordinal()
    737078
    
    weekday()

    以整数(0-6)返回星期几。

    >>> arrow.utcnow().weekday()
    5
    
    isoweekday()

    以整数(1-7)返回一周的ISO日期。

    >>> arrow.utcnow().isoweekday()
    6
    
    isocalendar()

    返回3元组(ISO年份,ISO周数,ISO工作日)。

    >>> arrow.utcnow().isocalendar()
    (2019, 3, 6)
    
    isoformat(sep='T')

    返回ISO 8601格式的日期和时间表示。

    >>> arrow.utcnow().isoformat()
    '2019-01-19T18:30:52.442118+00:00'
    
    ctime()

    返回日期和时间的ctime格式表示。

    >>> arrow.utcnow().ctime()
    'Sat Jan 19 18:26:50 2019'
    
    strftime(format)

    datetime.strftime的格式


    >>>
    arrow.utcnow().strftime('%d-%m-%Y %H:%M:%S')Usage:

    '23-01-2019 12:28:17'
    
    for_json()

    序列for_json化为simplejson 协议。

    >>> arrow.utcnow().for_json()
    '2019-01-19T18:25:36.760079+00:00'
  • 相关阅读:
    模拟器登陆
    mab算法
    用户投票排名算法总结
    vue中路由
    利用ajax全局设置实现拦截器
    对于 前端请求Django 后端服务出现403 Forbidden (CSRF token missing or incorrect.) 问题的解析
    Datatable 插入一行数据到第一行
    Datatable 导出到execl 官网demo
    jquery cookie操作
    on绑定事件支持的事件类型
  • 原文地址:https://www.cnblogs.com/songyifan427/p/10723482.html
Copyright © 2011-2022 走看看