Rewrites dates written in the format a dataset declares as yyyy-mm-dd, yyyy-mm or yyyy. A value is never guessed at: one that does not match the declared format is returned unchanged, so dataset_test() reports it rather than the build storing a guess. Values that are already standard pass through untouched, and a range of years written with a hyphen, 2019-2020, becomes 2019/2020.

util_format_collection_date(x, format = NULL, timezone = NULL)

Arguments

x

Vector of dates as read from data.csv: character, numeric, Date or POSIXct

format

Character vector of the formats the values are written in, tried in turn for each value: any of "ymd", "dmy", "mdy", "ym", "my" and "y". The order sets the precision kept, so a column holding full dates, month-year values and bare years is declared c("dmy", "my", "y") – or dmy my y in metadata.yml. Formats that could read the same value two ways – "dmy" with "mdy", say – cannot be combined. "ym" needs a four-digit year, since 15-Mar is how Excel writes the 15th of March with no year.

"auto" works the format out from the whole column: it tries dmy my y, mdy my y and ymd ym y, and uses the one that reads every value any of them can. If more than one does and they give different dates – a column whose values could all be day-first or month-first, such as 4/07/2022 – it converts nothing, so the curator declares which. It decides for the column, never value by value, so it cannot read one value day-first and the next month-first.

Within a value, day, month and year may be separated by -, /, ., , or spaces, or not at all (20190206). Months may be numbers or English names, full or abbreviated, and a leading weekday name is ignored (Thr Nov 18 2010, as LI-6400 files write it). A two-digit year is taken as the most recent year ending in those digits that is not in the future, so 08 is 2008 and 78 is 1978. A time of day following the date is dropped.

timezone

Time zone to take the date in, for values that are a point in time – a POSIXct column, or text ending in Z or a UTC offset – as an Olson name such as "Australia/Sydney". Without it those values are returned unchanged: 2022-06-03T14:00:00Z is 3 June in UTC but 4 June in Sydney, and only the dataset's curator knows which is meant.

Value

Character vector the length of x

Details

The build calls this on the collection_date column(s) of data.csv for a dataset that sets collection_date_format or collection_date_timezone in its metadata.yml, after custom_R_code has run. It can also be called from custom_R_code, for a dataset that needs to convert dates before combining them – for example, to paste a start and an end column into a range.

Examples

util_format_collection_date(c("2-Sep-08", "15/10/2008", "Oct 2008", "2008"), c("dmy", "my", "y"))
#> [1] "2008-09-02" "2008-10-15" "2008-10"    "2008"      
util_format_collection_date("2022-06-03T14:00:00Z", timezone = "Australia/Sydney")
#> [1] "2022-06-04"