Home > Backend Development > Python Tutorial > python将MongoDB里的ObjectId转换为时间戳的方法

python将MongoDB里的ObjectId转换为时间戳的方法

WBOY
Release: 2016-06-10 15:17:21
Original
2179 people have browsed it

本文实例讲述了python将MongoDB里的ObjectId转换为时间戳的方法。分享给大家供大家参考。具体分析如下:

MongoDB里的_id字段前四位是时间戳的16进制表示,通过Python可以很容易从_id中提取出时间戳来

def timestamp_from_objectid(objectid):
 result = 0
 try:
  result = time.mktime(objectid.generation_time.timetuple())
 except:
  pass
 return result
Copy after login

调用方法:

print(timestamp_from_objectid(ObjectId('5217a543dd99a6d9e0f74702')))
Copy after login

返回:1377252547.0

PS:这里再为大家推荐一个本站Unix时间戳转换工具,附带了各种语言(Python/PHP/java/MySQL等)Unix时间戳的操作方法:

Unix时间戳(timestamp)转换工具:http://tools.jb51.net/code/unixtime

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template