use crate::describe::Describe;
use chrono::{Local, TimeZone, Utc};
use anyhow::Result;
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
pub struct Runic<'runic> {
pub script: &'runic str,
pub timestamp: Option<i64>,
pub offset: Option<i32>,
}
impl<'runic> Runic<'runic> {
pub fn new(script: &'runic str) -> Self {
let timestamp = Self::compute_timestamp();
Self {
script,
timestamp: Some(timestamp),
offset: Some(Self::compute_offset(timestamp)),
}
}
pub fn timestamp(&mut self, timestamp: i64) -> &mut Self {
self.timestamp = Some(timestamp);
self
}
pub fn offset(&mut self, offset: i32) -> &mut Self {
self.offset = Some(offset);
self
}
pub fn describe(&self) -> Result<u64> {
let timestamp = self.timestamp.unwrap_or_else(|| Self::compute_timestamp());
let offset = self
.offset
.unwrap_or_else(|| Self::compute_offset(timestamp));
Describe::with(self.script, timestamp, offset)
}
#[inline]
fn compute_timestamp() -> i64 {
Utc::now().timestamp()
}
#[inline]
fn compute_offset(base: i64) -> i32 {
Local.timestamp(base, 0).offset().local_minus_utc()
}
}