fix: make formattime follow system locale (#1177)
closes #869 Co-authored-by: CrumblyLiquid <crumblyliquid@gmail.com>
This commit is contained in:
parent
057297b4a3
commit
1d3a186092
6 changed files with 33 additions and 4 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -485,6 +485,7 @@ dependencies = [
|
|||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"pure-rust-locales",
|
||||
"wasm-bindgen",
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
@ -975,6 +976,7 @@ dependencies = [
|
|||
name = "eww_shared_util"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"derive_more",
|
||||
"ref-cast",
|
||||
"serde",
|
||||
|
@ -2238,6 +2240,12 @@ dependencies = [
|
|||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pure-rust-locales"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1190fd18ae6ce9e137184f207593877e70f39b015040156b1e05081cdfe3733a"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.37"
|
||||
|
|
|
@ -12,3 +12,4 @@ homepage = "https://github.com/elkowar/eww"
|
|||
serde.workspace = true
|
||||
derive_more.workspace = true
|
||||
ref-cast.workspace = true
|
||||
chrono = { workspace = true, features = ["unstable-locales"] }
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
pub mod locale;
|
||||
pub mod span;
|
||||
pub mod wrappers;
|
||||
|
||||
pub use locale::*;
|
||||
pub use span::*;
|
||||
pub use wrappers::*;
|
||||
|
||||
|
|
14
crates/eww_shared_util/src/locale.rs
Normal file
14
crates/eww_shared_util/src/locale.rs
Normal file
|
@ -0,0 +1,14 @@
|
|||
use chrono::Locale;
|
||||
use std::env::var;
|
||||
|
||||
/// Returns the `Locale` enum based on the `LC_TIME` environment variable.
|
||||
/// 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,
|
||||
}
|
||||
}
|
|
@ -16,7 +16,7 @@ eww_shared_util.workspace = true
|
|||
|
||||
cached.workspace = true
|
||||
chrono-tz.workspace = true
|
||||
chrono.workspace = true
|
||||
chrono = { workspace = true, features = ["unstable-locales"] }
|
||||
itertools.workspace = true
|
||||
jaq-core.workspace = true
|
||||
jaq-parse.workspace = true
|
||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
|||
ast::{AccessType, BinOp, SimplExpr, UnaryOp},
|
||||
dynval::{ConversionError, DynVal},
|
||||
};
|
||||
use eww_shared_util::{Span, Spanned, VarName};
|
||||
use eww_shared_util::{get_locale, Span, Spanned, VarName};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
convert::{Infallible, TryFrom, TryInto},
|
||||
|
@ -467,12 +467,16 @@ fn call_expr_function(name: &str, args: Vec<DynVal>) -> Result<DynVal, EvalError
|
|||
};
|
||||
|
||||
Ok(DynVal::from(match timezone.timestamp_opt(timestamp.as_i64()?, 0) {
|
||||
LocalResult::Single(t) | LocalResult::Ambiguous(t, _) => t.format(&format.as_string()?).to_string(),
|
||||
LocalResult::Single(t) | LocalResult::Ambiguous(t, _) => {
|
||||
t.format_localized(&format.as_string()?, get_locale()).to_string()
|
||||
}
|
||||
LocalResult::None => return Err(EvalError::ChronoError("Invalid UNIX timestamp".to_string())),
|
||||
}))
|
||||
}
|
||||
[timestamp, format] => Ok(DynVal::from(match Local.timestamp_opt(timestamp.as_i64()?, 0) {
|
||||
LocalResult::Single(t) | LocalResult::Ambiguous(t, _) => t.format(&format.as_string()?).to_string(),
|
||||
LocalResult::Single(t) | LocalResult::Ambiguous(t, _) => {
|
||||
t.format_localized(&format.as_string()?, get_locale()).to_string()
|
||||
}
|
||||
LocalResult::None => return Err(EvalError::ChronoError("Invalid UNIX timestamp".to_string())),
|
||||
})),
|
||||
_ => Err(EvalError::WrongArgCount(name.to_string())),
|
||||
|
|
Loading…
Add table
Reference in a new issue