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

Custom Formats Are Not Working #128

Closed
scarrick68 opened this issue Oct 31, 2022 · 4 comments
Closed

Custom Formats Are Not Working #128

scarrick68 opened this issue Oct 31, 2022 · 4 comments

Comments

@scarrick68
Copy link

scarrick68 commented Oct 31, 2022

I am trying to set a default DateTime format for my app, so I did this in a JS file and included it into my application.js

import LocalTime from "local-time"

LocalTime.config.i18n["en"]["date"]["formats"]["default"] = "%b %e, %Y"
LocalTime.config.i18n["en"]["datetime"]["formats"]["default"] = "%b %e, %Y"
LocalTime.config.i18n["en"]["time"]["formats"]["default"] = "%b %e, %Y"

LocalTime.start()

I printed in the console and see the updated default format, but all of my DateTimes still show in the original default format. I am only interested in DateTime. I set the default on all of them just in case I was passing an unexpected type.

I called local_time in the view like this with :my_format being added to the formats instead of overriding default:

<%= local_time(@my_date_time) %>

<%= local_time(@my_date_time, format: :my_format) %>
<%= local_time(@my_date_time, :my_format) %>
@Annedj
Copy link

Annedj commented Nov 21, 2022

HI @scarrick68, if that helps, we did it by passing the format like this:

local_time(@date_time, :time_with_tz)

and specifying the format in our config/locales/en.yml file as such:

en:
  time:
    formats:
      time_with_tz: "%H:%Mh %Z"

@josefarias
Copy link
Contributor

Ah I see what happened. Sorry this is confusing, but rails helpers (like #local_time) will call a different default format than the one specified in LocalTime.config.i18n.

See line 55 here:

def find_time_format(format)
if format.is_a?(Symbol)
if (i18n_format = I18n.t("time.formats.#{format}", default: [:"date.formats.#{format}", ''])).present?
i18n_format
elsif (date_format = Time::DATE_FORMATS[format] || Date::DATE_FORMATS[format])
date_format.is_a?(Proc) ? LocalTime.default_time_format : date_format
else
LocalTime.default_time_format
end
else
format.presence || LocalTime.default_time_format
end
end

Changing this is not trivial. Perhaps the action item here is adding a note to the readme saying that default values shouldn't be changed as it won't behave as one might expect. I'll look into it.

@josefarias
Copy link
Contributor

Actually, I'm going to reopen so we remember to document this somehow

@josefarias josefarias reopened this Jun 24, 2023
@josefarias
Copy link
Contributor

Done in 6294194

For the time being, I'd only recommend using the i18n configuration after understanding which helpers use our RelativeTime module behind the scenes. Not specifying a format will not result in grabbing the i18n config's default format — that's different to the mattr accessors defined on the Ruby side here:

mattr_accessor(:default_time_format) { "%B %e, %Y %l:%M%P" }
mattr_accessor(:default_date_format) { "%B %e, %Y" }

"default" is admittedly a confusing choice of name here. Might revisit later.

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

3 participants