将mysql时间戳转换为python中的纪元时间 - 是否有一种简单的方法可以做到这一点?
为什么不让MySQL做出艰苦的努力呢?
select unix_timestamp(fieldname) from tablename;
将mysql时间转换为epoch:
>>> 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
将epoch转换为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