lib/command_duration: avoid relying on a specific locale #2271
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.
Description
This fixes #2206 (and a duplicate #2269) as described in comments #2269 (comment) and #2206 (comment).
Describe your changes in detail---The original code in the
master
branch used the localeen_US.UTF-8
to ensure that the decimal point ofEPOCHREALTIME
is the period. However, the specific localeen_US.UTF-8
is not ensured to exist in the system. More specifically, the localeen_US.UTF-8
is the locale for English used in the United States. In other countries and regions, the locale installed in the system is probably the region's local language, anden_US.UTF-8
is not installed.The standard solution in this kind of situation is to specify the locale
C
orPOSIX
. The localeC
is ensured by the C and POSIX standards to be available in any environment conforming to the standards, and all the systems supportC
to the best of my knowledge. The existence of the localePOSIX
is also ensured by the POSIX standard, but some environments do not support it (though most environments are supported). Also, there is essentially no difference from the localeC
, so there is no reason to use the localePOSIX
instead ofC
. With the localeC
, the decimal point of the number inEPOCHREALTIME
is ensured to be the period (ASCII\x2e
) as desired.I also added
local
to explicitly confine the scope of the temporary value ofLC_ALL
. Since the function_shell_duration_en
is currently only used in a subshell, it doesn't affect the caller's context. However, if this function is called normally, it would break the locale of the global context. It is generally a good practice to design a function so that it doesn't break the environment even with unexpected usage.Motivation and Context
Why is this change required?---This change is required to fix the locale error reported in #2206 and #2269. The locale error needs to be solved because it may cause unexpected behavior in the result of the measured command duration, and also the error message is simply annoying and confusing.
What problem does it solve?---This solves the problem of the locale error reported in #2206 and #2269.
If it fixes an open issue, please link to the issue here.---Fixes #2206 (and a duplicate #2269).
How Has This Been Tested?
Please describe in detail how you tested your changes.---I cannot directly reproduce the problem in my environment because I installed
en_US.UTF-8
in my environment, but I can instead emulate the issue by setting another locale, which is not present in my system. I confirmed that I can reproduce the error message by rewriting the line toLC_ALL='it_IT.UTF-8'
:Then, I changed it to
local LC_ALL=C
and confirmed that the error message disappears.Include details of your testing environment, and the tests you ran to---Here it is:
Screenshots (if appropriate):
Types of changes
Checklist:
clean_files.txt
and formatted it usinglint_clean_files.sh
.