Crates.io | astrolabe |
lib.rs | astrolabe |
version | |
source | src |
created_at | 2022-07-05 23:48:54.266297 |
updated_at | 2024-11-22 17:14:08.174949 |
description | Date and time library for Rust. Aims to be feature rich, lightweight and easy-to-use. |
homepage | https://github.com/giyomoon/astrolabe |
repository | https://github.com/giyomoon/astrolabe |
max_upload_size | |
id | 620124 |
Cargo.toml error: | TOML parse error at line 26, column 1 | 26 | 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` |
size | 0 |
Astrolabe is a date and time library for Rust which aims to be feature rich, lightweight (zero dependencies) and easy-to-use. It implements formatting, parsing and manipulating functions for date and time values.
serde
)sqlx-postgres
)A basic example which demonstrates creating, formatting and manipulating a DateTime
instance.
use astrolabe::{DateTime, TimeUtilities, Precision};
// Create a DateTime instance from year, month, and days (day of month)
let date_time = DateTime::from_ymd(2022, 5, 2).unwrap();
// Use the format function to freely format your DateTime instance
assert_eq!("2022/05/02", date_time.format("yyyy/MM/dd"));
// Create a new instance with a modified DateTime
// The previous instance is not modified and is still in scope
let modified_dt = date_time.add_hours(11).add_minutes(23);
assert_eq!("2022/05/02 11:23:00", modified_dt.format("yyyy/MM/dd HH:mm:ss"));
assert_eq!("2022-05-02T11:23:00Z", modified_dt.format_rfc3339(Precision::Seconds));
To see all implementations for the DateTime
struct, check out it's documentation.
Astrolabe can parse the timezone from /etc/localtime
to get the local UTC offset. This only works on UNIX systems.
use astrolabe::{DateTime, Offset, OffsetUtilities, Precision};
// Equivalent to `DateTime::now().set_offset(Offset::Local)`
let now = DateTime::now_local();
// Prints for example:
// 2023-10-08T08:30:00+02:00
println!("{}", now.format_rfc3339(Precision::Seconds));
assert_eq!(Offset::Local, now.get_offset());
See Offset
use astrolabe::CronSchedule;
// Every 5 minutes
let schedule = CronSchedule::parse("*/5 * * * *").unwrap();
for date in schedule.take(3) {
println!("{}", date);
}
// Prints for example:
// 2022-05-02 16:15:00
// 2022-05-02 16:20:00
// 2022-05-02 16:25:00
// Every weekday at 10:00
let schedule = CronSchedule::parse("0 10 * * Mon-Fri").unwrap();
for date in schedule.take(3) {
println!("{}", date.format("yyyy-MM-dd HH:mm:ss eeee"));
}
// Prints for example:
// 2022-05-03 10:00:00 Tuesday
// 2022-05-04 10:00:00 Wednesday
// 2022-05-05 10:00:00 Thursday
See CronSchedule
This crate uses the Rust 2021 Edition and requires at least version 1.60
. When using features that require third-party crates (serde
or sqlx
), please refer to their MSRV.
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.