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

12hour format is wrong for 12:00 hour #79

Closed
dbp opened this issue Apr 10, 2024 · 8 comments
Closed

12hour format is wrong for 12:00 hour #79

dbp opened this issue Apr 10, 2024 · 8 comments

Comments

@dbp
Copy link

dbp commented Apr 10, 2024

For the 12:00 - 13:00 hour (in 24hr time), if you use ~format:"{12hour:X}", it renders as 0, which is wrong: it should be 12. I'm assuming this is due to subtracting 12 from the hour, which is correct for 13 and up, but not for 12 (or, obviously, below).

@darrenldl
Copy link
Contributor

darrenldl commented Apr 10, 2024

Ah yeah, I never really used that part a lot and forgot to test the logic.

The code right now is:

         let hour = if hour = 0 then 12 else hour mod 12 in

which is obviously wrong now that I look at it again.

An am pm option is also missing from the pretty printing system - I'll add that too.

@dbp
Copy link
Author

dbp commented Apr 10, 2024

That would be great! Right now I have string concatenation with a manual check for the hour to add AM/PM :)

@darrenldl
Copy link
Contributor

Does

         let hour = if hour = 0 || hour = 12 then 12 else hour mod 12 in

look more right?

I'll have to add test cases for this obviously, but I might as well ask while you're here : v

@dbp
Copy link
Author

dbp commented Apr 10, 2024 via email

@darrenldl
Copy link
Contributor

Added the 12hour fix (using the standard conversion you suggested), and also am/pm support:

utop # Timedesc.to_string ~format:"{12hour: X}" (Timedesc.now ());;
- : string = " 7"
─( 08:02:58 )─< command 3 >────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Timedesc.to_string ~format:"{12hour: X} {am/pm:XX}" (Timedesc.now ());;
- : string = " 7 AM"
─( 08:03:02 )─< command 4 >────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Timedesc.to_string ~format:"{12hour: X} {am/pm:xX}" (Timedesc.now ());;
- : string = " 7 aM"
─( 08:03:13 )─< command 5 >────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Timedesc.to_string ~format:"{12hour: X} {am/pm:xx}" (Timedesc.now ());;
- : string = " 7 am"
─( 08:03:16 )─< command 6 >────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Timedesc.to_string ~format:"{12hour: X} {am/pm:Xx}" (Timedesc.now ());;
- : string = " 7 Am"
─( 08:03:20 )─< command 7 >────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Timedesc.to_string ~format:"{12hour:0X} {am/pm:Xx}" (Timedesc.now ());;
- : string = "07 Am"
─( 08:03:23 )─< command 8 >────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Timedesc.to_string ~format:"{12hour:X} {am/pm:Xx}" (Timedesc.now ());;
- : string = "7 Am"
─( 08:03:28 )─< command 9 >────────────────────────────────────────────────────────────────{ counter: 0 }─
utop # Timedesc.to_iso8601 (Timedesc.now ());;
- : string = "2024-04-12T07:04:18.021737098Z"
─( 08:03:45 )─< command 10 >───────────────────────────────────────────────────────────────{ counter: 0 }─

Feel free to try pinning to main branch and see if it resolves your issues. Otherwise you can also wait till I've added the appropriate tests and published on opam.

@darrenldl
Copy link
Contributor

Added an additional {am/pm:x.x.} format string, and added a bulk of the tests wanted: the test suite now covers {year} up to {12hour} and {am/pm} format strings currently). Will add the remaining test cases then a release can be made.

@darrenldl
Copy link
Contributor

Submitted PR at opam repository: ocaml/opam-repository#25754

Will close this issue when PR is merged.

@darrenldl
Copy link
Contributor

PR merged, closing issue.

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

No branches or pull requests

2 participants