syd 3.58.0

rock-solid application kernel
Documentation
//
// Syd: rock-solid application kernel
// src/utils/syd-utc.rs: Print UTC date and time in JSON format
//
// Copyright (c) 2025, 2026 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0

use std::{
    io::{stdout, Write},
    process::ExitCode,
};

use nix::errno::Errno;
use serde_json::json;
use syd::{
    log::{now, Tm},
    printfln,
};
// Set global allocator to GrapheneOS allocator.
#[cfg(all(
    not(target_os = "android"),
    not(target_arch = "loongarch64"),
    not(target_arch = "riscv64"),
    target_page_size_4k,
    target_pointer_width = "64"
))]
#[global_allocator]
static GLOBAL: hardened_malloc::HardenedMalloc = hardened_malloc::HardenedMalloc;

syd::main! {
    syd::set_sigpipe_dfl()?;

    if std::env::args().len() != 1 {
        help()?;
        return Ok(ExitCode::FAILURE);
    }

    let ts: i64 = now().try_into()?;
    let tm: Tm = ts.try_into()?;
    #[expect(clippy::disallowed_methods, clippy::disallowed_types)]
    let dt = json!({
        "year": tm.year(),
        "month": tm.month(),
        "day": tm.day(),
        "hour": tm.hour(),
        "minute": tm.minute(),
        "second": tm.second(),
        "dt": tm.to_string(),
        "ts": ts,
        "tz": "UTC",
    });

    let mut dt = serde_json::to_string(&dt).or(Err(Errno::EINVAL))?;
    dt.push('\n');
    stdout().write_all(dt.as_bytes())?;

    Ok(ExitCode::SUCCESS)
}

fn help() -> Result<(), Errno> {
    printfln!("Usage: syd-utc")?;
    printfln!("Print UTC date and time in JSON format.")?;
    Ok(())
}