Don't construct a locale when formatting year_month_day - #4940
Open
tiagomacarios wants to merge 1 commit into
Open
Don't construct a locale when formatting year_month_day#4940tiagomacarios wants to merge 1 commit into
tiagomacarios wants to merge 1 commit into
Conversation
formatter<year_month_day>::format passes true to get_locale, so every default format constructs a std::locale, and tm_writer then compares it against the classic locale (is_classic_(loc_ == get_classic_locale())). The only thing written is on_iso_date(), which is pure arithmetic and reads neither loc_ nor is_classic_. It is the odd one out among its siblings: formatter<day> and formatter<year> pass false; formatter<weekday> and formatter<month> pass their localized() flag. Only year_month_day hardcodes true, and its parse() never sets localized in the first place. With FMT_USE_LOCALE=0 this costs more than a copy: the loc.get<std::locale>() arm of get_locale is compiled out, so it default-constructs instead, capturing the current global locale in a build that asked for no locale support. No output change: chrono_test.year_month_day already sets a non-classic global locale and expects "2024-01-01". Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
formatter<year_month_day>::formatpassestruetoget_locale, so every default format constructs astd::locale, andtm_writerthen compares it against the classic locale (is_classic_(loc_ == get_classic_locale())). The only thing written ison_iso_date(), which is pure arithmetic and reads neitherloc_noris_classic_.It is the odd one out among its siblings:
formatter<day>andformatter<year>passfalse;formatter<weekday>andformatter<month>pass theirlocalized()flag. Onlyyear_month_dayhardcodestrue, and itsparse()never sets localized in the first place.With
FMT_USE_LOCALE=0this costs more than a copy: theloc.get<std::locale>()arm ofget_localeis compiled out, so it default-constructs instead, which captures the current global locale in a build that asked for no locale support.No output change:
chrono_test.year_month_dayalready sets a non-classic global locale and expects"2024-01-01". Verified 33/33 chrono tests pass (MSVC 19.51,-DCMAKE_CXX_STANDARD=20).