xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Python timezone package All In One

Python timezone package All In One

中文日期格式化

Asia/Shanghai


#!/usr/bin/env python3
# coding: utf8

import time
from datetime import datetime, tzinfo, timezone

# import pytz
# ❌ ModuleNotFoundError: No module named 'pytz'
# pip3 install pytz

# tz = pytz.timezone("UTC")

# tz = "zh-cn"
# tz = "Asia/Shanghai"
# ❌ TypeError: tzinfo argument must be None or of a tzinfo subclass, not type 'str'

# ✅
# tz = timezone.utc
# now = datetime.fromtimestamp(time.time(), tz)

now = datetime.fromtimestamp(time.time())
# now = time.time()
print("now time =", now)

# nowTime = time.time()
# ❌ AttributeError: 'float' object has no attribute 'strftime'
# nowTime = int(time.time())
# ❌ AttributeError: 'int' object has no attribute 'strftime'
# nowTime = str(time.time())
# ❌ AttributeError: 'str' object has no attribute 'strftime'

# ✅
nowTime = datetime.now()
# 转换为指定的格式
currentTime = nowTime.strftime("%Y-%m-%d %H:%M:%S")
print("currentTime =", currentTime)


nowTime2 = int(time.time())
timeArray = time.localtime(nowTime2)
# 转换为指定的格式
currentTime2 = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print("currentTime2 =", currentTime2)

# https://www.cnblogs.com/xgqfrms/p/17309426.html

from datetime import datetime

now = datetime.now()
# 转换为指定的格式
currentTime = now.strftime("%Y-%m-%d %H:%M:%S")

tz / timezone

???

demos

full version solutions

use pip install Python arrow package

$ pip install -U arrow

https://arrow.readthedocs.io/en/latest/

  1. use Python REPL
$ python
>>> import arrow
>>> from datetime import datetime
>>> now = arrow.get(datetime.now(), 'Asia/Shanghai')
>>> print("now =", now)

>>> now = 2023-04-13T00:01:45.222910+08:00

>>> quit()

  1. run test.py as a shell script
$ touch test.py

$ vim ./test.py

$ chmod +x ./test.py
# now = 2023-04-13T00:01:45.222910+08:00

test.py

#!/usr/bin/env python3
# coding: utf8

import arrow
from datetime import datetime

# ✅
now = arrow.get(datetime.now(), 'Asia/Shanghai')
print("now =", now)

# ❌
# arrow.get(datetime.now(), 'zh-cn')

screenshots

enter image description here

enter image description here

refs

https://www.cnblogs.com/xgqfrms/p/17309426.html#5167310

https://stackoverflow.com/questions/43035691/time-zone-for-china-not-working-in-python-arrow/75997660#75997660



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(28)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2022-04-13 CSS Parent Selector :has All In One
2022-04-13 TypeScript Decorator Metadata All In One
2022-04-13 斯特鲁维测地弧 All In One
2021-04-13 _.cloneDeep & JSON deep copy bug All In One
2021-04-13 React useEffect compare old new dependency
2021-04-13 webpack & css-in-js
2021-04-13 git config & color log All In One
点击右上角即可分享
微信分享提示