简体   繁体   English

Python日期时间到纪元

[英]Python datetime to epoch

t='20180515102500'
d=datetime.strptime(t, "%Y%m%d%H%M%S")
millis_since_epoch = int(time.mktime(d.timetuple())) * 1000
print(millis_since_epoch)

gives me: 15263 799 00000 on repl.it(python 3.6.1) and on my local: 15263 979 00000 (python 2.7.14)给我:15263 799 00000 在 repl.it(python 3.6.1)和我的本地:15263 979 00000(python 2.7.14)

Why?为什么? What is the recommended way to convert datetime object to epoch?将日期时间对象转换为纪元的推荐方法是什么?

Your problem is that you're using naive datetime (and struct_tm and so on) objects.您的问题是您使用的是幼稚的datetime (和struct_tm等)对象。 So you're asking how far 15 May 2018 10:25:00 in "whatever the local timezone on this machine is" is from the Unix epoch.所以你要问“无论这台机器上的本地时区是什么”中的 15 May 2018 10:25:00 距离 Unix 时代有多远。 If you run it on two machines with timezones 5 hours apart, you will get results that are 5*3600*1000 milliseconds apart.如果在时区相隔 5 小时的两台机器上运行它,您将得到相隔 5*3600*1000 毫秒的结果。

The recommended way to do it is either:推荐的方法是:

d.timestamp() # returns difference from epoch as float seconds

… or: … 或者:

d - datetime.fromtimestamp(0) # returns difference as timedelta

(It should be obvious how to convert either of those to float millis, or whatever else you need.) (很明显,如何将其中任何一个转换为浮点数,或者您需要的任何其他内容。)

But neither of those will make any difference here, since you're still using naive datetime objects.但是这些都不会在这里产生任何区别,因为您仍在使用天真的日期时间对象。 (At least you aren't mixing in other types from the time module and throwing away precision, but you're not solving your problem.) (至少你没有从time模块中混入其他类型并丢弃精度,但你没有解决你的问题。)


The solution to your problem depends on what you're trying to do.您的问题的解决方案取决于您要尝试做什么。 Are these UTC timestamps?这些是 UTC 时间戳吗? Are they timestamps in some known timezone that you know out-of-band?它们是您知道带外的某个已知时区的时间戳吗?

If everything is UTC, the best thing to do is to explicitly create UTC objects, but the simplest thing to do (especially if you need your code to run in 2.7) is:如果一切都是 UTC,最好的办法是显式创建 UTC 对象,但最简单的方法(特别是如果您需要在 2.7 中运行代码时)是:

d - datetime.utcfromtimestamp(0)

This gives you a naive datetime representing 0 UTC seconds since the epoch, and subtracts it from your naive datetime representing X UTC seconds since the epoch, so you get a timedelta representing X seconds (specifically, 17666 days, 37500 seconds), without regard to what timezone the system is using.这给你,因为时代代表0 UTC秒天真的日期时间,并从天真的日期时间,因为时代代表X UTC秒中减去,所以你得到一个timedelta代表X秒(具体17666天,37500秒),不考虑系统使用的时区。

Or, the smallest change to your existing code is to use utctimetuple instead of timetuple , which does the same thing in a more convoluted way.或者,对现有代码的最小更改是使用utctimetuple而不是timetuple ,它以更复杂的方式做同样的事情。

in python 3.6:在python 3.6中:

datetime.timestamp()

for example:例如:

import datetime

timestamp = datetime.datetime.now().timestamp()

Try this:尝试这个:

t='20180515102500'
d=datetime.datetime.strptime(t, "%Y%m%d%H%M%S")
print(d)
epoch = datetime.datetime.utcfromtimestamp(0)


def unix_time_millis(dt):
  return (dt - epoch).total_seconds() * 1000

unix_time_millis(d)

Let me know if that helps!如果有帮助,请告诉我!

t = datetime.strptime(gr_dt_string, "%Y-%m-%dT%H:%M:%S")
if six.PY2:
    gr_mili = int((t - datetime.fromtimestamp(0)).total_seconds()) * 1000
else:
    gr_mili = int(t.timestamp() * 1000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM