我有一堆datetime对象,我想为每个对象计算从过去的固定时间(例如从1970年1月1日以来)开始的秒数。

import datetime
t = datetime.datetime(2009, 10, 21, 0, 0)

这似乎只是区分有不同日期的日期:

t.toordinal()

如何将datetime对象转换为秒?


当前回答

我试了试标准图书馆的日历。Timegm,它工作得很好:

# convert a datetime to milliseconds since Epoch
def datetime_to_utc_milliseconds(aDateTime):
    return int(calendar.timegm(aDateTime.timetuple())*1000)

裁判:https://docs.python.org/2/library/calendar.html # calendar.timegm

其他回答

import datetime
import math


def getSeconds(inputDate):
    time = datetime.date.today().strftime('%m/%d/%Y')
    date_time = datetime.datetime.strptime(time, '%m/%d/%Y')
    msg = inputDate
    props = msg.split(".")
    a_timedelta = datetime.timedelta
    if(len(props)==3):
        a_timedelta = date_time - datetime.datetime(int(props[0]),int(props[1]),int(props[2]))
    else:
        print("Invalid date format")
        return
    seconds = math.trunc(a_timedelta.total_seconds())
    print(seconds)
    return seconds

例子getSeconds(“2022.1.1”)

将表示UTC时间的datetime对象转换为POSIX时间戳:

from datetime import timezone

seconds_since_epoch = utc_time.replace(tzinfo=timezone.utc).timestamp()

将表示本地时区时间的datetime对象转换为POSIX时间戳:

import tzlocal # $ pip install tzlocal

local_timezone = tzlocal.get_localzone()
seconds_since_epoch = local_timezone.localize(local_time, is_dst=None).timestamp()

参见如何在Python中将本地时间转换为UTC ?如果tz数据库在给定平台上可用;只使用stdlib的解决方案可能有效。

如果需要<3.3 Python版本的解决方案,请参考链接。

比较4种最常见的方法,以确保准确性:

方法一:手动计算

from datetime import datetime
total1 = int(datetimeobj.strftime('%S'))
total1 += int(datetimeobj.strftime('%M')) * 60
total1 += int(datetimeobj.strftime('%H')) * 60 * 60
total1 += (int(datetimeobj.strftime('%j')) - 1) * 60 * 60 * 24
total1 += (int(datetimeobj.strftime('%Y')) - 1970) * 60 * 60 * 24 * 365
print ("Method #1: Manual")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total1)
print ("After: %s" % datetime.fromtimestamp(total1))

输出:

Method #1: Manual
Before: 1970-10-01 12:00:00 
Seconds: 23630400 
After: 1970-10-01 16:00:00

精度测试:FAIL(时区偏移)

方法二:时间模块

import time
from datetime import datetime
total2 = int(time.mktime(datetimeobj.timetuple()))
print ("Method #2: Time Module")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total2)
print ("After: %s" % datetime.fromtimestamp(total2))

输出:

Method #2: Time Module
Before: 1970-10-01 12:00:00 
Seconds: 23616000 
After: 1970-10-01 12:00:00

准确度测试:通过

方法三:日历模块

import calendar
from datetime import datetime
total3 = calendar.timegm(datetimeobj.timetuple())
print ("Method #3: Calendar Module")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total3)
print ("After: %s" % datetime.fromtimestamp(total3))

输出:

Method #3: Calendar Module
Before: 1970-10-01 12:00:00
Seconds: 23616000
After: 1970-10-01 16:00:00

精度测试:FAIL(时区偏移)

方法四:Datetime时间戳

from datetime import datetime
total4 = datetimeobj.timestamp()
print ("Method #4: datetime timestamp")
print ("Before: %s" % datetimeobj)
print ("Seconds: %s " % total4)
print ("After: %s" % datetime.fromtimestamp(total4))

输出:

Method #2: Time Module
Before: 1970-10-01 12:00:00 
Seconds: 23616000 
After: 1970-10-01 12:00:00

准确度测试:通过

结论

所有4个方法都将datetime转换为epoch(总秒数) Manual方法和Calendar模块方法都是时区感知的。 datetime.timestamp()和time.mktime()方法都不涉及时区。 最简单的方法:datetime.timestamp()

我试了试标准图书馆的日历。Timegm,它工作得很好:

# convert a datetime to milliseconds since Epoch
def datetime_to_utc_milliseconds(aDateTime):
    return int(calendar.timegm(aDateTime.timetuple())*1000)

裁判:https://docs.python.org/2/library/calendar.html # calendar.timegm

获取Unix时间(从1970年1月1日开始的秒数):

>>> import datetime, time
>>> t = datetime.datetime(2011, 10, 21, 0, 0)
>>> time.mktime(t.timetuple())
1319148000.0