use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CreditCycle {
Fixed {
day_of_month: u32,
},
Rolling {
days: u32,
},
}
impl CreditCycle {
pub fn cycle_start(&self, window_start: Option<i64>, now: NaiveDateTime) -> i64 {
match self {
CreditCycle::Fixed { day_of_month } => {
let day = *day_of_month;
let (year, month, current_day) = (now.year(), now.month(), now.day());
if current_day >= day {
chrono::NaiveDate::from_ymd_opt(year, month, day)
.unwrap_or_else(|| {
chrono::NaiveDate::from_ymd_opt(
year,
month,
last_day_of_month(year, month),
)
.unwrap()
})
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp()
} else {
let (prev_year, prev_month) = if month == 1 {
(year - 1, 12)
} else {
(year, month - 1)
};
chrono::NaiveDate::from_ymd_opt(prev_year, prev_month, day)
.unwrap_or_else(|| {
chrono::NaiveDate::from_ymd_opt(
prev_year,
prev_month,
last_day_of_month(prev_year, prev_month),
)
.unwrap()
})
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp()
}
},
CreditCycle::Rolling { .. } => {
window_start.unwrap_or_else(|| now.and_utc().timestamp())
},
}
}
pub fn cycle_end(&self, window_start: Option<i64>, now: NaiveDateTime) -> i64 {
match self {
CreditCycle::Fixed { day_of_month } => {
let day = *day_of_month;
let (year, month, current_day) = (now.year(), now.month(), now.day());
if current_day >= day {
let (next_year, next_month) = if month == 12 {
(year + 1, 1)
} else {
(year, month + 1)
};
chrono::NaiveDate::from_ymd_opt(next_year, next_month, day)
.unwrap_or_else(|| {
chrono::NaiveDate::from_ymd_opt(
next_year,
next_month,
last_day_of_month(next_year, next_month),
)
.unwrap()
})
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp()
} else {
chrono::NaiveDate::from_ymd_opt(year, month, day)
.unwrap_or_else(|| {
chrono::NaiveDate::from_ymd_opt(
year,
month,
last_day_of_month(year, month),
)
.unwrap()
})
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp()
}
},
CreditCycle::Rolling { days } => {
let start = window_start.unwrap_or_else(|| now.and_utc().timestamp());
start + (*days as i64) * 86400
},
}
}
pub fn is_expired(&self, window_start: Option<i64>, now: NaiveDateTime) -> bool {
let end = self.cycle_end(window_start, now);
now.and_utc().timestamp() >= end
}
pub fn next_reset_at(&self, window_start: Option<i64>, now: NaiveDateTime) -> i64 {
self.cycle_end(window_start, now)
}
pub fn type_tag(&self) -> &'static str {
match self {
CreditCycle::Fixed { .. } => "fixed",
CreditCycle::Rolling { .. } => "rolling",
}
}
pub fn from_tag(type_tag: &str, param: u32) -> Option<Self> {
match type_tag {
"fixed" if (1..=28).contains(¶m) => Some(CreditCycle::Fixed {
day_of_month: param,
}),
"rolling" if param >= 1 => Some(CreditCycle::Rolling { days: param }),
_ => None,
}
}
pub fn param(&self) -> u32 {
match self {
CreditCycle::Fixed { day_of_month } => *day_of_month,
CreditCycle::Rolling { days } => *days,
}
}
}
fn last_day_of_month(year: i32, month: u32) -> u32 {
let (next_year, next_month) = if month == 12 {
(year + 1, 1)
} else {
(year, month + 1)
};
chrono::NaiveDate::from_ymd_opt(next_year, next_month, 1)
.unwrap()
.pred_opt()
.unwrap()
.day()
}
use chrono::Datelike;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fixed_cycle_start_end() {
let cycle = CreditCycle::Fixed { day_of_month: 15 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 8, 20)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(None, now);
let expected_start = chrono::NaiveDate::from_ymd_opt(2026, 8, 15)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(start, expected_start);
let end = cycle.cycle_end(None, now);
let expected_end = chrono::NaiveDate::from_ymd_opt(2026, 9, 15)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(end, expected_end);
}
#[test]
fn test_fixed_cycle_start_before_day() {
let cycle = CreditCycle::Fixed { day_of_month: 15 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 8, 10)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(None, now);
let expected_start = chrono::NaiveDate::from_ymd_opt(2026, 7, 15)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(start, expected_start);
}
#[test]
fn test_rolling_cycle_start_end() {
let cycle = CreditCycle::Rolling { days: 30 };
let window_start_ts = 1_700_000_000i64;
let now = chrono::NaiveDate::from_ymd_opt(2026, 8, 20)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(Some(window_start_ts), now);
assert_eq!(start, window_start_ts);
let end = cycle.cycle_end(Some(window_start_ts), now);
assert_eq!(end, window_start_ts + 30 * 86400);
}
#[test]
fn test_fixed_cycle_is_expired_without_window_start() {
let cycle = CreditCycle::Fixed { day_of_month: 15 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 9, 16)
.unwrap()
.and_hms_opt(0, 0, 1)
.unwrap();
assert!(!cycle.is_expired(None, now));
let now2 = chrono::NaiveDate::from_ymd_opt(2026, 9, 14)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
assert!(!cycle.is_expired(None, now2));
}
#[test]
fn test_rolling_cycle_is_expired() {
let cycle = CreditCycle::Rolling { days: 30 };
let window_start_ts = 1_700_000_000i64;
let expired_now = chrono::DateTime::from_timestamp(window_start_ts + 31 * 86400, 0)
.unwrap()
.naive_utc();
assert!(cycle.is_expired(Some(window_start_ts), expired_now));
let active_now = chrono::DateTime::from_timestamp(window_start_ts + 15 * 86400, 0)
.unwrap()
.naive_utc();
assert!(!cycle.is_expired(Some(window_start_ts), active_now));
}
#[test]
fn test_fixed_day_of_month_boundary() {
let cycle = CreditCycle::Fixed { day_of_month: 28 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 2, 28)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 2, 28)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(start, expected);
}
#[test]
fn test_type_tag_roundtrip() {
let fixed = CreditCycle::Fixed { day_of_month: 15 };
assert_eq!(fixed.type_tag(), "fixed");
assert_eq!(fixed.param(), 15);
let rebuilt = CreditCycle::from_tag("fixed", 15).unwrap();
assert_eq!(rebuilt, fixed);
let rolling = CreditCycle::Rolling { days: 30 };
assert_eq!(rolling.type_tag(), "rolling");
assert_eq!(rolling.param(), 30);
let rebuilt = CreditCycle::from_tag("rolling", 30).unwrap();
assert_eq!(rebuilt, rolling);
}
#[test]
fn test_from_tag_unknown() {
assert!(CreditCycle::from_tag("unknown", 0).is_none());
}
#[test]
fn test_from_tag_validates_param() {
assert!(
CreditCycle::from_tag("fixed", 0).is_none(),
"day=0 应被拒绝"
);
assert!(
CreditCycle::from_tag("fixed", 29).is_none(),
"day=29 应被拒绝"
);
assert!(
CreditCycle::from_tag("fixed", 31).is_none(),
"day=31 应被拒绝"
);
assert!(CreditCycle::from_tag("fixed", u32::MAX).is_none());
assert!(CreditCycle::from_tag("fixed", 1).is_some());
assert!(CreditCycle::from_tag("fixed", 28).is_some());
assert!(
CreditCycle::from_tag("rolling", 0).is_none(),
"days=0 应被拒绝"
);
assert!(CreditCycle::from_tag("rolling", 1).is_some());
}
#[test]
fn test_fixed_cycle_start_day30_in_31_day_month() {
let cycle = CreditCycle::Fixed { day_of_month: 30 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 1, 31)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 1, 30)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(start, expected);
}
#[test]
fn test_fixed_cycle_start_current_month_fallback_feb() {
let cycle = CreditCycle::Fixed { day_of_month: 0 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 2, 10)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 2, 28)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(start, expected, "day=0 应回退为当月最后一天");
}
#[test]
fn test_fixed_cycle_start_prev_month_fallback() {
let cycle = CreditCycle::Fixed { day_of_month: 30 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 3, 10)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 2, 28)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(start, expected);
}
#[test]
fn test_fixed_cycle_start_prev_month_crosses_year() {
let cycle = CreditCycle::Fixed { day_of_month: 15 };
let now = chrono::NaiveDate::from_ymd_opt(2027, 1, 10)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let start = cycle.cycle_start(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 12, 15)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(start, expected);
}
#[test]
fn test_fixed_cycle_end_next_month_fallback() {
let cycle = CreditCycle::Fixed { day_of_month: 31 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 1, 31)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let end = cycle.cycle_end(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 2, 28)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(end, expected);
}
#[test]
fn test_fixed_cycle_end_current_month_fallback() {
let cycle = CreditCycle::Fixed { day_of_month: 31 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 2, 10)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let end = cycle.cycle_end(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 2, 28)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(end, expected);
}
#[test]
fn test_fixed_cycle_end_crosses_year() {
let cycle = CreditCycle::Fixed { day_of_month: 15 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 12, 20)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let end = cycle.cycle_end(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2027, 1, 15)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(end, expected);
}
#[test]
fn test_fixed_cycle_end_december_fallback_last_day() {
let cycle = CreditCycle::Fixed { day_of_month: 32 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 12, 5)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
let end = cycle.cycle_end(None, now);
let expected = chrono::NaiveDate::from_ymd_opt(2026, 12, 31)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap()
.and_utc()
.timestamp();
assert_eq!(end, expected);
}
#[test]
fn test_rolling_cycle_start_none_falls_back_to_now() {
let cycle = CreditCycle::Rolling { days: 7 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 8, 20)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
assert_eq!(
cycle.cycle_start(None, now),
now.and_utc().timestamp(),
"window_start=None 应回退为当前时间戳"
);
}
#[test]
fn test_rolling_is_expired_exact_boundary() {
let cycle = CreditCycle::Rolling { days: 30 };
let window_start_ts = 1_700_000_000i64;
let exact_end = chrono::DateTime::from_timestamp(window_start_ts + 30 * 86400, 0)
.unwrap()
.naive_utc();
assert!(
cycle.is_expired(Some(window_start_ts), exact_end),
"now == 周期结束点应判定过期"
);
}
#[test]
fn test_rolling_next_reset_at() {
let cycle = CreditCycle::Rolling { days: 14 };
let window_start_ts = 1_700_000_000i64;
let now = chrono::NaiveDate::from_ymd_opt(2026, 8, 20)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
assert_eq!(
cycle.next_reset_at(Some(window_start_ts), now),
window_start_ts + 14 * 86400
);
}
#[test]
fn test_fixed_next_reset_at() {
let cycle = CreditCycle::Fixed { day_of_month: 15 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 8, 20)
.unwrap()
.and_hms_opt(12, 0, 0)
.unwrap();
assert_eq!(cycle.next_reset_at(None, now), cycle.cycle_end(None, now));
}
#[test]
fn test_fixed_is_expired_always_false() {
let cycle = CreditCycle::Fixed { day_of_month: 15 };
let now = chrono::NaiveDate::from_ymd_opt(2026, 9, 15)
.unwrap()
.and_hms_opt(0, 0, 0)
.unwrap();
assert!(!cycle.is_expired(None, now));
}
#[test]
fn test_cycle_serde_roundtrip() {
let fixed = CreditCycle::Fixed { day_of_month: 28 };
let json = serde_json::to_string(&fixed).unwrap();
assert_eq!(serde_json::from_str::<CreditCycle>(&json).unwrap(), fixed);
let rolling = CreditCycle::Rolling { days: 90 };
let json = serde_json::to_string(&rolling).unwrap();
assert_eq!(serde_json::from_str::<CreditCycle>(&json).unwrap(), rolling);
}
#[test]
fn test_cycle_clone_eq() {
let cycle = CreditCycle::Fixed { day_of_month: 1 };
let cloned = cycle.clone();
assert_eq!(cycle, cloned);
assert_eq!(cloned.param(), 1);
}
}