tds-driver 0.2.0

A TDS (Microsoft SQL Server) driver for Rust
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
//! Mappings between TDS and and Chrono types (with `chrono` feature flag
//! enabled).
//!
//! The chrono library offers better ergonomy, but is known to hold certain
//! security vulnerabilities. The code here is for legacy purposes, please use
//! `time` crate for greenfield projects.

#[cfg(not(feature = "tds73"))]
use super::DateTime as DateTime1;
#[cfg(feature = "tds73")]
use super::{Date, DateTime2, DateTimeOffset, Time};
use crate::tds::codec::ColumnData;
#[cfg(feature = "tds73")]
#[cfg_attr(docsrs, doc(cfg(feature = "tds73")))]
pub use chrono::offset::{FixedOffset, Utc};
pub use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};

#[inline]
fn from_days(days: i64, start_year: i32) -> NaiveDate {
    // `days` derives from untrusted server bytes; clamp to the representable
    // range instead of panicking on overflow.
    let base = NaiveDate::from_ymd_opt(start_year, 1, 1).unwrap();
    base.checked_add_signed(chrono::Duration::days(days))
        .unwrap_or(if days < 0 {
            NaiveDate::MIN
        } else {
            NaiveDate::MAX
        })
}

/// Convert a server-supplied fractional-seconds `increments` at the given
/// `scale` into nanoseconds without panicking (`scale > 9` would underflow
/// `9 - scale`; a large `increments` would overflow the multiply).
#[inline]
#[cfg(feature = "tds73")]
fn nanos_from_increments(increments: u64, scale: u8) -> i64 {
    let pow = 9u32.saturating_sub(scale as u32);
    increments
        .saturating_mul(10u64.saturating_pow(pow))
        .min(i64::MAX as u64) as i64
}

fn from_millis(ms: u32) -> crate::Result<NaiveTime> {
    NaiveTime::from_num_seconds_from_midnight_opt(ms / 1000, ms % 1000 * 1_000_000)
        .ok_or_else(|| crate::Error::Conversion(format!("{ms} ms is past midnight").into()))
}

fn from_datetime(dt: super::DateTime) -> crate::Result<NaiveDateTime> {
    let time = from_millis(dt.millis_of_day()?)?;
    Ok(NaiveDateTime::new(from_days(dt.days() as i64, 1900), time))
}

#[cfg(feature = "tds73")]
fn from_smalldatetime(dt: super::SmallDateTime) -> crate::Result<NaiveDateTime> {
    let time = from_millis(dt.minutes_of_day()? * 60_000)?;
    Ok(NaiveDateTime::new(from_days(dt.days() as i64, 1900), time))
}

impl<'a> crate::FromSql<'a> for NaiveDateTime {
    fn from_sql(data: &'a ColumnData<'static>) -> crate::Result<Option<Self>> {
        match data {
            ColumnData::DateTime(dt) => dt.map(from_datetime).transpose(),
            #[cfg(feature = "tds73")]
            ColumnData::SmallDateTime(dt) => dt.map(from_smalldatetime).transpose(),
            #[cfg(feature = "tds73")]
            ColumnData::DateTime2(dt) => Ok(dt.map(|dt| {
                let ns = nanos_from_increments(dt.time.increments, dt.time.scale);
                NaiveDateTime::new(
                    from_days(dt.date.days() as i64, 1),
                    NaiveTime::MIN + chrono::Duration::nanoseconds(ns),
                )
            })),
            _ => Err(crate::Error::Conversion(
                format!("cannot interpret {:?} as an NaiveDateTime value", data).into(),
            )),
        }
    }
}

impl crate::FromSqlOwned for NaiveDateTime {
    fn from_sql_owned(data: ColumnData<'static>) -> crate::Result<Option<Self>> {
        <Self as crate::FromSql>::from_sql(&data)
    }
}

#[inline]
fn to_days(date: NaiveDate, start_year: i32) -> i64 {
    date.signed_duration_since(NaiveDate::from_ymd_opt(start_year, 1, 1).unwrap())
        .num_days()
}

/// 1/300 s since midnight, rounded to the nearest tick as the server rounds
/// a `datetime` literal (00:00:00.999 is 00:00:01.000). The last tick of a
/// day rounds up to 300 * 86,400, which `to_datetime` carries into the next
/// day. A leap second (nanosecond >= 1e9) becomes `DateTime::LEAP_SECOND`:
/// counted on, it would read as the next second, or past 24:00.
#[inline]
#[cfg(not(feature = "tds73"))]
fn to_sec_fragments(time: NaiveTime) -> u32 {
    use chrono::Timelike;

    match time.nanosecond() {
        n if n >= 1_000_000_000 => DateTime1::LEAP_SECOND,
        n => {
            // 3 * ns / 10^7, half up; a day's nanoseconds fit u64.
            let ns = u64::from(time.num_seconds_from_midnight()) * 1_000_000_000 + u64::from(n);
            ((ns * 3 + 5_000_000) / 10_000_000) as u32
        }
    }
}

/// `datetime`: days since 1900-01-01 and ticks since midnight.
#[cfg(not(feature = "tds73"))]
fn to_datetime(dt: NaiveDateTime) -> DateTime1 {
    let mut days = to_days(dt.date(), 1900) as i32;
    let mut ticks = to_sec_fragments(dt.time());

    if ticks == 300 * 86_400 {
        days += 1;
        ticks = 0;
    }

    DateTime1::new(days, ticks)
}

#[cfg(feature = "tds73")]
from_sql!(
    NaiveTime:
        ColumnData::Time(ref time) => time.map(|time| {
            let ns = nanos_from_increments(time.increments, time.scale);
            NaiveTime::from_hms_opt(0,0,0).unwrap() + chrono::Duration::nanoseconds(ns)
        });
    NaiveDate:
        ColumnData::Date(ref date) => date.map(|date| from_days(date.days() as i64, 1));
    chrono::DateTime<Utc>:
        // The datetime2 part of a datetimeoffset is already UTC.
        ColumnData::DateTimeOffset(ref dto) => dto.map(|dto| {
            let date = from_days(dto.datetime2.date.days() as i64, 1);
            let ns = nanos_from_increments(dto.datetime2.time.increments, dto.datetime2.time.scale);
            let time = NaiveTime::from_hms_opt(0,0,0).unwrap() + chrono::Duration::nanoseconds(ns);

            chrono::DateTime::from_naive_utc_and_offset(NaiveDateTime::new(date, time), Utc)
        }),
        ColumnData::DateTime2(ref dt2) => dt2.map(|dt2| {
            let date = from_days(dt2.date.days() as i64, 1);
            let ns = nanos_from_increments(dt2.time.increments, dt2.time.scale);
            let time = NaiveTime::from_hms_opt(0,0,0).unwrap() + chrono::Duration::nanoseconds(ns);
            let naive = NaiveDateTime::new(date, time);

            chrono::DateTime::from_naive_utc_and_offset(naive, Utc)
        });
    chrono::DateTime<FixedOffset>: ColumnData::DateTimeOffset(ref dto) => dto.map(|dto| {
        let date = from_days(dto.datetime2.date.days() as i64, 1);
        let ns = nanos_from_increments(dto.datetime2.time.increments, dto.datetime2.time.scale);
        let time = NaiveTime::from_hms_opt(0,0,0).unwrap() + chrono::Duration::nanoseconds(ns);

        let offset = FixedOffset::east_opt((dto.offset as i32) * 60).unwrap_or_else(|| FixedOffset::east_opt(0).unwrap());
        let naive = NaiveDateTime::new(date, time);

        chrono::DateTime::from_naive_utc_and_offset(naive, offset)
    })
);

/// 10^-7 s since midnight. A leap second (nanosecond >= 1e9) becomes
/// `Time::LEAP_SECOND`: counted on, it would read as the next second, or
/// past 24:00.
#[cfg(feature = "tds73")]
fn to_time(time: NaiveTime) -> Time {
    use chrono::Timelike;

    let increments = match time.nanosecond() {
        n if n >= 1_000_000_000 => Time::LEAP_SECOND,
        n => (u64::from(time.num_seconds_from_midnight()) * 1_000_000_000 + u64::from(n)) / 100,
    };

    Time {
        increments,
        scale: 7,
    }
}

#[cfg(feature = "tds73")]
fn to_datetime2(naive: NaiveDateTime) -> DateTime2 {
    DateTime2::new(
        Date::from_signed_days(to_days(naive.date(), 1)),
        to_time(naive.time()),
    )
}

/// datetimeoffset stores whole minutes: an offset with seconds becomes
/// `DateTimeOffset::OFFSET_WITH_SECONDS` instead of being truncated.
#[cfg(feature = "tds73")]
fn to_datetimeoffset(dt: &chrono::DateTime<FixedOffset>) -> DateTimeOffset {
    let secs = dt.offset().local_minus_utc();
    let offset = match secs % 60 {
        // A FixedOffset is under 24 hours: at most 1439 minutes.
        0 => (secs / 60) as i16,
        _ => DateTimeOffset::OFFSET_WITH_SECONDS,
    };

    DateTimeOffset::new(to_datetime2(dt.naive_utc()), offset)
}

#[cfg(feature = "tds73")]
to_sql!(self_,
        NaiveDate: (ColumnData::Date, Date::from_signed_days(to_days(*self_, 1)));
        NaiveTime: (ColumnData::Time, to_time(*self_));
        NaiveDateTime: (ColumnData::DateTime2, to_datetime2(*self_));
        chrono::DateTime<Utc>: (ColumnData::DateTime2, to_datetime2(self_.naive_utc()));
        chrono::DateTime<FixedOffset>: (ColumnData::DateTimeOffset, to_datetimeoffset(self_));
);

#[cfg(feature = "tds73")]
into_sql!(self_,
        NaiveDate: (ColumnData::Date, Date::from_signed_days(to_days(self_, 1)));
        NaiveTime: (ColumnData::Time, to_time(self_));
        NaiveDateTime: (ColumnData::DateTime2, to_datetime2(self_));
        chrono::DateTime<Utc>: (ColumnData::DateTime2, to_datetime2(self_.naive_utc()));
        chrono::DateTime<FixedOffset>: (ColumnData::DateTimeOffset, to_datetimeoffset(&self_));
);

#[cfg(not(feature = "tds73"))]
to_sql!(self_,
        NaiveDateTime: (ColumnData::DateTime, to_datetime(*self_));
);

#[cfg(not(feature = "tds73"))]
into_sql!(self_,
        NaiveDateTime: (ColumnData::DateTime, to_datetime(self_));
);

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{FromSql, IntoSql};

    #[test]
    fn from_days_clamps_on_overflow() {
        // A day offset far outside the representable `NaiveDate` range forces
        // the `checked_add_signed` fallback. Positive overflow must clamp to
        // MAX, negative overflow to MIN. This pins the `days < 0` sign test
        // (distinguishing it from `==` and `>`).
        assert_eq!(from_days(200_000_000, 1), NaiveDate::MAX);
        assert_eq!(from_days(-200_000_000, 1), NaiveDate::MIN);
    }

    #[test]
    fn datetime_rounds_to_the_millisecond() {
        let get = |ticks| {
            let cd = ColumnData::DateTime(Some(crate::tds::time::DateTime::new(0, ticks)));
            NaiveDateTime::from_sql(&cd).unwrap().unwrap().time()
        };
        assert_eq!(get(300), NaiveTime::from_hms_opt(0, 0, 1).unwrap());
        assert_eq!(
            get(299),
            NaiveTime::from_hms_milli_opt(0, 0, 0, 997).unwrap()
        );
        assert_eq!(get(2), NaiveTime::from_hms_milli_opt(0, 0, 0, 7).unwrap());
        assert_eq!(
            get(25919999),
            NaiveTime::from_hms_milli_opt(23, 59, 59, 997).unwrap()
        );
    }

    #[test]
    fn out_of_range_times_are_conversion_errors() {
        use crate::tds::time::{DateTime, SmallDateTime};
        use crate::FromSqlOwned;

        let dt = ColumnData::DateTime(Some(DateTime::new(0, 300 * 86_400)));
        assert!(matches!(
            NaiveDateTime::from_sql(&dt),
            Err(crate::Error::Conversion(_))
        ));
        assert!(NaiveDateTime::from_sql_owned(dt).is_err());

        let sdt = ColumnData::SmallDateTime(Some(SmallDateTime::new(0, 1440)));
        #[cfg(feature = "tds73")]
        assert!(matches!(
            NaiveDateTime::from_sql(&sdt),
            Err(crate::Error::Conversion(_))
        ));
        #[cfg(not(feature = "tds73"))]
        assert!(NaiveDateTime::from_sql(&sdt).is_err());
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn smalldatetime_converts() {
        let cd = ColumnData::SmallDateTime(Some(crate::tds::time::SmallDateTime::new(1, 1439)));
        assert_eq!(
            NaiveDateTime::from_sql(&cd).unwrap(),
            Some(NaiveDateTime::new(
                NaiveDate::from_ymd_opt(1900, 1, 2).unwrap(),
                NaiveTime::from_hms_opt(23, 59, 0).unwrap()
            ))
        );
    }

    #[cfg(not(feature = "tds73"))]
    #[test]
    fn to_sec_fragments_converts() {
        // One second == 300 sec-fragments (1/300 s units).
        assert_eq!(
            to_sec_fragments(NaiveTime::from_hms_opt(0, 0, 1).unwrap()),
            300
        );
    }

    #[cfg(not(feature = "tds73"))]
    #[test]
    fn to_datetime_rounds_to_the_nearest_tick_and_carries_the_day() {
        let at = |h, m, s, nano| {
            NaiveDate::from_ymd_opt(1900, 1, 1)
                .unwrap()
                .and_hms_nano_opt(h, m, s, nano)
                .unwrap()
        };
        let ticks = |dt: NaiveDateTime| {
            let dt = to_datetime(dt);
            (dt.days(), dt.seconds_fragments())
        };

        // 299.7 ticks rounds up to the next second; 299.49 stays.
        assert_eq!(ticks(at(0, 0, 0, 999_000_000)), (0, 300));
        assert_eq!(ticks(at(0, 0, 0, 998_300_000)), (0, 299));
        assert_eq!(ticks(at(0, 0, 0, 998_500_000)), (0, 300));
        assert_eq!(ticks(at(23, 59, 59, 999_000_000)), (1, 0));
        assert_eq!(ticks(at(23, 59, 59, 998_000_000)), (0, 25_919_999));
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn naive_date_round_trip() {
        let date = NaiveDate::from_ymd_opt(2021, 6, 15).unwrap();
        let cd: ColumnData<'static> = date.into_sql();
        assert!(matches!(cd, ColumnData::Date(Some(_))));
        assert_eq!(NaiveDate::from_sql(&cd).unwrap(), Some(date));
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn naive_time_round_trip() {
        let time = NaiveTime::from_hms_opt(13, 37, 42).unwrap();
        let cd: ColumnData<'static> = time.into_sql();
        assert!(matches!(cd, ColumnData::Time(Some(_))));
        assert_eq!(NaiveTime::from_sql(&cd).unwrap(), Some(time));
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn naive_datetime_round_trip() {
        let dt = NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2000, 12, 31).unwrap(),
            NaiveTime::from_hms_opt(23, 59, 58).unwrap(),
        );
        let cd: ColumnData<'static> = dt.into_sql();
        assert!(matches!(cd, ColumnData::DateTime2(Some(_))));
        assert_eq!(NaiveDateTime::from_sql(&cd).unwrap(), Some(dt));
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn datetime_utc_round_trip() {
        let naive = NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2015, 3, 4).unwrap(),
            NaiveTime::from_hms_opt(1, 2, 3).unwrap(),
        );
        let dt = chrono::DateTime::<Utc>::from_naive_utc_and_offset(naive, Utc);
        let cd: ColumnData<'static> = dt.into_sql();
        assert!(matches!(cd, ColumnData::DateTime2(Some(_))));
        assert_eq!(chrono::DateTime::<Utc>::from_sql(&cd).unwrap(), Some(dt));
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn datetime_fixed_offset_round_trip() {
        let offset = FixedOffset::east_opt(2 * 3600).unwrap();
        let naive = NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2015, 3, 4).unwrap(),
            NaiveTime::from_hms_opt(1, 2, 3).unwrap(),
        );
        let dt = chrono::DateTime::from_naive_utc_and_offset(naive, offset);
        let cd: ColumnData<'static> = dt.into_sql();
        assert!(matches!(cd, ColumnData::DateTimeOffset(Some(_))));
        assert_eq!(
            chrono::DateTime::<FixedOffset>::from_sql(&cd).unwrap(),
            Some(dt)
        );
    }

    // The tds73 `from_sql` NaiveDateTime path has a dedicated arm for the legacy
    // `ColumnData::DateTime` wire type; exercise it directly (round-trips produce
    // `DateTime2`, never `DateTime`, so this arm is otherwise unreachable).
    #[cfg(feature = "tds73")]
    #[test]
    fn naive_datetime_from_legacy_datetime_column() {
        let cd = ColumnData::DateTime(Some(crate::tds::time::DateTime::new(0, 0)));
        let dt = NaiveDateTime::from_sql(&cd).unwrap().unwrap();
        assert_eq!(
            dt,
            NaiveDateTime::new(
                NaiveDate::from_ymd_opt(1900, 1, 1).unwrap(),
                NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
            )
        );
    }

    // The `DateTimeOffset -> DateTime<Utc>` conversion arm (distinct from the
    // `DateTime<FixedOffset>` arm exercised by the round-trip test).
    #[cfg(feature = "tds73")]
    #[test]
    fn datetime_offset_reads_as_utc() {
        let offset = FixedOffset::east_opt(2 * 3600).unwrap();
        let naive = NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2015, 3, 4).unwrap(),
            NaiveTime::from_hms_opt(1, 2, 3).unwrap(),
        );
        let dt: chrono::DateTime<FixedOffset> =
            chrono::DateTime::from_naive_utc_and_offset(naive, offset);
        let cd: ColumnData<'static> = dt.into_sql();
        assert!(matches!(cd, ColumnData::DateTimeOffset(Some(_))));

        // Same instant: the wire time is UTC, the offset only labels it.
        let utc = chrono::DateTime::<Utc>::from_sql(&cd).unwrap().unwrap();
        assert_eq!(utc.naive_utc(), naive);
        assert_eq!(utc, dt);
    }

    fn encode(data: ColumnData<'_>) -> crate::Result<()> {
        use crate::tds::codec::Encode;

        let mut buf = bytes::BytesMut::new();
        data.encode(&mut crate::BytesMutWithTypeInfo::new(&mut buf))
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn dates_outside_year_1_to_9999_are_encode_errors() {
        use crate::ToSql;
        use chrono::TimeZone;

        let plus_one = FixedOffset::east_opt(3600).unwrap();

        for (y, m, d) in [(0, 12, 31), (-5, 1, 1), (10000, 1, 1), (50000, 1, 1)] {
            let date = NaiveDate::from_ymd_opt(y, m, d).unwrap();
            let naive = date.and_hms_opt(12, 0, 0).unwrap();
            let utc = chrono::DateTime::<Utc>::from_naive_utc_and_offset(naive, Utc);
            let fixed = chrono::DateTime::<FixedOffset>::from_naive_utc_and_offset(naive, plus_one);

            for data in [
                date.to_sql(),
                naive.to_sql(),
                utc.to_sql(),
                fixed.to_sql(),
                date.into_sql(),
                naive.into_sql(),
                utc.into_sql(),
                fixed.into_sql(),
            ] {
                let err = encode(data).unwrap_err();
                assert!(
                    matches!(err, crate::Error::Conversion(_)),
                    "{date}: {err:?}"
                );
            }
        }

        // Year 1 locally, year 0 in UTC, which is what datetimeoffset stores.
        let local = NaiveDate::from_ymd_opt(1, 1, 1)
            .unwrap()
            .and_hms_opt(0, 30, 0)
            .unwrap();
        let dto = plus_one.from_local_datetime(&local).unwrap();
        assert!(matches!(
            encode(dto.to_sql()),
            Err(crate::Error::Conversion(_))
        ));
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn year_1_and_9999_encode() {
        use crate::ToSql;

        let first = NaiveDate::from_ymd_opt(1, 1, 1).unwrap();
        let last = NaiveDate::from_ymd_opt(9999, 12, 31).unwrap();

        for date in [first, last] {
            let naive = date.and_hms_nano_opt(23, 59, 59, 999_999_900).unwrap();
            let utc = chrono::DateTime::<Utc>::from_naive_utc_and_offset(naive, Utc);
            for data in [date.to_sql(), naive.to_sql(), utc.to_sql()] {
                encode(data).unwrap();
            }
        }
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn leap_seconds_are_encode_errors() {
        use crate::ToSql;

        // At 23:59 it went past 24:00; at 12:34 the server read 12:35:00.5.
        for (h, m) in [(23, 59), (12, 34)] {
            let time = NaiveTime::from_hms_nano_opt(h, m, 59, 1_500_000_000).unwrap();
            let naive = NaiveDate::from_ymd_opt(2016, 12, 31)
                .unwrap()
                .and_time(time);
            let utc = chrono::DateTime::<Utc>::from_naive_utc_and_offset(naive, Utc);
            let fixed = utc.fixed_offset();

            for data in [
                time.to_sql(),
                naive.to_sql(),
                utc.to_sql(),
                fixed.to_sql(),
                time.into_sql(),
                naive.into_sql(),
                utc.into_sql(),
                fixed.into_sql(),
            ] {
                let err = encode(data).unwrap_err();
                assert!(
                    matches!(&err, crate::Error::Conversion(m) if m.contains("leap")),
                    "{time}: {err:?}"
                );
            }
        }

        let last = NaiveTime::from_hms_nano_opt(23, 59, 59, 999_999_999).unwrap();
        encode(last.to_sql()).unwrap();
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn offsets_with_seconds_are_encode_errors() {
        use crate::ToSql;
        use chrono::TimeZone;

        let naive = NaiveDate::from_ymd_opt(2024, 5, 6)
            .unwrap()
            .and_hms_opt(1, 2, 3)
            .unwrap();

        for secs in [30, -30, 3600 + 1] {
            let dto = FixedOffset::east_opt(secs)
                .unwrap()
                .from_local_datetime(&naive)
                .unwrap();
            for data in [dto.to_sql(), dto.into_sql()] {
                let err = encode(data).unwrap_err();
                assert!(matches!(err, crate::Error::Conversion(_)), "{dto}");
            }
        }

        for secs in [-14 * 3600, -90 * 60, 14 * 3600] {
            let dto = FixedOffset::east_opt(secs)
                .unwrap()
                .from_local_datetime(&naive)
                .unwrap();
            encode(dto.to_sql()).unwrap();
        }
    }

    #[cfg(feature = "tds73")]
    #[test]
    fn null_maps_to_none() {
        assert_eq!(NaiveDate::from_sql(&ColumnData::Date(None)).unwrap(), None);
        assert_eq!(NaiveTime::from_sql(&ColumnData::Time(None)).unwrap(), None);
    }

    #[cfg(not(feature = "tds73"))]
    #[test]
    fn naive_datetime_round_trip_legacy() {
        let dt = NaiveDateTime::new(
            NaiveDate::from_ymd_opt(1990, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(12, 0, 0).unwrap(),
        );
        let cd: ColumnData<'static> = dt.into_sql();
        assert!(matches!(cd, ColumnData::DateTime(Some(_))));
        assert_eq!(NaiveDateTime::from_sql(&cd).unwrap(), Some(dt));
    }

    #[cfg(not(feature = "tds73"))]
    #[test]
    fn leap_seconds_are_datetime_encode_errors() {
        use crate::ToSql;

        // At 23:59 it went past midnight (8023); at 12:34 it was the next
        // second, 12:35:00.5.
        for (h, m) in [(23, 59), (12, 34)] {
            let time = NaiveTime::from_hms_nano_opt(h, m, 59, 1_500_000_000).unwrap();
            let naive = NaiveDate::from_ymd_opt(2016, 12, 31)
                .unwrap()
                .and_time(time);

            for data in [naive.to_sql(), naive.into_sql()] {
                let err = encode(data).unwrap_err();
                assert!(
                    matches!(&err, crate::Error::Conversion(m) if m.contains("leap")),
                    "{time}: {err:?}"
                );
            }
        }

        let last = NaiveDate::from_ymd_opt(2016, 12, 31)
            .unwrap()
            .and_hms_nano_opt(23, 59, 59, 999_999_999)
            .unwrap();
        encode(last.to_sql()).unwrap();
    }
}