Skip to content

Commit e9c1a35

Browse files
committed
初始版本。
1 parent bfe7938 commit e9c1a35

28 files changed

+8741
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/target
2+
/Cargo.lock
3+
4+
#idea
5+
/.idea/
6+
7+
.DS_Store

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Changelog
2+
3+
## [1.0.0] - 2024-04-30
4+
1. 初始版本。

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "tyme4rs"
3+
version = "1.0.0"
4+
edition = "2021"
5+
authors = ["6tail <[email protected]>"]
6+
description = "Tyme是一个非常强大的日历工具库,可以看作 Lunar 的升级版,拥有更优的设计和扩展性,支持公历和农历、星座、干支、生肖、节气、法定假日等。"
7+
documentation = "https://6tail.cn/tyme.html"
8+
homepage = "https://6tail.cn/tyme.html"
9+
repository = "https://github.com/6tail/tyme4rs"
10+
license = "MIT"
11+
keywords = ["Solar", "Lunar"]
12+
13+
[dependencies]
14+
lazy_static = "1.4.0"
15+
regex = "1.10.4"

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Tyme [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/tyme4rs/blob/master/LICENSE)
2+
3+
Tyme是一个非常强大的日历工具库,可以看作 [Lunar](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html") 的升级版,拥有更优的设计和扩展性,支持公历和农历、星座、干支、生肖、节气、法定假日等。
4+
5+
## 示例
6+
7+
// install
8+
cargo install tyme4rs
9+
10+
// test.rs
11+
use crate::tyme::solar::{SolarDay};
12+
13+
let solar: SolarDay = SolarDay::from_ymd(1986, 5, 29).unwrap();
14+
15+
// 1986年5月29日
16+
println!("{}", solar.to_string());
17+
18+
// 农历丙寅年四月廿一
19+
println!("{}", solar.get_lunar_day().to_string());
20+
21+
22+
## 文档
23+
24+
请移步至 [https://6tail.cn/tyme.html](https://6tail.cn/tyme.html "https://6tail.cn/tyme.html")
25+
26+
## Star History
27+
28+
[![Star History Chart](https://api.star-history.com/svg?repos=6tail/tyme4rs&type=Date)](https://star-history.com/#6tail/tyme4rs&Date)

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod tyme;

src/tyme/culture/dog.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
use std::fmt::{Display, Formatter};
2+
use crate::tyme::{AbstractCulture, AbstractCultureDay, AbstractTyme, Culture, LoopTyme, Tyme};
3+
4+
pub static DOG_NAMES: [&str; 3] = ["初伏", "中伏", "末伏"];
5+
6+
/// 三伏
7+
#[derive(Debug, Clone)]
8+
pub struct Dog {
9+
parent: LoopTyme,
10+
}
11+
12+
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))
15+
}
16+
}
17+
18+
impl Culture for Dog {
19+
fn get_name(&self) -> String {
20+
self.parent.get_name()
21+
}
22+
}
23+
24+
impl Dog {
25+
pub fn from_index(index: isize) -> Self {
26+
Self {
27+
parent: LoopTyme::from_index(DOG_NAMES.to_vec().iter().map(|x| x.to_string()).collect(), index)
28+
}
29+
}
30+
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+
})
35+
}
36+
37+
pub fn get_index(&self) -> usize {
38+
self.parent.get_index()
39+
}
40+
41+
pub fn get_size(&self) -> usize {
42+
self.parent.get_size()
43+
}
44+
}
45+
46+
impl Display for Dog {
47+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48+
write!(f, "{}", self.get_name())
49+
}
50+
}
51+
52+
impl PartialEq for Dog {
53+
fn eq(&self, other: &Self) -> bool {
54+
self.to_string() == other.to_string()
55+
}
56+
}
57+
58+
impl Eq for Dog {}
59+
60+
impl Into<LoopTyme> for Dog {
61+
fn into(self) -> LoopTyme {
62+
self.parent
63+
}
64+
}
65+
66+
/// 三伏天
67+
#[derive(Debug, Clone)]
68+
pub struct DogDay {
69+
parent: AbstractCultureDay,
70+
dog: Dog
71+
}
72+
73+
impl Culture for DogDay {
74+
fn get_name(&self) -> String {
75+
self.dog.get_name()
76+
}
77+
}
78+
79+
impl DogDay {
80+
pub fn new(dog: Dog, day_index: usize) -> Self {
81+
let loop_tyme: LoopTyme = dog.clone().into();
82+
let abstract_tyme: AbstractTyme = loop_tyme.into();
83+
let culture: AbstractCulture = abstract_tyme.into();
84+
Self {
85+
parent: AbstractCultureDay::new(culture, day_index),
86+
dog
87+
}
88+
}
89+
90+
pub fn get_dog(&self) -> Dog {
91+
self.dog.clone()
92+
}
93+
94+
pub fn get_day_index(&self) -> usize {
95+
self.parent.get_day_index()
96+
}
97+
}
98+
99+
impl Display for DogDay {
100+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
101+
write!(f, "{}第{}天", self.get_name(), self.parent.get_day_index() + 1)
102+
}
103+
}
104+
105+
impl PartialEq for DogDay {
106+
fn eq(&self, other: &Self) -> bool {
107+
self.to_string() == other.to_string()
108+
}
109+
}
110+
111+
impl Eq for DogDay {}
112+
113+
impl Into<AbstractCultureDay> for DogDay {
114+
fn into(self) -> AbstractCultureDay {
115+
self.parent
116+
}
117+
}
118+
119+
#[cfg(test)]
120+
mod tests {
121+
use crate::tyme::culture::dog::{Dog, DogDay};
122+
123+
#[test]
124+
fn test1() {
125+
assert_eq!("初伏第3天", DogDay::new(Dog::from_index(0), 2).to_string());
126+
}
127+
128+
#[test]
129+
fn test2() {
130+
assert_eq!("初伏", DogDay::new(Dog::from_index(0), 2).get_dog().to_string());
131+
}
132+
133+
}

0 commit comments

Comments
 (0)