py库: arrow (时间)

arrow是个时间日期库,简洁易用。支持python3.6

https://arrow.readthedocs.io/en/latest/  arrow官网api

https://github.com/crsmithdev/arrow  arrow的GitHub托管的地址

http://www.iplaypy.com/module/m111.html  第三方时间日期库 Python Arrow模块


安装:

pip install arrow

  

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

import arrow

# local = utc.to('US/Pacific') # 时区修改
# local = utc.to('Asia/Shanghai') # 时区修改
local = arrow.now('local')  # 获取当前时间(推荐)
print(local)
print('-------------')


# 时间增减
print(local.replace(years=-1))
print(local.replace(months=-1))
print(local.replace(days=-5))
print(local.replace(hours=-24))
print(local.replace(minutes=-10))
print(local.replace(seconds=-60))
print('-------------')


# 字符串转时间
print(arrow.get('2017-10-28T00:00:00+0800'))
# print(arrow.get('2017-10-28', 'YYYY-MM-DD'))  # 字符串转时间(不推荐这样用,没有设时区)
print(arrow.get('2017-10-28', 'YYYY-MM-DD', tzinfo='local'))  # (推荐)
print(arrow.get('2017-10-28', 'YYYY-MM-DD', tzinfo='Asia/Shanghai'))  # 这样也可以
print(arrow.get('2017-10-28 05:30:30', 'YYYY-MM-DD HH:mm:ss', tzinfo='local'))  # (推荐)
print('-------------')


# 时间戳
print(local.timestamp)  # 时间戳
print(arrow.get('1509120000', tzinfo='local'))  # 时间戳字符串,转换为本时区的时间


# 时间转为字符串,输出(格式化)
print(local.format("YYYY-MM-DD"))
print(local.format("YYYY-MM-DD HH:mm:ss"))
print(local.replace(minutes=-1).humanize(locale='zh'))  # 本地化个性时间短语: 刚才,1分钟前,1天前,等  (zh_tw  更多语言的支持,去查看arrow/locales.py)

Arrow主要功能:
1、时区转换
2、简单的时间戳操作
3、时间跨度
4、非常人性化,支持越来越多的语言环境
5、实现datetime接口
6、支持Python 2.6、2.7和3.3
7、默认采用TZ-aware和UTC
8、创建简洁、智能的接口
9、可以轻松更换和改变属性
10、丰富的解析和格式化选项
11、可扩展的工厂架构来支持自定义Arrow派生类型


2017-11-27补充:

判断是否是闰年: 

def isLeapYear(year):
    if not year % 4 and year % 100 or not year % 400:
        return True
    return False

runyear = []
for i in range(1890, 2150):
    if isLeapYear(i):
        runyear.append(i)
print(runyear)

...

原文地址:https://www.cnblogs.com/qq21270/p/7745758.html