expressive-cron 0.1.0

A fluent builder for cron expressions, inspired by Laravel.
Documentation
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::correctness)]
//! A fluent builder for cron expressions, inspired by Laravel.

use chrono::{DateTime, Datelike, Duration, NaiveDate, NaiveTime, Utc};
use chrono_tz::Tz;

mod types;

pub use crate::types::{DayOfWeek, IntoCronValue};

type Filter = Box<dyn Fn() -> bool + Send + Sync>;

/// Fluent builder for a 5-field cron expression with optional time-of-day filters and a timezone.
pub struct Cron {
	expression: String,
	timezone: Tz,
	repeat_seconds: u32,
	filters: Vec<Filter>,
	reject_filters: Vec<Filter>,
}

impl Default for Cron {
	fn default() -> Self {
		Self::new()
	}
}

impl Cron {
	/// Create a new builder initialised to `"* * * * *"` (every minute) in UTC.
	#[must_use]
	pub fn new() -> Self {
		Self {
			expression: "* * * * *".to_string(),
			timezone: Tz::UTC,
			repeat_seconds: 0,
			filters: Vec::new(),
			reject_filters: Vec::new(),
		}
	}

	/// The current cron expression.
	#[must_use]
	pub fn expression(&self) -> &str {
		&self.expression
	}

	/// The sub-minute repeat interval in seconds, or `0` if none is configured.
	///
	/// Set by [`Cron::every_second`] and its siblings.
	#[must_use]
	pub const fn repeat_seconds(&self) -> u32 {
		self.repeat_seconds
	}

	/// Evaluate all registered filters: returns `true` if every `when` filter returns `true` and no `skip` filter returns `true`.
	#[must_use]
	pub fn filters_pass(&self) -> bool {
		self.filters.iter().all(|f| f()) && !self.reject_filters.iter().any(|f| f())
	}

	/// Set the raw cron expression.
	pub fn cron(&mut self, expression: &str) -> &mut Self {
		self.expression = expression.to_string();
		self
	}

	/// Register a filter; the event runs only when every `when` filter returns `true`.
	pub fn when<F: Fn() -> bool + Send + Sync + 'static>(&mut self, filter: F) -> &mut Self {
		self.filters.push(Box::new(filter));
		self
	}

	/// Register a reject filter; the event is skipped whenever any `skip` filter returns `true`.
	pub fn skip<F: Fn() -> bool + Send + Sync + 'static>(&mut self, filter: F) -> &mut Self {
		self.reject_filters.push(Box::new(filter));
		self
	}

	/// Schedule the event to run only between two times of day (e.g. `"09:00"`, `"17:00"`).
	///
	/// Times are parsed as `HH:MM[:SS]`. Windows that cross midnight (`end < start`)
	/// are handled by rolling one side to the adjacent day.
	pub fn between(&mut self, start_time: &str, end_time: &str) -> &mut Self {
		let filter = self.in_time_interval(start_time, end_time);
		self.filters.push(filter);
		self
	}

	/// Schedule the event to run except between two times of day.
	///
	/// The inverse of [`Cron::between`].
	pub fn unless_between(&mut self, start_time: &str, end_time: &str) -> &mut Self {
		let filter = self.in_time_interval(start_time, end_time);
		self.reject_filters.push(filter);
		self
	}

	/// Schedule the event to run every second.
	pub fn every_second(&mut self) -> &mut Self {
		self.repeat_every(1)
	}

	/// Schedule the event to run every two seconds.
	pub fn every_two_seconds(&mut self) -> &mut Self {
		self.repeat_every(2)
	}

	/// Schedule the event to run every five seconds.
	pub fn every_five_seconds(&mut self) -> &mut Self {
		self.repeat_every(5)
	}

	/// Schedule the event to run every ten seconds.
	pub fn every_ten_seconds(&mut self) -> &mut Self {
		self.repeat_every(10)
	}

	/// Schedule the event to run every fifteen seconds.
	pub fn every_fifteen_seconds(&mut self) -> &mut Self {
		self.repeat_every(15)
	}

	/// Schedule the event to run every twenty seconds.
	pub fn every_twenty_seconds(&mut self) -> &mut Self {
		self.repeat_every(20)
	}

	/// Schedule the event to run every thirty seconds.
	pub fn every_thirty_seconds(&mut self) -> &mut Self {
		self.repeat_every(30)
	}

	/// Schedule the event to run every minute.
	pub fn every_minute(&mut self) -> &mut Self {
		self.splice_into_position(1, "*")
	}

	/// Schedule the event to run every two minutes.
	pub fn every_two_minutes(&mut self) -> &mut Self {
		self.splice_into_position(1, "*/2")
	}

	/// Schedule the event to run every three minutes.
	pub fn every_three_minutes(&mut self) -> &mut Self {
		self.splice_into_position(1, "*/3")
	}

	/// Schedule the event to run every four minutes.
	pub fn every_four_minutes(&mut self) -> &mut Self {
		self.splice_into_position(1, "*/4")
	}

	/// Schedule the event to run every five minutes.
	pub fn every_five_minutes(&mut self) -> &mut Self {
		self.splice_into_position(1, "*/5")
	}

	/// Schedule the event to run every ten minutes.
	pub fn every_ten_minutes(&mut self) -> &mut Self {
		self.splice_into_position(1, "*/10")
	}

	/// Schedule the event to run every fifteen minutes.
	pub fn every_fifteen_minutes(&mut self) -> &mut Self {
		self.splice_into_position(1, "*/15")
	}

	/// Schedule the event to run every thirty minutes.
	pub fn every_thirty_minutes(&mut self) -> &mut Self {
		self.splice_into_position(1, "*/30")
	}

	/// Schedule the event to run hourly (at minute 0).
	pub fn hourly(&mut self) -> &mut Self {
		self.splice_into_position(1, "0")
	}

	/// Schedule the event to run hourly at a given offset (a minute, list, or pattern) in the hour.
	pub fn hourly_at(&mut self, offset: impl IntoCronValue) -> &mut Self {
		let s = offset.into_cron_value();
		self.hour_based_schedule(&s, "*")
	}

	/// Schedule the event to run every odd hour.
	pub fn every_odd_hour(&mut self) -> &mut Self {
		self.hour_based_schedule("0", "1-23/2")
	}

	/// Schedule the event to run every odd hour at a given minute offset.
	pub fn every_odd_hour_at(&mut self, offset: impl IntoCronValue) -> &mut Self {
		let s = offset.into_cron_value();
		self.hour_based_schedule(&s, "1-23/2")
	}

	/// Schedule the event to run every two hours.
	pub fn every_two_hours(&mut self) -> &mut Self {
		self.hour_based_schedule("0", "*/2")
	}

	/// Schedule the event to run every two hours at a given minute offset.
	pub fn every_two_hours_at(&mut self, offset: impl IntoCronValue) -> &mut Self {
		let s = offset.into_cron_value();
		self.hour_based_schedule(&s, "*/2")
	}

	/// Schedule the event to run every three hours.
	pub fn every_three_hours(&mut self) -> &mut Self {
		self.hour_based_schedule("0", "*/3")
	}

	/// Schedule the event to run every three hours at a given minute offset.
	pub fn every_three_hours_at(&mut self, offset: impl IntoCronValue) -> &mut Self {
		let s = offset.into_cron_value();
		self.hour_based_schedule(&s, "*/3")
	}

	/// Schedule the event to run every four hours.
	pub fn every_four_hours(&mut self) -> &mut Self {
		self.hour_based_schedule("0", "*/4")
	}

	/// Schedule the event to run every four hours at a given minute offset.
	pub fn every_four_hours_at(&mut self, offset: impl IntoCronValue) -> &mut Self {
		let s = offset.into_cron_value();
		self.hour_based_schedule(&s, "*/4")
	}

	/// Schedule the event to run every six hours.
	pub fn every_six_hours(&mut self) -> &mut Self {
		self.hour_based_schedule("0", "*/6")
	}

	/// Schedule the event to run every six hours at a given minute offset.
	pub fn every_six_hours_at(&mut self, offset: impl IntoCronValue) -> &mut Self {
		let s = offset.into_cron_value();
		self.hour_based_schedule(&s, "*/6")
	}

	/// Schedule the event to run daily at midnight.
	pub fn daily(&mut self) -> &mut Self {
		self.hour_based_schedule("0", "0")
	}

	/// Alias for [`Cron::daily_at`].
	pub fn at(&mut self, time: &str) -> &mut Self {
		self.daily_at(time)
	}

	/// Schedule the event to run daily at a given time, formatted as `HH:MM` (e.g. `"10:00"`, `"19:30"`).
	///
	/// Seconds, if provided, are ignored.
	pub fn daily_at(&mut self, time: &str) -> &mut Self {
		let segments: Vec<&str> = time.split(':').collect();
		let hour: u32 = segments.first().and_then(|s| s.parse().ok()).unwrap_or(0);
		let minute: u32 = segments.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);
		let hour_s = hour.to_string();
		let minute_s = minute.to_string();
		self.hour_based_schedule(&minute_s, &hour_s)
	}

	/// Schedule the event to run twice daily at the given hours (minute 0).
	pub fn twice_daily(&mut self, first: u32, second: u32) -> &mut Self {
		self.twice_daily_at(first, second, 0)
	}

	/// Schedule the event to run twice daily at the given hours and minute offset.
	pub fn twice_daily_at(&mut self, first: u32, second: u32, offset: u32) -> &mut Self {
		let hours = format!("{first},{second}");
		let offset_s = offset.to_string();
		self.hour_based_schedule(&offset_s, &hours)
	}

	/// Schedule the event to run only on weekdays (Monday–Friday).
	pub fn weekdays(&mut self) -> &mut Self {
		let s = format!("{}-{}", DayOfWeek::Monday as u32, DayOfWeek::Friday as u32);
		self.days(s)
	}

	/// Schedule the event to run only on weekends (Saturday and Sunday).
	pub fn weekends(&mut self) -> &mut Self {
		let s = format!(
			"{},{}",
			DayOfWeek::Saturday as u32,
			DayOfWeek::Sunday as u32
		);
		self.days(s)
	}

	/// Schedule the event to run only on Mondays.
	pub fn mondays(&mut self) -> &mut Self {
		self.days(DayOfWeek::Monday)
	}

	/// Schedule the event to run only on Tuesdays.
	pub fn tuesdays(&mut self) -> &mut Self {
		self.days(DayOfWeek::Tuesday)
	}

	/// Schedule the event to run only on Wednesdays.
	pub fn wednesdays(&mut self) -> &mut Self {
		self.days(DayOfWeek::Wednesday)
	}

	/// Schedule the event to run only on Thursdays.
	pub fn thursdays(&mut self) -> &mut Self {
		self.days(DayOfWeek::Thursday)
	}

	/// Schedule the event to run only on Fridays.
	pub fn fridays(&mut self) -> &mut Self {
		self.days(DayOfWeek::Friday)
	}

	/// Schedule the event to run only on Saturdays.
	pub fn saturdays(&mut self) -> &mut Self {
		self.days(DayOfWeek::Saturday)
	}

	/// Schedule the event to run only on Sundays.
	pub fn sundays(&mut self) -> &mut Self {
		self.days(DayOfWeek::Sunday)
	}

	/// Schedule the event to run weekly (Sundays at midnight).
	pub fn weekly(&mut self) -> &mut Self {
		self.splice_into_position(1, "0");
		self.splice_into_position(2, "0");
		self.splice_into_position(5, "0")
	}

	/// Schedule the event to run weekly on a given day and time.
	pub fn weekly_on(&mut self, day_of_week: impl IntoCronValue, time: &str) -> &mut Self {
		self.daily_at(time);
		self.days(day_of_week)
	}

	/// Schedule the event to run monthly (first of the month at midnight).
	pub fn monthly(&mut self) -> &mut Self {
		self.splice_into_position(1, "0");
		self.splice_into_position(2, "0");
		self.splice_into_position(3, "1")
	}

	/// Schedule the event to run monthly on a given day and time.
	pub fn monthly_on(&mut self, day_of_month: u32, time: &str) -> &mut Self {
		self.daily_at(time);
		let s = day_of_month.to_string();
		self.splice_into_position(3, &s)
	}

	/// Schedule the event to run twice monthly on the given days at midnight.
	pub fn twice_monthly(&mut self, first: u32, second: u32) -> &mut Self {
		self.twice_monthly_at(first, second, "0:0")
	}

	/// Schedule the event to run twice monthly on the given days at a given time.
	pub fn twice_monthly_at(&mut self, first: u32, second: u32, time: &str) -> &mut Self {
		let days = format!("{first},{second}");
		self.daily_at(time);
		self.splice_into_position(3, &days)
	}

	/// Schedule the event to run on the last day of the month at midnight.
	///
	/// The last-day value is resolved against *current* time at build, so the resulting
	/// expression is anchored to the day-count of the current month (28–31).
	pub fn last_day_of_month(&mut self) -> &mut Self {
		self.last_day_of_month_at("0:0")
	}

	/// Schedule the event to run on the last day of the month at a given time.
	///
	/// See [`Cron::last_day_of_month`] for the caveat on when the day-of-month is resolved.
	pub fn last_day_of_month_at(&mut self, time: &str) -> &mut Self {
		self.daily_at(time);
		let now = current_time(self.timezone);
		let last = last_day_of(now.year(), now.month());
		let s = last.to_string();
		self.splice_into_position(3, &s)
	}

	/// Schedule the event to run on specific days of the month at midnight.
	pub fn days_of_month(&mut self, days: &[u32]) -> &mut Self {
		self.daily_at("0:0");
		let s = days
			.iter()
			.map(ToString::to_string)
			.collect::<Vec<_>>()
			.join(",");
		self.splice_into_position(3, &s)
	}

	/// Schedule the event to run quarterly (first day of Jan, Apr, Jul, Oct at midnight).
	pub fn quarterly(&mut self) -> &mut Self {
		self.splice_into_position(1, "0");
		self.splice_into_position(2, "0");
		self.splice_into_position(3, "1");
		self.splice_into_position(4, "1-12/3")
	}

	/// Schedule the event to run quarterly on a given day-of-quarter and time.
	pub fn quarterly_on(&mut self, day_of_quarter: u32, time: &str) -> &mut Self {
		self.daily_at(time);
		let s = day_of_quarter.to_string();
		self.splice_into_position(3, &s);
		self.splice_into_position(4, "1-12/3")
	}

	/// Schedule the event to run yearly (January 1st at midnight).
	pub fn yearly(&mut self) -> &mut Self {
		self.splice_into_position(1, "0");
		self.splice_into_position(2, "0");
		self.splice_into_position(3, "1");
		self.splice_into_position(4, "1")
	}

	/// Schedule the event to run yearly on a given month, day, and time.
	pub fn yearly_on(
		&mut self,
		month: u32,
		day_of_month: impl IntoCronValue,
		time: &str,
	) -> &mut Self {
		self.daily_at(time);
		let m = month.to_string();
		let d = day_of_month.into_cron_value();
		self.splice_into_position(3, &d);
		self.splice_into_position(4, &m)
	}

	/// Set the days of the week the event should run on (the 5th cron field).
	pub fn days(&mut self, days: impl IntoCronValue) -> &mut Self {
		let s = days.into_cron_value();
		self.splice_into_position(5, &s)
	}

	/// Set the timezone the time-based filters ([`Cron::between`],
	/// [`Cron::unless_between`], [`Cron::last_day_of_month`]) are evaluated in.
	pub const fn timezone(&mut self, timezone: Tz) -> &mut Self {
		self.timezone = timezone;
		self
	}
}

impl Cron {
	fn in_time_interval(&self, start_time: &str, end_time: &str) -> Filter {
		let tz = self.timezone;
		let start = parse_time_of_day(start_time);
		let end = parse_time_of_day(end_time);

		Box::new(move || {
			let now = current_time(tz);
			let today = now.date_naive();
			let Some(mut start_dt) = today.and_time(start).and_local_timezone(tz).single() else {
				return false;
			};
			let Some(mut end_dt) = today.and_time(end).and_local_timezone(tz).single() else {
				return false;
			};

			if end_dt < start_dt {
				if start_dt > now {
					start_dt -= Duration::days(1);
				} else {
					end_dt += Duration::days(1);
				}
			}

			now >= start_dt && now <= end_dt
		})
	}

	fn repeat_every(&mut self, seconds: u32) -> &mut Self {
		assert!(
			seconds > 0,
			"The seconds [{seconds}] must be greater than zero."
		);
		assert!(
			60 % seconds == 0,
			"The seconds [{seconds}] are not evenly divisible by 60."
		);
		self.repeat_seconds = seconds;
		self.every_minute()
	}

	fn hour_based_schedule(&mut self, minutes: &str, hours: &str) -> &mut Self {
		self.splice_into_position(1, minutes);
		self.splice_into_position(2, hours)
	}

	fn splice_into_position(&mut self, position: usize, value: &str) -> &mut Self {
		let mut segments: Vec<String> = self
			.expression
			.split_whitespace()
			.map(ToString::to_string)
			.collect();
		while segments.len() < position {
			segments.push("*".to_string());
		}
		segments[position - 1] = value.to_string();
		self.expression = segments.join(" ");
		self
	}
}

fn parse_time_of_day(time: &str) -> NaiveTime {
	let parts: Vec<&str> = time.split(':').collect();
	let h: u32 = parts.first().and_then(|s| s.parse().ok()).unwrap_or(0);
	let m: u32 = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);
	let s: u32 = parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);
	NaiveTime::from_hms_opt(h, m, s).unwrap_or(NaiveTime::MIN)
}

fn last_day_of(year: i32, month: u32) -> u32 {
	let (y, m) = if month == 12 {
		(year + 1, 1)
	} else {
		(year, month + 1)
	};
	let first_of_next = NaiveDate::from_ymd_opt(y, m, 1).unwrap();
	let last = first_of_next - Duration::days(1);
	last.day()
}

#[cfg(test)]
thread_local! {
	static TEST_NOW: std::cell::Cell<Option<DateTime<Utc>>> =
		const { std::cell::Cell::new(None) };
}

fn current_time(tz: Tz) -> DateTime<Tz> {
	#[cfg(test)]
	if let Some(t) = TEST_NOW.with(std::cell::Cell::get) {
		return t.with_timezone(&tz);
	}

	Utc::now().with_timezone(&tz)
}

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

	#[must_use = "the guard restores the previous test-now on drop; bind it to a variable"]
	struct TestNowGuard(Option<DateTime<Utc>>);

	impl Drop for TestNowGuard {
		fn drop(&mut self) {
			TEST_NOW.with(|c| c.set(self.0));
		}
	}

	fn set_test_now(now: DateTime<Utc>) -> TestNowGuard {
		let prev = TEST_NOW.with(|c| c.replace(Some(now)));
		TestNowGuard(prev)
	}

	fn event() -> Cron {
		Cron::new()
	}

	#[test]
	fn every_minute() {
		let mut e = event();
		assert_eq!("* * * * *", e.expression());
		assert_eq!("* * * * *", e.every_minute().expression());
	}

	#[test]
	fn every_x_minutes() {
		let mut e = event();
		assert_eq!("*/2 * * * *", e.every_two_minutes().expression());
		assert_eq!("*/3 * * * *", e.every_three_minutes().expression());
		assert_eq!("*/4 * * * *", e.every_four_minutes().expression());
		assert_eq!("*/5 * * * *", e.every_five_minutes().expression());
		assert_eq!("*/10 * * * *", e.every_ten_minutes().expression());
		assert_eq!("*/15 * * * *", e.every_fifteen_minutes().expression());
		assert_eq!("*/30 * * * *", e.every_thirty_minutes().expression());
	}

	#[test]
	fn daily() {
		let mut e = event();
		assert_eq!("0 0 * * *", e.daily().expression());
	}

	#[test]
	fn daily_at() {
		let mut e = event();
		assert_eq!("8 13 * * *", e.daily_at("13:08").expression());
	}

	#[test]
	fn daily_at_parses_minutes_and_ignores_seconds_when_seconds_are_defined() {
		let mut e = event();
		assert_eq!("8 13 * * *", e.daily_at("13:08:10").expression());
	}

	#[test]
	fn twice_daily() {
		let mut e = event();
		assert_eq!("0 3,15 * * *", e.twice_daily(3, 15).expression());
	}

	#[test]
	fn twice_daily_at() {
		let mut e = event();
		assert_eq!("5 3,15 * * *", e.twice_daily_at(3, 15, 5).expression());
	}

	#[test]
	fn weekly() {
		let mut e = event();
		assert_eq!("0 0 * * 0", e.weekly().expression());
	}

	#[test]
	fn weekly_on() {
		let mut e = event();
		assert_eq!("0 8 * * 1", e.weekly_on(1u32, "8:00").expression());
	}

	#[test]
	fn override_with_hourly() {
		let mut e = event();
		assert_eq!("0 * * * *", e.every_five_minutes().hourly().expression());
		assert_eq!("37 * * * *", e.hourly_at(37).expression());
		assert_eq!("*/10 * * * *", e.hourly_at("*/10").expression());
		assert_eq!("15,30,45 * * * *", e.hourly_at([15, 30, 45]).expression());
	}

	#[test]
	fn hourly() {
		let mut e = event();
		assert_eq!("0 1-23/2 * * *", e.every_odd_hour().expression());
		assert_eq!("0 */2 * * *", e.every_two_hours().expression());
		assert_eq!("0 */3 * * *", e.every_three_hours().expression());
		assert_eq!("0 */4 * * *", e.every_four_hours().expression());
		assert_eq!("0 */6 * * *", e.every_six_hours().expression());

		assert_eq!("37 1-23/2 * * *", e.every_odd_hour_at(37).expression());
		assert_eq!("37 */2 * * *", e.every_two_hours_at(37).expression());
		assert_eq!("37 */3 * * *", e.every_three_hours_at(37).expression());
		assert_eq!("37 */4 * * *", e.every_four_hours_at(37).expression());
		assert_eq!("37 */6 * * *", e.every_six_hours_at(37).expression());

		assert_eq!(
			"*/10 1-23/2 * * *",
			e.every_odd_hour_at("*/10").expression()
		);
		assert_eq!("*/10 */2 * * *", e.every_two_hours_at("*/10").expression());
		assert_eq!(
			"*/10 */3 * * *",
			e.every_three_hours_at("*/10").expression()
		);
		assert_eq!("*/10 */4 * * *", e.every_four_hours_at("*/10").expression());
		assert_eq!("*/10 */6 * * *", e.every_six_hours_at("*/10").expression());

		assert_eq!(
			"15,30,45 1-23/2 * * *",
			e.every_odd_hour_at([15, 30, 45]).expression()
		);
		assert_eq!(
			"15,30,45 */2 * * *",
			e.every_two_hours_at([15, 30, 45]).expression()
		);
		assert_eq!(
			"15,30,45 */3 * * *",
			e.every_three_hours_at([15, 30, 45]).expression()
		);
		assert_eq!(
			"15,30,45 */4 * * *",
			e.every_four_hours_at([15, 30, 45]).expression()
		);
		assert_eq!(
			"15,30,45 */6 * * *",
			e.every_six_hours_at([15, 30, 45]).expression()
		);
	}

	#[test]
	fn monthly() {
		let mut e = event();
		assert_eq!("0 0 1 * *", e.monthly().expression());
	}

	#[test]
	fn monthly_on() {
		let mut e = event();
		assert_eq!("0 15 4 * *", e.monthly_on(4, "15:00").expression());
	}

	#[test]
	fn last_day_of_month() {
		let _g = set_test_now(Utc.with_ymd_and_hms(2020, 10, 10, 10, 10, 10).unwrap());
		let mut e = event();
		assert_eq!("0 0 31 * *", e.last_day_of_month().expression());
	}

	#[test]
	fn twice_monthly() {
		let mut e = event();
		assert_eq!("0 0 1,16 * *", e.twice_monthly(1, 16).expression());
	}

	#[test]
	fn twice_monthly_at_time() {
		let mut e = event();
		assert_eq!(
			"30 1 1,16 * *",
			e.twice_monthly_at(1, 16, "1:30").expression()
		);
	}

	#[test]
	fn monthly_on_with_minutes() {
		let mut e = event();
		assert_eq!("15 15 4 * *", e.monthly_on(4, "15:15").expression());
	}

	#[test]
	fn weekdays_daily() {
		let mut e = event();
		assert_eq!("0 0 * * 1-5", e.weekdays().daily().expression());
	}

	#[test]
	fn weekdays_hourly() {
		let mut e = event();
		assert_eq!("0 * * * 1-5", e.weekdays().hourly().expression());
	}

	#[test]
	fn weekdays() {
		let mut e = event();
		assert_eq!("* * * * 1-5", e.weekdays().expression());
	}

	#[test]
	fn weekends() {
		let mut e = event();
		assert_eq!("* * * * 6,0", e.weekends().expression());
	}

	#[test]
	fn sundays() {
		let mut e = event();
		assert_eq!("* * * * 0", e.sundays().expression());
	}

	#[test]
	fn mondays() {
		let mut e = event();
		assert_eq!("* * * * 1", e.mondays().expression());
	}

	#[test]
	fn tuesdays() {
		let mut e = event();
		assert_eq!("* * * * 2", e.tuesdays().expression());
	}

	#[test]
	fn wednesdays() {
		let mut e = event();
		assert_eq!("* * * * 3", e.wednesdays().expression());
	}

	#[test]
	fn thursdays() {
		let mut e = event();
		assert_eq!("* * * * 4", e.thursdays().expression());
	}

	#[test]
	fn fridays() {
		let mut e = event();
		assert_eq!("* * * * 5", e.fridays().expression());
	}

	#[test]
	fn saturdays() {
		let mut e = event();
		assert_eq!("* * * * 6", e.saturdays().expression());
	}

	#[test]
	fn quarterly() {
		let mut e = event();
		assert_eq!("0 0 1 1-12/3 *", e.quarterly().expression());
	}

	#[test]
	fn yearly() {
		let mut e = event();
		assert_eq!("0 0 1 1 *", e.yearly().expression());
	}

	#[test]
	fn yearly_on() {
		let mut e = event();
		assert_eq!("8 15 5 4 *", e.yearly_on(4, 5u32, "15:08").expression());
	}

	#[test]
	fn yearly_on_mondays_only() {
		let mut e = event();
		assert_eq!(
			"1 9 * 7 1",
			e.mondays().yearly_on(7, "*", "09:01").expression()
		);
	}

	#[test]
	fn yearly_on_tuesdays_and_day_of_month_20() {
		let mut e = event();
		assert_eq!(
			"1 9 20 7 2",
			e.tuesdays().yearly_on(7, 20u32, "09:01").expression()
		);
	}
}