Skip to content

Commit a2b89d7

Browse files
committed
fix: deprecation warnings in sqlite::types::chrono, document DATETIME behavior
1 parent 248d617 commit a2b89d7

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

sqlx-sqlite/src/types/chrono.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn decode_datetime_from_text(value: &str) -> Option<DateTime<FixedOffset>> {
156156
}
157157

158158
fn decode_datetime_from_int(value: i64) -> Option<DateTime<FixedOffset>> {
159-
NaiveDateTime::from_timestamp_opt(value, 0).map(|dt| Utc.fix().from_utc_datetime(&dt))
159+
Utc.fix().timestamp_opt(value, 0).single()
160160
}
161161

162162
fn decode_datetime_from_float(value: f64) -> Option<DateTime<FixedOffset>> {
@@ -166,7 +166,7 @@ fn decode_datetime_from_float(value: f64) -> Option<DateTime<FixedOffset>> {
166166
let seconds = timestamp as i64;
167167
let nanos = (timestamp.fract() * 1E9) as u32;
168168

169-
NaiveDateTime::from_timestamp_opt(seconds, nanos).map(|dt| Utc.fix().from_utc_datetime(&dt))
169+
Utc.fix().timestamp_opt(seconds, nanos).single()
170170
}
171171

172172
impl<'r> Decode<'r, Sqlite> for NaiveDateTime {

sqlx-sqlite/src/types/mod.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,45 @@
3737
//!
3838
//! | Rust type | Sqlite type(s) |
3939
//! |---------------------------------------|------------------------------------------------------|
40-
//! | `chrono::NaiveDateTime` | DATETIME |
41-
//! | `chrono::DateTime<Utc>` | DATETIME |
42-
//! | `chrono::DateTime<Local>` | DATETIME |
43-
//! | `chrono::NaiveDate` | DATE |
44-
//! | `chrono::NaiveTime` | TIME |
40+
//! | `chrono::NaiveDateTime` | DATETIME (TEXT, INTEGER, REAL) |
41+
//! | `chrono::DateTime<Utc>` | DATETIME (TEXT, INTEGER, REAL) |
42+
//! | `chrono::DateTime<Local>` | DATETIME (TEXT, INTEGER, REAL) |
43+
//! | `chrono::DateTime<FixedOffset>` | DATETIME (TEXT, INTEGER, REAL) |
44+
//! | `chrono::NaiveDate` | DATE (TEXT only) |
45+
//! | `chrono::NaiveTime` | TIME (TEXT only) |
46+
//!
47+
//! ##### NOTE: `DATETIME` conversions
48+
//! SQLite may represent `DATETIME` values as one of three types: `TEXT`, `REAL`, or `INTEGER`.
49+
//! Which one is used is entirely up to you and how you store timestamps in your database.
50+
//!
51+
//! The deserialization for `NaiveDateTime`, `DateTime<Utc>` and `DateTime<Local>` infer the date
52+
//! format from the type of the value they're being decoded from:
53+
//!
54+
//! * If `TEXT`, the format is assumed to be an ISO-8601 compatible datetime string.
55+
//! A number of possible formats are tried; see `sqlx-sqlite/src/types/chrono.rs` for the current
56+
//! set of formats.
57+
//! * If `INTEGER`, it is expected to be the number of seconds since January 1, 1970 00:00 UTC,
58+
//! as if returned from the `unixtime()` function (without the `subsec` modifier).
59+
//! * If `REAL`, it is expected to be the (possibly fractional) number of days since the Julian epoch,
60+
//! November 24, 4714 BCE 12:00 UTC, as if returned from the `julianday()` function.
61+
//!
62+
//! These types will always encode to a datetime string, either
63+
//! with (`DateTime<Tz>` for any `Tz: TimeZone`) or without (`NaiveDateTime`) a timezone offset.
4564
//!
4665
//! ### [`time`](https://crates.io/crates/time)
4766
//!
4867
//! Requires the `time` Cargo feature flag.
4968
//!
5069
//! | Rust type | Sqlite type(s) |
5170
//! |---------------------------------------|------------------------------------------------------|
52-
//! | `time::PrimitiveDateTime` | DATETIME |
53-
//! | `time::OffsetDateTime` | DATETIME |
54-
//! | `time::Date` | DATE |
55-
//! | `time::Time` | TIME |
71+
//! | `time::PrimitiveDateTime` | DATETIME (TEXT, INTEGER) |
72+
//! | `time::OffsetDateTime` | DATETIME (TEXT, INTEGER) |
73+
//! | `time::Date` | DATE (TEXT only) |
74+
//! | `time::Time` | TIME (TEXT only) |
75+
//!
76+
//! ##### NOTE: `DATETIME` conversions
77+
//! The behavior here is identical to the corresponding `chrono` types, minus the support for `REAL`
78+
//! values as Julian days (it's just not implemented).
5679
//!
5780
//! ### [`uuid`](https://crates.io/crates/uuid)
5881
//!

0 commit comments

Comments
 (0)