Reputation: 20232
I have problem with formatting date. This is my code:
myDate=datetime.datetime.now()
print myDate #2011-02-02 13:54:26.162000
stime = time.mktime(time.strptime(myDate, '%Y-%m-%d %I:%M%S.%f'))
At my last line I get exception:
File "c:\Python27\Lib\_strptime.py", line 322, in _strptime
found = format_regex.match(data_string)
What's wrong with this?
EDIT To be more clear, this is my entire code which is mix of what I find on stackoverflow:
import random
import time
def strTimeProp(start, end, format, prop):
"""Get a time at a proportion of a range of two formatted times.
start and end should be strings specifying times formated in the
given format (strftime-style), giving an interval [start, end].
prop specifies how a proportion of the interval to be taken after
start. The returned time will be in the specified format.
"""
stime = time.mktime(time.strptime(start, format))
etime = time.mktime(time.strptime(end, format))
ptime = stime + prop * (etime - stime)
return time.strftime(format, time.localtime(ptime))
def randomDate(start, end, prop):
return strTimeProp(start, end, '%Y-%m-%d %H:%M:%S.%f', prop)
b1=datetime.datetime.now()
print b1
startDate=b1-datetime.timedelta(27375)
print startDate
endDate=b1-datetime.timedelta(6571)
print endDate
randomDate=randomDate(str(startDate), str(endDate), random.random())
I'm trying to get random date of birth for adult.
I'm using Windows XP.
Upvotes: 1
Views: 3534
Reputation: 1216
Indeed, you'll hit the OverflowError
because you're going beyond the unix epoch. A simple solution would be to refactor your code to use datetime and timedelta objects, there you'll have no problem with date ranges. I did the refactoring myself and it seems to be working (Note that I'm not taking into consideration the subleties of timezones).
from datetime import datetime, timedelta
import random
def strTimeProp(start, end, format, prop):
"""Get a time at a proportion of a range of two formatted times.
start and end should be strings specifying times formated in the
given format (strftime-style), giving an interval [start, end].
prop specifies how a proportion of the interval to be taken after
start. The returned time will be in the specified format.
"""
sdatetime = datetime.strptime(start, format)
edatetime = datetime.strptime(end, format)
# get time delta and calculate new delta in days * prop
delta = edatetime - sdatetime # this is a timedelta
propdelta = timedelta(days = prop * delta.days)
pdatetime = sdatetime + propdelta
return pdatetime.strftime(format)
def randomDate(start, end, prop):
return strTimeProp(start, end, '%Y-%m-%d %H:%M:%S.%f', prop)
b1=datetime.now()
print b1
startDate=b1-timedelta(27375)
print startDate
endDate=b1-timedelta(6571)
print endDate
randomDate=randomDate(str(startDate), str(endDate), random.random())
print randomDate
Hope it helps.
Upvotes: 3
Reputation: 1159
The first argument to time.strptime()
needs to be a string, not a datetime
object you get back from datetime.datetime.now()
Change your last line to:
stime = time.mktime(time.strptime(str(myDate), '%Y-%m-%d %H:%M:%S.%f'))
(The code in your edit does convert the datetime object to a string though)
Upvotes: 0
Reputation: 212835
I'm trying to get random date of birth for adult.
OK, so if you say than an adult can be between 18 and 75 years old (6571 - 27375 days), then let's find a date that is so many days ago from today:
from datetime import datetime, timedelta
import random
birthday = datetime.today() - timedelta(days = random.randrange(6571, 27375))
print 'Person was born on %s' % (birthday.strftime('%Y-%m-%d'))
Upvotes: 4