relativedelta 0.3.2

Complex date arithmetic: add/subtract months and years without errors, target weekdays, lock dates. Better than Duration for semantic calendar operations.
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

// This file is partially derived from the chrono crate
// Copyright (c) 2014, Kang Seonghoon.
// Licensed under:
// - MIT license (http://opensource.org/licenses/MIT)
// - Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// Modifications Copyright (c) 2025 Tim Gorm Kaas-Rasmussen Olsen/RelativeDelta
// Original source: https://github.com/chronotope/chrono/blob/main/src/weekday.rs

use core::fmt;

/// The day of week.
///
/// The order of the days of week depends on the context.
/// (This is why this type does *not* implement `PartialOrd` or `Ord` traits.)
/// One should prefer `*_from_monday` or `*_from_sunday` methods to get the correct result.
///
/// # Example
/// ```
/// use relativedelta::Weekday;
///
/// let sunday = Weekday::try_from(6).unwrap();
/// assert_eq!(sunday, Weekday::Sun);
///
/// assert_eq!(sunday.num_days_from_monday(), 6); // starts counting with Monday = 0
/// assert_eq!(sunday.number_from_monday(), 7); // starts counting with Monday = 1
///
/// assert_eq!(sunday.succ(), Weekday::Mon);
/// assert_eq!(sunday.pred(), Weekday::Sat);
/// ```
#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum Weekday {
	/// Monday.
	#[cfg_attr(feature = "serde", serde(alias = "Monday"))]
	Mon = 0,
	/// Tuesday.
	#[cfg_attr(feature = "serde", serde(alias = "Tuesday"))]
	Tue = 1,
	/// Wednesday.
	#[cfg_attr(feature = "serde", serde(alias = "Wednesday"))]
	Wed = 2,
	/// Thursday.
	#[cfg_attr(feature = "serde", serde(alias = "Thursday"))]
	Thu = 3,
	/// Friday.
	#[cfg_attr(feature = "serde", serde(alias = "Friday"))]
	Fri = 4,
	/// Saturday.
	#[cfg_attr(feature = "serde", serde(alias = "Saturday"))]
	Sat = 5,
	/// Sunday.
	#[cfg_attr(feature = "serde", serde(alias = "Sunday"))]
	Sun = 6,
}

impl Weekday {
	/// The next day in the week.
	///
	/// `w`:        | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
	/// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
	/// `w.succ()`: | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun` | `Mon`
	#[inline]
	#[must_use]
	pub const fn succ(&self) -> Self {
		match *self {
			Self::Mon => Self::Tue,
			Self::Tue => Self::Wed,
			Self::Wed => Self::Thu,
			Self::Thu => Self::Fri,
			Self::Fri => Self::Sat,
			Self::Sat => Self::Sun,
			Self::Sun => Self::Mon,
		}
	}

	/// The previous day in the week.
	///
	/// `w`:        | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
	/// ----------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
	/// `w.pred()`: | `Sun` | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat`
	#[inline]
	#[must_use]
	pub const fn pred(&self) -> Self {
		match *self {
			Self::Mon => Self::Sun,
			Self::Tue => Self::Mon,
			Self::Wed => Self::Tue,
			Self::Thu => Self::Wed,
			Self::Fri => Self::Thu,
			Self::Sat => Self::Fri,
			Self::Sun => Self::Sat,
		}
	}

	/// Returns a day-of-week number starting from Monday = 1. (ISO 8601 weekday number)
	///
	/// `w`:                      | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
	/// ------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
	/// `w.number_from_monday()`: | 1     | 2     | 3     | 4     | 5     | 6     | 7
	#[inline]
	#[must_use]
	pub const fn number_from_monday(&self) -> u8 {
		self.days_since(Self::Mon) + 1
	}

	/// Returns a day-of-week number starting from Monday = 0.
	///
	/// `w`:                        | `Mon` | `Tue` | `Wed` | `Thu` | `Fri` | `Sat` | `Sun`
	/// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
	/// `w.num_days_from_monday()`: | 0     | 1     | 2     | 3     | 4     | 5     | 6
	#[inline]
	#[must_use]
	pub const fn num_days_from_monday(&self) -> u8 {
		self.days_since(Self::Mon)
	}

	/// The number of days since the given day.
	///
	/// # Examples
	///
	/// ```
	/// use relativedelta::Weekday::*;
	/// assert_eq!(Mon.days_since(Mon), 0);
	/// assert_eq!(Sun.days_since(Tue), 5);
	/// assert_eq!(Wed.days_since(Sun), 3);
	/// ```
	#[must_use]
	pub const fn days_since(&self, other: Self) -> u8 {
		let lhs = *self as u8;
		let rhs = other as u8;
		if lhs < rhs { 7 + lhs - rhs } else { lhs - rhs }
	}
}

impl fmt::Display for Weekday {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		f.pad(match *self {
			Self::Mon => "Mon",
			Self::Tue => "Tue",
			Self::Wed => "Wed",
			Self::Thu => "Thu",
			Self::Fri => "Fri",
			Self::Sat => "Sat",
			Self::Sun => "Sun",
		})
	}
}

/// Any weekday can be represented as an integer from 0 to 6, which equals to
/// [`Weekday::num_days_from_monday`](#method.num_days_from_monday) in this implementation.
/// Do not heavily depend on this though; use explicit methods whenever possible.
impl From<u8> for Weekday {
	fn from(value: u8) -> Self {
		let value = value % 7; // Ensure the value is within 0-6
		match value {
			0 => Self::Mon,
			1 => Self::Tue,
			2 => Self::Wed,
			3 => Self::Thu,
			4 => Self::Fri,
			5 => Self::Sat,
			6 => Self::Sun,
			_ => unreachable!(), // This case should never happen due to the modulo operation
		}
	}
}

/// Any weekday can be represented as an integer from 0 to 6, which equals to
/// [`Weekday::num_days_from_monday`](#method.num_days_from_monday) in this implementation.
/// Do not heavily depend on this though; use explicit methods whenever possible.
impl num_traits::FromPrimitive for Weekday {
	#[inline]
	fn from_i64(n: i64) -> Option<Weekday> {
		match n {
			0 => Some(Self::Mon),
			1 => Some(Self::Tue),
			2 => Some(Self::Wed),
			3 => Some(Self::Thu),
			4 => Some(Self::Fri),
			5 => Some(Self::Sat),
			6 => Some(Self::Sun),
			_ => None,
		}
	}

	#[inline]
	fn from_u64(n: u64) -> Option<Weekday> {
		match n {
			0 => Some(Self::Mon),
			1 => Some(Self::Tue),
			2 => Some(Self::Wed),
			3 => Some(Self::Thu),
			4 => Some(Self::Fri),
			5 => Some(Self::Sat),
			6 => Some(Self::Sun),
			_ => None,
		}
	}
}
#[cfg(test)]
mod tests {
	use num_traits::FromPrimitive;
	use similar_asserts::assert_eq;

	use super::*;

	#[test]
	fn test_days_since() {
		for i in 0..7 {
			let base_day = Weekday::from(i);

			assert_eq!(
				base_day.num_days_from_monday(),
				base_day.days_since(Weekday::Mon)
			);

			assert_eq!(base_day.days_since(base_day), 0);

			assert_eq!(base_day.days_since(base_day.pred()), 1);
			assert_eq!(base_day.days_since(base_day.pred().pred()), 2);
			assert_eq!(base_day.days_since(base_day.pred().pred().pred()), 3);
			assert_eq!(base_day.days_since(base_day.pred().pred().pred().pred()), 4);
			assert_eq!(
				base_day.days_since(base_day.pred().pred().pred().pred().pred()),
				5
			);
			assert_eq!(
				base_day.days_since(base_day.pred().pred().pred().pred().pred().pred()),
				6
			);

			assert_eq!(base_day.days_since(base_day.succ()), 6);
			assert_eq!(base_day.days_since(base_day.succ().succ()), 5);
			assert_eq!(base_day.days_since(base_day.succ().succ().succ()), 4);
			assert_eq!(base_day.days_since(base_day.succ().succ().succ().succ()), 3);
			assert_eq!(
				base_day.days_since(base_day.succ().succ().succ().succ().succ()),
				2
			);
			assert_eq!(
				base_day.days_since(base_day.succ().succ().succ().succ().succ().succ()),
				1
			);
		}
	}

	#[test]
	#[cfg(feature = "serde")]
	fn test_serde_serialize() {
		use std::vec;

		use Weekday::*;
		use serde_json::to_string;

		let cases: vec::Vec<(Weekday, &str)> = vec![
			(Mon, "\"Mon\""),
			(Tue, "\"Tue\""),
			(Wed, "\"Wed\""),
			(Thu, "\"Thu\""),
			(Fri, "\"Fri\""),
			(Sat, "\"Sat\""),
			(Sun, "\"Sun\""),
		];

		for (weekday, expected_str) in cases {
			let string = to_string(&weekday).unwrap();
			assert_eq!(string, expected_str);
		}
	}

	#[test]
	#[cfg(feature = "serde")]
	fn test_serde_deserialize() {
		use std::vec;

		use Weekday::*;
		use serde_json::from_str;

		let cases: vec::Vec<(&str, Weekday)> = vec![
			("\"Mon\"", Mon),
			("\"Monday\"", Mon),
			("\"Tue\"", Tue),
			("\"Tuesday\"", Tue),
			("\"Wed\"", Wed),
			("\"Wednesday\"", Wed),
			("\"Thu\"", Thu),
			("\"Thursday\"", Thu),
			("\"Fri\"", Fri),
			("\"Friday\"", Fri),
			("\"Sat\"", Sat),
			("\"Saturday\"", Sat),
			("\"Sun\"", Sun),
			("\"Sunday\"", Sun),
		];

		for (str, expected_weekday) in cases {
			let weekday = from_str::<Weekday>(str).unwrap();
			assert_eq!(weekday, expected_weekday);
		}

		let errors: vec::Vec<&str> = vec![
			"\"not a weekday\"",
			"\"monDAYs\"",
			"\"mond\"",
			"mon",
			"\"thur\"",
			"\"thurs\"",
		];

		for str in errors {
			from_str::<Weekday>(str).unwrap_err();
		}
	}

	#[test]
	fn test_number_from_monday() {
		for i in 0..7 {
			let day = Weekday::from(i);
			assert_eq!(day.number_from_monday(), i + 1);
			assert_eq!(day.num_days_from_monday(), i);
		}
	}

	#[test]
	fn test_from_u64() {
		assert_eq!(Weekday::from_u64(0), Some(Weekday::Mon));
		assert_eq!(Weekday::from_u64(1), Some(Weekday::Tue));
		assert_eq!(Weekday::from_u64(2), Some(Weekday::Wed));
		assert_eq!(Weekday::from_u64(3), Some(Weekday::Thu));
		assert_eq!(Weekday::from_u64(4), Some(Weekday::Fri));
		assert_eq!(Weekday::from_u64(5), Some(Weekday::Sat));
		assert_eq!(Weekday::from_u64(6), Some(Weekday::Sun));
		assert_eq!(Weekday::from_u64(7), None);
	}

	#[test]
	fn test_from_i64() {
		assert_eq!(Weekday::from_i64(0), Some(Weekday::Mon));
		assert_eq!(Weekday::from_i64(1), Some(Weekday::Tue));
		assert_eq!(Weekday::from_i64(2), Some(Weekday::Wed));
		assert_eq!(Weekday::from_i64(3), Some(Weekday::Thu));
		assert_eq!(Weekday::from_i64(4), Some(Weekday::Fri));
		assert_eq!(Weekday::from_i64(5), Some(Weekday::Sat));
		assert_eq!(Weekday::from_i64(6), Some(Weekday::Sun));
		assert_eq!(Weekday::from_i64(7), None);
	}

	#[test]
	#[cfg(feature = "schemars")]
	fn test_schemars_schema() {
		use schemars::JsonSchema;
		use schemars::generate::SchemaGenerator;

		let mut generator = SchemaGenerator::default();
		let schema = Weekday::json_schema(&mut generator);

		// Verify schema is generated successfully with enum variants
		// Enums are represented with oneOf containing const values
		let one_of = schema
			.get("oneOf")
			.and_then(|v| v.as_array())
			.expect("Schema should have oneOf variants");

		assert!(
			one_of.len() >= 7,
			"Schema should contain at least 7 weekday variants"
		);

		// Extract the const values from each variant
		let variants: Vec<String> = one_of
			.iter()
			.filter_map(|v| v.get("const").and_then(|c| c.as_str()).map(String::from))
			.collect();

		// Verify all weekday variants are in the schema
		assert!(
			variants.contains(&"Mon".to_string()),
			"Schema should contain Mon variant"
		);
		assert!(
			variants.contains(&"Tue".to_string()),
			"Schema should contain Tue variant"
		);
		assert!(
			variants.contains(&"Wed".to_string()),
			"Schema should contain Wed variant"
		);
		assert!(
			variants.contains(&"Thu".to_string()),
			"Schema should contain Thu variant"
		);
		assert!(
			variants.contains(&"Fri".to_string()),
			"Schema should contain Fri variant"
		);
		assert!(
			variants.contains(&"Sat".to_string()),
			"Schema should contain Sat variant"
		);
		assert!(
			variants.contains(&"Sun".to_string()),
			"Schema should contain Sun variant"
		);
	}

	#[test]
	#[cfg(all(feature = "schemars", feature = "serde"))]
	fn test_schemars_serde_compatibility() {
		use schemars::JsonSchema;
		use schemars::generate::SchemaGenerator;

		// Create instance and verify it can be serialized while schema is defined
		let weekday = Weekday::Mon;

		// Verify schema generation doesn't fail with serde enabled
		let mut generator = SchemaGenerator::default();
		let schema = Weekday::json_schema(&mut generator);
		assert!(
			schema.get("oneOf").is_some(),
			"Schema should generate successfully with serde"
		);

		// Verify serialization works with schema available
		let serialized = serde_json::to_string(&weekday).unwrap();
		let deserialized: Weekday = serde_json::from_str(&serialized).unwrap();
		assert_eq!(
			weekday, deserialized,
			"Serde + Schemars should work together"
		);
	}
}