Reputation: 171
My code is:
import datetime
tomorrow = datetime.date.today() + datetime.timedelta(days=1)
print "Tomorrow date is" + str(tomorrow)
tm_stme = datetime.time(0, 0, 0)
tm_etime = datetime.time(23,59,59)
tm_stdate = datetime.datetime.combine(tomorrow, tm_stme)
tm_enddate = datetime.datetime.combine(tomorrow,tm_etime)
print "tomorrow start date:" + str(tm_stdate)
print "tomorrow end date:" + str(tm_enddate)
tm_sdt_convert = time.mktime(time.strptime(tm_stdate, "%Y-%m-%d %H:%M:%S"))
tm_tdt_convert = time.mktime(time.strptime(tm_enddate, "%Y-%m-%d %H:%M:%S"))
And the error is:
administrator@Ashok-Dev:~/Desktop$ python testing.py
Tomorrow date is2012-11-24
tomorrow start date:2012-11-24 00:00:00
tomorrow end date:2012-11-24 23:59:59
Traceback (most recent call last):
File "testing.py", line 17, in <module>
tm_sdt_convert = time.mktime(time.strptime(tm_stdate, "%Y-%m-%d %H:%M:%S"))
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
I need the value of tm_sdt_convert
variable.
Upvotes: 1
Views: 1611
Reputation: 76
time.strptime
take a formatted string, its format and rerurn a tuple that contain datetime info.
But you already have a datetime
object whitch could by directly casted to tuple using method timetuple
.
So correct code should be:
>>> tm_sdt_convert = time.mktime(tm_stdate.timetuple())
>>> tm_sdt_convert
1353711600.0
Correct strptime
usage:
>>> time.mktime(time.strptime(str(tm_stdate), "%Y-%m-%d %H:%M:%S"))
Useful when you only have a string containing datetime.
Upvotes: 4