首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Python中将mysql时间戳转换为纪元时间?

在Python中将mysql时间戳转换为纪元时间?
EN

Stack Overflow用户
提问于 2018-06-22 06:18:10
回答 2查看 0关注 0票数 0

在python中将mysql时间戳转换为纪元时间,有没有简单的方法来做到这一点?

EN

回答 2

Stack Overflow用户

发布于 2018-06-22 14:39:01

将MySQL时间转换为时代:

>>> import time
>>> import calendar
>>> mysql_time = "2010-01-02 03:04:05"
>>> mysql_time_struct = time.strptime(mysql_time, '%Y-%m-%d %H:%M:%S')
>>> print mysql_time_struct
(2010, 1, 2, 3, 4, 5, 5, 2, -1)
>>> mysql_time_epoch = calendar.timegm(mysql_time_struct)
>>> print mysql_time_epoch
1262401445

将时代转换为MySQL可以使用的东西:

>>> import time
>>> time_epoch = time.time()
>>> print time_epoch
1268121070.7
>>> time_struct = time.gmtime(time_epoch)
>>> print time_struct
(2010, 3, 9, 7, 51, 10, 1, 68, 0)
>>> time_formatted = time.strftime('%Y-%m-%d %H:%M:%S', time_struct)
>>> print time_formatted
2010-03-09 07:51:10
票数 0
EN

Stack Overflow用户

发布于 2018-06-22 15:39:49

为什么不让MySQL努力工作?

从表名中选择unix_timestamp(fieldname);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100000313

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档