Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'from:addr:yahoo.co.uk': 0.04; 'static': 0.04; 'string.': 0.05; 'differently': 0.07; 'mysql,': 0.07; 'table.': 0.07; 'string': 0.09; 'lawrence': 0.09; 'locale': 0.09; 'method,': 0.09; 'objects,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'separately': 0.09; 'subject:How': 0.10; 'python': 0.11; 'language.': 0.14; 'accepts': 0.16; 'csv': 0.16; 'directive': 0.16; 'fields:': 0.16; 'formatted': 0.16; 'igor': 0.16; 'inserting': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'language': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; '(but': 0.19; 'header:User-Agent:1': 0.23; 'file.': 0.24; 'looks': 0.24; 'question': 0.24; 'script': 0.25; 'extension': 0.26; 'right.': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'characters': 0.30; "i'm": 0.30; 'code': 0.31; 'file': 0.32; 'url:python': 0.33; 'implemented': 0.33; 'subject:the': 0.34; 'convert': 0.35; 'but': 0.35; 'there': 0.35; 'consist': 0.36; 'dates': 0.36; 'i.e.': 0.36; 'object,': 0.36; "didn't": 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'so,': 0.37; 'thank': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'according': 0.40; 'how': 0.40; 'url:3': 0.61; 'simple': 0.61; 'kind': 0.63; 'field': 0.63; 'our': 0.64; 'different': 0.65; 'six': 0.68; 'date,': 0.68; 'generated.': 0.68; 'reads': 0.68; 'therefore': 0.72; 'sole': 0.78; 'subject:check': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: How to check the date validity? Date: Tue, 24 Dec 2013 00:42:53 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-29-205.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 In-Reply-To: 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387845790 news.xs4all.nl 2914 [2001:888:2000:d::a6]:36800 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62661 On 24/12/2013 00:30, Igor Korot wrote: > Hi, ALL, > I'm working on the python script which reads the data from the csv file. > In this file I have 3 different kind of fields: one consist of the > sole date, one - sole time and one - datetime. The time includes > milliseconds, i.e. "12:55:55.705" > All fields of the file including those 3 I am reading as the string. > All those strings after validating will go into mySQL table. > > Now, it looks that the python way of validating the date/time of the > string is to call strptime(). However looking at the docs and trying > to use the function I didn't find a way to check for the milliseconds. > Now the dates can be formatted differently I think according to the > locale under which the csv is generated. > > So, my question is: since there is a simple way of inserting > preformatted string into the datetime field of mySQL, how do I > validate the date string? > I don't want to convert that string to datetime object, just want to > check if all those types of dates are good dates and does not contain > garbage. > > I tried to do an RE, but its static validation and so will not work in > all cases. > > Thank you for any hints. > You actually need the %f format code for milliseconds, not microseconds. Note 5 from http://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior "When used with the strptime() method, the %f directive accepts from one to six digits and zero pads on the right. %f is an extension to the set of format characters in the C standard (but implemented separately in datetime objects, and therefore always available)" -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence