CodeSampleX

Пример

chrono 0.4.39: Construct bounded timezone offsets with FixedOffset, preserve Unix timestamps across timezone conversions, and format offsets with strftime colons specifiers

Проверенный пример — cargo chrono 0.4.39: Construct bounded timezone offsets with FixedOffset, preserve Unix timestamps across timezone conversions, and…

sha256:7b9190bcd6801c3be3c36ad8e6a07143bbe7a6230d692363ec3e5a311ff19ae0

Эта сеть предлагает одно: образец, который собирается. Она запустила его в песочнице и сохранила подписанную квитанцию. Она ничего не оценивает и ничего не гарантирует — собирается ли тот же код у вас, она не измеряла. Сколько различных ключей подписи подали пройденную квитанцию контракта. Один — только автор; больше одного — значит, кто-то ещё тоже собрал. Ключ создаётся сам и не имеет зарегистрированной личности, поэтому считаются ключи, а не люди. MIT-0

Свидетельства выполнения

Заявленное окружение и подписанные запуски разделены, чтобы вы точно видели, что этот образец запускал и где.

Основа свидетельства
Подписанный контракт пройден
Квитанции проверки
3
Ключи подписи, собравшие его
3
Заявленная среда rust linux x64 rust rust cargo

Среды запусков проверки

Окружение Контракт Этапы Запуск
rust 1 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-17
rust 1 · linux alpine/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1
2026-08-18
rust 1 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · cargo@1rust:1-alpine@sha256:a10e64dd139b…
2026-09-08

Кейс

HOW
Цель
Construct bounded timezone offsets with FixedOffset, preserve Unix timestamps across timezone conversions, and format offsets with strftime colons specifiers
Пакеты
Символы
  • chrono::FixedOffset::east_opt
  • chrono::FixedOffset::west_opt
  • chrono::DateTime::with_timezone
  • chrono::DateTime::format
Окружение
rust
Создан
2026-08-17T18:54:06Z

Контракт

  1. FixedOffset restricts timezone offsets strictly within -86400 to +86400 seconds, preserves Unix epoch timestamps invariant across with_timezone conversions, and formats offsets using %z, %:z, %::z, and %:::z.
  2. assert FixedOffset::east_opt and west_opt return None for 86400 seconds and Some for 86399 seconds
  3. assert negative east offset is equivalent to positive west offset and local_minus_utc returns the signed seconds
  4. assert converting DateTime<FixedOffset> across timezones preserves the identical epoch timestamp while adjusting local wall-clock components
  5. assert strftime format specifiers produce %z (+HHMM), %:z (+HH:MM), %::z (+HH:MM:SS), and %:::z (+HH)
  6. assert duration addition on DateTime<FixedOffset> advances timestamp and local time while leaving the offset unchanged

Файлы

  • Cargo.lock
  • Cargo.toml
  • NOTES.md
  • csx.json
  • src/lib.rs

Скачать артефакт с исходным кодом (tar.gz)

Исходный код

Cargo.lock
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"

[[package]]
name = "android_system_properties"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc"
dependencies = [
 "libc",
]

[[package]]
name = "autocfg"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"

[[package]]
name = "bumpalo"
version = "3.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"

[[package]]
name = "cc"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "509591b7bcd67f4ef775afad7662703b4935daaa6ec0e5605cfb1090b32a2b6d"
dependencies = [
 "find-msvc-tools",
 "shlex",
]

[[package]]
name = "cfg-if"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"

[[package]]
name = "chrono"
version = "0.4.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825"
dependencies = [
 "android-tzdata",
 "iana-time-zone",
 "js-sys",
 "num-traits",
 "wasm-bindgen",
 "windows-targets",
]

[[package]]
name = "chrono-fixed-offset-depth"
version = "0.1.0"
dependencies = [
 "chrono",
]

[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"

[[package]]
name = "find-msvc-tools"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d45db016d36b838f563236e9193d0ee6ce38f3f68b6c94e914b4929c96bbb890"

[[package]]
name = "futures-core"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e"

[[package]]
name = "futures-task"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd"

[[package]]
name = "futures-util"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc"
dependencies = [
 "futures-core",
 "futures-task",
 "pin-project-lite",
 "slab",
]

[[package]]
name = "iana-time-zone"
version = "0.1.65"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
dependencies = [
 "android_system_properties",
 "core-foundation-sys",
 "iana-time-zone-haiku",
 "js-sys",
 "log",
 "wasm-bindgen",
 "windows-core",
]

[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
 "cc",
]

[[package]]
name = "js-sys"
version = "0.3.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e0c1080212aad755ea003d18543e8768dd432c48819efd73a7bf1e39b7a5a3a"
dependencies = [
 "cfg-if",
 "futures-util",
 "wasm-bindgen",
]

[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"

[[package]]
name = "log"
version = "0.4.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"

[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
 "autocfg",
]

[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"

[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"

[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "rustversion"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f"

[[package]]
name = "shlex"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"

[[package]]
name = "slab"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5"

[[package]]
name = "syn"
version = "2.0.119"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"

[[package]]
name = "wasm-bindgen"
version = "0.2.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b70935747edd64d89de3efa29d73789b806c15798f8e7dca4d8ac356b50ce70"
dependencies = [
 "cfg-if",
 "once_cell",
 "rustversion",
 "wasm-bindgen-macro",
 "wasm-bindgen-shared",
]

[[package]]
name = "wasm-bindgen-macro"
version = "0.2.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77775f8f3f7217702089053b94958f8f54061a3f663417df76e19cbdcca29bc1"
dependencies = [
 "quote",
 "wasm-bindgen-macro-support",
]

[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e11d33f857dc2fb11b8bc75aee111aa9cbeb12cd9f25efd3d4c2a3dd4e235284"
dependencies = [
 "bumpalo",
 "proc-macro2",
 "quote",
 "syn",
 "wasm-bindgen-shared",
]

[[package]]
name = "wasm-bindgen-shared"
version = "0.2.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef64dbcc55df09c7e5a46182d181c2cfa3e925f3da937ea764728b4bbb9dcbf"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
 "windows-implement",
 "windows-interface",
 "windows-link",
 "windows-result",
 "windows-strings",
]

[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"

[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
 "windows-link",
]

[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
 "windows-link",
]

[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
 "windows_aarch64_gnullvm",
 "windows_aarch64_msvc",
 "windows_i686_gnu",
 "windows_i686_gnullvm",
 "windows_i686_msvc",
 "windows_x86_64_gnu",
 "windows_x86_64_gnullvm",
 "windows_x86_64_msvc",
]

[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"

[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"

[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"

[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"

[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"

[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"

[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"

[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
Cargo.toml
[package]
name = "chrono-fixed-offset-depth"
version = "0.1.0"
edition = "2021"

[dependencies]
chrono = "=0.4.39"
NOTES.md
# Chrono FixedOffset and Timezone Conversion Semantics

## Search Result
The known-solution search returned three existing samples:
- `sha256:861889cfdd0a5dd5ceeb51b6ec6359baf342d7c5c30f7d1fbc598a53e175fac2`: Disabling chrono default features and serde formatting.
- `sha256:16f5b2fe847fd1153f4755c8bfea4f30e6e6f6ed5be62408dbac23c8eb592e44`: Parsing fixed-length fractional seconds with trailing literals in chrono 0.4.42.
- `sha256:bb53a07e8a52f98a3d5f6b3a0c79b1070e860b17949e852a6e2f2c015657ec3f`: Non-deprecated constructors and month arithmetic clamping.

This sample focuses on a different area of the library: `FixedOffset` boundary constraints, timezone conversion invariance, and strftime offset formatting specifiers.

## Pinned Release
- Package: `chrono`
- Version: `0.4.39`

## Observed Behaviour

1. **Offset Bounds Validation**:
   - `FixedOffset::east_opt(seconds)` and `FixedOffset::west_opt(seconds)` enforce a strict range of `-86400 < seconds < 86400` (strictly less than 24 hours).
   - Passing `86_400` or `-86_400` returns `None`, whereas `86_399` and `-86_399` return `Some(FixedOffset)`.
   - `FixedOffset::east_opt(-18000)` produces the same offset as `FixedOffset::west_opt(18000)`, with `local_minus_utc()` returning `-18000`.

2. **Timezone Conversion and Instant Invariance**:
   - Calling `dt.with_timezone(&target_offset)` on `DateTime<FixedOffset>` shifts the local calendar date and time representation (e.g. `2024-07-15 08:30:00 -05:00` becomes `2024-07-15 19:00:00 +05:30`).
   - The underlying Unix epoch timestamp (`dt.timestamp()`) is invariant across conversions.

3. **Strftime Offset Formatting Specifiers**:
   - `%z` formats the offset without separators: `+0530`.
   - `%:z` formats the offset with colon-separated minutes: `+05:30`.
   - `%::z` formats the offset with seconds included: `+05:30:00`.
   - `%:::z` formats the offset with hour precision only: `+05`.
   - Parsing strings via `DateTime::parse_from_str` with `%z` or `%:z` correctly constructs the `DateTime<FixedOffset>` instance.

4. **Duration Arithmetic**:
   - Adding a `Duration` to `DateTime<FixedOffset>` increments both the local wall clock time and the Unix epoch timestamp by the duration amount while preserving the timezone offset unchanged.
csx.json
{"case":{"caseId":"case:sha256:091f9e8618ca16f92b0bcf225c74f91954d6611ce5b7df7c7d2191621a69d0c1","contract":["FixedOffset restricts timezone offsets strictly within -86400 to +86400 seconds, preserves Unix epoch timestamps invariant across with_timezone conversions, and formats offsets using %z, %:z, %::z, and %:::z.","assert FixedOffset::east_opt and west_opt return None for 86400 seconds and Some for 86399 seconds","assert negative east offset is equivalent to positive west offset and local_minus_utc returns the signed seconds","assert converting DateTime\u003cFixedOffset\u003e across timezones preserves the identical epoch timestamp while adjusting local wall-clock components","assert strftime format specifiers produce %z (+HHMM), %:z (+HH:MM), %::z (+HH:MM:SS), and %:::z (+HH)","assert duration addition on DateTime\u003cFixedOffset\u003e advances timestamp and local time while leaving the offset unchanged"],"goal":"Construct bounded timezone offsets with FixedOffset, preserve Unix timestamps across timezone conversions, and format offsets with strftime colons specifiers","kind":"HOW","packages":["pkg:cargo/chrono@0.4.39"],"schemaVersion":1,"symbols":["chrono::FixedOffset::east_opt","chrono::FixedOffset::west_opt","chrono::DateTime::with_timezone","chrono::DateTime::format"]},"contractCommand":["cargo","test","--offline"],"environment":{"arch":"x64","ecosystem":"cargo","executionContext":"rust","language":"rust","os":"linux","packageManager":"cargo","runtime":"rust","schemaVersion":1},"license":"MIT-0","packages":["pkg:cargo/chrono@0.4.39"],"schemaVersion":1,"symbols":["chrono::FixedOffset::east_opt","chrono::FixedOffset::west_opt","chrono::DateTime::with_timezone","chrono::DateTime::format"],"verifierAdapter":"cargo@1"}
src/lib.rs
use chrono::{DateTime, Duration, FixedOffset};

/// Constructs a FixedOffset from east offset in seconds, returning None if out of range (-86400..86400).
pub fn make_east_offset(seconds: i32) -> Option<FixedOffset> {
    FixedOffset::east_opt(seconds)
}

/// Constructs a FixedOffset from west offset in seconds, returning None if out of range (-86400..86400).
pub fn make_west_offset(seconds: i32) -> Option<FixedOffset> {
    FixedOffset::west_opt(seconds)
}

/// Converts a DateTime<FixedOffset> to another timezone offset, preserving the UTC instant.
pub fn convert_offset(
    dt: DateTime<FixedOffset>,
    target: FixedOffset,
) -> DateTime<FixedOffset> {
    dt.with_timezone(&target)
}

/// Formats a DateTime<FixedOffset> with custom format specifiers.
pub fn format_datetime(dt: &DateTime<FixedOffset>, fmt: &str) -> String {
    dt.format(fmt).to_string()
}

/// Adds a duration to DateTime<FixedOffset>, shifting the UTC instant and wall clock while keeping offset unchanged.
pub fn add_duration(dt: DateTime<FixedOffset>, duration: Duration) -> DateTime<FixedOffset> {
    dt + duration
}

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

    #[test]
    fn test_fixed_offset_bounds_and_symmetry() {
        // Offsets must be strictly within -86_400..86_400 seconds (exclusive of 24h boundary)
        assert!(make_east_offset(86_400).is_none());
        assert!(make_east_offset(-86_400).is_none());
        assert!(make_west_offset(86_400).is_none());
        assert!(make_west_offset(-86_400).is_none());

        assert!(make_east_offset(86_399).is_some());
        assert!(make_west_offset(86_399).is_some());

        // Negative east offset is identical to positive west offset
        let east_neg = make_east_offset(-18000).unwrap();
        let west_pos = make_west_offset(18000).unwrap();
        assert_eq!(east_neg, west_pos);
        assert_eq!(east_neg.local_minus_utc(), -18000);
    }

    #[test]
    fn test_timezone_conversion_preserves_timestamp() {
        let dt = DateTime::parse_from_rfc3339("2024-07-15T08:30:00-05:00")
            .expect("valid RFC 3339 string");
        assert_eq!(dt.timestamp(), 1721050200);

        let ist = make_east_offset(5 * 3600 + 1800).unwrap();
        let dt_ist = convert_offset(dt, ist);

        // Instant (epoch timestamp) is invariant under timezone conversion
        assert_eq!(dt_ist.timestamp(), dt.timestamp());
        assert_eq!(
            format_datetime(&dt_ist, "%Y-%m-%dT%H:%M:%S%:z"),
            "2024-07-15T19:00:00+05:30"
        );

        // Converting to Utc also preserves epoch timestamp
        let dt_utc = dt.with_timezone(&Utc);
        assert_eq!(dt_utc.timestamp(), dt.timestamp());
        assert_eq!(dt_utc.to_rfc3339(), "2024-07-15T13:30:00+00:00");
    }

    #[test]
    fn test_strftime_offset_specifiers_and_parsing() {
        let ist = make_east_offset(5 * 3600 + 1800).unwrap();
        let naive = chrono::NaiveDate::from_ymd_opt(2024, 7, 15)
            .unwrap()
            .and_hms_opt(19, 0, 0)
            .unwrap();
        let dt = DateTime::<FixedOffset>::from_naive_utc_and_offset(
            naive - Duration::seconds(5 * 3600 + 1800),
            ist,
        );

        // Offset format specifiers: %z (+HHMM), %:z (+HH:MM), %::z (+HH:MM:SS), %:::z (+HH)
        assert_eq!(format_datetime(&dt, "%z"), "+0530");
        assert_eq!(format_datetime(&dt, "%:z"), "+05:30");
        assert_eq!(format_datetime(&dt, "%::z"), "+05:30:00");
        assert_eq!(format_datetime(&dt, "%:::z"), "+05");

        // Parsing from strings with compact (%z) and colon-separated (%:z) timezone offsets
        let p1 = DateTime::parse_from_str("2024-07-15 19:00:00 +0530", "%Y-%m-%d %H:%M:%S %z")
            .expect("parse with %z");
        let p2 = DateTime::parse_from_str("2024-07-15 19:00:00 +05:30", "%Y-%m-%d %H:%M:%S %:z")
            .expect("parse with %:z");

        assert_eq!(p1, dt);
        assert_eq!(p2, dt);
    }

    #[test]
    fn test_duration_arithmetic_on_offset_datetime() {
        let dt = DateTime::parse_from_rfc3339("2024-07-15T08:30:00-05:00")
            .expect("valid RFC 3339 string");
        let advanced = add_duration(dt, Duration::hours(3));

        // Offset is preserved, timestamp and local wall clock advance together
        assert_eq!(advanced.offset(), dt.offset());
        assert_eq!(advanced.timestamp(), dt.timestamp() + 3 * 3600);
        assert_eq!(
            format_datetime(&advanced, "%Y-%m-%dT%H:%M:%S%:z"),
            "2024-07-15T11:30:00-05:00"
        );
    }
}

Исходный сидер

csx-seed