rsimple_date 0.1.0

PHP-style date() formatting for Rust — use Y-m-d H:i:s instead of strftime
Documentation
  • Coverage
  • 100%
    18 out of 18 items documented6 out of 6 items with examples
  • Size
  • Source code size: 47.3 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 182.2 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • praburaj0605/rsimple_date
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • praburaj0605

rsimple_date

PHP-style date() formatting for Rust.

If you know date("Y-m-d H:i:s") from PHP, you can use the same format tokens in Rust — no need to learn Chrono’s strftime-style %Y-%m-%d %H:%M:%S for everyday formatting.

Chrono is used internally and is not part of this crate’s public API.

Quick start

[dependencies]
rsimple_date = "0.1"
use rsimple_date::{date, date_with, gmdate, gmdate_with, time, DATE_ATOM};

fn main() {
    // Current local time
    println!("{}", date("Y-m-d H:i:s"));

    // Escaped literals (same as PHP)
    println!("{}", date(r"l jS \of F Y h:i:s A"));

    // Fixed Unix timestamp in UTC
    println!("{}", gmdate_with("Y-m-d H:i:s", 978307200)); // 2001-01-01 00:00:00

    // Predefined PHP constants
    println!("{}", gmdate(DATE_ATOM));

    println!("{}", time());
}

API

Function PHP analog Description
date(format) date($format) Format current local time
date_with(format, ts) date($format, $ts) Format Unix timestamp in local TZ
gmdate(format) gmdate($format) Format current UTC time
gmdate_with(format, ts) gmdate($format, $ts) Format Unix timestamp in UTC
time() time() Current Unix timestamp (seconds)

Also exports PHP-style constants such as DATE_ATOM, DATE_RFC2822, DATE_RSS, …

Format tokens

Supports the common PHP DateTimeInterface::format characters, including:

  • Date: Y y m n d j F M D l N w z W t L o S
  • Time: H G h g i s a A B u v
  • TZ: e I O P p T Z (best-effort without a TZ database)
  • Full: c r U
  • Escape: \X prints literal X

Unrecognized characters are copied through as literals (PHP behavior).

Limitations

  • Chrono is an internal dependency; you do not import or learn Chrono to use this crate.
  • Offset tokens (O, P, p, Z) reflect the real UTC offset.
  • Without an IANA timezone database, e / T are best-effort ("UTC" or an offset string), and I (DST) is always "0".
  • Extremely out-of-range Unix timestamps are clamped to the Unix epoch so formatting never panics.

License

Licensed under either of

  • Apache License, Version 2.0
  • MIT license

at your option.

Links