py库: arrow (时间)

本文介绍Python Arrow库的使用方法,包括安装、基本操作如时间增减、字符串与时间的相互转换、时间戳操作及格式化输出等。同时提供了一些实用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

 

 

 

 

 

 

...

posted on 2017-10-28 00:29 bjhhh 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/qq21270/p/7745758.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值