2个版本
使用旧的Rust 2015
0.1.1 | 2017年1月21日 |
---|---|
0.1.0 | 2017年1月21日 |
#497 in 日期和时间
24 每月下载量
在 persian-tools 中使用
23KB
449 代码行
Rust 波斯日历
Rust 波斯日历 v0.1.1 提供波斯(太阳历)和公历之间的转换功能。所有转换都使用儒略历来作为接口。crate名称为 ptime
,它与crate time 兼容。此源代码根据MIT许可证授权,许可证文件可在LICENSE文件中找到。
安装
将 ptime = "0.1"
添加到 dependencies
部分的 Cargo.toml
[dependencies]
time = "0.1"
ptime = "0.1"
入门
1- 导入crate ptime
。大部分情况下,您还需要导入 time
crate。
extern crate ptime;
extern crate time;
2- 将公历转换为波斯历。
let p_tm = ptime::from_gregorian_date(2016, 2, 21).unwrap();
assert_eq!(p_tm.tm_year, 1395);
assert_eq!(p_tm.tm_mon, 0);
assert_eq!(p_tm.tm_mday, 2);
3- 将波斯历转换为公历。
let g_tm = ptime::from_persian_date(1395, 0, 2).unwrap().to_gregorian();
assert_eq!(g_tm.tm_year, 2016);
assert_eq!(g_tm.tm_mon, 2);
assert_eq!(g_tm.tm_mday, 21);
4- 获取当前时间。
let p_tm = ptime::now();
println!("Current time: {}", p_tm);
let p_tm_utc = ptime::now_utc();
println!("Current time at UTC: {}", p_tm_utc);
5- 格式化时间。
let p_tm = ptime::from_gregorian(time::now());
println!("{}", p_tm.to_string("yyyy-MM-dd HH:mm:ss.ns"));
/// yyyy, yyy, y year (e.g. 1394)
/// yy 2-digits representation of year (e.g. 94)
/// MMM the Persian name of month (e.g. فروردین)
/// MM 2-digits representation of month (e.g. 01)
/// M month (e.g. 1)
/// DD day of year (starting from 1)
/// D day of year (starting from 0)
/// dd 2-digits representation of day (e.g. 01)
/// d day (e.g. 1)
/// E the Persian name of weekday (e.g. شنبه)
/// e the Persian short name of weekday (e.g. ش)
/// A the Persian name of 12-Hour marker (e.g. قبل از ظهر)
/// a the Persian short name of 12-Hour marker (e.g. ق.ظ)
/// HH 2-digits representation of hour [00-23]
/// H hour [0-23]
/// kk 2-digits representation of hour [01-24]
/// k hour [1-24]
/// hh 2-digits representation of hour [01-12]
/// h hour [1-12]
/// KK 2-digits representation of hour [00-11]
/// K hour [0-11]
/// mm 2-digits representation of minute [00-59]
/// m minute [0-59]
/// ss 2-digits representation of seconds [00-59]
/// s seconds [0-59]
/// ns nanoseconds
有关更多信息,请查看 tests
文件夹中的测试文件。
依赖关系
~0.6–1MB
~15K SLoC