reifydb-value 0.9.0

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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

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

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

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

#[repr(transparent)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Default)]
pub struct Date {
	days_since_epoch: i32,
}

impl Date {
	#[inline]
	pub fn is_leap_year(year: i32) -> bool {
		(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
	}

	#[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,
		}
	}

	fn ymd_to_days_since_epoch(year: i32, month: u32, day: u32) -> Option<i32> {
		if !(1..=12).contains(&month) || day < 1 || day > Self::days_in_month(year, month) {
			return None;
		}

		let (y, m) = if month <= 2 {
			(year - 1, month as i32 + 9)
		} else {
			(year, month as i32 - 3)
		};

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

		Some(days)
	}

	fn days_since_epoch_to_ymd(days: i32) -> (i32, u32, u32) {
		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;
		let yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
		let y = yoe + era * 400;
		let doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
		let mp = (5 * doy + 2) / 153;
		let d = doy - (153 * mp + 2) / 5 + 1;
		let m = if mp < 10 {
			mp + 3
		} else {
			mp - 9
		};
		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
	}

	pub fn to_days_since_epoch(&self) -> i32 {
		self.days_since_epoch
	}

	pub fn from_days_since_epoch(days: i32) -> Option<Self> {
		if !(-365_250_000..=365_250_000).contains(&days) {
			return None;
		}
		Some(Self {
			days_since_epoch: days,
		})
	}

	pub fn saturating_add(self, rhs: Duration) -> Date {
		const NANOS_PER_DAY: i128 = 86_400_000_000_000;
		let total = rhs.as_nanos().unwrap_or(if rhs.is_negative() {
			i64::MIN
		} else {
			i64::MAX
		});
		let days = (self.days_since_epoch as i128 + total as i128 / NANOS_PER_DAY)
			.clamp(-365_250_000, 365_250_000);
		Self {
			days_since_epoch: days as i32,
		}
	}

	pub fn saturating_sub(self, rhs: Duration) -> Date {
		const NANOS_PER_DAY: i128 = 86_400_000_000_000;
		let total = rhs.as_nanos().unwrap_or(if rhs.is_negative() {
			i64::MIN
		} else {
			i64::MAX
		});
		let days = (self.days_since_epoch as i128 - total as i128 / NANOS_PER_DAY)
			.clamp(-365_250_000, 365_250_000);
		Self {
			days_since_epoch: days as i32,
		}
	}
}

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)
		}
	}
}

impl Serialize for Date {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		serializer.serialize_i32(self.days_since_epoch)
	}
}

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 as days since the Unix epoch (i32)")
	}

	fn visit_i32<E>(self, value: i32) -> Result<Date, E>
	where
		E: de::Error,
	{
		Date::from_days_since_epoch(value)
			.ok_or_else(|| E::custom(format!("date days out of range: {}", value)))
	}

	fn visit_i64<E>(self, value: i64) -> Result<Date, E>
	where
		E: de::Error,
	{
		let days = i32::try_from(value).map_err(|_| E::custom(format!("date days out of range: {}", value)))?;
		self.visit_i32(days)
	}

	fn visit_u64<E>(self, value: u64) -> Result<Date, E>
	where
		E: de::Error,
	{
		let days = i32::try_from(value).map_err(|_| E::custom(format!("date days out of range: {}", value)))?;
		self.visit_i32(days)
	}
}

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

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

	use postcard::{from_bytes, to_allocvec};
	use serde_json::{from_str, to_string};

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

	#[test]
	fn test_date_display_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() {
		let date = Date::new(1970, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "1970-01-01");

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

		// Single-digit month and day must be zero-padded to keep the width fixed.
		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() {
		let date = Date::new(1, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "0001-01-01");

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

		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 is 1 BC in the proleptic calendar; BC years render with a leading minus.
		let date = Date::new(0, 1, 1).unwrap();
		assert_eq!(format!("{}", date), "0000-01-01");

		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() {
		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() {
		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();
		// Wire format is the raw days-since-epoch integer, not an ISO-8601 string.
		assert_eq!(json, date.to_days_since_epoch().to_string());

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

	#[test]
	fn test_serde_postcard_roundtrip_negative_years() {
		// Postcard is the CDC wire format; pre-epoch dates are negative and must survive it.
		for (y, m, d) in [(-100, 12, 31), (0, 1, 1), (1970, 1, 1), (2024, 3, 15), (9999, 12, 31)] {
			let date = Date::new(y, m, d).unwrap();
			let bytes = to_allocvec(&date).unwrap();
			let recovered: Date = from_bytes(&bytes).unwrap();
			assert_eq!(date, recovered);
			assert_eq!(recovered.year(), y);
			assert_eq!(recovered.month(), m);
			assert_eq!(recovered.day(), d);
		}
	}

	#[test]
	fn test_deserialize_rejects_out_of_range_days() {
		// Days beyond the supported Date range must not decode.
		let json = 400_000_000i64.to_string();
		assert!(from_str::<Date>(&json).is_err());
	}

	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));
	}

	#[test]
	fn saturating_add_sub_whole_days() {
		// Adding/subtracting a whole-day Duration shifts the date by exactly that many days.
		let base = Date::from_ymd(2024, 1, 15).unwrap();

		let forward = base.saturating_add(Duration::from_days(2).unwrap());
		assert_eq!(forward, Date::from_ymd(2024, 1, 17).unwrap());

		let backward = base.saturating_sub(Duration::from_days(2).unwrap());
		assert_eq!(backward, Date::from_ymd(2024, 1, 13).unwrap());
	}

	#[test]
	fn saturating_sub_day_truncates() {
		// Date has day resolution, so a duration truncates to whole days rather than rounding.
		let base = Date::from_ymd(2024, 1, 15).unwrap();

		let half_day = base.saturating_add(Duration::from_seconds(12 * 3600).unwrap());
		assert_eq!(half_day, base, "12h is sub-day and must not change the date");

		let day_and_half = base.saturating_add(Duration::from_seconds(36 * 3600).unwrap());
		assert_eq!(day_and_half, Date::from_ymd(2024, 1, 16).unwrap(), "36h truncates to 1 whole day");
	}

	#[test]
	fn saturating_add_clamps_at_max() {
		// Adding past the valid upper bound saturates rather than overflowing the i32.
		let max = Date::from_days_since_epoch(365_250_000).unwrap();
		let clamped = max.saturating_add(Duration::from_days(10).unwrap());
		assert_eq!(clamped.to_days_since_epoch(), 365_250_000);
	}

	#[test]
	fn saturating_sub_clamps_at_min() {
		// Subtracting past the valid lower bound saturates rather than underflowing the i32.
		let min = Date::from_days_since_epoch(-365_250_000).unwrap();
		let clamped = min.saturating_sub(Duration::from_days(10).unwrap());
		assert_eq!(clamped.to_days_since_epoch(), -365_250_000);
	}
}