strptime: a Beginner's Mistake
I’m using jq to process CSV files to use in a Plain Text Accounting environment, specifically using hledger. I mention all this, not because it’s salient, but purely to make it easier for you to find this article by a web search.
The Symptom
$ jq -n '"8/4/2023" | strptime("%D")'
jq: error (at <unknown>): date "8/4/2023" does not match format "%D"
$ jq -n '"8/4/2023" | strptime("%m/%d/%y")'
jq: error (at <unknown>): date "8/4/2023" does not match format "%m/%d/%y"
WAT?!
The Problem
The strptime pattern %y matches the string 23, but not the string 2023. It’s pretty clear in the man page, if a little unexpected.
%y The year within century (0–99). When a century is not otherwise specified, values in the range 69–99 refer to years in the
twentieth century (1969–1999); values in the range 00–68 refer to years in the twenty-first century (2000–2068).
%Y The year, including century (for example, 1991).
The Solution
$ jq -n '"8/4/2023" | strptime("%m/%d/%Y")'
[
2023,
7,
4,
0,
0,
0,
5,
215
]