1

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.

5
  • 1
    are you trying to get number seconds since the epoch? Commented Feb 2, 2011 at 13:16
  • @user: I beg your pardon? So what are you trying to do? Commented Feb 2, 2011 at 13:19
  • Sorry, I don't understand your question I think :) When I put old date like 1936-02-21 14:16:18.936000 I get mktime argument out of range. I read that it related to epoch, but my english is to weak to understand it :/ Commented Feb 2, 2011 at 13:23
  • What are you trying to do? What's the aim of your code? What is your desired output? use google translate, if you need to. There isn't any point recasting this conversation. Commented Feb 2, 2011 at 13:24
  • 1
    'Seconds since the Epoch' means the number of seconds since '1970-01-01 00:00:00 UTC'. That's why it does not work for 1936. So what should stime represent? Commented Feb 2, 2011 at 13:24

3 Answers 3

4

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'))    
Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.