相关疑难解决方法(0)

获取Python中的当前时间(以毫秒为单位)?

如何在Python中获取当前时间(以毫秒为单位)?

python time datetime

468
推荐指数
13
解决办法
66万
查看次数

在Python中将datetime.date转换为UTC时间戳

我正在处理Python中的日期,我需要将它们转换为UTC时间戳,以便在Javascript中使用.以下代码不起作用:

>>> d = datetime.date(2011,01,01)
>>> datetime.datetime.utcfromtimestamp(time.mktime(d.timetuple()))
datetime.datetime(2010, 12, 31, 23, 0)
Run Code Online (Sandbox Code Playgroud)

将日期对象首先转换为datetime也无济于事.我试过这个链接的例子,但是:

from pytz import utc, timezone
from datetime import datetime
from time import mktime
input_date = datetime(year=2011, month=1, day=15)
Run Code Online (Sandbox Code Playgroud)

现在要么:

mktime(utc.localize(input_date).utctimetuple())
Run Code Online (Sandbox Code Playgroud)

要么

mktime(timezone('US/Eastern').localize(input_date).utctimetuple())
Run Code Online (Sandbox Code Playgroud)

确实有效.

所以一般的问题:如何根据UTC获得自纪元以来转换为秒的日期?

python datetime utc

281
推荐指数
6
解决办法
51万
查看次数

查看日期时间之间是否已经过了24小时 - Python

我有以下方法:

# last_updated is a datetime() object, representing the last time this program ran
def time_diff(last_updated):
    day_period = last_updated.replace(day=last_updated.day + 1, 
                                      hour=1,
                                      minute=0,  
                                      second=0,
                                      microsecond=0)
    delta_time = day_period - last_updated
    hours = delta_time.seconds // 3600
    # make sure a period of 24hrs have passed before shuffling
    if hours >= 24:
        print "hello"
    else:
        print "do nothing"
Run Code Online (Sandbox Code Playgroud)

我想知道从那以后24小时过去了last_updated,我怎么能这样做last_updated呢?

python timezone datetime

14
推荐指数
1
解决办法
9016
查看次数

标签 统计

datetime ×3

python ×3

time ×1

timezone ×1

utc ×1