Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse_time(%M) fails to parse single digit minutes #1489

Open
twest820 opened this issue Apr 3, 2023 · 1 comment
Open

parse_time(%M) fails to parse single digit minutes #1489

twest820 opened this issue Apr 3, 2023 · 1 comment
Labels
bug an unexpected problem or unintended behavior datetime 📆

Comments

@twest820
Copy link

twest820 commented Apr 3, 2023

This was reported on StackOverflow in August 2021 or so. I just hit it with stringr 1.5.0 on

readr::parse_time("9:0:0.057", format = "%H:%M:%OS")

From what I can tell, the essential issue is DateTimeParser.h hard codes %M as

      case 'M': // minute
        if (!consumeInteger(2, &min_))
          return false;
        break;

rather than testing the width of the minutes field (as is done for unrestricted hours) or using more flexible parsing (as is done for seconds). This is a departure from, say, %M in lubridate::parse_date_time(), which accepts both the 0–59 and 00–59 formats

Since I don't have access to the source code generating the .csv files being parsed here changing the data format isn't an option. One option for a workaround is to read the times into character column and then convert with lubridate::hms(), ms(), hm(), or similar.

@hadley
Copy link
Member

hadley commented Jul 31, 2023

x <- "1:2:3.057"

readr::parse_time(x, format = "%H:%M:%OS")
#> Warning: 1 parsing failure.
#> row col            expected    actual
#>   1  -- time like %H:%M:%OS 1:2:3.057
#> NA
strptime(x, format = "%H:%M:%OS")
#> [1] "2023-07-31 01:02:03 CDT"

Created on 2023-07-31 with reprex v2.0.2

@hadley hadley added bug an unexpected problem or unintended behavior datetime 📆 labels Jul 31, 2023
@hadley hadley changed the title parse_time(%M) returns NA for single digit minutes parse_time(%M) fails to parse single digit minutes Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior datetime 📆
Projects
None yet
Development

No branches or pull requests

2 participants