xat 0.3.4

An ease of use library for xattrs
Documentation
use chrono::{DateTime, FixedOffset, Local, Utc};
use serde_json::Value;

use crate::Errors;

// DateTime stuff
pub(crate) fn now2rfc3339() -> Value {
    let now: DateTime<FixedOffset> = Local::now().into();
    dt2rfc3339(now)
}

pub(crate) fn dt2rfc3339(dt: DateTime<FixedOffset>) -> Value {
    Value::String(dt.to_rfc3339_opts(chrono::SecondsFormat::Secs, true))
}

pub(crate) fn now2unix_ts() -> Value {
    let now: DateTime<FixedOffset> = Utc::now().into();
    dt2unix_ts(now)
}

pub(crate) fn dt2unix_ts(dt: DateTime<FixedOffset>) -> Value {
    Value::Number(dt.timestamp().into())
}

pub(crate) fn dt_from_value(value: Value) -> Result<DateTime<FixedOffset>, Errors> {
    let dt = match value {
        Value::Number(n) => DateTime::from_timestamp(n.as_i64().unwrap(), 0).unwrap(),
        Value::String(s) => DateTime::parse_from_rfc3339(&s)?.into(),
        _ => {
            return Err(Errors::Generic(
                "Could not parse json value to datetime".to_string(),
            ))
        }
    };
    Ok(dt.into())
}