reifydb-type 0.4.11

Core type system and value representations for ReifyDB
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2025 ReifyDB

use std::fmt::{self, Display, Formatter};

use serde::{
	Deserialize, Deserializer, Serialize, Serializer,
	de::{self, Visitor},
};

use crate::{
	error::{TemporalKind, TypeError},
	fragment::Fragment,
};

/// A date value representing a calendar date (year, month, day) without time
/// information. Always interpreted in SVTC.
///
/// Internally stored as days since Unix epoch (1970-01-01).
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct Date {
	// Days since Unix epoch (1970-01-01)
	// Negative values represent dates before 1970
	days_since_epoch: i32,
}

// Calendar utilities
impl Date {
	/// Check if a year is a leap year
	#[inline]
	pub fn is_leap_year(year: i32) -> bool {
		(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
	}

	/// Get the number of days in a month
	#[inline]
	pub fn days_in_month(year: i32, month: u32) -> u32 {
		match month {
			1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
			4 | 6 | 9 | 11 => 30,
			2 => {
				if Self::is_leap_year(year) {
					29
				} else {
					28
				}
			}
			_ => 0,
		}
	}

	/// Convert year/month/day to days since Unix epoch
	fn ymd_to_days_since_epoch(year: i32, month: u32, day: u32) -> Option<i32> {
		// Validate input
		if !(1..=12).contains(&month) || day < 1 || day > Self::days_in_month(year, month) {
			return None;
		}

		// Algorithm based on Howard Hinnant's date algorithms
		// Convert month from [1,12] to [0,11] where Mar=0
		let (y, m) = if month <= 2 {
			(year - 1, month as i32 + 9) // Jan->10, Feb->11
		} else {
			(year, month as i32 - 3) // Mar->0, Apr->1, ..., Dec->9
		};

		let era = if y >= 0 {
			y
		} else {
			y - 399
		} / 400;
		let yoe = y - era * 400; // [0, 399]
		let doy = (153 * m + 2) / 5 + day as i32 - 1; // [0, 365]
		let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; // [0, 146096]
		let days = era * 146097 + doe - 719468;

		Some(days)
	}

	/// Convert days since Unix epoch to year/month/day
	fn days_since_epoch_to_ymd(days: i32) -> (i32, u32, u32) {
		// Adjust to the algorithm's epoch
		let days_since_ce = days + 719468;

		let era = if days_since_ce >= 0 {
			days_since_ce
		} else {
			days_since_ce - 146096
		} / 146097;
		let doe = days_since_ce - era * 146097; // [0, 146096]
		let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365; // [0, 399]
		let y = yoe + era * 400;
		let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); // [0, 365]
		let mp = (5 * doy + 2) / 153; // [0, 11]
		let d = doy - (153 * mp + 2) / 5 + 1; // [1, 31]
		let m = if mp < 10 {
			mp + 3
		} else {
			mp - 9
		}; // [1, 12]
		let year = if m <= 2 {
			y + 1
		} else {
			y
		};

		(year, m as u32, d as u32)
	}
}

impl Date {
	fn overflow_err(message: impl Into<String>) -> TypeError {
		TypeError::Temporal {
			kind: TemporalKind::DateOverflow {
				message: message.into(),
			},
			message: "date overflow".to_string(),
			fragment: Fragment::None,
		}
	}

	pub fn new(year: i32, month: u32, day: u32) -> Option<Self> {
		Self::ymd_to_days_since_epoch(year, month, day).map(|days_since_epoch| Self {
			days_since_epoch,
		})
	}

	pub fn from_ymd(year: i32, month: u32, day: u32) -> Result<Self, Box<TypeError>> {
		Self::new(year, month, day).ok_or_else(|| {
			Box::new(Self::overflow_err(format!("invalid date: {}-{:02}-{:02}", year, month, day)))
		})
	}

	pub fn year(&self) -> i32 {
		Self::days_since_epoch_to_ymd(self.days_since_epoch).0
	}

	pub fn month(&self) -> u32 {
		Self::days_since_epoch_to_ymd(self.days_since_epoch).1
	}

	pub fn day(&self) -> u32 {
		Self::days_since_epoch_to_ymd(self.days_since_epoch).2
	}

	/// Convert to days since Unix epoch for storage
	pub fn to_days_since_epoch(&self) -> i32 {
		self.days_since_epoch
	}

	/// Create from days since Unix epoch for storage
	pub fn from_days_since_epoch(days: i32) -> Option<Self> {
		// Validate the range (approximately -1 million to +1 million
		// years from 1970)
		if !(-365_250_000..=365_250_000).contains(&days) {
			return None;
		}
		Some(Self {
			days_since_epoch: days,
		})
	}
}

impl Display for Date {
	fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
		let (year, month, day) = Self::days_since_epoch_to_ymd(self.days_since_epoch);
		if year < 0 {
			write!(f, "-{:04}-{:02}-{:02}", -year, month, day)
		} else {
			write!(f, "{:04}-{:02}-{:02}", year, month, day)
		}
	}
}

// Serde implementation for ISO 8601 format
impl Serialize for Date {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		serializer.serialize_str(&self.to_string())
	}
}

struct DateVisitor;

impl<'de> Visitor<'de> for DateVisitor {
	type Value = Date;

	fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
		formatter.write_str("a date in ISO 8601 format (YYYY-MM-DD)")
	}

	fn visit_str<E>(self, value: &str) -> Result<Date, E>
	where
		E: de::Error,
	{
		// Parse ISO 8601 date format: YYYY-MM-DD
		let parts: Vec<&str> = value.split('-').collect();

		if parts.len() != 3 {
			return Err(E::custom(format!("invalid date format: {}", value)));
		}

		// Handle negative years
		let (year_str, month_str, day_str) = if parts[0].is_empty() && parts.len() == 4 {
			// Negative year case: "-YYYY-MM-DD" splits as
			// ["", "YYYY", "MM", "DD"]
			(format!("-{}", parts[1]), parts[2], parts[3])
		} else {
			(parts[0].to_string(), parts[1], parts[2])
		};

		let year = year_str.parse::<i32>().map_err(|_| E::custom(format!("invalid year: {}", year_str)))?;
		let month = month_str.parse::<u32>().map_err(|_| E::custom(format!("invalid month: {}", month_str)))?;
		let day = day_str.parse::<u32>().map_err(|_| E::custom(format!("invalid day: {}", day_str)))?;

		Date::new(year, month, day)
			.ok_or_else(|| E::custom(format!("invalid date: {}-{:02}-{:02}", year, month, day)))
	}
}

impl<'de> Deserialize<'de> for Date {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: Deserializer<'de>,
	{
		deserializer.deserialize_str(DateVisitor)
	}
}

#[cfg(test)]
pub mod tests {
	use std::fmt::Debug;

	use serde_json::{from_str, to_string};

	use super::*;
	use crate::error::{TemporalKind, TypeError};

	#[test]
	fn test_date_display_standard_dates() {
		// Standard dates
		let date = Date::new(2024, 3, 15).unwrap();
		assert_eq!(format!("{}", date), "2024-03-15");

		let date = Date::new(2000, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "2000-01-01");

		let date = Date::new(1999, 12, 31).unwrap();
		assert_eq!(format!("{}", date), "1999-12-31");
	}

	#[test]
	fn test_date_display_edge_cases() {
		// Unix epoch
		let date = Date::new(1970, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "1970-01-01");

		// Leap year
		let date = Date::new(2024, 2, 29).unwrap();
		assert_eq!(format!("{}", date), "2024-02-29");

		// Single digit day/month
		let date = Date::new(2024, 1, 9).unwrap();
		assert_eq!(format!("{}", date), "2024-01-09");

		let date = Date::new(2024, 9, 1).unwrap();
		assert_eq!(format!("{}", date), "2024-09-01");
	}

	#[test]
	fn test_date_display_boundary_dates() {
		// Very early date
		let date = Date::new(1, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "0001-01-01");

		// Far future date
		let date = Date::new(9999, 12, 31).unwrap();
		assert_eq!(format!("{}", date), "9999-12-31");

		// Century boundaries
		let date = Date::new(1900, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "1900-01-01");

		let date = Date::new(2000, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "2000-01-01");

		let date = Date::new(2100, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "2100-01-01");
	}

	#[test]
	fn test_date_display_negative_years() {
		// Year 0 (1 BC)
		let date = Date::new(0, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "0000-01-01");

		// Negative years (BC)
		let date = Date::new(-1, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "-0001-01-01");

		let date = Date::new(-100, 12, 31).unwrap();
		assert_eq!(format!("{}", date), "-0100-12-31");
	}

	#[test]
	fn test_date_display_default() {
		let date = Date::default();
		assert_eq!(format!("{}", date), "1970-01-01");
	}

	#[test]
	fn test_date_display_all_months() {
		let months = [
			(1, "01"),
			(2, "02"),
			(3, "03"),
			(4, "04"),
			(5, "05"),
			(6, "06"),
			(7, "07"),
			(8, "08"),
			(9, "09"),
			(10, "10"),
			(11, "11"),
			(12, "12"),
		];

		for (month, expected) in months {
			let date = Date::new(2024, month, 15).unwrap();
			assert_eq!(format!("{}", date), format!("2024-{}-15", expected));
		}
	}

	#[test]
	fn test_date_display_days_in_month() {
		// Test first and last days of various months
		let test_cases = [
			(2024, 1, 1, "2024-01-01"),
			(2024, 1, 31, "2024-01-31"),
			(2024, 2, 1, "2024-02-01"),
			(2024, 2, 29, "2024-02-29"), // Leap year
			(2024, 4, 1, "2024-04-01"),
			(2024, 4, 30, "2024-04-30"),
			(2024, 12, 1, "2024-12-01"),
			(2024, 12, 31, "2024-12-31"),
		];

		for (year, month, day, expected) in test_cases {
			let date = Date::new(year, month, day).unwrap();
			assert_eq!(format!("{}", date), expected);
		}
	}

	#[test]
	fn test_date_roundtrip() {
		// Test that converting to/from days preserves the date
		let test_dates = [
			(1900, 1, 1),
			(1970, 1, 1),
			(2000, 2, 29), // Leap year
			(2024, 12, 31),
			(2100, 6, 15),
		];

		for (year, month, day) in test_dates {
			let date = Date::new(year, month, day).unwrap();
			let days = date.to_days_since_epoch();
			let recovered = Date::from_days_since_epoch(days).unwrap();

			assert_eq!(date.year(), recovered.year());
			assert_eq!(date.month(), recovered.month());
			assert_eq!(date.day(), recovered.day());
		}
	}

	#[test]
	fn test_leap_year_detection() {
		assert!(Date::is_leap_year(2000)); // Divisible by 400
		assert!(Date::is_leap_year(2024)); // Divisible by 4, not by 100
		assert!(!Date::is_leap_year(1900)); // Divisible by 100, not by 400
		assert!(!Date::is_leap_year(2023)); // Not divisible by 4
	}

	#[test]
	fn test_invalid_dates() {
		assert!(Date::new(2024, 0, 1).is_none()); // Invalid month
		assert!(Date::new(2024, 13, 1).is_none()); // Invalid month
		assert!(Date::new(2024, 1, 0).is_none()); // Invalid day
		assert!(Date::new(2024, 1, 32).is_none()); // Invalid day
		assert!(Date::new(2023, 2, 29).is_none()); // Not a leap year
		assert!(Date::new(2024, 4, 31).is_none()); // April has 30 days
	}

	#[test]
	fn test_serde_roundtrip() {
		let date = Date::new(2024, 3, 15).unwrap();
		let json = to_string(&date).unwrap();
		assert_eq!(json, "\"2024-03-15\"");

		let recovered: Date = from_str(&json).unwrap();
		assert_eq!(date, recovered);
	}

	fn assert_date_overflow<T: Debug>(result: Result<T, Box<TypeError>>) {
		let err = result.expect_err("expected DateOverflow error");
		match *err {
			TypeError::Temporal {
				kind: TemporalKind::DateOverflow {
					..
				},
				..
			} => {}
			other => panic!("expected DateOverflow, got: {:?}", other),
		}
	}

	#[test]
	fn test_from_ymd_invalid_month() {
		assert_date_overflow(Date::from_ymd(2024, 0, 1));
		assert_date_overflow(Date::from_ymd(2024, 13, 1));
	}

	#[test]
	fn test_from_ymd_invalid_day() {
		assert_date_overflow(Date::from_ymd(2024, 1, 0));
		assert_date_overflow(Date::from_ymd(2024, 1, 32));
	}

	#[test]
	fn test_from_ymd_non_leap_year() {
		assert_date_overflow(Date::from_ymd(2023, 2, 29));
	}
}