From 537e1360bb26f02e70b26b63ff10bc738b7fd8fe Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sat, 15 Aug 2020 18:11:23 +0200 Subject: [PATCH] Add offset parameter to date extension. Fix #311 --- src/extension/date.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/extension/date.rs b/src/extension/date.rs index 918a1e9..98aa62b 100644 --- a/src/extension/date.rs +++ b/src/extension/date.rs @@ -1,7 +1,7 @@ /* * This file is part of espanso. * - * Copyright (C) 2019 Federico Terzi + * Copyright (C) 2019-2020 Federico Terzi * * espanso is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ * along with espanso. If not, see . */ -use chrono::{DateTime, Local}; +use chrono::{DateTime, Local, Duration}; use serde_yaml::{Mapping, Value}; use std::collections::HashMap; use crate::extension::ExtensionResult; @@ -36,7 +36,15 @@ impl super::Extension for DateExtension { } fn calculate(&self, params: &Mapping, _: &Vec, _: &HashMap) -> Option { - let now: DateTime = Local::now(); + let mut now: DateTime = Local::now(); + + // Compute the given offset + let offset = params.get(&Value::from("offset")); + if let Some(offset) = offset { + let seconds = offset.as_i64().unwrap_or_else(|| { 0 }); + let offset = Duration::seconds(seconds); + now = now + offset; + } let format = params.get(&Value::from("format"));