Найти - Пользователи
Полная версия: Начало суток
Начало » Python для новичков » Начало суток
1
iga
Задача: нужно определить начало суток текущего дня, и сделать его в формате epoch. Делаю так:
import time
currentdate = datetime.datetime.today()
currentdate2 = currentdate.combine(currentdate.date(), currentdate.min.time())
int(time.mktime(time.strptime(currentdate2, '%Y-%m-%d %H:%M:%S')))
получаю
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "/usr/lib/python2.6/_strptime.py", line 322, in _strptime
found = format_regex.match(data_string)
TypeError: expected string or buffer
Если же сделать
int(time.mktime(time.strptime('2011-06-18 00:00:00', '%Y-%m-%d %H:%M:%S')))
То все окей. Подскажите где ошибка, или может есть более легкий способ. Спасибо.
doza_and
посмотрите http://seehuhn.de/pages/pdate
может вам подойдет:
import datetime,time
print time.mktime(datetime.datetime.now().date().timetuple())
py.user.next
>>> time.mktime(time.localtime()[:3] + (0,) * 5 + (1,))
1308574800.0
>>> time.ctime(1308574800)
'Tue Jun 21 00:00:00 2011'
>>>
без учёта DST
>>> time.mktime(time.localtime()[:3] + (0,) * 6)
1308578400.0
>>> time.ctime(1308578400)
'Tue Jun 21 01:00:00 2011'
>>>
iga
всем спасибо. не ругайте за код, я новичок. у меня вышло вот так:

    currentdate = datetime.datetime.utcnow()
currentdate = str(currentdate.replace(hour=00, minute=0, second=00, microsecond=00))
int(calendar.timegm(time.strptime(currentdate, '%Y-%m-%d %H:%M:%S'))
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB