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

Parsing a day of month not zero padded #1355

Open
ajfisher opened this issue Aug 17, 2023 · 5 comments
Open

Parsing a day of month not zero padded #1355

ajfisher opened this issue Aug 17, 2023 · 5 comments
Assignees

Comments

@ajfisher
Copy link

I'm trying to parse a date and reformat it into something a little bit more workable. Unfortunately I can't change the source system this gets exported from.

Example dates:

1/07/2022
22/10/2022

Day of month is the first value.

I'm using this strptime to parse the date: t = strptime($created_date, "%d/%m/%Y")

For 22/10/2022 this parses fine and then I can then use t in strftime to output the format I need. Unfortunately 1/07/2022 doesn't work and results in (error). I understand the reason as the library providing strptime expects the %d to be a zero-padded value (as it also does for %m - and which happens to be true in this case).

What I'd like to know though is whether there's a way to be able to fix my date so that strptime might be able to interpret it?

@aborruso
Copy link
Contributor

I'm adding a note, not useful for you. This works using Miller 5.

echo x="1/07/2022" | mlr --ojson put '$y=strptime($x,"%d/%m/%Y")'

@ajfisher
Copy link
Author

I did think my way around the problem in the end but required a bit of a lateral approach:

put 'd = leftpad($created_date, 10, "0");
 t = strptime(d, "%d/%m/%Y");
 $ftm = strftime(t, "%F");'

Specifically handles the fact my dodgy dates are 1 character shorter than the rest and then leftpad it with a 0 when it is...

Not elegant but definitely does the job for the use case I outlined.

@aborruso
Copy link
Contributor

Dear @johnkerl is this a bug?

If I run this in Miller 6

echo x="1/07/2022" | mlr --ojson put '$y=strptime($x,"%d/%m/%Y")'

I have

[
{
  "x": "1/07/2022",
  "y": (error)
}
]

@johnkerl
Copy link
Owner

johnkerl commented Aug 27, 2023

@aborruso strptime in Go is brittle, and is based on Go's date-parsing which I find also brittle. I'll look into how I can hack on strptime (which I have done before).

@ajesler-hatch
Copy link

I just hit this issue where both month and day could be single digits, and worked around it with gsub. Posting here in case this is useful to anyone else reading this thread.

# Find any single digits surrounded by non-word characters and prefix them with a 0
echo x="1/5/2023" | mlr --ojson put '$y=strptime(gsub($x, "\b(\d)\b", "0\1"),"%d/%m/%Y")'

gives

[
{
  "x": "1/5/2023",
  "y": 1682899200
}
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants