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
//! Naive `PrimitiveDateTime` connections.
//!
//! Hosts [`PDTMDATE`] — drops sub-day time fragments to a calendar-
//! date rung.
use crate::extended::Extended;
use time::{Date, PrimitiveDateTime, Time};
fn pdtmdate_ceil(p: PrimitiveDateTime) -> Extended<Date> {
if p.eq(&PrimitiveDateTime::MIN) {
return Extended::NegInf;
}
let d = p.date();
if p.time().eq(&Time::MIDNIGHT) {
return Extended::Finite(d);
}
// time() > MIDNIGHT — round up to the next day.
match d.next_day() {
Some(next) => Extended::Finite(next),
None => Extended::PosInf,
}
}
fn pdtmdate_inner(b: Extended<Date>) -> PrimitiveDateTime {
match b {
Extended::NegInf => PrimitiveDateTime::MIN,
Extended::Finite(d) => PrimitiveDateTime::new(d, Time::MIDNIGHT),
Extended::PosInf => PrimitiveDateTime::MAX,
}
}
crate::conn_l! {
/// `PrimitiveDateTime → Extended<Date>` — naive datetime ↔ calendar
/// date, rounding sub-day fractions up to the next whole day.
///
/// One-sided left-Galois Conn (`Conn::new_l(ceil, inner)`).
/// `inner` is non-injective at the source extremes — both
/// `inner(NegInf)` and `inner(Finite(Date::MIN))` decode to
/// `PrimitiveDateTime::MIN` (per the `time` crate's representation),
/// which makes `inner` non-order-reflecting on the rung side. The
/// constructor wires `floor = ceil` as a fn pointer so `floor(a) ≤
/// ceil(a)` holds structurally.
///
/// **Behavioral note on rounding:** `ceil(p) = Finite(p.date())` when
/// `p.time() == MIDNIGHT`, otherwise `Finite(p.date().next_day())`
/// (or `PosInf` if `p.date() == Date::MAX` and `p` carries a sub-day
/// fraction). Because `floor = ceil` under `new_left`, callers
/// reading `PDTMDATE.ceil(p)` get the **rounded-up** answer (next
/// day), not the truncated `Finite(p.date())` they'd get under the
/// previous full-triple. Callers needing the truncating direction
/// can compute it explicitly: `Extended::Finite(p.date())` for `p !=
/// PrimitiveDateTime::MAX`.
///
/// # Examples
///
/// ```rust
/// use connections::time::PDTMDATE;
/// use connections::extended::Extended;
/// use time::{Date, Month, PrimitiveDateTime, Time};
///
/// let day = Date::from_calendar_date(2026, Month::April, 26).unwrap();
///
/// // Midnight: ceil resolves to the same day.
/// let p_midnight = PrimitiveDateTime::new(day, Time::MIDNIGHT);
/// assert_eq!(PDTMDATE.ceil(p_midnight), Extended::Finite(day));
///
/// // Any later time: ceil rolls forward to next day.
/// let p_later = PrimitiveDateTime::new(day, Time::from_hms(0, 0, 1).unwrap());
/// assert_eq!(PDTMDATE.ceil(p_later), Extended::Finite(day.next_day().unwrap()));
///
/// assert_eq!(PDTMDATE.upper(Extended::Finite(day)), p_midnight);
/// ```
pub PDTMDATE : PrimitiveDateTime => Extended<Date> {
ceil: pdtmdate_ceil,
inner: pdtmdate_inner,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[allow(unused_imports)]
use crate::conn::{ConnL, ConnR};
use crate::prop::arb::{arb_date, arb_extended_date, arb_primitive_dt};
use crate::prop::{conn as conn_laws, lattice as lattice_laws};
use proptest::prelude::*;
use time::Month;
// ── Preorder laws on `PrimitiveDateTime` ────────────────────
mod primitive_dt_preorder {
use super::*;
proptest! {
#[test]
fn reflexive(x in arb_primitive_dt()) {
prop_assert!(lattice_laws::lattice_reflexive(&x));
}
#[test]
fn transitive(
x in arb_primitive_dt(),
y in arb_primitive_dt(),
z in arb_primitive_dt(),
) {
prop_assert!(lattice_laws::lattice_transitive(&x, &y, &z));
}
#[test]
fn antisymmetric(x in arb_primitive_dt(), y in arb_primitive_dt()) {
prop_assert!(lattice_laws::lattice_antisymmetric(&x, &y));
}
#[test]
fn bot(x in arb_primitive_dt()) {
prop_assert!(lattice_laws::lattice_bot(&PrimitiveDateTime::MIN, &x));
}
#[test]
fn top(x in arb_primitive_dt()) {
prop_assert!(lattice_laws::lattice_top(&PrimitiveDateTime::MAX, &x));
}
}
}
// ── PDTMDATE spot checks ────────────────────────────────────
#[test]
fn midnight_fixes_date() {
let d = Date::from_calendar_date(2000, Month::January, 1).unwrap();
let p = PrimitiveDateTime::new(d, Time::MIDNIGHT);
assert_eq!(PDTMDATE.ceil(p), Extended::Finite(d));
assert_eq!(PDTMDATE.upper(Extended::Finite(d)), p);
}
#[test]
fn one_ns_after_midnight_ceils_to_next_day() {
let d = Date::from_calendar_date(2000, Month::January, 1).unwrap();
let p = PrimitiveDateTime::new(d, Time::from_hms_nano(0, 0, 0, 1).unwrap());
assert_eq!(PDTMDATE.ceil(p), Extended::Finite(d.next_day().unwrap()));
// `new_left` wires `floor = ceil` structurally.
assert_eq!(PDTMDATE.ceil(p), Extended::Finite(d.next_day().unwrap()));
}
#[test]
fn extremes() {
assert_eq!(PDTMDATE.ceil(PrimitiveDateTime::MIN), Extended::NegInf);
// `floor = ceil` structurally — both NegInf at PDT::MIN.
assert_eq!(PDTMDATE.ceil(PrimitiveDateTime::MIN), Extended::NegInf);
assert_eq!(PDTMDATE.ceil(PrimitiveDateTime::MAX), Extended::PosInf);
assert_eq!(PDTMDATE.ceil(PrimitiveDateTime::MAX), Extended::PosInf);
assert_eq!(PDTMDATE.upper(Extended::NegInf), PrimitiveDateTime::MIN);
assert_eq!(PDTMDATE.upper(Extended::PosInf), PrimitiveDateTime::MAX);
}
#[test]
fn date_max_with_subday_ceils_to_posinf() {
let p = PrimitiveDateTime::new(Date::MAX, Time::from_hms_nano(0, 0, 0, 1).unwrap());
assert_eq!(PDTMDATE.ceil(p), Extended::PosInf);
}
// ── PDTMDATE Galois law battery (one-sided L) ───────────────
proptest! {
#[test]
fn galois_l(p in arb_primitive_dt(), b in arb_extended_date()) {
prop_assert!(conn_laws::galois_l(&PDTMDATE, p, b));
}
#[test]
fn closure_l(p in arb_primitive_dt()) {
prop_assert!(conn_laws::closure_l(&PDTMDATE, p));
}
#[test]
fn kernel_l(b in arb_extended_date()) {
prop_assert!(conn_laws::kernel_l(&PDTMDATE, b));
}
#[test]
fn monotone_l(a in arb_primitive_dt(), b in arb_primitive_dt()) {
prop_assert!(conn_laws::monotone_l(&PDTMDATE, a, b));
}
#[test]
fn idempotent(p in arb_primitive_dt()) {
prop_assert!(conn_laws::idempotent(&PDTMDATE, p));
}
// Roundtrip on Finite Date excluding Date::MIN. At
// exactly d = Date::MIN, `inner(Finite(MIN))` = PDT::MIN
// and `ceil(PDT::MIN)` = NegInf (Galois-forced — same
// equivalence class as Finite(MIN), but distinct
// representatives); ceil's roundtrip is therefore
// non-strict at the lower extreme.
#[test]
fn roundtrip_ceil(d in arb_date().prop_filter("excludes Date::MIN", |d| *d != Date::MIN)) {
prop_assert!(conn_laws::roundtrip_ceil(&PDTMDATE, Extended::Finite(d)));
}
}
}