我在Python中有一个表示unix时间戳(即"1284101485")的字符串,我想将其转换为可读日期.当我使用时time.strftime,我得到一个TypeError:
>>>import time
>>>print time.strftime("%B %d %Y", "1284101485")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: argument must be 9-item sequence, not str
Run Code Online (Sandbox Code Playgroud) 我正在处理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获得自纪元以来转换为秒的日期?
我有一个使用datetime.utcnow()创建并保存在数据库中的python datetime实例.
为了显示,我想使用默认的本地时区将从数据库检索的日期时间实例转换为本地日期时间(即,就像使用datetime.now()创建日期时间一样).
如何仅使用python标准库将UTC日期时间转换为本地日期时间(例如,没有pytz依赖项)?
似乎一个解决方案是使用datetime.astimezone(tz),但是如何获得默认的本地时区?
是否有可能,如果是,如何获得datetime.timezone用于datetime.datetime.fromtimestamp()将POSIX时间戳(自纪元以来的秒数)转换为datetime对象的时区(即UTC偏移或具有该偏移的实例)?
datetime.datetime.fromtimestamp()将POSIX时间戳转换为天真datetime对象(即没有a tzinfo),但使用系统的语言环境将其调整为当地时区和当时生效的UTC偏移量.
例如,使用日期2008-12-27午夜UTC(自纪元以来40*356*86400秒):
>>> datetime.datetime.fromtimestamp(40 * 356 * 86400)
datetime.datetime(2008, 12, 27, 1, 0)
Run Code Online (Sandbox Code Playgroud)
该时间戳datetime在早上1点被转换为对象(当时是在CET/CEST时区).100天后,这是结果:
>>> datetime.datetime.fromtimestamp((40 * 356 + 100) * 86400)
datetime.datetime(2009, 4, 6, 2, 0)
Run Code Online (Sandbox Code Playgroud)
现在是凌晨2点.这是因为到那时,DST是活跃的.
我希望它datetime.datetime.fromtimestamp()会tzinfo在返回的datetime实例中设置它的用途,但事实并非如此.
我有一段python 3代码,它在22:00调用一个函数.
# Imports
from datetime import datetime, date, time, timedelta
import sched
import time as mod_time
# Find the next datetime corresponding to 22:00
first_run = datetime.combine(date.today(), time(22,0))
first_run = first_run if first_run > datetime.now() else first_run + timedelta(1)
# Dumb test function
def my_function():
print('my_function')
# Run the function at 22:00
scheduler = sched.scheduler(mod_time.time, mod_time.sleep)
scheduler.enterabs(first_run.timestamp(), 1, my_function, ())
scheduler.run()
Run Code Online (Sandbox Code Playgroud)
此代码目前在python 3中工作.我希望它能在python 2中运行.我唯一的问题来自以下方面:
first_run.timestamp()
Run Code Online (Sandbox Code Playgroud)
我尝试用以下内容替换它:
(first_run - datetime(1970, 1, 1)).total_seconds()
Run Code Online (Sandbox Code Playgroud)
但是我的时区似乎有问题(UTC太容易了,我在UTC + 2).first_run中应该有tzinfo.也许我应该添加一些东西?
我很失落,任何帮助都会受到赞赏.非常感谢提前帮助我.
EDIT1: …
我在其中一列上有一个带有datetime64对象的pandas数据帧.
time volume complete closeBid closeAsk openBid openAsk highBid highAsk lowBid lowAsk closeMid
0 2016-08-07 21:00:00+00:00 9 True 0.84734 0.84842 0.84706 0.84814 0.84734 0.84842 0.84706 0.84814 0.84788
1 2016-08-07 21:05:00+00:00 10 True 0.84735 0.84841 0.84752 0.84832 0.84752 0.84846 0.84712 0.8482 0.84788
2 2016-08-07 21:10:00+00:00 10 True 0.84742 0.84817 0.84739 0.84828 0.84757 0.84831 0.84735 0.84817 0.847795
3 2016-08-07 21:15:00+00:00 18 True 0.84732 0.84811 0.84737 0.84813 0.84737 0.84813 0.84721 0.8479 0.847715
4 2016-08-07 21:20:00+00:00 4 True 0.84755 0.84822 0.84739 0.84812 0.84755 0.84822 …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的会话中保存日期。我总是收到错误Object of type 'datetime' is not JSON serializable。我发现这这里的Django的文档:stored as seconds since epoch since datetimes are not serializable in JSON.
如何将我的保存expiry_date为秒而不是日期时间?
code = social_ticketing_form.cleaned_data['a']
expiry_date = timezone.now() + timezone.timedelta(days=settings.SOCIAL_TICKETING_ATTRIBUTION_WINDOW)
request.session[social_ticketing_cookie_name(request.event)] = {'code': code, 'expiry_date': expiry_date}
Run Code Online (Sandbox Code Playgroud)