-
-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: get_locale follows posix standard (#1264)
* fix: get_locale follows posix standard * refactor: more concise and legible get_locale function Co-authored-by: Wölfchen <[email protected]> --------- Co-authored-by: Wölfchen <[email protected]>
- Loading branch information
Showing
2 changed files
with
7 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
use chrono::Locale; | ||
use std::env::var; | ||
|
||
/// Returns the `Locale` enum based on the `LC_TIME` environment variable. | ||
/// Returns the `Locale` enum based on the `LC_ALL`, `LC_TIME`, and `LANG` environment variables in | ||
/// that order, which is the precedence order prescribed by Section 8.2 of POSIX.1-2017. | ||
/// If the environment variable is not defined or is malformed use the POSIX locale. | ||
pub fn get_locale() -> Locale { | ||
let locale_string: String = | ||
var("LC_TIME").map_or_else(|_| "C".to_string(), |v| v.split(".").next().unwrap_or("C").to_string()); | ||
|
||
match (&*locale_string).try_into() { | ||
Ok(x) => x, | ||
Err(_) => Locale::POSIX, | ||
} | ||
var("LC_ALL") | ||
.or_else(|_| var("LC_TIME")) | ||
.or_else(|_| var("LANG")) | ||
.map_or(Locale::POSIX, |v| v.split('.').next().and_then(|x| x.try_into().ok()).unwrap_or_default()) | ||
} |