Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'exception': 0.03; 'string.': 0.04; 'method.': 0.05; 'mrab': 0.05; 'exit': 0.07; 'formatting': 0.07; 'thats': 0.07; 'try:': 0.07; 'be:': 0.09; "object's": 0.09; 'object;': 0.09; 'parsed': 0.09; 'raised.': 0.09; 'subject:skip:a 10': 0.09; 'result.': 0.15; "%y'": 0.16; "'%d": 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'string': 0.17; 'wrote:': 0.17; 'string,': 0.17; 'import': 0.21; 'thanks.': 0.21; 'defined': 0.22; 'task': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'object,': 0.27; "doesn't": 0.28; 'crash': 0.29; 'convert': 0.29; 'this.': 0.29; 'received:84': 0.32; 'correctly.': 0.33; 'date.': 0.33; 'to:addr:python-list': 0.33; 'entered': 0.34; 'wrong': 0.34; 'but': 0.36; 'method': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'store': 0.38; 'object': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'telling': 0.61; 'show': 0.63; 'date,': 0.65; 'price': 0.66; 'header:Reply-To:1': 0.68; '8bit%:100': 0.70; '8bit%:92': 0.70; 'reply-to:no real name:2**0': 0.72; '2013': 0.84; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=_vX2mLoUpDQA:10 a=qvbgYuf3kYAA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=doxkKeqih_sA:10 a=VuphAFSSuFV-zTGYJ9gA:9 a=QEXdDO2ut3YA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 21 Feb 2013 21:46:34 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Checking for valid date input and convert appropriately References: <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361483381 news.xs4all.nl 6950 [2001:888:2000:d::a6]:54874 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39465 On 2013-02-21 21:22, Ferrous Cranus wrote: > Τη Πέμπτη, 21 Φεβρουαρίου 2013 10:14:13 μ.μ. UTC+2, ο χρήστης MRAB έγραψε: >> On 2013-02-21 19:38, Ferrous Cranus wrote: >> > import datetime from datetime >> >> Should be: >> >> from datetime import datetime >> >> > >> > try: >> > datetime.strptime( date, '%d %m %Y' ) >> >> That parses the date and discards the result. >> >> > date = date.strptime( '%Y-%m-%d' ) >> >> 'date' is a string, it doesn't have a 'strptime' method. >> >> When you parsed the date you got a datetime object; _that_ has the >> 'strptime' method. Correction: the datetime object has the 'strftime' for formatting the date. > > I don't need to store the date i just want to make sure is entered correctly. > I would like to have the user type the date like > > 21 02 2013 > and then convert it to 2013-02-21 because thats how mysql wants the date in order to store it. > Please show me how to write this. > The 'strptime' method parses the string and returns a datetime object, and you can then use that object's 'strftime' method to format it to the desired string. > Also, can you show me how to write it that if so even if the user entered date is wrong it doesn't just crash the cgi-script?i know i can use tr: expect: but i want to avoid it, somehow i need to check the date in the same if statemnt like i do with the other user defined varibles for validity. > It _doesn't_ crash the cgi-script. You're telling it to exit if an exception is raised. If you don't want the cgi-script to exit, don't call 'exit'. > if( task and ( price and price.isdigit() and price.__len__() <= 3 ) and ( date and datetime.strptime(date, '%Y-%m-%d') ) ): > > Thanks. >