timefilter 0.1.0

Human-readable time string parsing and filtering with comparison operators (e.g., ">=7d", "<2h", "2024-05-01")
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Core types, constants, and parsing logic.
//!
//! All error strings live in `.rodata` — no heap `String` allocation
//! in error paths.

use std::error::Error;
use std::fmt;
use std::str::FromStr;

use chrono::{DateTime, Duration, Local, NaiveDateTime, Utc};

// ── TimeOp ───────────────────────────────────────────────────────────────────

/// Time comparison operator.
///
/// Mirrors [`sizefilter::SizeOp`](https://docs.rs/sizefilter) but is an
/// independent type — the two may evolve differently.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeOp {
    /// Greater than (`>`)
    Gt,
    /// Greater than or equal to (`>=`)
    Ge,
    /// Less than (`<`)
    Lt,
    /// Less than or equal to (`<=`)
    Le,
    /// Equal to (`=`)
    Eq,
}

impl TimeOp {
    /// All variants, in declaration order.
    pub const ALL: [TimeOp; 5] = [TimeOp::Gt, TimeOp::Ge, TimeOp::Lt, TimeOp::Le, TimeOp::Eq];

    /// Apply this operator to two `DateTime<Utc>` values.
    #[inline]
    #[must_use]
    pub fn applies(self, value: DateTime<Utc>, threshold: DateTime<Utc>) -> bool {
        match self {
            TimeOp::Gt => value > threshold,
            TimeOp::Ge => value >= threshold,
            TimeOp::Lt => value < threshold,
            TimeOp::Le => value <= threshold,
            TimeOp::Eq => value == threshold,
        }
    }
}

impl fmt::Display for TimeOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            TimeOp::Gt => ">",
            TimeOp::Ge => ">=",
            TimeOp::Lt => "<",
            TimeOp::Le => "<=",
            TimeOp::Eq => "=",
        })
    }
}

// ── TimeFilter ───────────────────────────────────────────────────────────────

/// A time filter with operator (e.g., `>=7d`, `<2026-05-01`).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TimeFilter {
    pub op: TimeOp,
    pub time: DateTime<Utc>,
}

impl TimeFilter {
    /// Create a new filter from an operator and threshold time.
    #[inline]
    #[must_use]
    pub const fn new(op: TimeOp, time: DateTime<Utc>) -> Self {
        TimeFilter { op, time }
    }

    /// Filter: `value > threshold`.
    #[inline]
    #[must_use]
    pub fn gt(time: DateTime<Utc>) -> Self { TimeFilter { op: TimeOp::Gt, time } }

    /// Filter: `value >= threshold`.
    #[inline]
    #[must_use]
    pub fn ge(time: DateTime<Utc>) -> Self { TimeFilter { op: TimeOp::Ge, time } }

    /// Filter: `value < threshold`.
    #[inline]
    #[must_use]
    pub fn lt(time: DateTime<Utc>) -> Self { TimeFilter { op: TimeOp::Lt, time } }

    /// Filter: `value <= threshold`.
    #[inline]
    #[must_use]
    pub fn le(time: DateTime<Utc>) -> Self { TimeFilter { op: TimeOp::Le, time } }

    /// Filter: `value == threshold`.
    #[inline]
    #[must_use]
    pub fn eq(time: DateTime<Utc>) -> Self { TimeFilter { op: TimeOp::Eq, time } }

    /// Check whether `value` passes this filter.
    #[inline]
    #[must_use]
    pub fn matches(self, value: DateTime<Utc>) -> bool {
        self.op.applies(value, self.time)
    }
}

impl fmt::Display for TimeFilter {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}", self.op, format_datetime(&self.time))
    }
}

impl FromStr for TimeFilter {
    type Err = TimeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        parse_time_filter(s)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for TimeFilter {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.collect_str(self)
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for TimeFilter {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

// ── TimeError ────────────────────────────────────────────────────────────────

/// Errors that can occur during time parsing and formatting.
///
/// All variants carry zero heap-allocated data — error strings are
/// `&'static str` literals in `.rodata`.
///
/// This enum is `#[non_exhaustive]` — new variants may be added in
/// minor releases without breaking changes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TimeError {
    /// Filter string lacks `>=`, `>`, `<=`, `<`, or `=` prefix.
    MissingOperator,
    /// Empty input string.
    EmptyInput,
    /// Unknown or unsupported time suffix.
    UnknownSuffix,
    /// Numeric value can't be parsed.
    InvalidNumber,
    /// Date/time string doesn't match expected format.
    InvalidDate,
}

impl fmt::Display for TimeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            TimeError::MissingOperator => {
                "time filter must start with an operator (>=, >, <=, <, =)"
            }
            TimeError::EmptyInput => "empty input",
            TimeError::UnknownSuffix => "unknown time suffix (expected h, hr, m, min, d, s)",
            TimeError::InvalidNumber => "failed to parse number",
            TimeError::InvalidDate => "failed to parse date/time",
        })
    }
}

impl Error for TimeError {}

/// `Result` type alias for `timefilter` operations.
pub type TimeResult<T> = Result<T, TimeError>;

// ── parsing ──────────────────────────────────────────────────────────────────

/// Parse a time filter string like `">=7d"`, `"<2h"`, `"=2026-05-01"`.
///
/// Operator is required — returns error if missing.
///
/// # Errors
///
/// Returns [`TimeError::MissingOperator`] if no operator is found,
/// or [`TimeError`] variants from time parsing.
pub fn parse_time_filter(s: &str) -> TimeResult<TimeFilter> {
    let s = s.trim();
    let (op, rest) = if let Some(r) = s.strip_prefix(">=") {
        (TimeOp::Ge, r)
    } else if let Some(r) = s.strip_prefix("<=") {
        (TimeOp::Le, r)
    } else if let Some(r) = s.strip_prefix('>') {
        (TimeOp::Gt, r)
    } else if let Some(r) = s.strip_prefix('<') {
        (TimeOp::Lt, r)
    } else if let Some(r) = s.strip_prefix('=') {
        (TimeOp::Eq, r)
    } else {
        return Err(TimeError::MissingOperator);
    };
    let time = parse_time(rest)?;
    Ok(TimeFilter { op, time })
}

/// Parse human-readable time string to `DateTime<Utc>`.
///
/// Supports relative formats:
/// - `"7d"`, `"7 days"` — days ago
/// - `"2h"`, `"2hr"` — hours ago
/// - `"30m"`, `"30min"` — minutes ago
/// - `"30s"` — seconds ago
///
/// And absolute formats:
/// - `"2024-05-01"` — date-only (midnight UTC)
/// - `"2024-05-01 10:00"` — date and hour:minute
/// - `"2024-05-01 10:00:00"` — date and hour:minute:second
///
/// # Errors
///
/// Returns [`TimeError::EmptyInput`], [`TimeError::UnknownSuffix`],
/// [`TimeError::InvalidNumber`], or [`TimeError::InvalidDate`].
pub fn parse_time(time_str: &str) -> TimeResult<DateTime<Utc>> {
    let s = time_str.trim();
    if s.is_empty() {
        return Err(TimeError::EmptyInput);
    }

    // Try relative time formats (suffix-based)
    if let Some(time) = try_parse_relative(s) {
        return Ok(time);
    }

    // Try absolute time formats
    try_parse_absolute(s)
}

fn try_parse_relative(s: &str) -> Option<DateTime<Utc>> {
    // We need to split digits from suffix. Find where alphabetic part starts.
    let alpha_pos = s.find(|c: char| c.is_ascii_alphabetic())?;
    let (num_str, suffix) = s.split_at(alpha_pos);
    let num: i64 = num_str.trim().parse().ok()?;

    let suf = suffix.trim();
    let duration = match () {
        _ if suf.eq_ignore_ascii_case("d")
            || suf.eq_ignore_ascii_case("day")
            || suf.eq_ignore_ascii_case("days") => Duration::days(num),
        _ if suf.eq_ignore_ascii_case("h")
            || suf.eq_ignore_ascii_case("hr")
            || suf.eq_ignore_ascii_case("hour")
            || suf.eq_ignore_ascii_case("hours") => Duration::hours(num),
        _ if suf.eq_ignore_ascii_case("m")
            || suf.eq_ignore_ascii_case("min")
            || suf.eq_ignore_ascii_case("minute")
            || suf.eq_ignore_ascii_case("minutes") => Duration::minutes(num),
        _ if suf.eq_ignore_ascii_case("s")
            || suf.eq_ignore_ascii_case("sec")
            || suf.eq_ignore_ascii_case("second")
            || suf.eq_ignore_ascii_case("seconds") => Duration::seconds(num),
        _ => return None,
    };
    Some(Utc::now() - duration)
}

fn try_parse_absolute(s: &str) -> TimeResult<DateTime<Utc>> {
    // "2024-05-01 10:00:00"
    if let Ok(naive) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S") {
        return Ok(DateTime::from_naive_utc_and_offset(naive, Utc));
    }
    // "2024-05-01 10:00"
    if let Ok(naive) = NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M") {
        return Ok(DateTime::from_naive_utc_and_offset(naive, Utc));
    }
    // "2024-05-01" — parse as NaiveDate, then convert
    if let Ok(naive_date) = chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d") {
        let naive = naive_date.and_hms_opt(0, 0, 0).unwrap();
        return Ok(DateTime::from_naive_utc_and_offset(naive, Utc));
    }
    Err(TimeError::InvalidDate)
}

// ── formatting ───────────────────────────────────────────────────────────────

/// Format a `DateTime<Utc>` for display in **local timezone**.
///
/// ```
/// use timefilter::format_datetime;
/// use chrono::{DateTime, NaiveDateTime, Utc};
///
/// let naive = NaiveDateTime::parse_from_str("2024-05-01 10:30:45", "%Y-%m-%d %H:%M:%S").unwrap();
/// let dt: DateTime<Utc> = DateTime::from_naive_utc_and_offset(naive, Utc);
/// let out = format_datetime(&dt);
/// assert!(out.contains("2024"));
/// ```
#[must_use]
pub fn format_datetime(dt: &DateTime<Utc>) -> String {
    dt.with_timezone(&Local)
        .format("%Y-%m-%d %H:%M:%S")
        .to_string()
}

// ── tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::{Datelike, TimeZone, Timelike};

    // -- parse_time: relative --

    #[test]
    fn relative_hours() {
        let now = Utc::now();
        let parsed = parse_time("1h").unwrap();
        let diff = now - parsed;
        assert!(diff >= Duration::minutes(59) && diff <= Duration::minutes(61));
    }

    #[test]
    fn relative_hours_hr_suffix() {
        let now = Utc::now();
        let parsed = parse_time("2hr").unwrap();
        let diff = now - parsed;
        assert!(diff >= Duration::minutes(119) && diff <= Duration::minutes(121));
    }

    #[test]
    fn relative_minutes() {
        let now = Utc::now();
        let parsed = parse_time("30m").unwrap();
        let diff = now - parsed;
        assert!(diff >= Duration::minutes(29) && diff <= Duration::minutes(31));
    }

    #[test]
    fn relative_minutes_min_suffix() {
        let now = Utc::now();
        let parsed = parse_time("15min").unwrap();
        let diff = now - parsed;
        assert!(diff >= Duration::minutes(14) && diff <= Duration::minutes(16));
    }

    #[test]
    fn relative_days() {
        let now = Utc::now();
        let parsed = parse_time("7d").unwrap();
        let diff = now - parsed;
        assert!(diff >= Duration::hours(167) && diff <= Duration::hours(169));
    }

    #[test]
    fn relative_seconds() {
        let now = Utc::now();
        let parsed = parse_time("30s").unwrap();
        let diff = now - parsed;
        assert!(diff >= Duration::seconds(29) && diff <= Duration::seconds(31));
    }

    #[test]
    fn relative_verbose_suffixes() {
        assert!(parse_time("1 day").is_ok());
        assert!(parse_time("7 days").is_ok());
        assert!(parse_time("2 hour").is_ok());
        assert!(parse_time("3 hours").is_ok());
        assert!(parse_time("10 minute").is_ok());
        assert!(parse_time("5 minutes").is_ok());
        assert!(parse_time("30 second").is_ok());
        assert!(parse_time("45 seconds").is_ok());
    }

    // -- parse_time: absolute --

    #[test]
    fn absolute_datetime() {
        let parsed = parse_time("2024-05-01 10:00").unwrap();
        assert_eq!(parsed.year(), 2024);
        assert_eq!(parsed.month(), 5);
        assert_eq!(parsed.day(), 1);
        assert_eq!(parsed.hour(), 10);
        assert_eq!(parsed.minute(), 0);
    }

    #[test]
    fn absolute_with_seconds() {
        let parsed = parse_time("2024-12-25 15:30:45").unwrap();
        assert_eq!(parsed.year(), 2024);
        assert_eq!(parsed.month(), 12);
        assert_eq!(parsed.day(), 25);
        assert_eq!(parsed.hour(), 15);
        assert_eq!(parsed.minute(), 30);
        assert_eq!(parsed.second(), 45);
    }

    #[test]
    fn absolute_date_only() {
        let parsed = parse_time("2024-01-15").unwrap();
        assert_eq!(parsed.year(), 2024);
        assert_eq!(parsed.month(), 1);
        assert_eq!(parsed.day(), 15);
        assert_eq!(parsed.hour(), 0);
        assert_eq!(parsed.minute(), 0);
    }

    // -- parse_time: errors --

    #[test]
    fn parse_time_invalid() {
        assert_eq!(parse_time("invalid"), Err(TimeError::InvalidDate));
        assert_eq!(parse_time("2024-13-01 10:00"), Err(TimeError::InvalidDate));
        assert_eq!(parse_time("abc"), Err(TimeError::InvalidDate));
        assert_eq!(parse_time(""), Err(TimeError::EmptyInput));
        assert_eq!(parse_time("  "), Err(TimeError::EmptyInput));
    }

    #[test]
    fn parse_time_unknown_suffix() {
        // "1x" → finds alpha at position 1, num=1, suffix="x" → UnknownSuffix from try_parse_relative
        // But we match err, it goes to try_parse_absolute which also fails → InvalidDate
        assert_eq!(parse_time("1x"), Err(TimeError::InvalidDate));
    }

    // -- parse_time_filter --

    #[test]
    fn filter_gt() {
        let f = parse_time_filter(">1h").unwrap();
        assert_eq!(f.op, TimeOp::Gt);
    }

    #[test]
    fn filter_ge() {
        let f = parse_time_filter(">=7d").unwrap();
        assert_eq!(f.op, TimeOp::Ge);
    }

    #[test]
    fn filter_lt() {
        let f = parse_time_filter("<2026-05-01").unwrap();
        assert_eq!(f.op, TimeOp::Lt);
        assert_eq!(f.time.year(), 2026);
        assert_eq!(f.time.month(), 5);
        assert_eq!(f.time.day(), 1);
    }

    #[test]
    fn filter_le() {
        let f = parse_time_filter("<=30m").unwrap();
        assert_eq!(f.op, TimeOp::Le);
    }

    #[test]
    fn filter_eq() {
        let f = parse_time_filter("=2026-05-01 10:00").unwrap();
        assert_eq!(f.op, TimeOp::Eq);
        assert_eq!(f.time.year(), 2026);
        assert_eq!(f.time.month(), 5);
        assert_eq!(f.time.day(), 1);
        assert_eq!(f.time.hour(), 10);
    }

    #[test]
    fn filter_no_operator_errors() {
        assert_eq!(parse_time_filter("1h"), Err(TimeError::MissingOperator));
        assert_eq!(parse_time_filter("30d"), Err(TimeError::MissingOperator));
        assert_eq!(parse_time_filter("2026-05-01"), Err(TimeError::MissingOperator));
    }

    #[test]
    fn filter_invalid() {
        assert_eq!(parse_time_filter(">abc"), Err(TimeError::InvalidDate));
    }

    // -- TimeFilter FromStr / Display --

    #[test]
    fn filter_from_str() {
        let f: TimeFilter = ">=7d".parse().unwrap();
        assert_eq!(f.op, TimeOp::Ge);
    }

    #[test]
    fn filter_display() {
        let dt = Utc.with_ymd_and_hms(2024, 5, 1, 10, 30, 45).unwrap();
        let f = TimeFilter::gt(dt);
        let s = f.to_string();
        assert!(s.starts_with('>'));
        assert!(s.contains("2024"));
    }

    // -- TimeOp --

    #[test]
    fn time_op_all() {
        assert_eq!(TimeOp::ALL.len(), 5);
    }

    #[test]
    fn time_op_applies() {
        let t1: DateTime<Utc> = Utc.with_ymd_and_hms(2024, 6, 1, 0, 0, 0).unwrap();
        let t2: DateTime<Utc> = Utc.with_ymd_and_hms(2024, 5, 1, 0, 0, 0).unwrap();
        assert!(TimeOp::Gt.applies(t1, t2));
        assert!(!TimeOp::Gt.applies(t2, t1));
        assert!(TimeOp::Ge.applies(t1, t1));
        assert!(TimeOp::Eq.applies(t1, t1));
    }

    // -- format_datetime --

    #[test]
    fn format_datetime_contains_year() {
        let dt = Utc.with_ymd_and_hms(2024, 5, 1, 10, 30, 45).unwrap();
        let s = format_datetime(&dt);
        assert!(!s.is_empty());
        assert!(s.contains("2024"));
    }

    // -- TimeFilter convenience ctors --

    #[test]
    fn filter_convenience_ctors() {
        let now = Utc::now();
        assert_eq!(TimeFilter::gt(now).op, TimeOp::Gt);
        assert_eq!(TimeFilter::ge(now).op, TimeOp::Ge);
        assert_eq!(TimeFilter::lt(now).op, TimeOp::Lt);
        assert_eq!(TimeFilter::le(now).op, TimeOp::Le);
        assert_eq!(TimeFilter::eq(now).op, TimeOp::Eq);
    }

    // -- cross-crate extraction risks --

    #[test]
    fn negative_relative_time() {
        // "-1h" means 1 hour in the FUTURE (now - (-1h) = now + 1h)
        let now = Utc::now();
        let parsed = parse_time("-1h").unwrap();
        let diff = parsed - now;
        assert!(diff.num_minutes() >= 59 && diff.num_minutes() <= 61);
    }

    #[test]
    fn filter_empty_after_operator() {
        assert_eq!(parse_time_filter(">="), Err(TimeError::EmptyInput));
        assert_eq!(parse_time_filter("<"), Err(TimeError::EmptyInput));
        assert_eq!(parse_time_filter(">"), Err(TimeError::EmptyInput));
        assert_eq!(parse_time_filter("<="), Err(TimeError::EmptyInput));
    }

    #[test]
    fn leap_year_parses() {
        let dt = parse_time("2024-02-29").unwrap();
        assert_eq!(dt.month(), 2);
        assert_eq!(dt.day(), 29);
    }

    #[test]
    fn invalid_leap_year_fails() {
        // 2023 is not a leap year, Feb 29 should fail
        assert_eq!(parse_time("2023-02-29"), Err(TimeError::InvalidDate));
    }

    #[test]
    fn edge_day_month_boundaries() {
        // Month boundaries: Jan 31 → Feb 1 should not be valid
        assert_eq!(parse_time("2024-01-32"), Err(TimeError::InvalidDate));
        assert_eq!(parse_time("2024-13-01"), Err(TimeError::InvalidDate));
        // Apr 31 doesn't exist
        assert_eq!(parse_time("2024-04-31"), Err(TimeError::InvalidDate));
    }

    #[test]
    fn time_error_is_proper_error() {
        use std::error::Error;
        assert!(TimeError::EmptyInput.source().is_none());
        assert_eq!(TimeError::EmptyInput.to_string(), "empty input");
        assert_eq!(TimeError::InvalidDate.to_string(), "failed to parse date/time");
        assert_eq!(TimeError::UnknownSuffix.to_string(), "unknown time suffix (expected h, hr, m, min, d, s)");
        assert_eq!(TimeError::InvalidNumber.to_string(), "failed to parse number");
        assert!(TimeError::MissingOperator.to_string().contains("time filter must start"));
    }

    #[test]
    fn time_op_display_all() {
        assert_eq!(TimeOp::Gt.to_string(), ">");
        assert_eq!(TimeOp::Ge.to_string(), ">=");
        assert_eq!(TimeOp::Lt.to_string(), "<");
        assert_eq!(TimeOp::Le.to_string(), "<=");
        assert_eq!(TimeOp::Eq.to_string(), "=");
    }

    #[test]
    fn time_filter_roundtrip_absolute() {
        // format_datetime uses local timezone, so full roundtrip depends on TZ.
        // Just verify the string format has operator + date.
        let dt: DateTime<Utc> = parse_time("2024-06-15 08:30").unwrap();
        let f = TimeFilter::ge(dt);
        let s = f.to_string();
        assert!(s.starts_with(">="), "starts with >=: {}", s);
        assert!(s.contains("2024-06-15"), "contains date: {}", s);
        assert!(s.contains(":"), "contains time: {}", s);
    }

    #[test]
    fn time_serde_compile_check() {
        // Compile-time: TimeFilter + TimeOp implement Send + Sync
        fn assert_send<T: Send>() {}
        fn assert_sync<T: Sync>() {}
        assert_send::<TimeFilter>();
        assert_sync::<TimeFilter>();
        assert_send::<TimeOp>();
        assert_sync::<TimeOp>();
    }

    #[test]
    fn format_datetime_various_times() {
        // Just verify output is non-empty and contains the date
        let dt: DateTime<Utc> = parse_time("2024-01-01").unwrap();
        let s = format_datetime(&dt);
        assert!(s.contains("2024"), "contains year: {}", s);
        assert!(s.contains("01"), "contains month/day: {}", s);
        assert!(s.contains(":"), "contains time separator: {}", s);
    }
}