tzcraft 0.1.0

A schema-driven date & time library: one 128-bit nanosecond timeline, a const civil calendar, and codec-aware wire formats on nextjson / rustbinary.
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
//! Civil date-time: [`CivilDateTime`].
//!
//! A zone-less calendar timestamp: a [`Date`] plus a [`TimeOfDay`]. It is
//! exactly the "wall clock" reading before any offset is applied, so it is
//! the natural type for parsing a local timestamp that carries no zone.
//!
//! Arithmetic is implemented by projecting onto the single `i128` nanosecond
//! timeline (day count × nanoseconds-per-day + nanoseconds-of-day), which
//! makes cross-midnight and cross-year carries a non-event.

use alloc::string::String;
use core::fmt;
use core::str::FromStr;

use crate::calendar::{Month, Weekday, NS_PER_DAY};
use crate::date::Date;
use crate::duration::Duration;
use crate::error::{Error, Result};
use crate::format::{self, FractionDigits};
use crate::strftime;
use crate::ticks::Ticks;
use crate::time::TimeOfDay;
use crate::units::{Days, Months};

/// A calendar date and time without a timezone.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CivilDateTime {
    date: Date,
    time: TimeOfDay,
}

impl CivilDateTime {
    /// Build from parts.
    pub const fn new(date: Date, time: TimeOfDay) -> CivilDateTime {
        CivilDateTime { date, time }
    }

    /// Build from year/month/day/hour/minute/second.
    pub fn from_ymd_hms(
        year: i32,
        month: u32,
        day: u32,
        hour: u32,
        minute: u32,
        second: u32,
    ) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            Date::from_ymd(year, month, day)?,
            TimeOfDay::from_hms(hour, minute, second)?,
        ))
    }

    /// Build from year/month/day/hour/minute/second/millisecond.
    pub fn from_ymd_hms_milli(
        year: i32,
        month: u32,
        day: u32,
        hour: u32,
        minute: u32,
        second: u32,
        milli: u32,
    ) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            Date::from_ymd(year, month, day)?,
            TimeOfDay::from_hms_milli(hour, minute, second, milli)?,
        ))
    }

    /// Build from year/month/day/hour/minute/second/nanosecond.
    pub fn from_ymd_hms_nano(
        year: i32,
        month: u32,
        day: u32,
        hour: u32,
        minute: u32,
        second: u32,
        nano: u32,
    ) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            Date::from_ymd(year, month, day)?,
            TimeOfDay::from_hms_nano(hour, minute, second, nano)?,
        ))
    }

    /// The date part.
    pub const fn date(self) -> Date {
        self.date
    }

    /// The time-of-day part.
    pub const fn time(self) -> TimeOfDay {
        self.time
    }

    /// `(date, time)` parts.
    pub const fn parts(self) -> (Date, TimeOfDay) {
        (self.date, self.time)
    }

    /// Weekday of the date part.
    pub const fn weekday(self) -> Weekday {
        self.date.weekday()
    }

    /// 1-based day of year of the date part.
    pub const fn day_of_year(self) -> u32 {
        self.date.day_of_year()
    }

    /// ISO 8601 week date of the date part.
    pub const fn iso_week(self) -> crate::units::IsoWeek {
        self.date.iso_week()
    }

    /// Year.
    pub const fn year(self) -> i32 {
        self.date.year()
    }

    /// Month (1-based).
    pub const fn month(self) -> u32 {
        self.date.month()
    }

    /// Month as a [`Month`].
    pub fn month_enum(self) -> Month {
        self.date.month_enum()
    }

    /// Day of month.
    pub const fn day(self) -> u32 {
        self.date.day()
    }

    /// Hour.
    pub const fn hour(self) -> u32 {
        self.time.hour()
    }

    /// Minute.
    pub const fn minute(self) -> u32 {
        self.time.minute()
    }

    /// Second.
    pub const fn second(self) -> u32 {
        self.time.second()
    }

    /// Nanosecond within the second.
    pub const fn nanosecond(self) -> u32 {
        self.time.nanosecond()
    }

    /// Checked duration addition; carries across every boundary.
    pub fn checked_add(self, delta: Duration) -> Result<CivilDateTime> {
        let day_ns = self.date.days_since_epoch() as i128 * NS_PER_DAY;
        let total = day_ns
            .checked_add(self.time.nanos_since_midnight() as i128)
            .and_then(|t| t.checked_add(delta.as_nanos()))
            .ok_or_else(Error::overflow)?;
        let days = total.div_euclid(NS_PER_DAY);
        let rem = total.rem_euclid(NS_PER_DAY);
        let days = i64::try_from(days).map_err(|_| Error::out_of_range("date"))?;
        let date = Date::from_days_checked(days)?;
        let time = TimeOfDay::from_nanos_since_midnight(rem as u64)?;
        Ok(CivilDateTime { date, time })
    }

    /// Checked duration subtraction.
    pub fn checked_sub(self, delta: Duration) -> Result<CivilDateTime> {
        self.checked_add(delta.checked_neg()?)
    }

    /// Alias for [`CivilDateTime::checked_add`] (chrono-compatible name).
    pub fn checked_add_signed(self, rhs: Duration) -> Result<CivilDateTime> {
        self.checked_add(rhs)
    }

    /// Alias for [`CivilDateTime::checked_sub`] (chrono-compatible name).
    pub fn checked_sub_signed(self, rhs: Duration) -> Result<CivilDateTime> {
        self.checked_sub(rhs)
    }

    /// The signed duration between `earlier` and `self`.
    pub fn signed_duration_since(self, earlier: CivilDateTime) -> Duration {
        let day_ns = (self.date.days_since_epoch() as i128
            - earlier.date.days_since_epoch() as i128)
            * NS_PER_DAY;
        let time_ns =
            self.time.nanos_since_midnight() as i128 - earlier.time.nanos_since_midnight() as i128;
        Duration::from_nanos(day_ns + time_ns)
    }

    /// Checked day offset; the time part is preserved.
    pub fn checked_add_days(self, days: Days) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date.checked_add_days(days)?,
            self.time,
        ))
    }

    /// Checked day offset in the negative direction.
    pub fn checked_sub_days(self, days: Days) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date.checked_sub_days(days)?,
            self.time,
        ))
    }

    /// Saturating duration addition.
    pub fn saturating_add(self, delta: Duration) -> CivilDateTime {
        match self.checked_add(delta) {
            Ok(dt) => dt,
            Err(_) => {
                if delta.as_nanos() >= 0 {
                    CivilDateTime::new(Date::MAX, TimeOfDay::MAX)
                } else {
                    CivilDateTime::new(Date::MIN, TimeOfDay::MIDNIGHT)
                }
            }
        }
    }

    /// Calendar-aware month stepping; the time part is preserved.
    pub fn checked_add_months(self, months: Months) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date.checked_add_months(months)?,
            self.time,
        ))
    }

    /// Calendar-aware month stepping in the negative direction.
    pub fn checked_sub_months(self, months: Months) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date.checked_sub_months(months)?,
            self.time,
        ))
    }

    /// Calendar-aware year stepping.
    pub fn checked_add_years(self, years: i32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date.checked_add_years(years)?,
            self.time,
        ))
    }

    /// Replace the year (fails for e.g. Feb 29 in a non-leap year).
    pub fn with_year(self, year: i32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(self.date.with_year(year)?, self.time))
    }

    /// Replace the month.
    pub fn with_month(self, month: u32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(self.date.with_month(month)?, self.time))
    }

    /// Replace the day.
    pub fn with_day(self, day: u32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(self.date.with_day(day)?, self.time))
    }

    /// Replace the hour.
    pub fn with_hour(self, hour: u32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(self.date, self.time.with_hour(hour)?))
    }

    /// Replace the minute.
    pub fn with_minute(self, minute: u32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date,
            self.time.with_minute(minute)?,
        ))
    }

    /// Replace the second.
    pub fn with_second(self, second: u32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date,
            self.time.with_second(second)?,
        ))
    }

    /// Replace the nanosecond within the second.
    pub fn with_nanosecond(self, nano: u32) -> Result<CivilDateTime> {
        Ok(CivilDateTime::new(
            self.date,
            self.time.with_nanosecond(nano)?,
        ))
    }

    /// Build from a Unix timestamp (chrono's `from_timestamp_opt`).
    pub fn from_timestamp(seconds: i64, nanos: u32) -> Result<CivilDateTime> {
        Ticks::from_timestamp(seconds, nanos)?.to_civil_utc()
    }

    /// Build from a Unix millisecond timestamp.
    pub fn from_timestamp_millis(millis: i64) -> Result<CivilDateTime> {
        Ticks::from_timestamp_millis(millis)?.to_civil_utc()
    }

    /// Build from a Unix microsecond timestamp.
    pub fn from_timestamp_micros(micros: i64) -> Result<CivilDateTime> {
        Ticks::from_timestamp_micros(micros)?.to_civil_utc()
    }

    /// Interpret this civil reading as UTC and project onto the timeline.
    pub fn to_ticks_utc(self) -> Result<Ticks> {
        let ns = self.date.days_since_epoch() as i128 * NS_PER_DAY
            + self.time.nanos_since_midnight() as i128;
        Ok(Ticks::from_unix_nanos(ns))
    }

    /// Strict ISO 8601 rendering (`YYYY-MM-DDTHH:MM:SS[.fffffffff]`).
    pub fn to_iso(self) -> String {
        format::format_civil(self, FractionDigits::Auto)
    }

    /// Parse a strict ISO 8601 local date-time (no zone designator).
    pub fn from_iso(s: &str) -> Result<CivilDateTime> {
        let (date, time) = format::parse_civil_iso(s)?;
        Ok(CivilDateTime::new(date, time))
    }

    /// strftime-style rendering, e.g. `dt.format("%Y-%m-%d %H:%M:%S")`.
    ///
    /// The `%`-directive set (with `%-`/`%_`/`%0` padding modifiers) is
    /// documented on [`crate::Ticks::format`].
    pub fn format(self, fmt: &str) -> Result<String> {
        strftime::format_civil(self, fmt)
    }

    /// Parse with a strftime-style format string (chrono's
    /// `NaiveDateTime::parse_from_str`).
    pub fn parse_from_str(s: &str, fmt: &str) -> Result<CivilDateTime> {
        strftime::parse_civil(fmt, s)
    }
}

impl fmt::Display for CivilDateTime {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_iso())
    }
}

impl FromStr for CivilDateTime {
    type Err = Error;

    fn from_str(s: &str) -> Result<CivilDateTime> {
        CivilDateTime::from_iso(s)
    }
}

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

    #[test]
    fn parts_and_projection() {
        let dt = CivilDateTime::from_ymd_hms_nano(2024, 1, 2, 3, 4, 5, 6).unwrap();
        assert_eq!(dt.year(), 2024);
        assert_eq!(dt.month(), 1);
        assert_eq!(dt.day(), 2);
        assert_eq!(dt.hour(), 3);
        assert_eq!(dt.minute(), 4);
        assert_eq!(dt.second(), 5);
        assert_eq!(dt.nanosecond(), 6);
        assert_eq!(dt.weekday(), Weekday::Tuesday);

        let t = dt.to_ticks_utc().unwrap();
        let back = t.to_civil_utc().unwrap();
        assert_eq!(back, dt);
    }

    #[test]
    fn arithmetic_carries_across_days() {
        let dt = CivilDateTime::from_ymd_hms(2024, 12, 31, 23, 59, 59).unwrap();
        let next = dt.checked_add(Duration::from_seconds(2)).unwrap();
        assert_eq!(next.to_iso(), "2025-01-01T00:00:01");
        let back = dt.checked_sub(Duration::from_seconds(2)).unwrap();
        assert_eq!(back.to_iso(), "2024-12-31T23:59:57");
    }

    #[test]
    fn months_keep_time() {
        let dt = CivilDateTime::from_ymd_hms(2024, 1, 31, 12, 30, 0).unwrap();
        let next = dt.checked_add_months(Months::new(1)).unwrap();
        assert_eq!(next.to_iso(), "2024-02-29T12:30:00");
        assert_eq!(next.time(), TimeOfDay::from_hms(12, 30, 0).unwrap());
    }

    #[test]
    fn iso_round_trips() {
        for s in [
            "2024-01-01T00:00:00",
            "2024-02-29T23:59:59.123",
            "2024-06-15T08:30:00.123456789",
        ] {
            let dt = CivilDateTime::from_iso(s).unwrap_or_else(|e| panic!("{s}: {e}"));
            assert_eq!(dt.to_iso(), s, "{s}");
        }
        assert!(CivilDateTime::from_iso("2024-01-01T00:00:00Z").is_err());
        assert!(CivilDateTime::from_iso("2024-01-01").is_err());
    }
}