Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Tom P Newsgroups: comp.lang.python Subject: Re: problem with dateutil Date: Sun, 14 Feb 2016 13:26:00 +0100 Lines: 51 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net Si4AtQlaN6HJ5FmRm3qB6w3bPCmWX5N8G9TQD3tk0IcqjmQe8= Cancel-Lock: sha1:GwNZMNSwunXln9lpXLUeEwqGicM= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 In-Reply-To: Xref: csiph.com comp.lang.python:102912 On 02/13/2016 09:45 PM, Gary Herron wrote: > On 02/13/2016 12:27 PM, Tom P wrote: >> On 02/13/2016 07:13 PM, Gary Herron wrote: >>> On 02/13/2016 09:58 AM, Tom P wrote: >>>> I am writing a program that has to deal with various date/time formats >>>> and convert these into timestamps. It looks as if >>>> dateutil.parser.parse should be able to handle about any format, but >>>> what I get is: >>>> >>>> datetimestr = '2012-10-22 11:22:33' >>>> print(dateutil.parser.parse(datetimestr)) >>>> result: datetime.datetime(2012, 10, 22, 11, 22, 33) >>>> >>>> However: >>>> datetimestr = '2012:10:22 11:22:33' >>>> print(dateutil.parser.parse(datetimestr)) >>>> result: datetime.datetime(2016, 2, 13, 11, 22, 33) >>>> >>>> In other words, it's getting the date wrong when colons are used to >>>> separate YYYY:MM:DD. Is there a way to include this as a valid format? >>>> >>> >>> Yes, there is a way to specify your own format. Search the datetime >>> documentation for >>> datetime.strptime(date_string, format) >>> >>> Gary Herron >>> >> >> Thanks. I started out with datetime.strptime but AFAICS that means I >> have to go through try/except for every conceivable format. Are you >> saying that I can't use dateutil.parser? > > Well now... If by "every conceivable format" you are including formats > that the author of dateutil.parser did not conceive of, then of course > you cannot use dateutil.parser. But you have the code for > dateutil.parser -- perhaps you could modify it to accept whatever odd > formats you care about. > > Gary Herron > > I had a look at the code for dateutil.parser. Have you looked at it? Meanwhile I'm living with try: dt = datetime.datetime.strptime(datetimestr, '%Y:%m:%d %H:%M:%S') except ValueError: dt = dateutil.parser.parse(datetimestr) unixtime = time.mktime(dt.timetuple())