Skip to content

Commit 1aa4db0

Browse files
committed
v1.1.1 新增五行和方位互转;新增天干五合、地支六合、地支六害;优化性能;告别烦人的unwrap。
1 parent 099bd8c commit 1aa4db0

24 files changed

+1044
-867
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@
2525
3. 新增:每日宜忌、时辰宜忌。
2626
4. 修复:农历日获取时辰列表遇闰月报错的问题。
2727
5. 修复:获取当年第几周报错的问题。
28+
29+
## [1.1.1] - 2024-07-30
30+
1. 新增:五行和方位互转。
31+
2. 新增:天干五合、地支六合、地支六害。
32+
3. 优化:性能。
33+
4. 优化:告别烦人的unwrap。

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tyme4rs"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55
authors = ["6tail <[email protected]>"]
66
description = "Tyme是一个非常强大的日历工具库,可以看作 Lunar 的升级版,拥有更优的设计和扩展性,支持公历和农历、星座、干支、生肖、节气、法定假日等。"

src/tyme/culture/dog.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ pub struct Dog {
1010
}
1111

1212
impl Tyme for Dog {
13-
fn next(&self, n: isize) -> Result<Self, String> {
14-
Ok(Self::from_index(self.parent.next_index(n) as isize))
13+
fn next(&self, n: isize) -> Self {
14+
Self::from_index(self.parent.next_index(n) as isize)
1515
}
1616
}
1717

@@ -28,10 +28,10 @@ impl Dog {
2828
}
2929
}
3030

31-
pub fn from_name(name: &str) -> Result<Self, String> {
32-
Ok(Self {
33-
parent: LoopTyme::from_name(DOG_NAMES.to_vec().iter().map(|x| x.to_string()).collect(), name).unwrap()
34-
})
31+
pub fn from_name(name: &str) -> Self {
32+
Self {
33+
parent: LoopTyme::from_name(DOG_NAMES.to_vec().iter().map(|x| x.to_string()).collect(), name)
34+
}
3535
}
3636

3737
pub fn get_index(&self) -> usize {

src/tyme/culture/eightchar/mod.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ impl Culture for EightChar {
2828
impl EightChar {
2929
pub fn new(year: &str, month: &str, day: &str, hour: &str) -> Self {
3030
Self {
31-
year: SixtyCycle::from_name(year).unwrap(),
32-
month: SixtyCycle::from_name(month).unwrap(),
33-
day: SixtyCycle::from_name(day).unwrap(),
34-
hour: SixtyCycle::from_name(hour).unwrap(),
31+
year: SixtyCycle::from_name(year),
32+
month: SixtyCycle::from_name(month),
33+
day: SixtyCycle::from_name(day),
34+
hour: SixtyCycle::from_name(hour),
3535
}
3636
}
3737

@@ -61,25 +61,25 @@ impl EightChar {
6161
}
6262

6363
pub fn get_fetal_origin(&self) -> SixtyCycle {
64-
SixtyCycle::from_name(format!("{}{}", self.month.get_heaven_stem().next(1).unwrap().get_name(), self.month.get_earth_branch().next(3).unwrap().get_name()).as_str()).unwrap()
64+
SixtyCycle::from_name(format!("{}{}", self.month.get_heaven_stem().next(1).get_name(), self.month.get_earth_branch().next(3).get_name()).as_str())
6565
}
6666

6767
pub fn get_fetal_breath(&self) -> SixtyCycle {
68-
SixtyCycle::from_name(format!("{}{}", self.day.get_heaven_stem().next(5).unwrap().get_name(), EarthBranch::from_index(13 - (self.day.get_earth_branch().get_index() as isize)).get_name()).as_str()).unwrap()
68+
SixtyCycle::from_name(format!("{}{}", self.day.get_heaven_stem().next(5).get_name(), EarthBranch::from_index(13 - (self.day.get_earth_branch().get_index() as isize)).get_name()).as_str())
6969
}
7070

7171
pub fn get_own_sign(&self) -> SixtyCycle {
72-
let mut offset: isize = (self.month.get_earth_branch().next(-1).unwrap().get_index() + self.hour.get_earth_branch().next(-1).unwrap().get_index()) as isize;
72+
let mut offset: isize = (self.month.get_earth_branch().next(-1).get_index() + self.hour.get_earth_branch().next(-1).get_index()) as isize;
7373
offset = if offset >= 14 { 26 } else { 14 } - offset;
7474
offset -= 1;
75-
SixtyCycle::from_name(format!("{}{}", HeavenStem::from_index(((self.year.get_heaven_stem().get_index() as isize) + 1) * 2 + offset).get_name(), EarthBranch::from_index(2 + offset).get_name()).as_str()).unwrap()
75+
SixtyCycle::from_name(format!("{}{}", HeavenStem::from_index(((self.year.get_heaven_stem().get_index() as isize) + 1) * 2 + offset).get_name(), EarthBranch::from_index(2 + offset).get_name()).as_str())
7676
}
7777

7878
pub fn get_body_sign(&self) -> SixtyCycle {
7979
let mut offset: isize = (self.month.get_earth_branch().get_index() + self.hour.get_earth_branch().get_index()) as isize;
8080
offset %= 12;
8181
offset -= 1;
82-
SixtyCycle::from_name(format!("{}{}", HeavenStem::from_index(((self.year.get_heaven_stem().get_index() as isize) + 1) * 2 + offset).get_name(), EarthBranch::from_index(2 + offset).get_name()).as_str()).unwrap()
82+
SixtyCycle::from_name(format!("{}{}", HeavenStem::from_index(((self.year.get_heaven_stem().get_index() as isize) + 1) * 2 + offset).get_name(), EarthBranch::from_index(2 + offset).get_name()).as_str())
8383
}
8484

8585
pub fn get_duty(&self) -> Duty {
@@ -89,13 +89,13 @@ impl EightChar {
8989
pub fn get_solar_times(&self, start_year: isize, end_year: isize) -> Vec<SolarTime> {
9090
let mut l: Vec<SolarTime> = Vec::new();
9191
// 月地支距寅月的偏移值
92-
let mut m: isize = self.month.get_earth_branch().next(-2).unwrap().get_index() as isize;
92+
let mut m: isize = self.month.get_earth_branch().next(-2).get_index() as isize;
9393
// 月天干要一致
9494
if HeavenStem::from_index((self.year.get_heaven_stem().get_index() as isize + 1) * 2 + m) != self.month.get_heaven_stem() {
9595
return l;
9696
}
9797
// 1年的立春是辛酉,序号57
98-
let mut y: isize = self.year.next(-57).unwrap().get_index() as isize + 1;
98+
let mut y: isize = self.year.next(-57).get_index() as isize + 1;
9999
// 节令偏移值
100100
m *= 2;
101101
// 时辰地支转时刻,子时按零点算
@@ -107,24 +107,24 @@ impl EightChar {
107107
let mut term: SolarTerm = SolarTerm::from_index(y, 3);
108108
// 节令推移,年干支和月干支就都匹配上了
109109
if m > 0 {
110-
term = term.next(m).unwrap();
110+
term = term.next(m);
111111
}
112112
let solar_time: SolarTime = term.get_julian_day().get_solar_time();
113113
if solar_time.get_year() >= start_year {
114114
let mut mi: usize = 0;
115115
let mut s: usize = 0;
116116
// 日干支和节令干支的偏移值
117117
let mut solar_day: SolarDay = solar_time.get_solar_day();
118-
let d: isize = self.day.next(-(solar_day.get_lunar_day().get_sixty_cycle().get_index() as isize)).unwrap().get_index() as isize;
118+
let d: isize = self.day.next(-(solar_day.get_lunar_day().get_sixty_cycle().get_index() as isize)).get_index() as isize;
119119
if d > 0 {
120120
// 从节令推移天数
121-
solar_day = solar_day.next(d).unwrap();
121+
solar_day = solar_day.next(d);
122122
} else if h == solar_time.get_hour() {
123123
// 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上
124124
mi = solar_time.get_minute();
125125
s = solar_time.get_second();
126126
}
127-
let time: SolarTime = SolarTime::from_ymd_hms(solar_day.get_year(), solar_day.get_month(), solar_day.get_day(), h, mi, s).unwrap();
127+
let time: SolarTime = SolarTime::from_ymd_hms(solar_day.get_year(), solar_day.get_month(), solar_day.get_day(), h, mi, s);
128128
// 验证一下
129129
if time.get_lunar_hour().get_eight_char() == *self {
130130
l.push(time);
@@ -232,10 +232,10 @@ impl ChildLimit {
232232
let forward: bool = (yang && man) || (!yang && !man);
233233
let mut term: SolarTerm = birth_time.get_term();
234234
if !term.is_jie() {
235-
term = term.next(-1).unwrap();
235+
term = term.next(-1);
236236
}
237237
if forward {
238-
term = term.next(2).unwrap();
238+
term = term.next(2);
239239
}
240240
let info: ChildLimitInfo = provider.get_info(birth_time, term);
241241

@@ -318,8 +318,8 @@ impl Culture for DecadeFortune {
318318
}
319319

320320
impl Tyme for DecadeFortune {
321-
fn next(&self, n: isize) -> Result<Self, String> {
322-
Ok(Self::new(self.get_child_limit(), (self.index as isize + n) as usize))
321+
fn next(&self, n: isize) -> Self {
322+
Self::new(self.get_child_limit(), (self.index as isize + n) as usize)
323323
}
324324
}
325325

@@ -352,16 +352,16 @@ impl DecadeFortune {
352352
}
353353

354354
pub fn get_start_lunar_year(&self) -> LunarYear {
355-
self.child_limit.get_end_time().get_lunar_hour().get_lunar_day().get_lunar_month().get_lunar_year().next(self.index as isize * 10).unwrap()
355+
self.child_limit.get_end_time().get_lunar_hour().get_lunar_day().get_lunar_month().get_lunar_year().next(self.index as isize * 10)
356356
}
357357

358358
pub fn get_end_lunar(&self) -> LunarYear {
359-
self.get_start_lunar_year().next(9).unwrap()
359+
self.get_start_lunar_year().next(9)
360360
}
361361

362362
pub fn get_sixty_cycle(&self) -> SixtyCycle {
363363
let n: isize = self.index as isize + 1;
364-
self.child_limit.get_eight_char().get_month().next(if self.child_limit.is_forward() { n } else { -n }).unwrap()
364+
self.child_limit.get_eight_char().get_month().next(if self.child_limit.is_forward() { n } else { -n })
365365
}
366366

367367
pub fn get_start_fortune(&self) -> Fortune {
@@ -391,8 +391,8 @@ impl Culture for Fortune {
391391
}
392392

393393
impl Tyme for Fortune {
394-
fn next(&self, n: isize) -> Result<Self, String> {
395-
Ok(Self::new(self.get_child_limit(), (self.get_index() as isize + n) as usize))
394+
fn next(&self, n: isize) -> Self {
395+
Self::new(self.get_child_limit(), (self.get_index() as isize + n) as usize)
396396
}
397397
}
398398

@@ -421,12 +421,12 @@ impl Fortune {
421421
}
422422

423423
pub fn get_lunar_year(&self) -> LunarYear {
424-
self.child_limit.get_end_time().get_lunar_hour().get_lunar_day().get_lunar_month().get_lunar_year().next(self.index as isize).unwrap()
424+
self.child_limit.get_end_time().get_lunar_hour().get_lunar_day().get_lunar_month().get_lunar_year().next(self.index as isize)
425425
}
426426

427427
pub fn get_sixty_cycle(&self) -> SixtyCycle {
428428
let n: isize = self.get_age() as isize;
429-
self.child_limit.get_eight_char().get_hour().next(if self.child_limit.is_forward() { n } else { -n }).unwrap()
429+
self.child_limit.get_eight_char().get_hour().next(if self.child_limit.is_forward() { n } else { -n })
430430
}
431431
}
432432

src/tyme/culture/eightchar/provider.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ impl ChildLimitProvider for DefaultChildLimitProvider {
4343
d += h / 24;
4444
h %= 24;
4545

46-
let mut sm: SolarMonth = SolarMonth::from_ym(birth_time.get_year() + year as isize, birth_time.get_month()).unwrap().next(month as isize).unwrap();
46+
let mut sm: SolarMonth = SolarMonth::from_ym(birth_time.get_year() + year as isize, birth_time.get_month()).next(month as isize);
4747

4848
let dc: usize = sm.get_day_count();
4949
if d > dc {
5050
d -= dc;
51-
sm = sm.next(1).unwrap();
51+
sm = sm.next(1);
5252
}
5353

5454
ChildLimitInfo {
5555
start_time: birth_time,
56-
end_time: SolarTime::from_ymd_hms(sm.get_year(), sm.get_month(), d, h, mi, birth_time.get_second()).unwrap(),
56+
end_time: SolarTime::from_ymd_hms(sm.get_year(), sm.get_month(), d, h, mi, birth_time.get_second()),
5757
year_count: year,
5858
month_count: month,
5959
day_count: day,
@@ -83,18 +83,18 @@ impl ChildLimitProvider for China95ChildLimitProvider {
8383
minutes %= 360;
8484
let day: usize = minutes / 12;
8585

86-
let mut sm: SolarMonth = SolarMonth::from_ym(birth_time.get_year() + year as isize, birth_time.get_month()).unwrap().next(month as isize).unwrap();
86+
let mut sm: SolarMonth = SolarMonth::from_ym(birth_time.get_year() + year as isize, birth_time.get_month()).next(month as isize);
8787

8888
let mut d: usize = birth_time.get_day() + day;
8989
let dc: usize = sm.get_day_count();
9090
if d > dc {
9191
d -= dc;
92-
sm = sm.next(1).unwrap();
92+
sm = sm.next(1);
9393
}
9494

9595
ChildLimitInfo {
9696
start_time: birth_time,
97-
end_time: SolarTime::from_ymd_hms(sm.get_year(), sm.get_month(), d, birth_time.get_hour(), birth_time.get_minute(), birth_time.get_second()).unwrap(),
97+
end_time: SolarTime::from_ymd_hms(sm.get_year(), sm.get_month(), d, birth_time.get_hour(), birth_time.get_minute(), birth_time.get_second()),
9898
year_count: year,
9999
month_count: month,
100100
day_count: day,

src/tyme/culture/fetus.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ pub struct FetusHeavenStem {
9898
}
9999

100100
impl Tyme for FetusHeavenStem {
101-
fn next(&self, n: isize) -> Result<Self, String> {
102-
Ok(Self::from_index(self.parent.next_index(n) as isize))
101+
fn next(&self, n: isize) -> Self {
102+
Self::from_index(self.parent.next_index(n) as isize)
103103
}
104104
}
105105

@@ -154,8 +154,8 @@ pub struct FetusEarthBranch {
154154
}
155155

156156
impl Tyme for FetusEarthBranch {
157-
fn next(&self, n: isize) -> Result<Self, String> {
158-
Ok(Self::from_index(self.parent.next_index(n) as isize))
157+
fn next(&self, n: isize) -> Self {
158+
Self::from_index(self.parent.next_index(n) as isize)
159159
}
160160
}
161161

@@ -210,8 +210,8 @@ pub struct FetusMonth {
210210
}
211211

212212
impl Tyme for FetusMonth {
213-
fn next(&self, n: isize) -> Result<Self, String> {
214-
Ok(Self::from_index(self.parent.next_index(n) as isize))
213+
fn next(&self, n: isize) -> Self {
214+
Self::from_index(self.parent.next_index(n) as isize)
215215
}
216216
}
217217

@@ -270,16 +270,16 @@ mod tests {
270270

271271
#[test]
272272
fn test1() {
273-
assert_eq!("碓磨厕 外东南", SolarDay::from_ymd(2021, 11, 13).unwrap().get_lunar_day().get_fetus_day().get_name());
273+
assert_eq!("碓磨厕 外东南", SolarDay::from_ymd(2021, 11, 13).get_lunar_day().get_fetus_day().get_name());
274274
}
275275

276276
#[test]
277277
fn test2() {
278-
assert_eq!("占门碓 外东南", SolarDay::from_ymd(2021, 11, 12).unwrap().get_lunar_day().get_fetus_day().get_name());
278+
assert_eq!("占门碓 外东南", SolarDay::from_ymd(2021, 11, 12).get_lunar_day().get_fetus_day().get_name());
279279
}
280280

281281
#[test]
282282
fn test3() {
283-
assert_eq!("厨灶厕 外西南", SolarDay::from_ymd(2011, 11, 12).unwrap().get_lunar_day().get_fetus_day().get_name());
283+
assert_eq!("厨灶厕 外西南", SolarDay::from_ymd(2011, 11, 12).get_lunar_day().get_fetus_day().get_name());
284284
}
285285
}

0 commit comments

Comments
 (0)