dhall-lang / dhall-lang

Maintainable configuration files
https://dhall-lang.org
BSD 3-Clause "New" or "Revised" License
4.22k stars 174 forks source link

Year, Month, and Date extraction from a Date #1383

Open s-zeng opened 3 months ago

s-zeng commented 3 months ago

Hello, is it possible to have Date/year, Date/month, Date/date: Date -> Natural functions added to the standard? Usecase: well-typed way to have templated directories in the format .../YYYY/MM/DD/...

kukimik commented 2 months ago

You may have a look at https://github.com/kukimik/dhall-date-format as a possible workaround.

s-zeng commented 2 months ago

Wow, did not realize this was doable in pure Dhall. Their parse function was exactly what I was looking for Thanks!

s-zeng commented 2 months ago

Upon further inspections, that package (and Dhall in general) can only extract text components from a date. It would be nice to get integer components

kukimik commented 2 months ago

Upon further inspections, that package (and Dhall in general) can only extract text components from a date.

Yeah, that's why I called it a workaround. I thought it might be sufficient for your use case.

The proposed Date -> Natural functions should be easy to include in the standard and implement. And they would allow to create some utilities (e.g. for date comparison) at the library level.

On the other hand, I'm not sure whether the ability to inspect Date values could lead to some bad programming practices. As far as I understand this is the main reason for keeping Text opaque.

Gabriella439 commented 2 months ago

Yeah, I'd support adding functions like these to the standard. I'd also support the same thing for Time/{hour,minute,second}

kukimik commented 2 months ago

I can help with the Haskell implementation if somebody (@s-zeng ?) does the standardization work. It's rather easy, but somewhat tedious. Here are some recent examples to follow: https://github.com/dhall-lang/dhall-lang/commit/74233ca6af7485877c6c4fd230ba2dc897730b47, https://github.com/dhall-lang/dhall-lang/pull/1351.