首页 > 编程语言 > Python timezone package All In One

Python timezone package All In One

时间:2023-04-13 09:00:10浏览次数:46  
标签:package Python import datetime arrow time timezone now

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

(

标签:package,Python,import,datetime,arrow,time,timezone,now
From: https://www.cnblogs.com/xgqfrms/p/17311982.html

相关文章

  • 使用shell,python,go来实现ansible的自定义模块
    一、自定义模块运行原理二、自定义模块实战2.1shell方式2.2python方式2.3golang方式三、测试验证3.1shell方式验证3.2python方式验证3.3golang方式验证ansible已经提供了非常多的模块,涵盖了系统、网络、数据库、容器、以及其他的方方面面的领域,几乎可以不用重复......
  • 今日总结-python连接数据库的学习
          ......
  • 3-面试题(python)
    1、列表和字典的区别字典是{}表示的,列表是[]表示的;字典是无序的不能通过索引来取值,列表是有序的;字典是以键值对的形式存在的,列表相当于一个容器,里面可以放置任何的数据类型; 2、python中的数据类型string、number、tuple、list、dictionary、set;3、python怎么将一个对象转......
  • 【NLP开发】Python实现聊天机器人(OpenAI,开发指南笔记)
    1、开始使用1.1介绍OpenAIAPI几乎可以应用于任何涉及理解或生成自然语言或代码的任务。我们提供一系列具有不同功率水平的型号,适用于不同的任务,并能够微调您自己的定制模型。这些模型可用于从内容生成到语义搜索和分类的所有内容。提示和完成(Promptsandcompletions)compl......
  • 【视频】随机波动率SV模型原理和Python对标普SP500股票指数预测|数据分享|附代码数据
    全文链接:http://tecdat.cn/?p=22546最近我们被客户要求撰写关于随机波动率SV模型的研究报告,包括一些图形和统计输出。什么是随机波动率?随机波动率(SV)是指资产价格的波动率是变化的而不是恒定的。 “随机”一词意味着某些变量是随机确定的,无法精确预测。在金融建模的背景......
  • 2-面试题:python
    1、python对象的比较和拷贝?答:'=='操作符比较对象之间的值是否相等;'is'操作符比较的是对象的身份标识是否相等,即它们是否是同一个对象,是否指向同一个内存地址;比较操作符'is'的速度效率,通常优于'==';浅拷贝和深拷贝:浅拷贝,将原对象或原数组的引用直接赋值给新对象、新数组,新对象/......
  • c sharp与python通信
    最近在学unity,想在unity调用python。因此学习了使用udp来建立通信。python发送,csharp接收python代码importsocketimporttimesock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)serverAddressPort=("127.0.0.1",10086)#5052定义localhost与端口,当然可......
  • 摸鱼用python代码收集每天大瓜内容信息,并发送到自己的邮箱
    本篇文章内容主要为如何用代码,把你想要的内容,以邮件的形式发送出去内容可以自己完善,还可以设置一个定时发送,或者开机启动自动运行代码代理注册与使用注册账号并登录生成api将自己电脑加入白名单http://api.tianqiip.com/white/add?key=xxx&brand=2&sign=xxx&ip=输入自己电脑的ip地......
  • 面试题:python
    列表和元组的区别列表是动态的,长度可变,可以对元素进行增、删、改操作;列表存储空间略大于元组,性能略逊于元组;元组是静态的,长度大小固定,不可以对元素进行增、删、改操作;元组相对于列表更加轻量级,性能稍优;字典和集合字典是有序的数据结构,而集合是无序的,其内部的哈希表存储结构,......
  • python习题-筛法求素数
    【题目描述】用户输入整数n和m(1<n<m<1000),应用筛法求[n,m]范围内的所有素数。【基本思想】用筛法求素数的基本思想是:把从2到N的一组正整数从小到大按顺序排列。从中依次删除2的倍数、3的倍数、5的倍数,直到根号N的倍数为止,剩余的即为2~N之间的所有素数。【源代码程序】defsie......