use super::layout::into_node;
use crate::render::Render;
use crate::screen::{Component, Node};
use crate::time::DateTime;
use serde_json::{Value as Json, json};
into_node!(RelativeTime);
pub struct RelativeTime {
at: DateTime,
}
impl RelativeTime {
pub fn new(at: DateTime) -> Self {
RelativeTime { at }
}
}
impl Component for RelativeTime {
fn render(&self, out: &mut String, r: &Render) {
let exact = crate::render::escape(&self.at.format(r.tz, r.locale));
out.push_str(&format!(
r#"<time class="z-relative" datetime="{}" title="{exact}" data-zeyn-relative>{exact}</time>"#,
self.at
));
}
fn schema(&self) -> Json {
json!({ "type": "relative_time", "at": self.at.to_string() })
}
}