Repository: BurntSushi/jiff (language: rust)

Issue:
feat: bootstrap jiff-sqlx development 
This refers to #50.

Firstly, I considered to add a separated `sqlx-jiff` method, but soon I found that due to Rust's orphan rules, implementing sqlx's trait for `Timestamp` and other types requires the code to be located at the same crate (`jiff`). Otherwise we need an extra wrapper struct like:

```rust
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Timestamp(pub jiff::Timestamp);
```

I prefer to implement code in `jiff` and gate it with feature flags.

@BurntSushi This demonstrates the integration implementation direction. I'll complete other parts when we agree on this direction.


Discussion / hints:
https://github.com/BurntSushi/jiff/issues/50#issuecomment-2253457694 discusses this. And the orphan rule is taken into account there. The way to do this integration, short of getting it merged upstream in `sqlx` itself, is with a wrapper type in a `jiff-sqlx` crate. The comment linked explains how to do this.

Jiff is a "low level" crate. Having it depend on "high level" crates like `sqlx` would lead to all sorts of problems, especially if and when the time ever comes for `sqlx` to add Jiff integration on their end. It's really a non-starter.
Also, the crate should be called `jiff-sqlx`, since it lives with `jiff`.
Other type mapping to be wrapped once we agree on the pattern:

* [x] `jiff::civil::Date` <-> `DATE`
* [x] `jiff::civil:Time` <-> `TIME`
* [x] `jiff:civil::DateTime` <-> `TIMESTAMP`
* [x] `jiff::Timestamp` <-> `TIMESTAMPTZ`
* [x] `jiff::SignedDuration` <-> `INTERVAL` 

Ref -

* https://github.com/launchbadge/sqlx/pull/3511
* https://github.com/sfackler/rust-postgres/pull/1164
All bridges submitted. Note that integrations for `PgRange` and `PgTimeTz` cannot be added due to the orphan rule.

I'd suspend the task here to start `jiff-sqlx` with Postgres support first since it's my real use case and I'd avoid adding too many things at the very beginning.
The failing wasm CI job doesn't seem to be relevant to this patch ..
> * `jiff::SignedDuration` <-> `INTERVAL`

I'm curious why this wouldn't be `jiff::Span`? That's how I'm doing these conversions manually.
@maxcountryman 

It's possible to support multiple type mappings, so it's possible to support both Span and Duration.

However, PG's interval supports only months, days and microseconds parts. Span supports other units. I'm not sure if we can always assume 1 year=12 months, and 1 week=7days.

Anyway, we may support two types together.
>  Span supports other units. I'm not sure if we can always assume 1 year=12 months, and 1 week=7days.

As I understand, `Span` doesn't assume 12 months == 1 year (ergo keeping each unit as a discrete property). I think that might be a reason to [use `Span`](https://docs.rs/jiff/latest/jiff/struct.SignedDuration.html#when-should-i-use-signedduration-versus-span) here, right? (In particular, I believe `Span` is needed for full ISO 8601 support.)
Hi @BurntSushi!

IMO code gets evolved and mature by tested in real world.

It's understandable that your context is not for such an integration, and you don't want release code that you don't look carefully into.

So, I wonder if it's OK for you that I release this crate under the name `jiff-sqlx` while I'm the crate owner for all the followups. And once you or the sqlx's maintainer accept to maintain it in either upstream (for the benefits to evolve together and catch up changes in the first place), we can work a transfer solution then.
Can you please pick a different name? That way we don't need to do any transfers or have any other project expectations. 

I also thought you had this code in your own project internally? Why do you need to publish a crate to do it?
> Can you please pick a different name? That way we don't need to do any transfers or have any other project expectations.

Then I'd  leave this PR here so that anyone need it can use it by copying the code. I don't think such an integration worth a dedicated name.

> I also thought you had this code in your own project internally? Why do you need to publish a crate to do it?

This is somehow like a question "why do you write opensource code". I write opensource code everyday so sharing code is by default (as shown in my GitHub profile). Also you can see that there are several other users want it. This is enough.
Yes sorry, it is totally cool to publish a crate for the good of open source. I was in a "are you yourself blocked" frame of mind versus "you want to publish a crate for others to use." So in the latter case, I think it's just a matter of either you going ahead and doing it (which is your right, although I request that you not use a `jiff-` prefix) or waiting until I'm able to dig into it.

Ecosystem integration is a big problem and I need to context switch into it. I'm just not there yet. And I'm currently thinking I'll do it with a `jiff 0.2` release, since there are a number of breaking changes I'd like to make.
My two cents: this is not the correct implementation re Postgres intervals. I think that should be addressed before publishing.
@maxcountryman I haven't looked too closely yet. It looks like only `SignedDuration` is supported here and it collapses everything down to microseconds. Can you say more about what you see as incorrect?
@BurntSushi that doesn't support ISO 8601 correct? See: https://www.postgresql.org/docs/13/datatype-datetime.html#DATATYPE-INTERVAL-OUTPUT
`SignedDuration` would still certainly emit ISO 8601.

Regardless, I'll dig into this more when I address ecosystem integration. But if you have any more explanation, I would be happy to hear it!
@BurntSushi This passage [in the docs](https://docs.rs/jiff/latest/jiff/struct.SignedDuration.html#parsing-and-printing): "Unlike the Span type, though, only uniform units are supported. This means that ISO 8601 durations with non-zero units of days or greater cannot be parsed directly into a SignedDuration" along with this from the Postgres docs, "As previously explained, PostgreSQL stores interval values as months, days, and microseconds" seem incompatible.

Fix patch:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 34088979..b2f513e5 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -124,6 +124,24 @@ jobs:
     # - name: Run integration tests
       # run: cargo test --test integration
 
+  # This job tests that examples outside the workspace build.
+  #
+  # These are outside the workspace because their dependency graphs are
+  # absolutely ludicrous.
+  examples:
+    runs-on: ubuntu-latest
+    steps:
+    - name: Checkout repository
+      uses: actions/checkout@v4
+    - name: Install Rust
+      uses: dtolnay/rust-toolchain@master
+      with:
+        toolchain: stable
+    - name: Build sqlx-postgres example
+      run: cargo build --manifest-path ./examples/sqlx-postgres/Cargo.toml
+    - name: Run sqlx-sqlite example
+      run: cargo run --manifest-path ./examples/sqlx-sqlite/Cargo.toml
+
   # Generic testing for most cross targets. Some get special treatment in
   # other jobs.
   cross:
diff --git a/.vim/coc-settings.json b/.vim/coc-settings.json
index 97042fb0..21b7e80b 100644
--- a/.vim/coc-settings.json
+++ b/.vim/coc-settings.json
@@ -1,6 +1,8 @@
 {
   "rust-analyzer.linkedProjects": [
     "bench/Cargo.toml",
+    "examples/sqlx-postgres/Cargo.toml",
+    "examples/sqlx-sqlite/Cargo.toml",
     "Cargo.toml"
   ]
 }
diff --git a/Cargo.toml b/Cargo.toml
index 6ba2e94c..894bea38 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -32,10 +32,12 @@ include = [
 members = [
   "crates/jiff-cli",
   "crates/jiff-icu",
+  "crates/jiff-sqlx",
   "crates/jiff-tzdb",
   "crates/jiff-tzdb-platform",
   "examples/*",
 ]
+exclude = ["examples/sqlx-postgres", "examples/sqlx-sqlite"]
 
 # Features are documented in the "Crate features" section of the crate docs:
 # https://docs.rs/jiff/*/#crate-features
diff --git a/crates/jiff-icu/Cargo.toml b/crates/jiff-icu/Cargo.toml
index a236f32d..26edf5b6 100644
--- a/crates/jiff-icu/Cargo.toml
+++ b/crates/jiff-icu/Cargo.toml
@@ -6,9 +6,9 @@ license = "Unlicense OR MIT"
 homepage = "https://github.com/BurntSushi/jiff/tree/master/crates/jiff-icu"
 repository = "https://github.com/BurntSushi/jiff"
 documentation = "https://docs.rs/jiff-icu"
-description = "The entire Time Zone Database embedded into your binary."
+description = "Conversion routines for Jiff and ICU4X."
 categories = ["date-and-time"]
-keywords = ["date", "time", "temporal", "zone", "iana"]
+keywords = ["date", "time", "temporal", "zone", "icu"]
 workspace = "../.."
 edition = "2021"
 rust-version = "1.70"
diff --git a/crates/jiff-sqlx/COPYING b/crates/jiff-sqlx/COPYING
new file mode 100644
index 00000000..bb9c20a0
--- /dev/null
+++ b/crates/jiff-sqlx/COPYING
@@ -0,0 +1,3 @@
+This project is dual-licensed under the Unlicense and MIT licenses.
+
+You may use this code under the terms of either license.
diff --git a/crates/jiff-sqlx/Cargo.toml b/crates/jiff-sqlx/Cargo.toml
new file mode 100644
index 00000000..23b995cd
--- /dev/null
+++ b/crates/jiff-sqlx/Cargo.toml
@@ -0,0 +1,34 @@
+[package]
+name = "jiff-sqlx"
+version = "0.0.1"  #:version
+authors = ["Andrew Gallant <jamslam@gmail.com>"]
+license = "Unlicense OR MIT"
+homepage = "https://github.com/BurntSushi/jiff/tree/master/crates/jiff-sqlx"
+repository = "https://github.com/BurntSushi/jiff"
+documentation = "https://docs.rs/jiff-sqlx"
+description = "Integration for Jiff in sqlx."
+categories = ["date-and-time"]
+keywords = ["date", "time", "jiff", "sqlx", "zone"]
+workspace = "../.."
+edition = "2021"
+rust-version = "1.70"
+include = ["/*.rs", "/*.dat", "COPYING", "LICENSE-MIT", "UNLICENSE"]
+
+[lib]
+name = "jiff_sqlx"
+bench = false
+path = "src/lib.rs"
+
+[features]
+default = []
+postgres = ["dep:sqlx-postgres"]
+sqlite = ["dep:sqlx-sqlite"]
+
+[dependencies]
+jiff = { path = "../..", default-features = false }
+sqlx-core = { version = "0.8.0", default-features = false }
+sqlx-postgres = { version = "0.8.0", default-features = false, optional = true }
+sqlx-sqlite = { version = "0.8.0", default-features = false, optional = true }
+
+[dev-dependencies]
+jiff = { path = "../..", default-features = true }
diff --git a/crates/jiff-sqlx/LICENSE-MIT b/crates/jiff-sqlx/LICENSE-MIT
new file mode 100644
index 00000000..3b0a5dc0
--- /dev/null
+++ b/crates/jiff-sqlx/LICENSE-MIT
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 Andrew Gallant
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/crates/jiff-sqlx/README.md b/crates/jiff-sqlx/README.md
new file mode 100644
index 00000000..20dd0fad
--- /dev/null
+++ b/crates/jiff-sqlx/README.md
@@ -0,0 +1,7 @@
+jiff-icu
+========
+WIP
+
+### Documentation
+
+https://docs.rs/jiff-icu
diff --git a/crates/jiff-sqlx/UNLICENSE b/crates/jiff-sqlx/UNLICENSE
new file mode 100644
index 00000000..68a49daa
--- /dev/null
+++ b/crates/jiff-sqlx/UNLICENSE
@@ -0,0 +1,24 @@
+This is free and unencumbered software released into the public domain.
+
+Anyone is free to copy, modify, publish, use, compile, sell, or
+distribute this software, either in source code form or as a compiled
+binary, for any purpose, commercial or non-commercial, and by any
+means.
+
+In jurisdictions that recognize copyright laws, the author or authors
+of this software dedicate any and all copyright interest in the
+software to the public domain. We make this dedication for the benefit
+of the public at large and to the detriment of our heirs and
+successors. We intend this dedication to be an overt act of
+relinquishment in perpetuity of all present and future rights to this
+software under copyright law.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+For more information, please refer to <http://unlicense.org/>
diff --git a/crates/jiff-sqlx/src/lib.rs b/crates/jiff-sqlx/src/lib.rs
new file mode 100644
index 00000000..9145041d
--- /dev/null
+++ b/crates/jiff-sqlx/src/lib.rs
@@ -0,0 +1,73 @@
+/*!
+This crate provides integration points for [Jiff](jiff) and [SQLx][sqlx].
+
+Examples can be found in the
+[examples directory of the Jiff repository][examples].
+
+# Organization
+
+This crates defines several types that wrap corresponding types in Jiff. Each
+wrapper type provides implementations of traits found in SQLx. For most types,
+these are the [`sqlx_core::types::Type`], [`sqlx_core::decode::Decode`] and
+[`sqlx_core::encode::Encode`] traits.
+
+The intended workflow is to use these wrapper types within your wire types for
+encoding and decoding data from databases such as PostgreSQL. The wrapper types
+own the logic for encoding and decoding the data in database specific formats.
+
+In order to the minimize the annoyance of wrapper types, the following
+conveniences are afforded:
+
+* A [`ToSqlx`] trait is provided. Several Jiff types implement this trait. The
+trait provides easy conversion to the corresponding wrapper type in this crate.
+* A concrete `to_jiff` method is provided on each wrapper type. For example,
+[`Timestamp::to_jiff`]. This method is the reverse of `ToSqlx`. This converts
+from the wrapper type to the corresponding Jiff type.
+* There are `From` trait implementations from the wrapper type to the
+corresponding Jiff type, and vice versa.
+
+# Database support
+
+At present, both PostgreSQL and SQLite are supported.
+
+Ideally, MySQL support would be present too, but it
+[appears impossible until SQLx exposes some APIs][sqlx-mysql-bunk].
+
+# Future
+
+This crate exists because there are generally only three ways to implement
+the necessary traits in SQLx:
+
+1. Make Jiff depend on SQLx, and implement the corresponding traits where
+Jiff's types are defined.
+2. Make SQLx depend on Jiff, and implement the corresponding traits where the
+traits are defined.
+3. Make a crate like this one with types that wrap Jiff's types, and implements
+the corresponding traits for the wrapper types.
+
+This was done because it seems inappropriate for a "lower level" crate like
+Jiff to depend on SQLx. And while it might be appropriate for SQLx to optionally
+depend on Jiff (like it does for [`chrono`] or [`time`]), at time of writing,
+Jiff is still early in its life. It's totally reasonable to wait for it to
+mature. Plus, the thought of three different datetime integrations is,
+admittedly, tough to stomach.
+
+In the future, it may be prudent for this crate to be upstreamed into SQLx
+itself.
+
+[sqlx]: https://docs.rs/sqlx/0.8
+[examples]: https://github.com/BurntSushi/jiff/tree/master/examples/uptime
+[`chrono`]: https://docs.rs/chrono
+[`time`]: https://docs.rs/time
+[sqlx-mysql-bunk]: https://github.com/launchbadge/sqlx/issues/3487#issuecomment-2641843693
+*/
+
+#![deny(missing_docs)]
+
+pub use self::wrappers::{Date, DateTime, Span, Time, Timestamp, ToSqlx};
+
+#[cfg(feature = "postgres")]
+mod postgres;
+#[cfg(feature = "sqlite")]
+mod sqlite;
+mod wrappers;
diff --git a/crates/jiff-sqlx/src/postgres.rs b/crates/jiff-sqlx/src/postgres.rs
new file mode 100644
index 00000000..edcb179c
--- /dev/null
+++ b/crates/jiff-sqlx/src/postgres.rs
@@ -0,0 +1,290 @@
+use jiff::{civil, tz};
+use sqlx_core::{
+    decode::Decode,
+    encode::{Encode, IsNull},
+    error::BoxDynError,
+    types::Type,
+};
+use sqlx_postgres::{
+    types::{Oid, PgInterval},
+    PgArgumentBuffer, PgHasArrayType, PgTypeInfo, PgValueFormat, PgValueRef,
+    Postgres,
+};
+
+use crate::{Date, DateTime, Span, Time, Timestamp, ToSqlx};
+
+/// Apprently the actual format of values on the wire is not
+/// a documented guarantee of PostgreSQL.[1] Instead, I just `sqlx`'s
+/// source code for `chrono` to figure out what the type of the source
+/// data is.
+///
+/// [1]: https://www.postgresql.org/docs/current/protocol-overview.html#PROTOCOL-FORMAT-CODES
+static POSTGRES_EPOCH_DATE: civil::Date = civil::date(2000, 1, 1);
+static POSTGRES_EPOCH_DATETIME: civil::DateTime =
+    civil::date(2000, 1, 1).at(0, 0, 0, 0);
+static POSTGRES_EPOCH_TIMESTAMP: i64 = 946684800;
+static MIDNIGHT: civil::Time = civil::Time::midnight();
+static UTC: tz::TimeZone = tz::TimeZone::UTC;
+
+// We currently don't support `Zoned` integration in this wrapper crate.
+// See comments in `src/wrappers.rs`.
+//
+// Ref: https://github.com/launchbadge/sqlx/issues/3487#issuecomment-2636542379
+/*
+impl Type<Postgres> for Zoned {
+    fn type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L473
+        PgTypeInfo::with_oid(Oid(25))
+    }
+}
+
+impl PgHasArrayType for Zoned {
+    fn array_type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L503
+        PgTypeInfo::with_oid(Oid(1009))
+    }
+}
+
+impl Encode<'_, Postgres> for Zoned {
+    fn encode_by_ref(
+        &self,
+        buf: &mut PgArgumentBuffer,
+    ) -> Result<IsNull, BoxDynError> {
+        // There's no PostgreSQL data type for storing timestamps with time
+        // zones (despite the existence of a `TIMESTAMP WITH TIME ZONE` type,
+        // which is in fact just a timestamp), so we just use strings and
+        // RFC 9557 timestamps.
+        Encode::<Postgres>::encode(self.to_jiff().to_string(), buf)
+    }
+}
+
+impl<'r> Decode<'r, Postgres> for Zoned {
+    fn decode(value: PgValueRef<'r>) -> Result<Zoned, BoxDynError> {
+        Ok(value.as_str()?.parse::<jiff::Zoned>()?.to_sqlx())
+    }
+}
+*/
+
+impl Type<Postgres> for Timestamp {
+    fn type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L525
+        PgTypeInfo::with_oid(Oid(1184))
+    }
+}
+
+impl PgHasArrayType for Timestamp {
+    fn array_type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L526
+        PgTypeInfo::with_oid(Oid(1185))
+    }
+}
+
+impl Encode<'_, Postgres> for Timestamp {
+    fn encode_by_ref(
+        &self,
+        buf: &mut PgArgumentBuffer,
+    ) -> Result<IsNull, BoxDynError> {
+        // I guess the encoding here, based on sqlx, is the same as civil time.
+        // But the assumption is that the civil time is in UTC.
+        let dt = UTC.to_datetime(self.to_jiff()).to_sqlx();
+        Encode::<Postgres>::encode(dt, buf)
+    }
+}
+
+impl<'r> Decode<'r, Postgres> for Timestamp {
+    fn decode(value: PgValueRef<'r>) -> Result<Timestamp, BoxDynError> {
+        match value.format() {
+            PgValueFormat::Binary => {
+                // The encoding is the number of *microseconds* since
+                // POSTGRES_EPOCH_DATETIME.
+                let micros: i64 = Decode::<Postgres>::decode(value)?;
+                let micros = jiff::SignedDuration::from_micros(micros);
+                // OK because the timestamp is known to be valid and in range.
+                let epoch =
+                    jiff::Timestamp::from_second(POSTGRES_EPOCH_TIMESTAMP)
+                        .unwrap();
+                Ok(epoch.checked_add(micros)?.to_sqlx())
+            }
+            PgValueFormat::Text => {
+                // The encoding is just ISO 8601 I think? Close to RFC 3339,
+                // but not quite I think. Either way, Jiff's default parser
+                // will handle it.
+                //
+                // This does swallow the offset (but respects it correctl so
+                // that the proper instant is parsed). If one needs the offset,
+                // we'll need to expose a new `TimestampWithOffset` wrapper
+                // type. Please file an issue. (But this seems fraught since
+                // it's only available in text mode I guess? WTF.)
+                Ok(value.as_str()?.parse::<jiff::Timestamp>()?.to_sqlx())
+            }
+        }
+    }
+}
+
+impl Type<Postgres> for DateTime {
+    fn type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L521
+        // Note that we use the oid for a type called "timestamp," even
+        // though this is clearly not a timestamp. It's a civil datetime.
+        // But that's PostgreSQL (or I guess just SQL) for you.
+        PgTypeInfo::with_oid(Oid(1114))
+    }
+}
+
+impl PgHasArrayType for DateTime {
+    fn array_type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L522
+        PgTypeInfo::with_oid(Oid(1115))
+    }
+}
+
+impl Encode<'_, Postgres> for DateTime {
+    fn encode_by_ref(
+        &self,
+        buf: &mut PgArgumentBuffer,
+    ) -> Result<IsNull, BoxDynError> {
+        // The encoding is the number of *microseconds* since
+        // POSTGRES_EPOCH_DATETIME.
+        let micros =
+            self.to_jiff().duration_since(POSTGRES_EPOCH_DATETIME).as_micros();
+        // OK because the maximum duration between two Jiff civil datetimes
+        // is 631,107,417,599,999,999, which is less than i64::MAX.
+        let micros = i64::try_from(micros).unwrap();
+        Encode::<Postgres>::encode(micros, buf)
+    }
+}
+
+impl<'r> Decode<'r, Postgres> for DateTime {
+    fn decode(value: PgValueRef<'r>) -> Result<DateTime, BoxDynError> {
+        match value.format() {
+            PgValueFormat::Binary => {
+                // The encoding is the number of *microseconds* since
+                // POSTGRES_EPOCH_DATETIME.
+                let micros: i64 = Decode::<Postgres>::decode(value)?;
+                let micros = jiff::SignedDuration::from_micros(micros);
+                Ok(POSTGRES_EPOCH_DATETIME.checked_add(micros)?.to_sqlx())
+            }
+            PgValueFormat::Text => {
+                // The encoding is just ISO 8601 I think?
+                // The `chrono` implementation in `sqlx` does a dance with
+                // trying to parse offsets, but Jiff's `civil::DateTime`
+                // parser will handle that automatically.
+                Ok(value.as_str()?.parse::<civil::DateTime>()?.to_sqlx())
+            }
+        }
+    }
+}
+
+impl Type<Postgres> for Date {
+    fn type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L519
+        PgTypeInfo::with_oid(Oid(1082))
+    }
+}
+
+impl PgHasArrayType for Date {
+    fn array_type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L523
+        PgTypeInfo::with_oid(Oid(1182))
+    }
+}
+
+impl Encode<'_, Postgres> for Date {
+    fn encode_by_ref(
+        &self,
+        buf: &mut PgArgumentBuffer,
+    ) -> Result<IsNull, BoxDynError> {
+        // The encoding is the number of days since
+        // POSTGRES_EPOCH_DATE.
+        let days = (self.to_jiff() - POSTGRES_EPOCH_DATE).get_days();
+        Encode::<Postgres>::encode(days, buf)
+    }
+}
+
+impl<'r> Decode<'r, Postgres> for Date {
+    fn decode(value: PgValueRef<'r>) -> Result<Date, BoxDynError> {
+        match value.format() {
+            PgValueFormat::Binary => {
+                // The encoding is the number of days since
+                // POSTGRES_EPOCH_DATE.
+                let days: i32 = Decode::<Postgres>::decode(value)?;
+                let span = jiff::Span::new().try_days(days)?;
+                Ok(POSTGRES_EPOCH_DATE.checked_add(span)?.to_sqlx())
+            }
+            PgValueFormat::Text => {
+                // The encoding is just ISO 8601.
+                Ok(value.as_str()?.parse::<civil::Date>()?.to_sqlx())
+            }
+        }
+    }
+}
+
+impl Type<Postgres> for Time {
+    fn type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L520
+        PgTypeInfo::with_oid(Oid(1083))
+    }
+}
+
+impl PgHasArrayType for Time {
+    fn array_type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L524
+        PgTypeInfo::with_oid(Oid(1183))
+    }
+}
+
+impl Encode<'_, Postgres> for Time {
+    fn encode_by_ref(
+        &self,
+        buf: &mut PgArgumentBuffer,
+    ) -> Result<IsNull, BoxDynError> {
+        // The encoding is the number of *microseconds* since midnight.
+        let micros = self.to_jiff().duration_since(MIDNIGHT).as_micros();
+        // OK since the max number of microseconds here is
+        // 86399999999, which always fits into an `i64`.
+        let micros = i64::try_from(micros).unwrap();
+        Encode::<Postgres>::encode(micros, buf)
+    }
+}
+
+impl<'r> Decode<'r, Postgres> for Time {
+    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
+        match value.format() {
+            PgValueFormat::Binary => {
+                // The encoding is the number of *microseconds* since midnight.
+                let micros: i64 = Decode::<Postgres>::decode(value)?;
+                let micros = jiff::SignedDuration::from_micros(micros);
+                Ok(MIDNIGHT.checked_add(micros)?.to_sqlx())
+            }
+            PgValueFormat::Text => {
+                // The encoding is just ISO 8601.
+                Ok(value.as_str()?.parse::<civil::Time>()?.to_sqlx())
+            }
+        }
+    }
+}
+
+impl Type<Postgres> for Span {
+    fn type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L527
+        PgTypeInfo::with_oid(Oid(1186))
+    }
+}
+
+impl PgHasArrayType for Span {
+    fn array_type_info() -> PgTypeInfo {
+        // https://github.com/launchbadge/sqlx/blob/65229f7ff91ecd38be7c10fb61ff3e05bedabe87/sqlx-postgres/src/type_info.rs#L528
+        PgTypeInfo::with_oid(Oid(1187))
+    }
+}
+
+impl<'r> Decode<'r, Postgres> for Span {
+    fn decode(value: PgValueRef<'r>) -> Result<Self, BoxDynError> {
+        let interval: PgInterval = Decode::<Postgres>::decode(value)?;
+        let span = jiff::Span::new()
+            .try_months(interval.months)?
+            .try_days(interval.days)?
+            .try_microseconds(interval.microseconds)?;
+        Ok(span.to_sqlx())
+    }
+}
diff --git a/crates/jiff-sqlx/src/sqlite.rs b/crates/jiff-sqlx/src/sqlite.rs
new file mode 100644
index 00000000..dc498989
--- /dev/null
+++ b/crates/jiff-sqlx/src/sqlite.rs
@@ -0,0 +1,143 @@
+use jiff::fmt::temporal::DateTimeParser;
+use sqlx_core::{
+    decode::Decode,
+    encode::{Encode, IsNull},
+    error::BoxDynError,
+    types::Type,
+};
+use sqlx_sqlite::{
+    Sqlite, SqliteArgumentValue, SqliteTypeInfo, SqliteValueRef,
+};
+
+use crate::{Date, DateTime, Time, Timestamp, ToSqlx};
+
+static PARSER: DateTimeParser = DateTimeParser::new();
+
+impl Type<Sqlite> for Timestamp {
+    fn type_info() -> SqliteTypeInfo {
+        <str as Type<Sqlite>>::type_info()
+    }
+
+    fn compatible(ty: &SqliteTypeInfo) -> bool {
+        <str as Type<Sqlite>>::compatible(ty)
+            || <f64 as Type<Sqlite>>::compatible(ty)
+    }
+}
+
+impl Encode<'_, Sqlite> for Timestamp {
+    fn encode_by_ref(
+        &self,
+        buf: &mut Vec<SqliteArgumentValue<'_>>,
+    ) -> Result<IsNull, BoxDynError> {
+        Encode::<Sqlite>::encode(self.to_jiff().to_string(), buf)
+    }
+}
+
+impl<'r> Decode<'r, Sqlite> for Timestamp {
+    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
+        // We use a `&str` here because we might need to parse an `f64` from
+        // it. std doesn't support parsing `f64` from `&[u8]` AND it seems like
+        // we can pass `value` by reference or clone it, so we are limited
+        // to exactly one decode. WTF.
+        let text = <&str as Decode<Sqlite>>::decode(value)?;
+        // If there's a `:` somewhere, then it must be a textual timestamp.
+        // Moreover, a textual timestamp requires that a `:` be present
+        // somewhere for Jiff to parse it. (SQLite might not strictly require
+        // this though...)
+        if text.contains(':') {
+            let date = PARSER.parse_timestamp(text)?;
+            return Ok(date.to_sqlx());
+        }
+        let julian_days = text.parse::<f64>()?;
+        julian_days_to_timestamp(julian_days).map(jiff::Timestamp::to_sqlx)
+    }
+}
+
+impl Type<Sqlite> for DateTime {
+    fn type_info() -> SqliteTypeInfo {
+        <str as Type<Sqlite>>::type_info()
+    }
+}
+
+impl Encode<'_, Sqlite> for DateTime {
+    fn encode_by_ref(
+        &self,
+        buf: &mut Vec<SqliteArgumentValue<'_>>,
+    ) -> Result<IsNull, BoxDynError> {
+        Encode::<Sqlite>::encode(self.to_jiff().to_string(), buf)
+    }
+}
+
+impl<'r> Decode<'r, Sqlite> for DateTime {
+    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
+        let text = <&[u8] as Decode<Sqlite>>::decode(value)?;
+        let date = PARSER.parse_datetime(text)?;
+        Ok(date.to_sqlx())
+    }
+}
+
+impl Type<Sqlite> for Date {
+    fn type_info() -> SqliteTypeInfo {
+        <str as Type<Sqlite>>::type_info()
+    }
+}
+
+impl Encode<'_, Sqlite> for Date {
+    fn encode_by_ref(
+        &self,
+        buf: &mut Vec<SqliteArgumentValue<'_>>,
+    ) -> Result<IsNull, BoxDynError> {
+        Encode::<Sqlite>::encode(self.to_jiff().to_string(), buf)
+    }
+}
+
+impl<'r> Decode<'r, Sqlite> for Date {
+    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
+        let text = <&[u8] as Decode<Sqlite>>::decode(value)?;
+        let date = PARSER.parse_date(text)?;
+        Ok(date.to_sqlx())
+    }
+}
+
+impl Type<Sqlite> for Time {
+    fn type_info() -> SqliteTypeInfo {
+        <str as Type<Sqlite>>::type_info()
+    }
+}
+
+impl Encode<'_, Sqlite> for Time {
+    fn encode_by_ref(
+        &self,
+        buf: &mut Vec<SqliteArgumentValue<'_>>,
+    ) -> Result<IsNull, BoxDynError> {
+        Encode::<Sqlite>::encode(self.to_jiff().to_string(), buf)
+    }
+}
+
+impl<'r> Decode<'r, Sqlite> for Time {
+    fn decode(value: SqliteValueRef<'r>) -> Result<Self, BoxDynError> {
+        static PARSER: DateTimeParser = DateTimeParser::new();
+
+        let text = <&[u8] as Decode<Sqlite>>::decode(value)?;
+        let date = PARSER.parse_time(text)?;
+        Ok(date.to_sqlx())
+    }
+}
+
+fn julian_days_to_timestamp(
+    days: f64,
+) -> Result<jiff::Timestamp, BoxDynError> {
+    // The Unix epoch in terms of SQLite julian days:
+    //
+    //     sqlite> select julianday('1970-01-01T00:00:00Z');
+    //     julianday('1970-01-01T00:00:00Z')
+    //     ---------------------------------
+    //     2440587.5
+    static UNIX_EPOCH_AS_JULIAN_DAYS: f64 = 2440587.5;
+    // SQLite assumes 24 hours in every day.
+    static SECONDS_PER_DAY: f64 = 86400.0;
+
+    let timestamp = (days - UNIX_EPOCH_AS_JULIAN_DAYS) * SECONDS_PER_DAY;
+    let sdur = jiff::SignedDuration::try_from_secs_f64(timestamp)?;
+    Ok(jiff::Timestamp::from_duration(sdur)?)
+}
diff --git a/crates/jiff-sqlx/src/wrappers.rs b/crates/jiff-sqlx/src/wrappers.rs
new file mode 100644
index 00000000..a8d9d455
--- /dev/null
+++ b/crates/jiff-sqlx/src/wrappers.rs
@@ -0,0 +1,267 @@
+/// A trait for convenient conversions from Jiff types to SQLx types.
+///
+/// # Example
+///
+/// This shows how to convert a [`jiff::Timestamp`] to a [`Timestamp`]:
+///
+/// ```
+/// use jiff_sqlx::ToSqlx;
+///
+/// let ts: jiff::Timestamp = "2025-02-20T17:00-05".parse()?;
+/// let wrapper = ts.to_sqlx();
+/// assert_eq!(format!("{wrapper:?}"), "Timestamp(2025-02-20T22:00:00Z)");
+///
+/// # Ok::<(), Box<dyn std::error::Error>>(())
+/// ```
+pub trait ToSqlx {
+    /// The wrapper type to convert to.
+    type Target;
+
+    /// A conversion method that converts a Jiff type to a SQLx wrapper type.
+    fn to_sqlx(self) -> Self::Target;
+}
+
+// We currently don't support `Zoned` integration in this wrapper crate. To
+// briefly explain why, a `Zoned` is _both_ a timestamp and a time zone. And
+// it isn't necessarily a dumb time zone like `-05:00`. It is intended to be a
+// real time zone like `America/New_York` or `Australia/Tasmania`.
+//
+// However, PostgreSQL doesn't really have a primitive type that specifically
+// supports "timestamp with time zone." Comically, it does have a `TIMESTAMP
+// WITH TIME ZONE` type (from the SQL standard, as I understand it), but it's
+// actually just a timestamp. It doesn't store any other data than a timestamp.
+// The difference between `TIMESTAMP WITHOUT TIME ZONE` and `TIMESTAMP WITH
+// TIME ZONE` is, principally, that fixed offsets are respected in the former
+// but completely ignored in the latter. (PostgreSQL's documentation refers
+// to fixed offsets as "time zones," which is rather antiquated IMO.) And,
+// conventionally, `TIMESTAMP WITHOUT TIME ZONE` is civil (local) time.
+//
+// So what's the problem? Well, if we try to stuff a `Zoned` into a
+// `TIMESTAMP WITH TIME ZONE`, then the *only* thing that actually
+// gets stored is a timestamp. No time zone. No offset. Nothing. That
+// means the time zone attached to `Zoned` gets dropped. So if you put
+// `2025-02-15T17:00-05[America/New_york]` into the database, then you'll
+// always get `2025-02-15T22:00+00[UTC]` out. That's a silent dropping of the
+// time zone data. I personally think this would be extremely surprising,
+// and it could lead to bugs if you assume the time zone is correctly
+// round-tripped. (For example, DST safe arithmetic would no longer apply.)
+// And indeed, this is a principle design goal of Jiff: `Zoned` values can be
+// losslessly transmitted.
+//
+// An alternative here is to provide a `Zoned` wrapper, but store it in
+// a `TEXT` field as an RFC 9557 timestamp. This would permit lossless
+// round-tripping. The problem is that this may also violate user expectations
+// because a datetime is not stored in a datetime field. Thus, you won't be
+// able to use PostgreSQL native functionality to handle it as a date.
+//
+// So for now, I opted to take the conservative choice and just not provide a
+// `Zoned` impl. This way, we can gather use cases and make a better informed
+// decision of what to do. Plus, the maintainer of `sqlx` seemed unconvinced
+// that a `Zoned` impl that drops time zone data was a problem, so out of
+// deference there, I started with this more conservative strategy.
+//
+// Of course, if you want to store something in `TIMESTAMP WITHOUT TIME ZONE`,
+// then you can just use `Timestamp`.
+//
+// Ref: https://github.com/launchbadge/sqlx/issues/3487#issuecomment-2636542379
+/*
+#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
+pub struct Zoned(jiff::Zoned);
+
+impl Zoned {
+    pub fn to_jiff(&self) -> jiff::Zoned {
+        self.0.clone()
+    }
+}
+
+impl<'a> ToSqlx for &'a jiff::Zoned {
+    type Target = Zoned;
+
+    fn to_sqlx(self) -> Zoned {
+        Zoned(self.clone())
+    }
+}
+
+impl From<jiff::Zoned> for Zoned {
+    fn from(x: jiff::Zoned) -> Zoned {
+        Zoned(x)
+    }
+}
+
+impl From<Zoned> for jiff::Zoned {
+    fn from(x: Zoned) -> jiff::Zoned {
+        x.0
+    }
+}
+
+impl core::ops::Deref for Zoned {
+    type Target = jiff::Zoned;
+
+    fn deref(&self) -> &jiff::Zoned {
+        &self.0
+    }
+}
+*/
+
+/// A wrapper type for [`jiff::Timestamp`].
+#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
+pub struct Timestamp(jiff::Timestamp);
+
+impl Timestamp {
+    /// Converts this wrapper to a [`jiff::Timestamp`].
+    pub fn to_jiff(self) -> jiff::Timestamp {
+        self.0
+    }
+}
+
+impl ToSqlx for jiff::Timestamp {
+    type Target = Timestamp;
+
+    fn to_sqlx(self) -> Timestamp {
+        Timestamp(self)
+    }
+}
+
+impl From<jiff::Timestamp> for Timestamp {
+    fn from(x: jiff::Timestamp) -> Timestamp {
+        Timestamp(x)
+    }
+}
+
+impl From<Timestamp> for jiff::Timestamp {
+    fn from(x: Timestamp) -> jiff::Timestamp {
+        x.0
+    }
+}
+
+/// A wrapper type for [`jiff::civil::DateTime`].
+#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
+pub struct DateTime(jiff::civil::DateTime);
+
+impl DateTime {
+    /// Converts this wrapper to a [`jiff::civil::DateTime`].
+    pub fn to_jiff(self) -> jiff::civil::DateTime {
+        self.0
+    }
+}
+
+impl ToSqlx for jiff::civil::DateTime {
+    type Target = DateTime;
+
+    fn to_sqlx(self) -> DateTime {
+        DateTime(self)
+    }
+}
+
+impl From<jiff::civil::DateTime> for DateTime {
+    fn from(x: jiff::civil::DateTime) -> DateTime {
+        DateTime(x)
+    }
+}
+
+impl From<DateTime> for jiff::civil::DateTime {
+    fn from(x: DateTime) -> jiff::civil::DateTime {
+        x.0
+    }
+}
+
+/// A wrapper type for [`jiff::civil::Date`].
+#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
+pub struct Date(jiff::civil::Date);
+
+impl Date {
+    /// Converts this wrapper to a [`jiff::civil::Date`].
+    pub fn to_jiff(self) -> jiff::civil::Date {
+        self.0
+    }
+}
+
+impl ToSqlx for jiff::civil::Date {
+    type Target = Date;
+
+    fn to_sqlx(self) -> Date {
+        Date(self)
+    }
+}
+
+impl From<jiff::civil::Date> for Date {
+    fn from(x: jiff::civil::Date) -> Date {
+        Date(x)
+    }
+}
+
+impl From<Date> for jiff::civil::Date {
+    fn from(x: Date) -> jiff::civil::Date {
+        x.0
+    }
+}
+
+/// A wrapper type for [`jiff::civil::Time`].
+#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
+pub struct Time(jiff::civil::Time);
+
+impl Time {
+    /// Converts this wrapper to a [`jiff::civil::Time`].
+    pub fn to_jiff(self) -> jiff::civil::Time {
+        self.0
+    }
+}
+
+impl ToSqlx for jiff::civil::Time {
+    type Target = Time;
+
+    fn to_sqlx(self) -> Time {
+        Time(self)
+    }
+}
+
+impl From<jiff::civil::Time> for Time {
+    fn from(x: jiff::civil::Time) -> Time {
+        Time(x)
+    }
+}
+
+impl From<Time> for jiff::civil::Time {
+    fn from(x: Time) -> jiff::civil::Time {
+        x.0
+    }
+}
+
+/// A wrapper type for [`jiff::Span`].
+///
+/// # PostgreSQL: Limited support
+///
+/// This type _only_ has a [`sqlx_core::decode::Decode`] trait implementation
+/// for PostgreSQL. The reason for this is that encoding an arbitrary
+/// `Span` into a PostgreSQL interval requires a relative datetime.
+/// Therefore, users wanting to store a `Span` will need to explicitly use a
+/// [`sqlx_postgres::types::PgInterval`] at least at encoding time.
+#[derive(Clone, Copy, Debug)]
+pub struct Span(jiff::Span);
+
+impl Span {
+    /// Converts this wrapper to a [`jiff::Span`].
+    pub fn to_jiff(self) -> jiff::Span {
+        self.0
+    }
+}
+
+impl ToSqlx for jiff::Span {
+    type Target = Span;
+
+    fn to_sqlx(self) -> Span {
+        Span(self)
+    }
+}
+
+impl From<jiff::Span> for Span {
+    fn from(x: jiff::Span) -> Span {
+        Span(x)
+    }
+}
+
+impl From<Span> for jiff::Span {
+    fn from(x: Span) -> jiff::Span {
+        x.0
+    }
+}
diff --git a/examples/sqlx-postgres/.gitignore b/examples/sqlx-postgres/.gitignore
new file mode 100644
index 00000000..1b72444a
--- /dev/null
+++ b/examples/sqlx-postgres/.gitignore
@@ -0,0 +1,2 @@
+/Cargo.lock
+/target
diff --git a/examples/sqlx-postgres/Cargo.toml b/examples/sqlx-postgres/Cargo.toml
new file mode 100644
index 00000000..28f161b5
--- /dev/null
+++ b/examples/sqlx-postgres/Cargo.toml
@@ -0,0 +1,22 @@
+[package]
+publish = false
+name = "sqlx-postgres-test"
+version = "0.0.1"
+edition = "2021"
+
+# This example has very fat dependencies. And running it requires a PostgreSQL
+# dataabase connection. This makes it so this example won't get compiled or run
+# with `cargo test --all`.
+[workspace]
+
+[dependencies]
+anyhow = "1.0.95"
+chrono = "0.4.39"
+jiff = { path = "../.." }
+jiff-sqlx = { path = "../../crates/jiff-sqlx", features = ["postgres"] }
+sqlx = { version = "0.8.3", features = ["chrono", "postgres", "runtime-tokio"] }
+tokio = { version = "1.43.0", features = ["full"] }
+
+[[bin]]
+name = "sqlx-postgres-test"
+path = "main.rs"
diff --git a/examples/sqlx-postgres/main.rs b/examples/sqlx-postgres/main.rs
new file mode 100644
index 00000000..378d1b68
--- /dev/null
+++ b/examples/sqlx-postgres/main.rs
@@ -0,0 +1,94 @@
+use jiff::civil;
+use jiff_sqlx::ToSqlx;
+use sqlx::postgres::{PgPool, PgPoolOptions};
+
+type Record = (
+    jiff_sqlx::Timestamp,
+    jiff_sqlx::DateTime,
+    jiff_sqlx::Date,
+    jiff_sqlx::Time,
+);
+
+#[tokio::main]
+async fn main() -> anyhow::Result<()> {
+    let pool = PgPoolOptions::new()
+        .max_connections(1)
+        .connect("postgres://postgres:password@localhost/test")
+        .await?;
+
+    example_datetime_roundtrip(&pool).await?;
+    example_span_decode(&pool).await?;
+    example_time_zone_setting(&pool).await?;
+
+    Ok(())
+}
+
+/// Performs a round-trip with all of Jiff's datetime types.
+async fn example_datetime_roundtrip(pool: &PgPool) -> anyhow::Result<()> {
+    let given = (
+        "1970-01-01T00:00:00Z".parse::<jiff::Timestamp>()?.to_sqlx(),
+        civil::date(2025, 7, 20).at(0, 0, 0, 0).to_sqlx(),
+        civil::date(1999, 1, 8).to_sqlx(),
+        civil::time(23, 59, 59, 999_999_000).to_sqlx(),
+    );
+    let query = "SELECT $1, $2, $3, $4";
+    let got: Record = sqlx::query_as(query)
+        .bind(&given.0)
+        .bind(given.1)
+        .bind(given.2)
+        .bind(given.3)
+        .fetch_one(pool)
+        .await?;
+    assert_eq!(got, given);
+    Ok(())
+}
+
+/// Shows how to decode a PostgreSQL interval into a Jiff `Span`.
+///
+/// Jiff doesn't support encoding a `Span` as a PostgreSQL interval, but
+/// Jiff can decode a PostgreSQL interval into a `Span`. The reason for this
+/// is that encoding an arbitrary `Span` into a PostgreSQL interval requires
+/// a relative datetime. Therefore, users wanting to store a `Span` will
+/// need to explicitly use a `sqlx::postgres::types::PgInterval` at least at
+/// encoding time.
+async fn example_span_decode(pool: &PgPool) -> anyhow::Result<()> {
+    let query =
+        "SELECT '2 years 15 months 100 weeks 99 hours 123456789 milliseconds'::interval;";
+    let (span,): (jiff_sqlx::Span,) =
+        sqlx::query_as(query).fetch_one(pool).await?;
+    assert_eq!(
+        span.to_jiff().fieldwise(),
+        // The reason the span is in months/days/micros is because this
+        // is how te interval is transmitted from PostgreSQL. Years and
+        // months collapse into months, weeks and days collapses into days
+        // and everything else collapses into microseconds. If you need to
+        // preserve a `Span` as-is, then you'll need to encode it using Jiff's
+        // "friendly" duration format.
+        jiff::Span::new().months(39).days(700).microseconds(479856789000i64),
+    );
+    Ok(())
+}
+
+/// Demonstrates that PostgreSQL's time zone session setting doesn't fuck up
+/// decoding.
+///
+/// Ref: https://github.com/launchbadge/sqlx/issues/703#issuecomment-939133588
+async fn example_time_zone_setting(pool: &PgPool) -> anyhow::Result<()> {
+    use sqlx::{Executor, Row};
+
+    sqlx::query("SET TIME ZONE 'Australia/Tasmania'")
+        .fetch_optional(pool)
+        .await?;
+
+    let row =
+        pool.fetch_one("SELECT '2020-01-01 05:01:01+00'::timestamptz").await?;
+    let ts = "2020-01-01T05:01:01Z".parse::<jiff::Timestamp>()?.to_sqlx();
+    assert_eq!(row.get::<jiff_sqlx::Timestamp, _>(0), ts);
+
+    let row =
+        pool.fetch_one("SELECT '2020-01-01 05:01:01+00'::timestamp").await?;
+    let dt = "2020-01-01T05:01:01".parse::<jiff::civil::DateTime>()?.to_sqlx();
+    assert_eq!(row.get::<jiff_sqlx::DateTime, _>(0), dt);
+
+    Ok(())
+}
diff --git a/examples/sqlx-sqlite/.gitignore b/examples/sqlx-sqlite/.gitignore
new file mode 100644
index 00000000..1b72444a
--- /dev/null
+++ b/examples/sqlx-sqlite/.gitignore
@@ -0,0 +1,2 @@
+/Cargo.lock
+/target
diff --git a/examples/sqlx-sqlite/Cargo.toml b/examples/sqlx-sqlite/Cargo.toml
new file mode 100644
index 00000000..dfcc3fbf
--- /dev/null
+++ b/examples/sqlx-sqlite/Cargo.toml
@@ -0,0 +1,23 @@
+[package]
+publish = false
+name = "sqlx-sqlite-test"
+version = "0.0.1"
+edition = "2021"
+
+# This example has very fat dependencies. And running it requires a PostgreSQL
+# dataabase connection. This makes it so this example won't get compiled or run
+# with `cargo test --all`.
+[workspace]
+
+[dependencies]
+anyhow = "1.0.95"
+chrono = "0.4.39"
+jiff = { path = "../.." }
+jiff-sqlx = { path = "../../crates/jiff-sqlx", features = ["sqlite"] }
+tempfile = "3.16.0"
+sqlx = { version = "0.8.3", features = ["chrono", "runtime-tokio", "sqlite"] }
+tokio = { version = "1.43.0", features = ["full"] }
+
+[[bin]]
+name = "sqlx-sqlite-test"
+path = "main.rs"
diff --git a/examples/sqlx-sqlite/main.rs b/examples/sqlx-sqlite/main.rs
new file mode 100644
index 00000000..23cc9b4b
--- /dev/null
+++ b/examples/sqlx-sqlite/main.rs
@@ -0,0 +1,76 @@
+use anyhow::Context;
+use jiff::civil;
+use jiff_sqlx::ToSqlx;
+use sqlx::SqlitePool;
+
+#[tokio::main]
+async fn main() -> anyhow::Result<()> {
+    let tmpfile = tempfile::NamedTempFile::new()?;
+    let path = tmpfile.path().to_str().context("invalid temporary file")?;
+    let pool = SqlitePool::connect(path).await?;
+
+    example_datetime_roundtrip(&pool).await?;
+    example_timestamp_julian(&pool).await?;
+
+    Ok(())
+}
+
+/// Performs a round-trip with all of Jiff's datetime types.
+async fn example_datetime_roundtrip(pool: &SqlitePool) -> anyhow::Result<()> {
+    type Record = (
+        jiff_sqlx::Timestamp,
+        jiff_sqlx::DateTime,
+        jiff_sqlx::Date,
+        jiff_sqlx::Time,
+    );
+
+    let given = (
+        "1970-01-01T00:00:00Z".parse::<jiff::Timestamp>()?.to_sqlx(),
+        civil::date(2025, 7, 20).at(0, 0, 0, 0).to_sqlx(),
+        civil::date(1999, 1, 8).to_sqlx(),
+        civil::time(23, 59, 59, 999_999_999).to_sqlx(),
+    );
+    let query = "SELECT $1, $2, $3, $4";
+    let got: Record = sqlx::query_as(query)
+        .bind(&given.0)
+        .bind(&given.1)
+        .bind(&given.2)
+        .bind(&given.3)
+        .fetch_one(pool)
+        .await?;
+    assert_eq!(got, given);
+    Ok(())
+}
+
+/// Demonstrates that Jiff works with SQLite's "julian day" format.
+///
+/// Here's a sample transcript to show how SQLite itself works:
+///
+/// ```text
+/// sqlite> select julianday('2025-02-06T21:33:30-05:00');
+/// julianday('2025-02-06T21:33:30-05:00')
+/// --------------------------------------
+/// 2460713.60659722
+/// sqlite> select datetime(2460713.60659722);
+/// datetime(2460713.60659722)
+/// --------------------------
+/// 2025-02-07 02:33:30
+/// sqlite> select unixepoch(2460713.60659722);
+/// unixepoch(2460713.60659722)
+/// ---------------------------
+/// 1738895610
+/// ```
+async fn example_timestamp_julian(pool: &SqlitePool) -> anyhow::Result<()> {
+    let given = "2025-02-06T21:33:30-05".parse::<jiff::Timestamp>()?.to_sqlx();
+    let query = "SELECT julianday($1);";
+    let (got,): (jiff_sqlx::Timestamp,) =
+        sqlx::query_as(query).bind(&given).fetch_one(pool).await?;
+
+    // Play stupid games, win stupid prizes. The loss of precision here
+    // is what you get when you use floating point to represent datetimes.
+    let expected =
+        "2025-02-07T02:33:29.99981308Z".parse::<jiff::Timestamp>()?.to_sqlx();
+    assert_eq!(got, expected);
+
+    Ok(())
+}


Test patch: