Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 8th, 2004, 06:44 PM
soultaco soultaco is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 soultaco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old December 8th, 2004, 07:09 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old December 8th, 2004, 07:12 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
Quote:
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

Reply With Quote
  #4  
Old December 8th, 2004, 07:18 PM
soultaco soultaco is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 2 soultaco User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Problem with time.strptime()


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT