Skip to content

Commit

Permalink
fix: make formattime follow system locale (elkowar#1177)
Browse files Browse the repository at this point in the history
closes elkowar#869

Co-authored-by: CrumblyLiquid <[email protected]>
  • Loading branch information
w-lfchen and CrumblyLiquid authored Aug 28, 2024
1 parent 057297b commit 1d3a186
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 4 deletions.
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/eww_shared_util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 2 additions & 0 deletions crates/eww_shared_util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
pub mod locale;
pub mod span;
pub mod wrappers;

pub use locale::*;
pub use span::*;
pub use wrappers::*;

Expand Down
14 changes: 14 additions & 0 deletions crates/eww_shared_util/src/locale.rs
Original file line number Diff line number Diff line change
@@ -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,
}
}
2 changes: 1 addition & 1 deletion crates/simplexpr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions crates/simplexpr/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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())),
Expand Down

0 comments on commit 1d3a186

Please sign in to comment.