在Python中以秒和纳秒为单位获取POSIX/Unix时间? [英] Get POSIX/Unix time in seconds and nanoseconds in Python?

查看:520
本文介绍了在Python中以秒和纳秒为单位获取POSIX/Unix时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自1970年1月1日00:00:00以来,我一直在尝试找到一种方法来在python中以秒和纳秒为单位获取时间,而我找不到任何能给我适当精度的东西.

I've been trying to find a way to get the time since 1970-01-01 00:00:00 UTC in seconds and nanoseconds in python and I cannot find anything that will give me the proper precision.

我尝试使用时间模块,但是精度只有微秒,所以我尝试的代码是:

I have tried using time module, but that precision is only to microseconds, so the code I tried was:

import time

print time.time()

给了我这样的结果:

1267918039.01

但是,我需要一个看起来像这样的结果:

However, I need a result that looks like this:

1267918039.331291406

有人知道一种以秒和纳秒为单位表示UNIX时间的可能方法吗?我找不到设置正确精度或以正确格式获得结果的方法.谢谢您的帮助

Does anyone know a possible way to express UNIX time in seconds and nanoseconds? I cannot find a way to set the proper precision or get a result in the correct format. Thank you for any help

推荐答案

由于字符串格式的原因,您的精度刚刚丢失:

Your precision is just being lost due to string formatting:

>>> import time
>>> print "%.20f" % time.time()
1267919090.35663390159606933594

这篇关于在Python中以秒和纳秒为单位获取POSIX/Unix时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆