Skip to main content

local_offsets/
local_offsets.rs

1// Copyright 2026 The android-chrono-tz Authors.
2// This project is dual-licensed under Apache 2.0 and MIT terms.
3// See LICENSE-APACHE and LICENSE-MIT for details.
4
5//! Example to print offsets of the local timezone from UTC at various times, from both UTC and the
6//! local timezone.
7//!
8//! To run on an attached Android device:
9//!
10//! ```sh
11//! CARGO_NDK_PLATFORM=35 ANDROID_NDK_HOME=/usr/lib/android-ndk cargo ndk run --target aarch64-linux-android --example local_offsets
12//! ```
13
14use android_chrono_tz::Local;
15use chrono::{NaiveDate, TimeZone, Utc};
16
17fn main() {
18    println!("Local time: {}", Local::now());
19
20    let winter = Utc.with_ymd_and_hms(2026, 3, 1, 12, 0, 0).unwrap();
21    println!(
22        "Offset at {winter}: {}",
23        Local.offset_from_utc_datetime(&winter.naive_utc())
24    );
25
26    let summer = Utc.with_ymd_and_hms(2026, 4, 1, 12, 0, 0).unwrap();
27    println!(
28        "Offset at {summer}: {}",
29        Local.offset_from_utc_datetime(&summer.naive_utc())
30    );
31
32    let winter_local = Local.with_ymd_and_hms(2026, 3, 1, 12, 0, 0).unwrap();
33    println!(
34        "Offset at {winter_local}: {:?}",
35        Local.offset_from_local_datetime(&winter_local.naive_local())
36    );
37
38    let summer_local = Local.with_ymd_and_hms(2026, 4, 1, 12, 0, 0).unwrap();
39    println!(
40        "Offset at {summer_local}: {:?}",
41        Local.offset_from_local_datetime(&summer_local.naive_local())
42    );
43
44    // Start of summer time in the UK and western Europe. This should be `None`.
45    let start_local = NaiveDate::from_ymd_opt(2026, 3, 29)
46        .unwrap()
47        .and_hms_opt(1, 30, 0)
48        .unwrap();
49    println!(
50        "At start of DST {start_local}: {:?}",
51        Local.offset_from_local_datetime(&start_local)
52    );
53
54    // End of summer time in the UK and western Europe. This should be `Ambiguous`.
55    let end_local = NaiveDate::from_ymd_opt(2026, 10, 25)
56        .unwrap()
57        .and_hms_opt(1, 30, 0)
58        .unwrap();
59    println!(
60        "At end of DST {end_local}: {:?}",
61        Local.offset_from_local_datetime(&end_local)
62    );
63}