9 个稳定版本
1.8.2 | 2024年3月26日 |
---|---|
1.8.1 | 2024年3月16日 |
1.7.6 |
|
1.7.4 |
|
1.0.7 |
|
#22 in 日期和时间
2,366 每月下载量
用于 date-cli
235KB
2.5K SLoC
PHASEXave
日历的有效性
- 公历相对于春分点的绝对值每年前进0.000031天。在3225~3232年间,与春分点的偏差将达到大约2天。
- 太阳历解决了公历和儒略历的所有闰年问题,但由于计算操作更复杂,所以它比其他历法慢一些。
闰年算法的有效性
- 太阳年:u128 - 2^64
- 儒略年:u128 - 2^64
- 公历年:u128 - 2^64
为什么是 2^64?因为 u128 * 366(一年的最大天数)在Unix纪元后的时代天数和秒溢出,因此临时使用u64,在下一个版本中将再次使用或更大的u128。
功能
将功能添加到Cargo.toml以使用本地时区和绝对时间函数
- macOS
[dependencies]
PHASEXave = { version = "*", features = ["platform_specific_functions_darwin"] }
- Unix
[dependencies]
PHASEXave = { version = "*", features = ["platform_specific_functions_unix"] }
- Windows
[dependencies]
PHASEXave = { version = "*", features = ["platform_specific_functions_windows"] }
用法
时间
UTC
获取UTC时间
use PHASEXave::{Time};
fn main() {
let time: Time = Time::utc();
println!("{hours}:{minutes}:{seconds}", hours = time.hours, minutes = time.minutes, seconds = time.seconds);
}
现在
获取任何时区的当前时间
use PHASEXave::{Time, Sign, Zone};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Signed, hours: 4, minutes: 30, seconds: 0 };
let time: Time = Time::now(zone);
println!("{hours}:{minutes}:{seconds}", hours = time.hours, minutes = time.minutes, seconds = time.seconds);
}
本地
获取本地时区的当前时间
use PHASEXave::{Time};
fn main() {
let time: Time = Time::local();
println!("{hours}:{minutes}:{seconds}", hours = time.hours, minutes = time.minutes, seconds = time.seconds);
}
Uptime
获取uptime(不含休眠时间的不是启动时间)(macOS/Darwin/XNU,Unix/Linux,Windows)
use PHASEXave::{Time, Uptime};
fn main() {
let time: Uptime = Time::absolute();
println!("{hours}:{minutes}:{seconds}", hours = time.hours, minutes = time.minutes, seconds = time.seconds);
}
日期
UTC
太阳
获取太阳历的UTC日期
use PHASEXave::{CalendarView, Date};
fn main() {
let date: Date = Date::utc(CalendarView::Solar);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
儒略
获取儒略历的UTC日期
use PHASEXave::{CalendarView, Date};
fn main() {
let date: Date = Date::utc(CalendarView::Julian);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
公历
获取公历的UTC日期
use PHASEXave::{CalendarView, Date};
fn main() {
let date: Date = Date::utc(CalendarView::Gregorian);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
现在
太阳
获取太阳历的任何时区的当前日期
use PHASEXave::{CalendarView, Date, Zone, Sign};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 };
let date: Date = Date::now(CalendarView::Solar, time_zone);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
儒略
获取儒略历的任何时区的当前日期
use PHASEXave::{CalendarView, Date, Zone, Sign};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 };
let date: Date = Date::now(CalendarView::Julian, time_zone);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
公历
获取公历的任何时区的当前日期
use PHASEXave::{CalendarView, Date, Zone, Sign};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 };
let date: Date = Date::now(CalendarView::Gregorian, time_zone);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
本地
太阳
获取太阳历的本地时区的当前日期
use PHASEXave::{CalendarView, Date};
fn main() {
let date: Date = Date::local(CalendarView::Solar);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
儒略
获取儒略历的本地时区的当前日期
use PHASEXave::{CalendarView, Date};
fn main() {
let date: Date = Date::local(CalendarView::Julian);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
公历
获取公历的本地时区的当前日期
use PHASEXave::{CalendarView, Date};
fn main() {
let date: Date = Date::local(CalendarView::Gregorian);
println!("{yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
演示
将不同日历之间的任何日期表示形式转换为相互之间的表示形式
use PHASEXave::{CalendarView, Date, Sign, Zone, Julian, Gregorian, Solar};
fn main() {
let mut date: Date = Date {
day: 27,
month: 2,
year: 3226,
time_zone: Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 },
unix_time: 0,
era_days: 0,
view: CalendarView::Solar
};
<Date as Julian>::to_presentation(&mut date, true);
println!("JL: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
<Date as Gregorian>::to_presentation(&mut date, true);
println!("GR: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
<Date as Solar>::to_presentation(&mut date, true);
println!("SL: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
JL: 3226/2/27:1177987:39642393600
GR: 3226/2/27:1177965:39640492800
SL: 3226/2/27:1177964:39640406400
* - 任何,但除了儒略历中的第一个BCE天,
转换
太阳到儒略
将任何太阳日期转换为儒略日期
use PHASEXave::{Date, Sign, Zone, Julian, CalendarView};
fn main() {
let mut date: Date = Date {
day: 27,
month: 2,
year: 3226,
time_zone: Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 },
unix_time: 0,
era_days: 0,
view: CalendarView::Solar
};
<Date as Julian>::to_date(&mut date, false);
println!("JL: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
JL: 3226/2/4:1177964:39640406400
太阳到公历
将任何太阳日期转换为公历日期
use PHASEXave::{Date, Sign, Zone, Gregorian, CalendarView};
fn main() {
let mut date: Date = Date {
day: 1,
month: 3,
year: 3226,
time_zone: Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 },
unix_time: 0,
era_days: 0,
view: CalendarView::Solar
};
<Date as Gregorian>::to_date(&mut date, false);
println!("GR: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
GR: 3226/2/28:1177966:39640579200
儒略到太阳
将任何儒略日期转换为太阳日期
use PHASEXave::{Date, Sign, Zone, Solar, CalendarView};
fn main() {
let mut date: Date = Date {
day: 5,
month: 2,
year: 3226,
time_zone: Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 },
unix_time: 0,
era_days: 0,
view: CalendarView::Julian
};
<Date as Solar>::to_date(&mut date, false);
println!("SL: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
SL: 3226/2/28:1177965:39640492800
儒略到公历
将任何儒略日期转换为公历日期
use PHASEXave::{Date, Sign, Zone, Gregorian, CalendarView};
fn main() {
let mut date: Date = Date {
day: 30,
month: 10,
year: 2023,
time_zone: Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 },
unix_time: 0,
era_days: 0,
view: CalendarView::Julian
};
<Date as Gregorian>::to_date(&mut date, false);
println!("GR: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
GR: 2023/11/12:738836:1699747200
* - 任何,但除了公历中缺失的前两天。
公历转阴历
将任何公历日期转换为阴历日期
use PHASEXave::{Date, Sign, Zone, Solar, CalendarView};
fn main() {
let mut date: Date = Date {
day: 28,
month: 2,
year: 3226,
time_zone: Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 },
unix_time: 0,
era_days: 0,
view: CalendarView::Gregorian
};
<Date as Solar>::to_date(&mut date, false);
println!("SL: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
SL: 3226/3/1:1177966:39640579200
公历转儒略
将任何公历日期转换为儒略日期
use PHASEXave::{Date, Sign, Zone, Julian, CalendarView};
fn main() {
let mut date: Date = Date {
day: 12,
month: 11,
year: 2023,
time_zone: Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 },
unix_time: 0,
era_days: 0,
view: CalendarView::Gregorian
};
<Date as Julian>::to_date(&mut date, false);
println!("JL: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
JL: 2023/10/30:738836:1699747200
之间
将任何公历转换为儒略和儒略转换为公历
use PHASEXave::{CalendarView, Date, Julian, Gregorian, Solar, Sign, Zone};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Unsigned, hours: 0, minutes: 0, seconds: 0 };
let mut date: Date = Date::now(CalendarView::Gregorian, time_zone);
<Date as Solar>::to_date(&mut date, true);
<Date as Julian>::to_date(&mut date, true);
<Date as Gregorian>::to_date(&mut date, true);
println!("GR: {yyyy}/{mm}/{dd}:{era_days}:{unix_time}", yyyy = date.year, mm = date.month, dd = date.day, era_days = date.era_days, unix_time = date.unix_time);
}
输出
GR: 2024/3/4:738949:1709574752
星期
从阴历、儒略和公历日历获取星期几
太阳
从日期获取星期几
use PHASEXave::{CalendarView, Date, RataDie, Xavetar, Sakamoto, Sign, Zone};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Unsigned, hours: 8, minutes: 0, seconds: 0 };
let date: Date = Date::now(CalendarView::Solar, time_zone);
println!(
"Solar Week day:\n\nRata Die: {rata_die}\nXavetar: {xavetar}\nSakamoto: {sakamoto}\n",
rata_die = <Date as RataDie>::week_day(&date).name(),
xavetar = <Date as Xavetar>::week_day(&date).name(),
sakamoto = <Date as Sakamoto>::week_day(&date).name(),
);
println!("Date: {yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
输出
Solar Week day:
Rata Die: Tuesday
Xavetar: Tuesday
Sakamoto: Tuesday
Date: 2024/3/6
从任何日期获取星期几
use PHASEXave::{CalendarView, Date, RataDie, Sakamoto, Xavetar};
fn main() {
let (yyyy, mm, dd): (u64, u8, u8) = (1582, 10, 5);
println!(
"Solar Week day:\n\nRata Die: {rata_die}\nXavetar: {xavetar}\nSakamoto: {sakamoto}\n",
rata_die = <Date as RataDie>::from(CalendarView::Solar, yyyy, mm, dd).name(),
xavetar = <Date as Xavetar>::from(CalendarView::Solar, yyyy, mm, dd).name(),
sakamoto = <Date as Sakamoto>::from(CalendarView::Solar, yyyy, mm, dd).name(),
);
println!("Date: {yyyy}/{mm}/{dd}", yyyy = yyyy, mm = mm, dd = dd);
}
输出
Solar Week day:
Rata Die: Tuesday
Xavetar: Tuesday
Sakamoto: Tuesday
Date: 1582/10/5
儒略
从日期获取星期几
use PHASEXave::{CalendarView, Date, RataDie, Xavetar, Sakamoto, Sign, Zone};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Unsigned, hours: 8, minutes: 0, seconds: 0 };
let date: Date = Date::now(CalendarView::Julian, time_zone);
println!(
"Julian Week day:\n\nRata Die: {rata_die}\nXavetar: {xavetar}\nSakamoto: {sakamoto}\n",
rata_die = <Date as RataDie>::week_day(&date).name(),
xavetar = <Date as Xavetar>::week_day(&date).name(),
sakamoto = <Date as Sakamoto>::week_day(&date).name(),
);
println!("Date: {yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
输出
Julian Week day:
Rata Die: Tuesday
Xavetar: Tuesday
Sakamoto: Tuesday
Date: 2024/2/21
从任何日期获取星期几
use PHASEXave::{CalendarView, Date, RataDie, Xavetar, Sakamoto};
fn main() {
let (yyyy, mm, dd): (u64, u8, u8) = (1582, 10, 5);
println!(
"Julian Week day:\n\nRata Die: {rata_die}\nXavetar: {xavetar}\nSakamoto: {sakamoto}\n",
rata_die = <Date as RataDie>::from(CalendarView::Julian, yyyy, mm, dd).name(),
xavetar = <Date as Xavetar>::from(CalendarView::Julian, yyyy, mm, dd).name(),
sakamoto = <Date as Sakamoto>::from(CalendarView::Julian, yyyy, mm, dd).name(),
);
println!("Date: {yyyy}/{mm}/{dd}", yyyy = yyyy, mm = mm, dd = dd);
}
输出
Julian Week day:
Rata Die: Friday
Xavetar: Friday
Sakamoto: Friday
Date: 1582/10/5
公历
从日期获取星期几
use PHASEXave::{CalendarView, Date, RataDie, Xavetar, Sakamoto, Sign, Zone};
fn main() {
let time_zone: Zone = Zone { sign: Sign::Unsigned, hours: 8, minutes: 0, seconds: 0 };
let date: Date = Date::now(CalendarView::Gregorian, time_zone);
println!(
"Gregorian Week day:\n\nRata Die: {rata_die}\nSakamoto: {sakamoto}\nXavetar: {xavetar}\n",
rata_die = <Date as RataDie>::week_day(&date).name(),
xavetar = <Date as Xavetar>::week_day(&date).name(),
sakamoto = <Date as Sakamoto>::week_day(&date).name(),
);
println!("Date: {yyyy}/{mm}/{dd}", yyyy = date.year, mm = date.month, dd = date.day);
}
输出
Gregorian Week day:
Rata Die: Tuesday
Sakamoto: Tuesday
Xavetar: Tuesday
Date: 2024/3/5
从任何日期获取星期几
use PHASEXave::{CalendarView, Date, RataDie, Xavetar, Sakamoto};
fn main() {
let (yyyy, mm, dd): (u64, u8, u8) = (1582, 10, 5);
println!(
"Gregorian Week day:\n\nRata Die: {rata_die}\nXavetar: {xavetar}\nSakamoto: {sakamoto}\n",
rata_die = <Date as RataDie>::from(CalendarView::Gregorian, yyyy, mm, dd).name(),
xavetar = <Date as Xavetar>::from(CalendarView::Gregorian, yyyy, mm, dd).name(),
sakamoto = <Date as Sakamoto>::from(CalendarView::Gregorian, yyyy, mm, dd).name(),
);
println!("Date: {yyyy}/{mm}/{dd}", yyyy = yyyy, mm = mm, dd = dd);
}
输出
Gregorian Week day:
Rata Die: Tuesday
Xavetar: Tuesday
Sakamoto: Tuesday
Date: 1582/10/5
许可证
PHASEXave主要在三个反病毒许可证、MIT许可证和Apache许可证(版本2.0)的条款下分发
有关详细信息,请参阅LICENSE-ANTI-VIRUS、LICENSE-APACHE和LICENSE-MIT。
依赖关系
~240KB