Best-effort conversion of raw date values into the format dataset_test()'s collection_date-parses check requires: a year (yyyy), a year and month (yyyy-mm), or a full date (yyyy-mm-dd), singly or as a start/end range. Meant to be called from a dataset's custom_R_code, before collection_date is otherwise read in, e.g.:

util_parse_collection_date(x, order = NULL, excel_origin = "1899-12-30")

Arguments

x

A character, Date, or POSIXct vector of raw date values – the whole column at once, not one value at a time, since resolving a plain numeric date's order depends on seeing the rest of x

order

Optional, "dmy" or "mdy": the curator's own statement of which way round an all-numeric date is written, for when nothing in the column can settle it – e.g. a single 4/07/2022, or two-digit dotted dates like 18.3.12 (d.m.yy). It replaces the column-wide inference, but a value that settles its own order (e.g. 10/23/2021, since no month is 23) is still read that way. Converting a custom_R_code that called lubridate::dmy() or lubridate::mdy() is then a like-for-like swap

excel_origin

Origin date for numeric Excel serial dates. The default (1899-12-30) is correct for a workbook built on Windows Excel; one built on old Mac Excel needs 1904-01-01 instead

Value

Character vector the same length as x. Values already valid, or safely resolved to yyyy, yyyy-mm or yyyy-mm-dd (singly or as a /-delimited range), are returned in that form; anything left ambiguous or unrecognised is returned completely unchanged

Details

custom_R_code: 'data %>% mutate(Date = util_parse_collection_date(Date))'

What this deliberately will not touch: a bare numeric date like 01/02/2008, seen in isolation, is genuinely ambiguous – day-first or month-first? – and guessing wrong silently corrupts the data worse than not guessing at all. Two things narrow that down first, though: (1) the value's own two leading numbers – 10/23/2021 can only be month-first, since no month is 23 – and only when that alone doesn't settle it, (2) the rest of the column, on the view that a single source normally writes dates one way (e.g. 9/14/2020 elsewhere rules out month 14, settling an otherwise-ambiguous 8/11/2020). Real data can still mix conventions within one column, though – e.g. automated timestamps in month-first order alongside hand-entered dates in day-first order – so (1) is checked per value, never overridden by (2), and if the column as a whole disagrees with itself, (2) is not applied at all rather than picking a side. Only once neither of those applies does a value come back unchanged. Run dataset_test() again afterwards: whatever it still flags under "Some date values are not parsing" needs a human decision, not a better regex.

What it does handle:

  • Values already valid are passed through untouched

  • A named month removes the day/month ambiguity regardless of which order it was written in, so 2-Sep-08, September 2, 2008, 2 Sep 2008 and similar all resolve safely

  • A month name with no day (Sep 2008) resolves to the yyyy-mm resolution, not a fabricated day-1 date

  • A plain numeric d/m/y-shaped date, resolved per-value or from the rest of the column as described above

  • mm-yyyy (month first, the reverse of the schema's yyyy-mm) – unambiguous on its own, since a 4-digit year can't be mistaken for anything else

  • A dot-separated yy.mm.dd/yyyy.mm.dd, confirmed (not assumed) by checking the middle component is a plausible month and the last a plausible day. A two-digit dotted date written day-first instead (18.3.12) can't be told apart from that by its values, so it needs order = "dmy"

  • A dot-separated d.m.yyyy (21.11.2019), resolved like the / and - numeric dates above

  • A compact yyyymmdd (20190206)

  • A span of years written with a dash (2019-2020 becomes 2019/2020)

  • A comma- or semicolon-separated list of dates (Jan-98, Feb-98, Mar-98), collapsed to the first/last range it covers (1998-01/1998-03) – tried only once the value has failed to parse whole, so a comma inside one date (September 2, 2008) is never split on

  • Excel's numeric serial dates (e.g. 39692), distinguished from a bare 4-digit year by digit count. A number that is really R's own day count (left behind when ifelse() strips a Date column's class) looks exactly like one and would be read decades too early – pass the raw column to this function instead of converting it with lubridate first

  • A trailing time-of-day is dropped, both "yyyy-mm-ddTHH:MM:SSZ" (ISO 8601) and "d/m/y HH:MM" (e.g. a Google Forms timestamp column) – collection_date has no way to record a time, only the date

  • A start/end range is split on / and each side parsed independently, unless the whole value already looks like a plain d/m/y-shaped date (in which case / is a date separator, not a range separator, and the rules above apply to the whole thing)

  • Date/POSIXct columns (already parsed by read_csv()'s own type guessing before custom_R_code runs) are formatted directly, dropping any time-of-day component

Examples

util_parse_collection_date(c(
  "2-Sep-08", "September 2, 2008", "Sep 2008", "39692",
  "2008-09-02/2008-09-03", "01/02/2008", "2020", NA
))
#> [1] "2008-09-02"            "2008-09-02"            "2008-09"              
#> [4] "2008-09-01"            "2008-09-02/2008-09-03" "01/02/2008"           
#> [7] "2020"                  NA                     

# the whole column is used to resolve a plain numeric date's order:
# `9/14/2020` can only be month-first, so `8/11/2020` is read the same way
util_parse_collection_date(c("8/11/2020 10:09", "9/14/2020 12:06"))
#> [1] "2020-08-11" "2020-09-14"

# nothing in a lone `4/07/2022` says which way round it is, so say so:
util_parse_collection_date("4/07/2022", order = "dmy")
#> [1] "2022-07-04"