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",
|
"iana-time-zone",
|
||||||
"js-sys",
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"pure-rust-locales",
|
||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
"windows-targets 0.52.6",
|
"windows-targets 0.52.6",
|
||||||
]
|
]
|
||||||
|
@ -975,6 +976,7 @@ dependencies = [
|
||||||
name = "eww_shared_util"
|
name = "eww_shared_util"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"chrono",
|
||||||
"derive_more",
|
"derive_more",
|
||||||
"ref-cast",
|
"ref-cast",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -2238,6 +2240,12 @@ dependencies = [
|
||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pure-rust-locales"
|
||||||
|
version = "0.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1190fd18ae6ce9e137184f207593877e70f39b015040156b1e05081cdfe3733a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.37"
|
version = "1.0.37"
|
||||||
|
|
|
@ -12,3 +12,4 @@ homepage = "https://github.com/elkowar/eww"
|
||||||
serde.workspace = true
|
serde.workspace = true
|
||||||
derive_more.workspace = true
|
derive_more.workspace = true
|
||||||
ref-cast.workspace = true
|
ref-cast.workspace = true
|
||||||
|
chrono = { workspace = true, features = ["unstable-locales"] }
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
|
pub mod locale;
|
||||||
pub mod span;
|
pub mod span;
|
||||||
pub mod wrappers;
|
pub mod wrappers;
|
||||||
|
|
||||||
|
pub use locale::*;
|
||||||
pub use span::*;
|
pub use span::*;
|
||||||
pub use wrappers::*;
|
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
|
cached.workspace = true
|
||||||
chrono-tz.workspace = true
|
chrono-tz.workspace = true
|
||||||
chrono.workspace = true
|
chrono = { workspace = true, features = ["unstable-locales"] }
|
||||||
itertools.workspace = true
|
itertools.workspace = true
|
||||||
jaq-core.workspace = true
|
jaq-core.workspace = true
|
||||||
jaq-parse.workspace = true
|
jaq-parse.workspace = true
|
||||||
|
|
|
@ -7,7 +7,7 @@ use crate::{
|
||||||
ast::{AccessType, BinOp, SimplExpr, UnaryOp},
|
ast::{AccessType, BinOp, SimplExpr, UnaryOp},
|
||||||
dynval::{ConversionError, DynVal},
|
dynval::{ConversionError, DynVal},
|
||||||
};
|
};
|
||||||
use eww_shared_util::{Span, Spanned, VarName};
|
use eww_shared_util::{get_locale, Span, Spanned, VarName};
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
convert::{Infallible, TryFrom, TryInto},
|
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) {
|
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())),
|
LocalResult::None => return Err(EvalError::ChronoError("Invalid UNIX timestamp".to_string())),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
[timestamp, format] => Ok(DynVal::from(match Local.timestamp_opt(timestamp.as_i64()?, 0) {
|
[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())),
|
LocalResult::None => return Err(EvalError::ChronoError("Invalid UNIX timestamp".to_string())),
|
||||||
})),
|
})),
|
||||||
_ => Err(EvalError::WrongArgCount(name.to_string())),
|
_ => Err(EvalError::WrongArgCount(name.to_string())),
|
||||||
|
|
Loading…
Add table
Reference in a new issue