10 个版本 (5 个破坏性版本)
0.5.2 | 2024年4月12日 |
---|---|
0.5.1 | 2023年10月9日 |
0.4.0 | 2023年5月18日 |
0.3.0 | 2023年4月11日 |
0.0.2 | 2022年7月13日 |
#51 在 日期和时间
每月下载量 77
290KB
6K SLoC
天球仪
Rust 的日期/时间库。功能丰富、轻量级且易于使用。
概述
天球仪是一个为 Rust 设计的日期/时间库,旨在功能丰富、轻量级(无依赖)且易于使用。它实现了日期和时间值的格式化、解析和操作功能。
特性
- 格式化 和 解析 基于基于 Unicode 日期字段符号 的格式字符串
- RFC 3339 时间戳解析和格式化
- 操作 函数用于添加、减去、设置和清除日期单位
- Cron 表达式解析器
- 时区 偏移量
- UNIX 平台上的 本地 时区
- 零 依赖
- Serde 序列化和反序列化(带有功能标志
serde
)
示例
基本
一个基本的示例,演示创建、格式化和操作 DateTime
实例。
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));
要查看 DateTime
结构体的所有实现,请参阅其 文档。
本地时区(仅限 UNIX 系统)
天球仪可以解析 /etc/localtime
中的时区以获取本地 UTC 偏移量。这仅在 UNIX 系统上有效。
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());
见 Offset
CRON 解析
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
MSRV
此 crate 使用 Rust 2021 版本,并要求至少版本 1.56
。
许可
根据以下之一许可
- Apache License,版本 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT许可(LICENSE-MIT 或 http://opensource.org/licenses/MIT)
由您选择。
贡献
除非您明确说明,否则根据Apache-2.0许可定义的,您有意提交以包含在作品中的任何贡献,将按照上述方式双授权,无需任何额外条款或条件。
依赖项
~165KB