JsonTime Package - Rust Implementation

Crate: giztoy-jsontime

📚 Rust Documentation

Types

Unix

#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Unix(DateTime<Utc>);
}

A timestamp that serializes to/from Unix seconds in JSON.

Methods:

MethodSignatureDescription
newfn new(dt: DateTime<Utc>) -> SelfCreate from DateTime
nowfn now() -> SelfCurrent time
from_secsfn from_secs(secs: i64) -> SelfCreate from seconds
as_secsfn as_secs(&self) -> i64Get seconds value
datetimefn datetime(&self) -> DateTime<Utc>Get underlying DateTime
beforefn before(&self, other: &Self) -> boolIs this before other?
afterfn after(&self, other: &Self) -> boolIs this after other?
is_zerofn is_zero(&self) -> boolIs zero time?
subfn sub(&self, other: &Self) -> DurationDuration between times
addfn add(&self, d: Duration) -> SelfReturn self+d

Trait Implementations:

  • Serialize / Deserialize (serde)
  • Display
  • From<DateTime<Utc>>, From<i64>
  • PartialOrd, Ord, Hash

Milli

#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Milli(DateTime<Utc>);
}

A timestamp that serializes to/from Unix milliseconds in JSON.

Methods: Same as Unix, but with milliseconds.

MethodSignatureDescription
from_millisfn from_millis(ms: i64) -> SelfCreate from milliseconds
as_millisfn as_millis(&self) -> i64Get milliseconds value
...(same operations as Unix)

Duration

#![allow(unused)]
fn main() {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Duration(StdDuration);
}

A duration that serializes to string (e.g., "1h30m") and deserializes from string or nanoseconds.

Methods:

MethodSignatureDescription
newfn new(d: StdDuration) -> SelfCreate from std Duration
from_secsfn from_secs(secs: u64) -> SelfCreate from seconds
from_millisfn from_millis(ms: u64) -> SelfCreate from milliseconds
from_nanosfn from_nanos(nanos: u64) -> SelfCreate from nanoseconds
as_stdfn as_std(&self) -> StdDurationGet std Duration
as_secsfn as_secs(&self) -> u64Get whole seconds
as_secs_f64fn as_secs_f64(&self) -> f64Get floating seconds
as_millisfn as_millis(&self) -> u128Get milliseconds
as_nanosfn as_nanos(&self) -> u128Get nanoseconds
is_zerofn is_zero(&self) -> boolIs zero duration?

Usage

In Struct Fields

#![allow(unused)]
fn main() {
use giztoy_jsontime::{Unix, Milli, Duration};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Event {
    id: String,
    created_at: Unix,
    expires_at: Milli,
    timeout: Duration,
}

let event = Event {
    id: "evt-123".to_string(),
    created_at: Unix::now(),
    expires_at: Milli::now(),
    timeout: Duration::from_secs(30),
};

// Serializes to:
// {"id":"evt-123","created_at":1705315800,"expires_at":1705315800000,"timeout":"30s"}
}

Duration Parsing

#![allow(unused)]
fn main() {
use giztoy_jsontime::Duration;

// String format
let d: Duration = serde_json::from_str(r#""1h30m""#).unwrap();
assert_eq!(d.as_secs(), 5400);

// Integer format (nanoseconds)
let d: Duration = serde_json::from_str("5400000000000").unwrap();
assert_eq!(d.as_secs(), 5400);
}

Time Arithmetic

#![allow(unused)]
fn main() {
use giztoy_jsontime::Unix;
use std::time::Duration;

let now = Unix::now();
let later = now.add(Duration::from_secs(86400));

if later.after(&now) {
    let diff = later.sub(&now);
    println!("{:?}", diff);  // 86400s
}
}

From Conversions

#![allow(unused)]
fn main() {
// From i64
let unix = Unix::from(1705315800i64);
let milli = Milli::from(1705315800000i64);

// From DateTime<Utc>
let unix = Unix::from(Utc::now());

// From std::time::Duration
let dur = Duration::from(std::time::Duration::from_secs(60));
}

Duration String Format

The parser supports Go-style duration strings:

InputParsed As
"1h"3600 seconds
"30m"1800 seconds
"45s"45 seconds
"1h30m"5400 seconds
"1h30m45s"5445 seconds
""0 seconds

Dependencies

  • chrono crate (for DateTime handling)
  • serde crate (for serialization)

Differences from Go

AspectGoRust
Time typeType alias time.TimeNewtype over DateTime<Utc>
Duration rangeSigned (int64 ns)Unsigned (u64 + u32 ns)
OrderingVia method callsVia Ord trait
Hash supportN/AImplemented
sub() returnSigned durationUnsigned duration