Fix formatting strings
This commit is contained in:
@@ -34,8 +34,10 @@ use crate::{
|
||||
lazy_static! {
|
||||
pub static ref TIMEFROM_REGEX: Regex =
|
||||
Regex::new(r#"<<timefrom:(?P<time>\d+):(?P<format>.+)?>>"#).unwrap();
|
||||
pub static ref TIMENOW_REGEX: Regex =
|
||||
Regex::new(r#"<<timenow:(?P<timezone>(?:\w|/|_)+):(?P<format>.+)?>>"#).unwrap();
|
||||
pub static ref TIMENOW_REGEX: Regex = Regex::new(
|
||||
r#"<<timenow(?:(?P<sign>[+-])(?P<offset>\d+))?:(?P<timezone>(?:\w|/|_)+?):(?P<format>.+?)?>>"#
|
||||
)
|
||||
.unwrap();
|
||||
pub static ref LOG_TO_DATABASE: bool = env::var("LOG_TO_DATABASE").map_or(true, |v| v == "1");
|
||||
}
|
||||
|
||||
@@ -64,7 +66,7 @@ fn fmt_displacement(format: &str, seconds: u64) -> String {
|
||||
}
|
||||
|
||||
pub fn substitute(string: &str) -> String {
|
||||
let new = TIMEFROM_REGEX.replace(string, |caps: &Captures| {
|
||||
let new = TIMEFROM_REGEX.replace_all(string, |caps: &Captures| {
|
||||
let final_time = caps.name("time").map(|m| m.as_str().parse::<i64>().ok()).flatten();
|
||||
let format = caps.name("format").map(|m| m.as_str());
|
||||
|
||||
@@ -92,12 +94,26 @@ pub fn substitute(string: &str) -> String {
|
||||
});
|
||||
|
||||
TIMENOW_REGEX
|
||||
.replace(&new, |caps: &Captures| {
|
||||
.replace_all(&new, |caps: &Captures| {
|
||||
let timezone = caps.name("timezone").map(|m| m.as_str().parse::<Tz>().ok()).flatten();
|
||||
let format = caps.name("format").map(|m| m.as_str());
|
||||
let sign = caps.name("sign").map(|m| m.as_str());
|
||||
let offset = caps.name("offset").map(|m| m.as_str().parse::<i64>().ok()).flatten();
|
||||
|
||||
if let (Some(timezone), Some(format)) = (timezone, format) {
|
||||
let now = Utc::now().with_timezone(&timezone);
|
||||
let mut now = Utc::now().with_timezone(&timezone);
|
||||
if let (Some(sign), Some(offset)) = (sign, offset) {
|
||||
now = now
|
||||
.checked_add_signed(TimeDelta::seconds(
|
||||
offset * {
|
||||
match sign {
|
||||
"-" => -1,
|
||||
_ => 1,
|
||||
}
|
||||
},
|
||||
))
|
||||
.unwrap_or(now)
|
||||
}
|
||||
|
||||
now.format(format).to_string()
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user