strptime

Get date from week number

喜夏-厌秋 提交于 2019-11-26 03:28:59
问题 Please what\'s wrong with my code: import datetime d = \"2013-W26\" r = datetime.datetime.strptime(d, \"%Y-W%W\") print(r) Display \"2013-01-01 00:00:00\", Thanks. 回答1: A week number is not enough to generate a date; you need a day of the week as well. Add a default: import datetime d = "2013-W26" r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w") print(r) The -1 and -%w pattern tells the parser to pick the Monday in that week. This outputs: 2013-07-01 00:00:00 %W uses Monday as the first

What are the “standard unambiguous date” formats for string-to-date conversion in R?

我们两清 提交于 2019-11-26 02:07:36
问题 Please consider the following $ R --vanilla > as.Date(\"01 Jan 2000\") Error in charToDate(x) : character string is not in a standard unambiguous format But that date clearly is in a standard unambiguous format. Why the error message? Worse, an ambiguous date is apparently accepted without warning or error and then read incorrectly! > as.Date(\"01/01/2000\") [1] \"0001-01-20\" I\'ve searched and found 28 other questions in the [R] tag containing this error message. All with solutions and

Parsing datetime strings containing nanoseconds

那年仲夏 提交于 2019-11-26 02:01:52
问题 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? 回答1: 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

strptime, as.POSIXct and as.Date return unexpected NA

时间秒杀一切 提交于 2019-11-25 22:45:23
问题 When I try to parse a timestamp in the following format: \"Thu Nov 8 15:41:45 2012\", only NA is returned. I am using Mac OS X, R 2.15.2 and Rstudio 0.97.237. The language of my OS is Dutch: I presume this has something to do with it. When I try strptime , NA is returned: var <- \"Thu Nov 8 15:41:45 2012\" strptime(var, \"%a %b %d %H:%M:%S %Y\") # [1] NA Neither does as.POSIXct work: as.POSIXct(var, \"%a %b %d %H:%M:%S %Y\") # [1] NA I also tried as.Date on the string above but without %H:%M: