Last active
July 4, 2018 03:10
-
-
Save chen206/67267500fff6c66c48e2 to your computer and use it in GitHub Desktop.
Python日期时间函数库arrow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
pip install arrow | |
https://github.com/crsmithdev/arrow | |
""" | |
import arrow | |
utcnow = arrow.utcnow() | |
bjnow = arrow.now('Asia/Shanghai') | |
bjnow = utcnow.to('Asia/Shanghai') | |
bjnow.format('YYYY-MM-DD HH:mm:ss ZZ') | |
bjnow.floor('hour') | |
bjnow.floor('day') | |
bjnow.floor('week') | |
bjnow.floor('month') | |
bjnow.ceil('hour') | |
bjnow.ceil('day') | |
bjnow.ceil('week') | |
bjnow.ceil('month') | |
arrow.now('Asia/Shanghai').span('week') #周一 ~ 周日 | |
arrow.now('Asia/Shanghai').span('month') | |
def span(arw): | |
""" | |
周日 ~ 周一 | |
""" | |
monday, sunday = arw.span('week') | |
if arw.isoweekday() == 7: | |
return monday.replace(days=6), sunday.replace(days=6) | |
return monday.replace(days=-1), sunday.replace(days=-1) | |
three_minutes_ago = bjnow.replace(minutes=-3) | |
print three_minutes_ago.humanize(locale='zh_cn') | |
three_hours_ago = bjnow.replace(hours=-3) | |
print three_hours_ago.humanize(locale='zh_cn') | |
three_days_ago = bjnow.replace(days=-3) | |
print three_days_ago.humanize(locale='zh_cn') | |
# 指定日期时间的时区转换,考虑夏令时问题 | |
# https://www.timeanddate.com/time/change/usa/chicago?year=2018 | |
arrow.get("2018-03-11 15:59:59+08:00").to('America/Chicago') | |
#==> <Arrow [2018-03-11T01:59:59-06:00]> | |
arrow.get("2018-03-11 16:00:00+08:00").to('America/Chicago') | |
#==> <Arrow [2018-03-11T03:00:00-05:00]> | |
ts = arrow.get('2016-01-26T00:00:00+08:00').timestamp | |
arrow.get(ts).strftime('%Y-%m-%d') | |
arrow.get(ts).to('Asia/Shanghai').strftime('%Y-%m-%d') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment