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

[BUG] %P gets stripped from localize formats for Date objects #639

Closed
movermeyer opened this issue Oct 7, 2022 · 1 comment · Fixed by #640
Closed

[BUG] %P gets stripped from localize formats for Date objects #639

movermeyer opened this issue Oct 7, 2022 · 1 comment · Fixed by #640

Comments

@movermeyer
Copy link
Contributor

movermeyer commented Oct 7, 2022

What I tried to do

(byebug) date = Date.new(2018, 12, 26)
Wed, 26 Dec 2018
(byebug) date.to_datetime.iso8601
"2018-12-26T00:00:00+00:00"
(byebug) I18n.locale = :en
:en
(byebug) I18n.l(date, format: :friendly_date_time)
en:
  date:
    formats:
      friendly_date_time: "%b %-d, %Y, %-l:%M %P"

What I expected to happen

"Dec 26, 2018, 12:00 am"

What actually happened

"Dec 26, 2018, 12:00 "

The resulting datetime is 12 hours off, since without the Meridian indicator (i.e., am/pm), it can only be reasonably interpreted as a 24-hour clock datetime.

Versions of i18n, rails, and anything else you think is necessary

  • i18n: v1.12.0
  • Ruby: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21]

Explanation

I18::Backend::Base#translate_localization_format strips %p/%P unless object.respond_to?(:hour). Date#hour is a private method, so object.respond_to?(:hour) is false, despite Date#strftime supporting the %p/%P fields:

date.strftime("%b %-d, %Y, %-l:%M %P")
"Dec 26, 2018, 12:00 am"

This conditional was added in this commit (2008-06-17).

@radar
Copy link
Collaborator

radar commented Nov 13, 2022

What an obscure catch this is :)

Thank you very much for taking the time to investigate the issue and submit a fix as well.

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

Successfully merging a pull request may close this issue.

2 participants