Python的UTC时间转换讲解
作者:cpdoor 时间:2023-08-20 03:07:42
UTC时间转换,最终得到的都是UTC时间。
简单来说就是:
时间戳(timestamp) 转换-> UTC显示时间(datetime),使用time.gmtime(timestamp)。
显示时间(datetime) 转换-> UTC时间戳(timestamp),使用calendar.timegm(datetime.timetuple())。
注意:
VC下相应的接口是gmtime和_mkgmtime。
代码:
# -*- coding: gb2312 -*-
# UTC时间转换,最终得到的都是UTC时间:
# 时间戳(timestamp) 转换-> UTC显示时间(datetime)
# 显示时间(datetime) 转换-> UTC时间戳(timestamp)
# UTC,协调世界时,又称世界统一时间,可以认为是时区为0的时间。
import time
import datetime
import calendar
aDatetime = datetime.datetime(1970, 1, 1, 0, 0, 1)
aTimestamp = 1
# 获取时区时差
print "time.timezone: " , time.timezone
# 根据自定义时间,获取显示时间(datetime)。
print "datetime: ", aDatetime
print "timetuple: ", aDatetime.timetuple()
print "time.strptime: ", time.strptime("1970-1-1 0:1:1", "%Y-%m-%d %H:%M:%S")
# 根据时间戳(timestamp),获取UTC显示时间(datetime)。即:时间戳(timestamp) 转换-> 显示时间(datetime)。
print "time.gmtime: timestamp(%s)->datetime(%s)" % (aTimestamp, time.gmtime(aTimestamp))
print "datetime.datetime.utcfromtimestamp: timestamp(%s)->datetime(%s)" % (aTimestamp, datetime.datetime.utcfromtimestamp(aTimestamp))
# 根据显示时间(datetime),获取UTC时间戳(timestamp)。即:显示时间(datetime) 转换-> 时间戳(timestamp)。
print "calendar.timegm: datetime(%s)->timestamp(%s)" % (aDatetime.timetuple(), calendar.timegm(aDatetime.timetuple()))
dt = time.gmtime(aTimestamp - time.timezone) # time.mktime转换时间是带时区的,所以需要减掉时区时差
print "time.mktime: datetime(%s)->timestamp(%s)" % (dt, time.mktime(dt))
输出:
time.timezone: -28800
datetime: 1970-01-01 00:00:01
timetuple: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=-1)
time.strptime: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=1, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=-1)
time.gmtime: timestamp(1)->datetime(time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0))
datetime.datetime.utcfromtimestamp: timestamp(1)->datetime(1970-01-01 00:00:01)
calendar.timegm: datetime(time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=-1))->timestamp(1)
time.mktime: datetime(time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=8, tm_min=0, tm_sec=1, tm_wday=3, tm_yday=1, tm_isdst=0))->timestamp(1.0)
来源:https://blog.csdn.net/cpdoor2163_com/article/details/51330979
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
编写Vue项目,如何给数组的第一位添加对象数据
Python爬虫获取基金变动信息
![](https://img.aspxhome.com/file/2023/5/93975_0s.webp)
wxPython事件驱动实例详解
![](https://img.aspxhome.com/file/2023/1/124911_0s.jpg)
python中如何提高图像质量
![](https://img.aspxhome.com/file/2023/4/86424_0s.jpg)
基于laravel缓冲cache的用法详解
潜谈产品设计中的可用性和可访问性
![](https://img.aspxhome.com/file/UploadPic/20091/18/prdocut_reg-60s.jpg)
python使用KNN算法识别手写数字
.NET5控制台程序使用EF连接MYSQL数据库的方法
Python 连连看连接算法
Linux添加Python path方法及修改环境变量的三种方法
Python使用poplib模块和smtplib模块收发电子邮件的教程
javascript怎么禁用浏览器后退按钮
解决vue-cli + webpack 新建项目出错的问题
VB应用程序访问SQL Server的常用方法
python将原图裁剪为固定尺寸小图
![](https://img.aspxhome.com/file/2023/7/112187_0s.jpg)
Python制作CSDN免积分下载器
![](https://img.aspxhome.com/file/2023/5/71385_0s.jpg)
python中实现定制类的特殊方法总结
Go语言学习网络编程与Http教程示例
![](https://img.aspxhome.com/file/2023/5/114825_0s.png)
详解tensorflow训练自己的数据集实现CNN图像分类
![](https://img.aspxhome.com/file/2023/3/88993_0s.png)