chronos_parser_rs/
cron_interval.rs

1use std::marker::PhantomData;
2use std::rc::Rc;
3
4use chrono::{DateTime, TimeZone};
5use intervals_rs::{Interval, LimitValue};
6
7use crate::{CronIntervalIterator, Specification};
8
9/// A structure representing an interval of CROND date and time.<br/>
10/// CROND日時の区間を表す構造体。
11#[derive(Debug, Clone)]
12pub struct CronInterval<Tz: TimeZone, S: Specification<DateTime<Tz>>> {
13  pub(crate) underlying: Interval<i64>,
14  pub(crate) cron_specification: S,
15  phantom: PhantomData<Tz>,
16}
17
18impl<Tz: TimeZone, S: Specification<DateTime<Tz>>> CronInterval<Tz, S> {
19  fn convert_to_long_limit_value(value: LimitValue<DateTime<Tz>>) -> LimitValue<i64> {
20    match value {
21      LimitValue::Limitless => LimitValue::Limitless,
22      LimitValue::Limit(v) => LimitValue::Limit(v.timestamp_millis()),
23    }
24  }
25}
26
27impl<Tz: TimeZone, S: Specification<DateTime<Tz>>> CronInterval<Tz, S> {
28  /// The Factory method.<br/>
29  /// ファクトリメソッド。
30  pub fn new(
31    start_value: LimitValue<DateTime<Tz>>,
32    end_value: LimitValue<DateTime<Tz>>,
33    cron_specification: S,
34  ) -> Self {
35    let start = Self::convert_to_long_limit_value(start_value);
36    let end = Self::convert_to_long_limit_value(end_value);
37    Self {
38      underlying: Interval::closed(start, end),
39      cron_specification,
40      phantom: PhantomData,
41    }
42  }
43
44  /// Returns a CronIntervalIterator.<br/>
45  /// CronIntervalIteratorを返す。
46  pub fn iter(&self, timezone: Tz) -> CronIntervalIterator<Tz, S> {
47    let timestamp = self.underlying.as_lower_limit().as_value().unwrap();
48    let date_time = timezone.timestamp_millis_opt(*timestamp).unwrap();
49    CronIntervalIterator::new(timezone, date_time.clone(), date_time, Rc::new(self.clone()))
50  }
51}