strptime

A faster strptime?

落爺英雄遲暮 提交于 2019-11-27 03:18:59
问题 I have code which reads vast numbers of dates in 'YYYY-MM-DD' format. Parsing all these dates, so that it can add one, two, or three days then write back in the same format is slowing things down quite considerably. 3214657 14.330 0.000 103.698 0.000 trade.py:56(effective) 3218418 34.757 0.000 66.155 0.000 _strptime.py:295(_strptime) day = datetime.datetime.strptime(endofdaydate, "%Y-%m-%d").date() Any suggestions how to speed it up a bit (or a lot)? 回答1: Is factor 7 lot enough? datetime

Date time conversion and extract only time

大城市里の小女人 提交于 2019-11-27 02:38:34
问题 Want to change the class for Time to POSIXlt and extract only the hours minutes and seconds str(df3$Time) chr [1:2075259] "17:24:00" "17:25:00" "17:26:00" "17:27:00" ... Used the strptime function df33$Time <- strptime(df3$Time, format = "%H:%M:%S") This gives the date/time appended > str(df3$Time) POSIXlt[1:2075259], format: "2015-08-07 17:24:00" "2015-08-07 17:25:00" "2015-08-07 17:26:00" ... Wanted to extract just the time without changing the POSIXlt class. using the strftime function df3

How to remove unconverted data from a Python datetime object

感情迁移 提交于 2019-11-27 02:32:34
问题 I have a database of mostly correct datetimes but a few are broke like so: Sat Dec 22 12:34:08 PST 20102015 Without the invalid year, this was working for me: end_date = soup('tr')[4].contents[1].renderContents() end_date = time.strptime(end_date,"%a %b %d %H:%M:%S %Z %Y") end_date = datetime.fromtimestamp(time.mktime(end_date)) But once I hit an object with a invalid year I get ValueError: unconverted data remains: 2 , which is great but im not sure how best to strip the bad characters out

How to format date string via multiple formats in python

故事扮演 提交于 2019-11-27 01:40:43
I have three date formats: YYYY-MM-DD , DD.MM.YYYY , DD/MM/YYYY . Is it possible to validate and parse strings such as 2014-05-18 or 18.5.2014 or 18/05/2019 ? Try each format and see if it works: from datetime import datetime def try_parsing_date(text): for fmt in ('%Y-%m-%d', '%d.%m.%Y', '%d/%m/%Y'): try: return datetime.strptime(text, fmt) except ValueError: pass raise ValueError('no valid date format found') >>> import dateutil.parser >>> dateutil.parser.parse(date_string) This should take care of most of the standard date formats in Python 2.7+. If you really have super custom date formats

Converting a string to a formatted date-time string using Python

时光怂恿深爱的人放手 提交于 2019-11-26 19:59:14
问题 I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)" >>>import time >>>s = time.strptime("20091229050936", "%Y%m%d%H%M%S") >>>print s.strftime('%H:%M %d %B %Y (UTC)') gives AttributeError: 'time.struct_time' object has no attribute 'strftime' Clearly, I've made a mistake: time is wrong, it's a datetime object! It's got a date and a time component! >>>import datetime >>>s = datetime.strptime("20091229050936", "%Y%m%d%H%M%S") gives AttributeError: 'module' object

How to parse milliseconds?

三世轮回 提交于 2019-11-26 15:17:45
How do I use strptime or any other functions to parse time stamps with milliseconds in R? time[1] # [1] "2010-01-15 13:55:23.975" strptime(time[1], format="%Y-%m-%d %H:%M:%S.%f") # [1] NA strptime(time[1], format="%Y-%m-%d %H:%M:%S") # [1] "2010-01-15 13:55:23"` Courtesy of the ?strptime help file (with the example changed to your value): z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS") z # prints without fractional seconds op <- options(digits.secs=3) z options(op) #reset options You can also use strptime(time[1], "%OSn") where 0 <= n <= 6, without having to set digits.secs . 来源

Parsing datetime strings containing nanoseconds

不羁的心 提交于 2019-11-26 13:50:55
I have some log files with times in the format HH:MM::SS.nano_seconds (e.g. 01:02:03.123456789). I would like to create a datetime in python so I can neatly do math on the time (e.g. take time differences). strptime works well for microseconds using %f. Do the Python datetime and time modules really not support nanoseconds? Dougal You can see from the source that datetime objects don't support anything more fine than microseconds. As pointed out by Mike Pennington in the comments, this is because actual hardware clocks aren't nearly that precise . Wikipedia says that HPET has frequency "at

How to format date string via multiple formats in python

随声附和 提交于 2019-11-26 09:39:16
问题 I have three date formats: YYYY-MM-DD , DD.MM.YYYY , DD/MM/YYYY . Is it possible to validate and parse strings such as 2014-05-18 or 18.5.2014 or 18/05/2019 ? 回答1: Try each format and see if it works: from datetime import datetime def try_parsing_date(text): for fmt in ('%Y-%m-%d', '%d.%m.%Y', '%d/%m/%Y'): try: return datetime.strptime(text, fmt) except ValueError: pass raise ValueError('no valid date format found') 回答2: >>> import dateutil.parser >>> dateutil.parser.parse(date_string) This

How can I account for period (AM/PM) with datetime.strptime?

丶灬走出姿态 提交于 2019-11-26 06:30:00
问题 Specifically I have code that simplifies to this: from datetime import datetime date_string = \'2009-11-29 03:17 PM\' format = \'%Y-%m-%d %H:%M %p\' my_date = datetime.strptime(date_string, format) # This prints \'2009-11-29 03:17 AM\' print my_date.strftime(format) What gives? Does Python just ignore the period specifier when parsing dates or am I doing something stupid? 回答1: The Python time.strftime docs say: When used with the strptime() function, the %p directive only affects the output

How to parse milliseconds?

人盡茶涼 提交于 2019-11-26 04:20:02
问题 How do I use strptime or any other functions to parse time stamps with milliseconds in R? time[1] # [1] \"2010-01-15 13:55:23.975\" strptime(time[1], format=\"%Y-%m-%d %H:%M:%S.%f\") # [1] NA strptime(time[1], format=\"%Y-%m-%d %H:%M:%S\") # [1] \"2010-01-15 13:55:23\"` 回答1: Courtesy of the ?strptime help file (with the example changed to your value): z <- strptime("2010-01-15 13:55:23.975", "%Y-%m-%d %H:%M:%OS") z # prints without fractional seconds op <- options(digits.secs=3) z options(op)