use chrono::Duration;
use minijinja::{value::Value, Error, ErrorKind};
use std::time::SystemTime;
use super::values::UtcDateTimeValue;
use crate::events::UtcDateTime;
pub fn now() -> Value {
Value::from_object(UtcDateTimeValue::new(UtcDateTime::from(SystemTime::now())))
}
pub fn future(kwargs: Value) -> Result<Value, Error> {
let date = UtcDateTime::from(SystemTime::now());
let mut duration = Duration::zero();
if let Some(Ok(days)) = kwargs
.get_attr("days")
.ok()
.filter(|x| !x.is_undefined())
.and_then(|f| f.as_str().map(str::parse::<i64>))
{
duration = duration
.checked_add(&Duration::days(days))
.ok_or_else(|| Error::new(ErrorKind::InvalidOperation, "days couldn’t be added"))?;
}
if let Some(Ok(weeks)) = kwargs
.get_attr("weeks")
.ok()
.filter(|x| !x.is_undefined())
.and_then(|f| f.as_str().map(str::parse::<i64>))
{
duration = duration
.checked_add(&Duration::weeks(weeks))
.ok_or_else(|| Error::new(ErrorKind::InvalidOperation, "weeks couldn’t be added"))?;
}
if let Some(Ok(hours)) = kwargs
.get_attr("hours")
.ok()
.filter(|x| !x.is_undefined())
.and_then(|f| f.as_str().map(str::parse::<i64>))
{
duration = duration
.checked_add(&Duration::hours(hours))
.ok_or_else(|| Error::new(ErrorKind::InvalidOperation, "hours couldn’t be added"))?;
}
if let Some(Ok(minutes)) = kwargs
.get_attr("mins")
.ok()
.filter(|x| !x.is_undefined())
.and_then(|f| f.as_str().map(str::parse::<i64>))
{
duration = duration
.checked_add(&Duration::minutes(minutes))
.ok_or_else(|| Error::new(ErrorKind::InvalidOperation, "minutes couldn’t be added"))?;
}
if let Some(Ok(seconds)) = kwargs
.get_attr("secs")
.ok()
.filter(|x| !x.is_undefined())
.and_then(|f| f.as_str().map(str::parse::<i64>))
{
duration = duration
.checked_add(&Duration::seconds(seconds))
.ok_or_else(|| Error::new(ErrorKind::InvalidOperation, "seconds couldn’t be added"))?;
}
let val = Value::from_object(UtcDateTimeValue::new(date + duration));
Ok(val)
}