ext-time

Crates.ioext-time
lib.rsext-time
version
sourcesrc
created_at2025-02-01 10:24:21.44248+00
updated_at2025-02-05 08:55:49.406649+00
descriptionA Rust library that extends the time crate with additional useful methods for time manipulation and formatting
homepage
repositoryhttps://github.com/rain2307/ext-time
max_upload_size
id1538336
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(rain2307)

documentation

https://docs.rs/ext-time

README

ext-time

A Rust library that extends the time crate with additional useful methods for time manipulation and formatting.

Features

  • Extension methods for Time type:

    • Format time as HH:MM (to_shorten)
    • Parse time from HH:MM string (from_str)
    • Duration calculation with cross-day handling (sub_ext)
    • Time range checks (is_between)
    • Minute-based operations (reset_minute, is_same_minute, add_minutes)
  • Extension methods for OffsetDateTime:

    • Timestamp conversions (milliseconds/seconds)
    • Timezone-aware formatting (display/Chinese format)
    • Parse from various formats (milliseconds, date-time string, simple format)
    • Minute-based operations
  • Serde support for OffsetDateTime:

    • Serialize to Unix timestamp
    • Deserialize from Unix timestamp

Installation

Add this to your Cargo.toml:

[dependencies]
ext-time = "0.1.0"

Usage

use ext_time::prelude::*;
use time::macros::time;

// Time operations
let t = time!(23:30);
assert_eq!(t.to_shorten(), "23:30");
assert!(t.is_between(time!(23:00), time!(0:00)));
let next = t.add_minutes(40); // Handles midnight wraparound

// OffsetDateTime with timezone
let dt = OffsetDateTime::now_with_offset(8); // UTC+8
println!("{}", dt.to_display_string(8)); // "2024-03-20 15:30:45.123+08:00"
println!("{}", dt.to_chinese_string()); // "2024年03月20日 15时30分45秒 +8"

// Parse from various formats
let dt = OffsetDateTime::from_milliseconds(1694696340500, 8)?;
let dt = OffsetDateTime::from_date_time("20240320", "15:30:45", 123, 8)?;
let dt = OffsetDateTime::from_simple("20240320_1530", 8)?;

// Serde support
#[derive(Serialize, Deserialize)]
struct Event {
    #[serde(serialize_with = "serde_t2ts", deserialize_with = "serde_parse_ts")]
    timestamp: OffsetDateTime,
}

Documentation

For detailed documentation and examples, please check the API documentation.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Commit count: 5

cargo fmt