Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.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; 'case.': 0.05; 'method.': 0.05; 'mrab': 0.05; 'pretend': 0.07; 'thats': 0.07; 'try:': 0.07; 'accepts': 0.09; 'be:': 0.09; 'input,': 0.09; 'object;': 0.09; 'parsed': 0.09; 'subject:skip:a 10': 0.09; 'result.': 0.15; "%y'": 0.16; "'%d": 0.16; 'personally,': 0.16; 're.search(': 0.16; 'received:your-server.de': 0.16; 'wrote:': 0.17; 'string,': 0.17; 'thu,': 0.17; 'variables': 0.17; 'feb': 0.19; 'import': 0.21; 'defined': 0.22; 'posted': 0.22; "i'd": 0.22; 'task': 0.23; 'script': 0.24; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; "doesn't": 0.28; 'post': 0.28; '+0100,': 0.29; 'cgi': 0.29; 'crash': 0.29; 'convert': 0.29; 'this.': 0.29; 'that.': 0.30; 'fri,': 0.30; 'correctly.': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'entered': 0.34; 'wrong': 0.34; 'michael': 0.36; 'but': 0.36; 'should': 0.36; 'subject:: ': 0.38; 'store': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'end': 0.40; 'think': 0.40; 'your': 0.60; '8bit%:95': 0.61; 'show': 0.63; 'within': 0.64; 'become': 0.65; 'date,': 0.65; 'price': 0.66; '8bit%:100': 0.70; '8bit%:92': 0.70; 'now:': 0.71; '2013': 0.84 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: python-list@python.org Subject: Re: Checking for valid date input and convert appropriately References: <92536c34-cf00-4497-b646-ab79fa4ee062@googlegroups.com> <5bac38bb-df2e-47b8-96dd-2b1c090959e4@googlegroups.com> Date: Fri, 22 Feb 2013 00:37:10 +0100 MIME-Version: 1.0 Content-Transfer-Encoding: Quoted-Printable From: "Michael Ross" In-Reply-To: <5bac38bb-df2e-47b8-96dd-2b1c090959e4@googlegroups.com> User-Agent: Opera Mail/12.14 (Win32) X-Authenticated-Sender: gmx@ross.cx X-Virus-Scanned: Clear (ClamAV 0.97.5/16716/Thu Feb 21 22:52:30 2013) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 188 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361489840 news.xs4all.nl 6974 [2001:888:2000:d::a6]:51722 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39492 On Fri, 22 Feb 2013 00:08:01 +0100, Ferrous Cranus = wrote: > =CE=A4=CE=B7 =CE=A0=CE=B1=CF=81=CE=B1=CF=83=CE=BA=CE=B5=CF=85=CE=AE, 2= 2 =CE=A6=CE=B5=CE=B2=CF=81=CE=BF=CF=85=CE=B1=CF=81=CE=AF=CE=BF=CF=85 201= 3 12:03:59 =CF=80.=CE=BC. UTC+2, =CE=BF =CF=87=CF=81=CE=AE=CF=83=CF=84=CE= =B7=CF=82 Michael = > Ross =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: >> On Thu, 21 Feb 2013 22:22:15 +0100, Ferrous Cranus = >> >> >> wrote: >> >> >> >> > =CE=A4=CE=B7 =CE=A0=CE=AD=CE=BC=CF=80=CF=84=CE=B7, 21 =CE=A6=CE=B5=CE= =B2=CF=81=CE=BF=CF=85=CE=B1=CF=81=CE=AF=CE=BF=CF=85 2013 10:14:13 =CE=BC= .=CE=BC. UTC+2, =CE=BF =CF=87=CF=81=CE=AE=CF=83=CF=84=CE=B7=CF=82 MRAB >> >> > =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: >> >> >> 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 =3D 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. >> >> > >> >> > 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. >> >> > >> >> > 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 varib= les >> >> > for validity. >> >> > >> >> >> >> You *have* to try/expect in order to not have the script crash. >> >> Think user typo: 21 02 2=C3=9F13 >> >> >> >> Personally, I'd use a javascript on the html so users can't POST inva= lid >> >> dates. >> >> I use mootools for that. It accepts 21/02/2013, 21-02-2013 and 21.02.= 13 = >> as >> >> input, >> >> and you end up with 21.02.2013 posted to your cgi in any case. > > i just want to check for date validity from within ha same if statemnt= = > like i check the other variables and i tried a regualr expression just= = > now: > > if( task and ( price and price.isdigit() and price.__len__() <=3D 3 ) = and = > ( date and re.search( r'(\d+) (\d+) (\d+)', date ) ) ): > > Do i ahve somehting wrong in it? > > will the if become true if the user enters 21 02 2013 ? I pretend not to know, try it out? And try with invalid inputs, too: 211 02 2013 21 99 2013 31 02 2013