1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! # カレンダー
//!
//! 指定期間の日付を持つカレンダーとユーティリティ関数
use super::{KoyomiError, KoyomiResult};
use date::Date;

/// 指定年月が何日まであるかを返す
///
/// # Examples
///
/// ```rust
/// use koyomi::num_days;
///
/// assert_eq!(num_days(2018, 1), 31);
///
/// // うるう年
/// assert_eq!(num_days(2016, 2), 29);
/// ```
pub fn num_days(year: i32, month: u32) -> u32 {
    match month {
        1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
        4 | 6 | 9 | 11 => 30,
        2 => if is_leap(year) {
            29
        } else {
            28
        },
        _ => unreachable!(format!("{} is invalid month!", month)),
    }
}

/// うるう年かどうかを判定する
///
/// # Examples
///
/// ```rust
/// use koyomi::is_leap;
///
/// assert_eq!(is_leap(2016), true);
/// assert_eq!(is_leap(2018), false);
/// ```
pub fn is_leap(year: i32) -> bool {
    // @see https://www.nao.ac.jp/faq/a0306.html
    if year % 4 != 0 {
        false
    } else if year % 100 == 0 && year % 400 != 0 {
        false
    } else {
        true
    }
}

/// カレンダー
///
/// 指定期間の日付を生成することができる
#[derive(Debug)]
pub struct Calendar {
    from: Date,
    until: Date,
}

impl Calendar {
    /// カレンダーオブジェクトを生成する
    ///
    /// # Examples
    ///
    /// ```rust
    /// use koyomi::{Calendar, Date};
    ///
    /// let from = Date::from_ymd(2018, 1, 1).unwrap();
    /// let until = Date::from_ymd(2018, 12, 31).unwrap();
    /// let cal = Calendar::new(from, until);
    /// assert!(cal.is_ok());
    ///
    /// let from = Date::from_ymd(2018, 12, 31).unwrap();
    /// let until = Date::from_ymd(2018, 1, 1).unwrap();
    /// let cal = Calendar::new(from, until);
    /// assert!(cal.is_err());
    /// ```
    pub fn new(from: Date, until: Date) -> KoyomiResult<Self> {
        if until.num_days(&from) <= 0 {
            Err(KoyomiError::InvalidTerm(from, until))
        } else {
            Ok(Calendar { from, until })
        }
    }

    /// カレンダーを生成するためのビルダーを返す
    pub fn build<'a>() -> CalendarBuilder<'a> {
        CalendarBuilder {
            from: None,
            single: None,
            until: None,
        }
    }

    /// 開始日の文字列表現を返す
    ///
    /// # Examples
    ///
    /// ```rust
    /// use koyomi::{Calendar, Date};
    ///
    /// let from = Date::from_ymd(2018, 1, 1).unwrap();
    /// let until = Date::from_ymd(2018, 12, 31).unwrap();
    /// let cal = Calendar::new(from, until).unwrap();
    /// assert_eq!(cal.from(), "2018-01-01");
    /// ```
    pub fn from(&self) -> String {
        self.from.to_string()
    }

    pub fn make(&self) -> Vec<Date> {
        let days = self.until.num_days(&self.from) + 1;
        let mut cal = Vec::with_capacity(days as usize);
        let mut date = self.from.tomorrow();

        cal.push(self.from.clone());

        loop {
            if let Ok(d) = date {
                if d > self.until {
                    break;
                }
                date = d.tomorrow();
                cal.push(d);
            }
        }

        cal
    }

    /// 終了日の文字列表現を返す
    ///
    /// # Examples
    ///
    /// ```rust
    /// use koyomi::{Calendar, Date};
    ///
    /// let from = Date::from_ymd(2018, 1, 1).unwrap();
    /// let until = Date::from_ymd(2018, 12, 31).unwrap();
    /// let cal = Calendar::new(from, until).unwrap();
    /// assert_eq!(cal.until(), "2018-12-31");
    /// ```
    pub fn until(&self) -> String {
        self.until.to_string()
    }
}

/// カレンダー用ビルダー
///
/// カレンダーの範囲指定には複数のユースケースがあるので、
/// それぞれに応じたカレンダーを生成するためのビルダー
///
/// 1. 特定年月のカレンダー
/// 2. 特定年のカレンダー
/// 3. 期間を年月で指定したカレンダー
/// 4. 期間を年で指定したカレンダー
#[derive(Debug)]
pub struct CalendarBuilder<'a> {
    from: Option<&'a str>,
    single: Option<&'a str>,
    until: Option<&'a str>,
}

impl<'a> CalendarBuilder<'a> {
    /// カレンダーオブジェクトを生成する
    ///
    /// # Examples
    ///
    /// ```rust
    /// use koyomi::Calendar;
    ///
    /// let builder = Calendar::build().single("2018").finalize();
    /// assert!(builder.is_ok());
    ///
    /// let builder = Calendar::build().from("2018").until("2019").finalize();
    /// assert!(builder.is_ok());
    ///
    /// let builder = Calendar::build().from("January").finalize();
    /// assert!(builder.is_err());
    /// ```
    pub fn finalize(&self) -> KoyomiResult<Calendar> {
        if let Some(single) = self.single {
            self.single_calendar(single)
        } else {
            let from = self.date_from()?;
            let until = self.date_until()?;
            Calendar::new(from, until)
        }
    }

    /// 期間の始まりを指定する
    ///
    /// # Examples
    ///
    /// ```rust
    /// use koyomi::Calendar;
    ///
    /// let mut builder = Calendar::build();
    /// builder.from("2018");
    /// ```
    pub fn from(mut self, from: &'a str) -> Self {
        self.from = Some(from);
        self
    }

    /// 単一年または単一年月を指定する
    ///
    /// # Examples
    ///
    /// ```rust
    /// use koyomi::Calendar;
    ///
    /// let mut builder = Calendar::build();
    /// builder.single("2018-01");
    /// ```
    pub fn single(mut self, single: &'a str) -> Self {
        self.single = Some(single);
        self
    }

    /// 期間の終わりを指定する
    ///
    /// # Examples
    ///
    /// ```rust
    /// use koyomi::Calendar;
    ///
    /// let mut builder = Calendar::build();
    /// builder.until("2018");
    /// ```
    pub fn until(mut self, until: &'a str) -> Self {
        self.until = Some(until);
        self
    }

    /// カレンダーの開始日を導出する
    fn date_from(&self) -> KoyomiResult<Date> {
        match self.from {
            None => Err(KoyomiError::NotEnough),
            Some(f) => match f.split("-").count() {
                1 => Date::parse(&format!("{}-01-01", f)),
                2 => Date::parse(&format!("{}-01", f)),
                _ => Err(KoyomiError::InvalidFormat(f.into())),
            },
        }
    }

    /// カレンダーの終了日を導出する
    fn date_until(&self) -> KoyomiResult<Date> {
        match self.until {
            None => Err(KoyomiError::NotEnough),
            Some(u) => match u.split("-").count() {
                1 => Date::parse(&format!("{}-12-31", u)),
                2 => self.end_of_month(&format!("{}-01", u)),
                _ => Err(KoyomiError::InvalidFormat(u.into())),
            },
        }
    }

    /// 指定月の末日を導出する
    fn end_of_month(&self, fmt: &str) -> KoyomiResult<Date> {
        let first = Date::parse(fmt)?;
        let (y, m) = (first.year(), first.month());
        Date::from_ymd(y, m, num_days(y, m))
    }

    /// 単一年または単一年月からカレンダーオブジェクトを生成する
    fn single_calendar(&self, ym: &str) -> KoyomiResult<Calendar> {
        let splits = ym.split("-").collect::<Vec<_>>();
        match splits.len() {
            1 => {
                let from = Date::parse(&format!("{}-01-01", ym))?;
                let until = Date::parse(&format!("{}-12-31", ym))?;
                Ok(Calendar { from, until })
            }
            2 => {
                let fmt = format!("{}-{}-01", splits[0], splits[1]);
                let from = Date::parse(&fmt)?;
                let until = self.end_of_month(&fmt)?;
                Ok(Calendar { from, until })
            }
            _ => Err(KoyomiError::InvalidFormat(ym.into())),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn days_of_month() {
        assert_eq!(num_days(2018, 1), 31);
        assert_eq!(num_days(2018, 2), 28);
        assert_eq!(num_days(2018, 3), 31);
        assert_eq!(num_days(2018, 4), 30);
        assert_eq!(num_days(2018, 5), 31);
        assert_eq!(num_days(2018, 6), 30);
        assert_eq!(num_days(2018, 7), 31);
        assert_eq!(num_days(2018, 8), 31);
        assert_eq!(num_days(2018, 9), 30);
        assert_eq!(num_days(2018, 10), 31);
        assert_eq!(num_days(2018, 11), 30);
        assert_eq!(num_days(2018, 12), 31);
    }

    #[test]
    fn days_of_february_with_leap() {
        assert_eq!(num_days(2016, 2), 29);
    }

    #[test]
    #[should_panic]
    fn days_of_invalid_month() {
        assert_eq!(num_days(2018, 13), 30);
    }

    #[test]
    fn leap_year() {
        // うるう年(4で割り切れる)
        assert!(is_leap(2016));
        // うるう年(100と400で割り切れる)
        assert!(is_leap(2000));
        // うるう年ではない(4で割り切れない)
        assert!(!is_leap(2017));
        // うるう年ではない(100で割り切れるが400で割り切れない)
        assert!(!is_leap(2100));
    }

    #[test]
    fn valid_calendar() {
        let from = Date::parse("2018-04-01").unwrap();
        let until = Date::parse("2018-04-30").unwrap();
        assert!(Calendar::new(from, until).is_ok());
    }

    #[test]
    fn invalid_calendar() {
        let from = Date::parse("2018-04-01").unwrap();
        let until = Date::parse("2017-04-01").unwrap();
        assert!(Calendar::new(from, until).is_err());
    }

    #[test]
    fn calendar_from() {
        let from = Date::parse("2018-04-01").unwrap();
        let until = Date::parse("2018-04-30").unwrap();
        let calendar = Calendar::new(from, until).unwrap();
        assert_eq!(calendar.from(), "2018-04-01");
    }

    #[test]
    fn make_calendar() {
        let from = Date::parse("2018-04-01").unwrap();
        let until = Date::parse("2018-04-30").unwrap();
        let cal = Calendar::new(from, until).unwrap().make();
        assert_eq!(cal.len(), 30);
        assert_eq!(cal[0].to_string(), "2018-04-01");
        assert_eq!(cal[29].to_string(), "2018-04-30");
    }

    #[test]
    fn calendar_until() {
        let from = Date::parse("2018-04-01").unwrap();
        let until = Date::parse("2018-04-30").unwrap();
        let calendar = Calendar::new(from, until).unwrap();
        assert_eq!(calendar.until(), "2018-04-30");
    }

    #[test]
    fn builder_single() {
        let c = Calendar::build().single("2018").finalize();
        assert!(c.is_ok());

        let c = Calendar::build().single("2018-04").finalize();
        assert!(c.is_ok());

        let c = Calendar::build().single("abc").finalize();
        assert!(c.is_err());
    }

    #[test]
    fn builder_between() {
        let c = Calendar::build().from("2017").until("2018").finalize();
        assert!(c.is_ok());

        let c = Calendar::build()
            .from("2017-04")
            .until("2018-04")
            .finalize();
        assert!(c.is_ok());

        let c = Calendar::build().from("2017").until("2018-04").finalize();
        assert!(c.is_ok());

        let c = Calendar::build().from("2017-04").until("2018").finalize();
        assert!(c.is_ok());

        let c = Calendar::build().from("abc").until("2018").finalize();
        assert!(c.is_err());

        let c = Calendar::build().from("2017").until("abc").finalize();
        assert!(c.is_err());
    }
}