Форум сайта python.su
Задача: нужно определить начало суток текущего дня, и сделать его в формате 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')))
Офлайн
посмотрите http://seehuhn.de/pages/pdate
может вам подойдет:
import datetime,time
print time.mktime(datetime.datetime.now().date().timetuple())
Офлайн
>>> time.mktime(time.localtime()[:3] + (0,) * 5 + (1,))
1308574800.0
>>> time.ctime(1308574800)
'Tue Jun 21 00:00:00 2011'
>>>
>>> time.mktime(time.localtime()[:3] + (0,) * 6)
1308578400.0
>>> time.ctime(1308578400)
'Tue Jun 21 01:00:00 2011'
>>>
Отредактировано (Июнь 21, 2011 04:48:24)
Офлайн
всем спасибо. не ругайте за код, я новичок. у меня вышло вот так:
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'))
Офлайн