#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    2
    Rep Power
    0

    Problem with time.strptime()


    So I'm trying to use strptime() to strip the day of the month out of a string, but stptime() apparently doesn't like the date "February 29" and gives a Value Error, even though it is clearly (sometimes) valid. Is there any way I can prevent this? Thanks.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You have to pass strptime() the format of the time along with time itself; since you didn't post any code I can't tell you this for sure but it seems like the format you're giving could be wrong.

    Anyway, with such a simple format why not just use split() or a slice to remove the day?

    Code:
    >>> someTime = 'February 29'
    >>> someTime[:-3]
    'February'
    >>> splitTime = someTime.split()
    >>> splitTime[0]
    'February'
    >>> month, day = someTime.split()
    >>> month
    'February'
    >>>
    Mark .
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Is there any way I can prevent this?
    Only feed it "February 29" in leap years?

    Code:
    >>> time.strptime("2000 February 29", "%Y %B %d")
    (2000, 2, 29, 0, 0, 0, 1, 60, -1)
    >>>
    >>> time.strptime("2001 February 29", "%Y %B %d")
    Traceback (most recent call last):
      File "<interactive input>", line 1, in ?
      File "C:\Python23\lib\_strptime.py", line 513, in strptime
        julian = datetime_date(year, month, day).toordinal() - \
    ValueError: day is out of range for month
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    2
    Rep Power
    0
    Originally Posted by sfb
    Only feed it "February 29" in leap years?
    Yeah, that's what I just did, and it works. (I wasn't giving it a year to parse.) Seems slightly clumsy, but it works. Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo