parttime icon indicating copy to clipboard operation
parttime copied to clipboard

Parsing/formatting ISO 8601 years less than zero or greater than 9999

Open trevorld opened this issue 3 years ago • 2 comments

Not entirely sure if you should care about such years but I noticed while playing around that {parttime} doesn't always correctly parse/format ISO 8601 years less than zero or greater than 9999

According to Wikipedia years less than zero or greater than 9999 should be at least four digits wide and prependend with either a "+" or a "-" e.g.

+12345
-12345
-0004

I do observe that negative numbers strictly less than -999 seem to be formatted correctly.

trevorld avatar Dec 21 '22 21:12 trevorld

Thanks for reporting. I'm a bit embarrassed to say that it never crossed my mind to test this.

Seems like it would be a reasonably straightforward fix in the regex used for ISO-8601 parsing to allow years >4 digits.

- "(?<year>[\\+-]?\\d{4}(?!\\d{2}\\b))",
+ "(?<year>[\\+-]?\\d{4,}(?!\\d{2}\\b))",

dgkf avatar Dec 21 '22 22:12 dgkf

Just documenting another artifact of this behavior, printing of negative parttime years uses the - as part of the four digits. Not strictly incorrect, but makes it harder to round-trip the data as a string:

as.parttime("-0003")
## <partial_time<YMDhms+tz>[1]> 
## [1] "-003" 

dgkf avatar Jan 03 '23 19:01 dgkf