pub struct TimeZone { /* private fields */ }
Available on crate feature
alloc
only.Expand description
Time zone
Implementations§
Source§impl TimeZone
impl TimeZone
Sourcepub fn new(
transitions: Vec<Transition>,
local_time_types: Vec<LocalTimeType>,
leap_seconds: Vec<LeapSecond>,
extra_rule: Option<TransitionRule>,
) -> Result<Self, TzError>
pub fn new( transitions: Vec<Transition>, local_time_types: Vec<LocalTimeType>, leap_seconds: Vec<LeapSecond>, extra_rule: Option<TransitionRule>, ) -> Result<Self, TzError>
Construct a time zone
Sourcepub fn as_ref(&self) -> TimeZoneRef<'_>
pub fn as_ref(&self) -> TimeZoneRef<'_>
Returns a reference to the time zone
Examples found in repository?
examples/time.rs (line 58)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::{DateTime, LocalTimeType, TimeZone, UtcDateTime};
5
6 //
7 // TimeZone
8 //
9
10 // 2000-01-01T00:00:00Z
11 let unix_time = 946684800;
12
13 // Get UTC time zone
14 let time_zone_utc = TimeZone::utc();
15 println!("{:?}", time_zone_utc.find_local_time_type(unix_time)?);
16
17 // Get fixed time zone at GMT-1
18 let time_zone_fixed = TimeZone::fixed(-3600)?;
19 println!("{:?}", time_zone_fixed.find_local_time_type(unix_time)?.ut_offset());
20
21 // Get local time zone (UNIX only)
22 let time_zone_local = TimeZone::local()?;
23 println!("{:?}", time_zone_local.find_local_time_type(unix_time)?.ut_offset());
24
25 // Get the current local time type
26 println!("{:?}", time_zone_local.find_current_local_time_type()?);
27
28 // Get time zone from a TZ string:
29 // From an absolute file
30 let _ = TimeZone::from_posix_tz("/usr/share/zoneinfo/Pacific/Auckland");
31 // From a file relative to the system timezone directory
32 let _ = TimeZone::from_posix_tz("Pacific/Auckland");
33 // From a time zone description
34 TimeZone::from_posix_tz("HST10")?;
35 TimeZone::from_posix_tz("<-03>3")?;
36 TimeZone::from_posix_tz("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0")?;
37 // Use a leading colon to force searching for a corresponding file
38 let _ = TimeZone::from_posix_tz(":UTC");
39
40 //
41 // DateTime
42 //
43
44 // Get the current UTC date time
45 println!("{:?}", UtcDateTime::now()?);
46
47 // Create a new UTC date time (2000-01-01T00:00:00.123456789Z)
48 let utc_date_time = UtcDateTime::new(2000, 1, 1, 0, 0, 0, 123_456_789)?;
49 println!("{utc_date_time}");
50 println!("{utc_date_time:?}");
51
52 // Create a new UTC date time from a Unix time with nanoseconds (2000-01-01T00:00:00.123456789Z)
53 let other_utc_date_time = UtcDateTime::from_timespec(946684800, 123_456_789)?;
54 println!("{other_utc_date_time}");
55 println!("{other_utc_date_time:?}");
56
57 // Project the UTC date time to a time zone
58 let date_time = utc_date_time.project(TimeZone::fixed(-3600)?.as_ref())?;
59 println!("{date_time}");
60 println!("{date_time:#?}");
61
62 // Project the date time to another time zone
63 let other_date_time = date_time.project(TimeZone::fixed(3600)?.as_ref())?;
64 println!("{other_date_time}");
65 println!("{other_date_time:#?}");
66
67 // Create a new date time from a Unix time with nanoseconds and a time zone (2000-01-01T00:00:00.123456789Z)
68 let another_date_time = DateTime::from_timespec(946684800, 123_456_789, TimeZone::fixed(86400)?.as_ref())?;
69 println!("{another_date_time}");
70 println!("{another_date_time:#?}");
71
72 // Get the corresponding UTC Unix times with nanoseconds
73 println!("{:?}", (utc_date_time.unix_time(), utc_date_time.nanoseconds()));
74 println!("{:?}", (other_utc_date_time.unix_time(), other_utc_date_time.nanoseconds()));
75 println!("{:?}", (date_time.unix_time(), date_time.nanoseconds()));
76 println!("{:?}", (other_date_time.unix_time(), other_date_time.nanoseconds()));
77
78 // Nanoseconds are always added towards the future
79 let neg_utc_date_time = UtcDateTime::from_timespec(-1, 123_456_789)?;
80 println!("{neg_utc_date_time}");
81 println!("{}", neg_utc_date_time.total_nanoseconds());
82
83 // Get the current date time at the local time zone (UNIX only)
84 let time_zone_local = TimeZone::local()?;
85 println!("{:#?}", DateTime::now(time_zone_local.as_ref())?);
86
87 // Create a new date time with an UTC offset (2000-01-01T01:00:00.123456789+01:00)
88 println!("{:#?}", DateTime::new(2000, 1, 1, 1, 0, 0, 123_456_789, LocalTimeType::with_ut_offset(3600)?)?);
89
90 //
91 // Find the possible date times corresponding to a date, a time and a time zone
92 //
93 let time_zone = TimeZone::from_posix_tz("CET-1CEST,M3.5.0,M10.5.0/3")?;
94
95 // Found date time is unique
96 let found_date_times = DateTime::find(2000, 1, 1, 0, 0, 0, 0, time_zone.as_ref())?;
97 println!("{found_date_times:#?}");
98 println!("{:#?}", found_date_times.unique());
99 println!("{:#?}", found_date_times.earliest());
100 println!("{:#?}", found_date_times.latest());
101
102 // Found date time was skipped by a forward transition
103 let found_date_times = DateTime::find(2000, 3, 26, 2, 30, 0, 0, time_zone.as_ref())?;
104 println!("{found_date_times:#?}");
105 println!("{:#?}", found_date_times.unique());
106 println!("{:#?}", found_date_times.earliest());
107 println!("{:#?}", found_date_times.latest());
108
109 // Found date time is ambiguous because of a backward transition
110 let found_date_times = DateTime::find(2000, 10, 29, 2, 30, 0, 0, time_zone.as_ref())?;
111 println!("{found_date_times:#?}");
112 println!("{:#?}", found_date_times.unique());
113 println!("{:#?}", found_date_times.earliest());
114 println!("{:#?}", found_date_times.latest());
115 }
116
117 Ok(())
118}
Sourcepub fn utc() -> Self
pub fn utc() -> Self
Construct the time zone associated to UTC
Examples found in repository?
examples/time.rs (line 14)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::{DateTime, LocalTimeType, TimeZone, UtcDateTime};
5
6 //
7 // TimeZone
8 //
9
10 // 2000-01-01T00:00:00Z
11 let unix_time = 946684800;
12
13 // Get UTC time zone
14 let time_zone_utc = TimeZone::utc();
15 println!("{:?}", time_zone_utc.find_local_time_type(unix_time)?);
16
17 // Get fixed time zone at GMT-1
18 let time_zone_fixed = TimeZone::fixed(-3600)?;
19 println!("{:?}", time_zone_fixed.find_local_time_type(unix_time)?.ut_offset());
20
21 // Get local time zone (UNIX only)
22 let time_zone_local = TimeZone::local()?;
23 println!("{:?}", time_zone_local.find_local_time_type(unix_time)?.ut_offset());
24
25 // Get the current local time type
26 println!("{:?}", time_zone_local.find_current_local_time_type()?);
27
28 // Get time zone from a TZ string:
29 // From an absolute file
30 let _ = TimeZone::from_posix_tz("/usr/share/zoneinfo/Pacific/Auckland");
31 // From a file relative to the system timezone directory
32 let _ = TimeZone::from_posix_tz("Pacific/Auckland");
33 // From a time zone description
34 TimeZone::from_posix_tz("HST10")?;
35 TimeZone::from_posix_tz("<-03>3")?;
36 TimeZone::from_posix_tz("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0")?;
37 // Use a leading colon to force searching for a corresponding file
38 let _ = TimeZone::from_posix_tz(":UTC");
39
40 //
41 // DateTime
42 //
43
44 // Get the current UTC date time
45 println!("{:?}", UtcDateTime::now()?);
46
47 // Create a new UTC date time (2000-01-01T00:00:00.123456789Z)
48 let utc_date_time = UtcDateTime::new(2000, 1, 1, 0, 0, 0, 123_456_789)?;
49 println!("{utc_date_time}");
50 println!("{utc_date_time:?}");
51
52 // Create a new UTC date time from a Unix time with nanoseconds (2000-01-01T00:00:00.123456789Z)
53 let other_utc_date_time = UtcDateTime::from_timespec(946684800, 123_456_789)?;
54 println!("{other_utc_date_time}");
55 println!("{other_utc_date_time:?}");
56
57 // Project the UTC date time to a time zone
58 let date_time = utc_date_time.project(TimeZone::fixed(-3600)?.as_ref())?;
59 println!("{date_time}");
60 println!("{date_time:#?}");
61
62 // Project the date time to another time zone
63 let other_date_time = date_time.project(TimeZone::fixed(3600)?.as_ref())?;
64 println!("{other_date_time}");
65 println!("{other_date_time:#?}");
66
67 // Create a new date time from a Unix time with nanoseconds and a time zone (2000-01-01T00:00:00.123456789Z)
68 let another_date_time = DateTime::from_timespec(946684800, 123_456_789, TimeZone::fixed(86400)?.as_ref())?;
69 println!("{another_date_time}");
70 println!("{another_date_time:#?}");
71
72 // Get the corresponding UTC Unix times with nanoseconds
73 println!("{:?}", (utc_date_time.unix_time(), utc_date_time.nanoseconds()));
74 println!("{:?}", (other_utc_date_time.unix_time(), other_utc_date_time.nanoseconds()));
75 println!("{:?}", (date_time.unix_time(), date_time.nanoseconds()));
76 println!("{:?}", (other_date_time.unix_time(), other_date_time.nanoseconds()));
77
78 // Nanoseconds are always added towards the future
79 let neg_utc_date_time = UtcDateTime::from_timespec(-1, 123_456_789)?;
80 println!("{neg_utc_date_time}");
81 println!("{}", neg_utc_date_time.total_nanoseconds());
82
83 // Get the current date time at the local time zone (UNIX only)
84 let time_zone_local = TimeZone::local()?;
85 println!("{:#?}", DateTime::now(time_zone_local.as_ref())?);
86
87 // Create a new date time with an UTC offset (2000-01-01T01:00:00.123456789+01:00)
88 println!("{:#?}", DateTime::new(2000, 1, 1, 1, 0, 0, 123_456_789, LocalTimeType::with_ut_offset(3600)?)?);
89
90 //
91 // Find the possible date times corresponding to a date, a time and a time zone
92 //
93 let time_zone = TimeZone::from_posix_tz("CET-1CEST,M3.5.0,M10.5.0/3")?;
94
95 // Found date time is unique
96 let found_date_times = DateTime::find(2000, 1, 1, 0, 0, 0, 0, time_zone.as_ref())?;
97 println!("{found_date_times:#?}");
98 println!("{:#?}", found_date_times.unique());
99 println!("{:#?}", found_date_times.earliest());
100 println!("{:#?}", found_date_times.latest());
101
102 // Found date time was skipped by a forward transition
103 let found_date_times = DateTime::find(2000, 3, 26, 2, 30, 0, 0, time_zone.as_ref())?;
104 println!("{found_date_times:#?}");
105 println!("{:#?}", found_date_times.unique());
106 println!("{:#?}", found_date_times.earliest());
107 println!("{:#?}", found_date_times.latest());
108
109 // Found date time is ambiguous because of a backward transition
110 let found_date_times = DateTime::find(2000, 10, 29, 2, 30, 0, 0, time_zone.as_ref())?;
111 println!("{found_date_times:#?}");
112 println!("{:#?}", found_date_times.unique());
113 println!("{:#?}", found_date_times.earliest());
114 println!("{:#?}", found_date_times.latest());
115 }
116
117 Ok(())
118}
Sourcepub fn fixed(ut_offset: i32) -> Result<Self, LocalTimeTypeError>
pub fn fixed(ut_offset: i32) -> Result<Self, LocalTimeTypeError>
Construct a time zone with the specified UTC offset in seconds
Examples found in repository?
examples/time.rs (line 18)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::{DateTime, LocalTimeType, TimeZone, UtcDateTime};
5
6 //
7 // TimeZone
8 //
9
10 // 2000-01-01T00:00:00Z
11 let unix_time = 946684800;
12
13 // Get UTC time zone
14 let time_zone_utc = TimeZone::utc();
15 println!("{:?}", time_zone_utc.find_local_time_type(unix_time)?);
16
17 // Get fixed time zone at GMT-1
18 let time_zone_fixed = TimeZone::fixed(-3600)?;
19 println!("{:?}", time_zone_fixed.find_local_time_type(unix_time)?.ut_offset());
20
21 // Get local time zone (UNIX only)
22 let time_zone_local = TimeZone::local()?;
23 println!("{:?}", time_zone_local.find_local_time_type(unix_time)?.ut_offset());
24
25 // Get the current local time type
26 println!("{:?}", time_zone_local.find_current_local_time_type()?);
27
28 // Get time zone from a TZ string:
29 // From an absolute file
30 let _ = TimeZone::from_posix_tz("/usr/share/zoneinfo/Pacific/Auckland");
31 // From a file relative to the system timezone directory
32 let _ = TimeZone::from_posix_tz("Pacific/Auckland");
33 // From a time zone description
34 TimeZone::from_posix_tz("HST10")?;
35 TimeZone::from_posix_tz("<-03>3")?;
36 TimeZone::from_posix_tz("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0")?;
37 // Use a leading colon to force searching for a corresponding file
38 let _ = TimeZone::from_posix_tz(":UTC");
39
40 //
41 // DateTime
42 //
43
44 // Get the current UTC date time
45 println!("{:?}", UtcDateTime::now()?);
46
47 // Create a new UTC date time (2000-01-01T00:00:00.123456789Z)
48 let utc_date_time = UtcDateTime::new(2000, 1, 1, 0, 0, 0, 123_456_789)?;
49 println!("{utc_date_time}");
50 println!("{utc_date_time:?}");
51
52 // Create a new UTC date time from a Unix time with nanoseconds (2000-01-01T00:00:00.123456789Z)
53 let other_utc_date_time = UtcDateTime::from_timespec(946684800, 123_456_789)?;
54 println!("{other_utc_date_time}");
55 println!("{other_utc_date_time:?}");
56
57 // Project the UTC date time to a time zone
58 let date_time = utc_date_time.project(TimeZone::fixed(-3600)?.as_ref())?;
59 println!("{date_time}");
60 println!("{date_time:#?}");
61
62 // Project the date time to another time zone
63 let other_date_time = date_time.project(TimeZone::fixed(3600)?.as_ref())?;
64 println!("{other_date_time}");
65 println!("{other_date_time:#?}");
66
67 // Create a new date time from a Unix time with nanoseconds and a time zone (2000-01-01T00:00:00.123456789Z)
68 let another_date_time = DateTime::from_timespec(946684800, 123_456_789, TimeZone::fixed(86400)?.as_ref())?;
69 println!("{another_date_time}");
70 println!("{another_date_time:#?}");
71
72 // Get the corresponding UTC Unix times with nanoseconds
73 println!("{:?}", (utc_date_time.unix_time(), utc_date_time.nanoseconds()));
74 println!("{:?}", (other_utc_date_time.unix_time(), other_utc_date_time.nanoseconds()));
75 println!("{:?}", (date_time.unix_time(), date_time.nanoseconds()));
76 println!("{:?}", (other_date_time.unix_time(), other_date_time.nanoseconds()));
77
78 // Nanoseconds are always added towards the future
79 let neg_utc_date_time = UtcDateTime::from_timespec(-1, 123_456_789)?;
80 println!("{neg_utc_date_time}");
81 println!("{}", neg_utc_date_time.total_nanoseconds());
82
83 // Get the current date time at the local time zone (UNIX only)
84 let time_zone_local = TimeZone::local()?;
85 println!("{:#?}", DateTime::now(time_zone_local.as_ref())?);
86
87 // Create a new date time with an UTC offset (2000-01-01T01:00:00.123456789+01:00)
88 println!("{:#?}", DateTime::new(2000, 1, 1, 1, 0, 0, 123_456_789, LocalTimeType::with_ut_offset(3600)?)?);
89
90 //
91 // Find the possible date times corresponding to a date, a time and a time zone
92 //
93 let time_zone = TimeZone::from_posix_tz("CET-1CEST,M3.5.0,M10.5.0/3")?;
94
95 // Found date time is unique
96 let found_date_times = DateTime::find(2000, 1, 1, 0, 0, 0, 0, time_zone.as_ref())?;
97 println!("{found_date_times:#?}");
98 println!("{:#?}", found_date_times.unique());
99 println!("{:#?}", found_date_times.earliest());
100 println!("{:#?}", found_date_times.latest());
101
102 // Found date time was skipped by a forward transition
103 let found_date_times = DateTime::find(2000, 3, 26, 2, 30, 0, 0, time_zone.as_ref())?;
104 println!("{found_date_times:#?}");
105 println!("{:#?}", found_date_times.unique());
106 println!("{:#?}", found_date_times.earliest());
107 println!("{:#?}", found_date_times.latest());
108
109 // Found date time is ambiguous because of a backward transition
110 let found_date_times = DateTime::find(2000, 10, 29, 2, 30, 0, 0, time_zone.as_ref())?;
111 println!("{found_date_times:#?}");
112 println!("{:#?}", found_date_times.unique());
113 println!("{:#?}", found_date_times.earliest());
114 println!("{:#?}", found_date_times.latest());
115 }
116
117 Ok(())
118}
Sourcepub fn find_local_time_type(
&self,
unix_time: i64,
) -> Result<&LocalTimeType, TzError>
pub fn find_local_time_type( &self, unix_time: i64, ) -> Result<&LocalTimeType, TzError>
Find the local time type associated to the time zone at the specified Unix time in seconds
Examples found in repository?
examples/time.rs (line 15)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::{DateTime, LocalTimeType, TimeZone, UtcDateTime};
5
6 //
7 // TimeZone
8 //
9
10 // 2000-01-01T00:00:00Z
11 let unix_time = 946684800;
12
13 // Get UTC time zone
14 let time_zone_utc = TimeZone::utc();
15 println!("{:?}", time_zone_utc.find_local_time_type(unix_time)?);
16
17 // Get fixed time zone at GMT-1
18 let time_zone_fixed = TimeZone::fixed(-3600)?;
19 println!("{:?}", time_zone_fixed.find_local_time_type(unix_time)?.ut_offset());
20
21 // Get local time zone (UNIX only)
22 let time_zone_local = TimeZone::local()?;
23 println!("{:?}", time_zone_local.find_local_time_type(unix_time)?.ut_offset());
24
25 // Get the current local time type
26 println!("{:?}", time_zone_local.find_current_local_time_type()?);
27
28 // Get time zone from a TZ string:
29 // From an absolute file
30 let _ = TimeZone::from_posix_tz("/usr/share/zoneinfo/Pacific/Auckland");
31 // From a file relative to the system timezone directory
32 let _ = TimeZone::from_posix_tz("Pacific/Auckland");
33 // From a time zone description
34 TimeZone::from_posix_tz("HST10")?;
35 TimeZone::from_posix_tz("<-03>3")?;
36 TimeZone::from_posix_tz("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0")?;
37 // Use a leading colon to force searching for a corresponding file
38 let _ = TimeZone::from_posix_tz(":UTC");
39
40 //
41 // DateTime
42 //
43
44 // Get the current UTC date time
45 println!("{:?}", UtcDateTime::now()?);
46
47 // Create a new UTC date time (2000-01-01T00:00:00.123456789Z)
48 let utc_date_time = UtcDateTime::new(2000, 1, 1, 0, 0, 0, 123_456_789)?;
49 println!("{utc_date_time}");
50 println!("{utc_date_time:?}");
51
52 // Create a new UTC date time from a Unix time with nanoseconds (2000-01-01T00:00:00.123456789Z)
53 let other_utc_date_time = UtcDateTime::from_timespec(946684800, 123_456_789)?;
54 println!("{other_utc_date_time}");
55 println!("{other_utc_date_time:?}");
56
57 // Project the UTC date time to a time zone
58 let date_time = utc_date_time.project(TimeZone::fixed(-3600)?.as_ref())?;
59 println!("{date_time}");
60 println!("{date_time:#?}");
61
62 // Project the date time to another time zone
63 let other_date_time = date_time.project(TimeZone::fixed(3600)?.as_ref())?;
64 println!("{other_date_time}");
65 println!("{other_date_time:#?}");
66
67 // Create a new date time from a Unix time with nanoseconds and a time zone (2000-01-01T00:00:00.123456789Z)
68 let another_date_time = DateTime::from_timespec(946684800, 123_456_789, TimeZone::fixed(86400)?.as_ref())?;
69 println!("{another_date_time}");
70 println!("{another_date_time:#?}");
71
72 // Get the corresponding UTC Unix times with nanoseconds
73 println!("{:?}", (utc_date_time.unix_time(), utc_date_time.nanoseconds()));
74 println!("{:?}", (other_utc_date_time.unix_time(), other_utc_date_time.nanoseconds()));
75 println!("{:?}", (date_time.unix_time(), date_time.nanoseconds()));
76 println!("{:?}", (other_date_time.unix_time(), other_date_time.nanoseconds()));
77
78 // Nanoseconds are always added towards the future
79 let neg_utc_date_time = UtcDateTime::from_timespec(-1, 123_456_789)?;
80 println!("{neg_utc_date_time}");
81 println!("{}", neg_utc_date_time.total_nanoseconds());
82
83 // Get the current date time at the local time zone (UNIX only)
84 let time_zone_local = TimeZone::local()?;
85 println!("{:#?}", DateTime::now(time_zone_local.as_ref())?);
86
87 // Create a new date time with an UTC offset (2000-01-01T01:00:00.123456789+01:00)
88 println!("{:#?}", DateTime::new(2000, 1, 1, 1, 0, 0, 123_456_789, LocalTimeType::with_ut_offset(3600)?)?);
89
90 //
91 // Find the possible date times corresponding to a date, a time and a time zone
92 //
93 let time_zone = TimeZone::from_posix_tz("CET-1CEST,M3.5.0,M10.5.0/3")?;
94
95 // Found date time is unique
96 let found_date_times = DateTime::find(2000, 1, 1, 0, 0, 0, 0, time_zone.as_ref())?;
97 println!("{found_date_times:#?}");
98 println!("{:#?}", found_date_times.unique());
99 println!("{:#?}", found_date_times.earliest());
100 println!("{:#?}", found_date_times.latest());
101
102 // Found date time was skipped by a forward transition
103 let found_date_times = DateTime::find(2000, 3, 26, 2, 30, 0, 0, time_zone.as_ref())?;
104 println!("{found_date_times:#?}");
105 println!("{:#?}", found_date_times.unique());
106 println!("{:#?}", found_date_times.earliest());
107 println!("{:#?}", found_date_times.latest());
108
109 // Found date time is ambiguous because of a backward transition
110 let found_date_times = DateTime::find(2000, 10, 29, 2, 30, 0, 0, time_zone.as_ref())?;
111 println!("{found_date_times:#?}");
112 println!("{:#?}", found_date_times.unique());
113 println!("{:#?}", found_date_times.earliest());
114 println!("{:#?}", found_date_times.latest());
115 }
116
117 Ok(())
118}
Sourcepub fn from_tz_data(bytes: &[u8]) -> Result<Self, TzError>
pub fn from_tz_data(bytes: &[u8]) -> Result<Self, TzError>
Construct a time zone from the contents of a time zone file
Sourcepub fn local() -> Result<Self, Error>
Available on crate feature std
only.
pub fn local() -> Result<Self, Error>
std
only.Returns local time zone.
This method in not supported on non-UNIX platforms, and returns the UTC time zone instead.
Examples found in repository?
examples/time.rs (line 22)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::{DateTime, LocalTimeType, TimeZone, UtcDateTime};
5
6 //
7 // TimeZone
8 //
9
10 // 2000-01-01T00:00:00Z
11 let unix_time = 946684800;
12
13 // Get UTC time zone
14 let time_zone_utc = TimeZone::utc();
15 println!("{:?}", time_zone_utc.find_local_time_type(unix_time)?);
16
17 // Get fixed time zone at GMT-1
18 let time_zone_fixed = TimeZone::fixed(-3600)?;
19 println!("{:?}", time_zone_fixed.find_local_time_type(unix_time)?.ut_offset());
20
21 // Get local time zone (UNIX only)
22 let time_zone_local = TimeZone::local()?;
23 println!("{:?}", time_zone_local.find_local_time_type(unix_time)?.ut_offset());
24
25 // Get the current local time type
26 println!("{:?}", time_zone_local.find_current_local_time_type()?);
27
28 // Get time zone from a TZ string:
29 // From an absolute file
30 let _ = TimeZone::from_posix_tz("/usr/share/zoneinfo/Pacific/Auckland");
31 // From a file relative to the system timezone directory
32 let _ = TimeZone::from_posix_tz("Pacific/Auckland");
33 // From a time zone description
34 TimeZone::from_posix_tz("HST10")?;
35 TimeZone::from_posix_tz("<-03>3")?;
36 TimeZone::from_posix_tz("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0")?;
37 // Use a leading colon to force searching for a corresponding file
38 let _ = TimeZone::from_posix_tz(":UTC");
39
40 //
41 // DateTime
42 //
43
44 // Get the current UTC date time
45 println!("{:?}", UtcDateTime::now()?);
46
47 // Create a new UTC date time (2000-01-01T00:00:00.123456789Z)
48 let utc_date_time = UtcDateTime::new(2000, 1, 1, 0, 0, 0, 123_456_789)?;
49 println!("{utc_date_time}");
50 println!("{utc_date_time:?}");
51
52 // Create a new UTC date time from a Unix time with nanoseconds (2000-01-01T00:00:00.123456789Z)
53 let other_utc_date_time = UtcDateTime::from_timespec(946684800, 123_456_789)?;
54 println!("{other_utc_date_time}");
55 println!("{other_utc_date_time:?}");
56
57 // Project the UTC date time to a time zone
58 let date_time = utc_date_time.project(TimeZone::fixed(-3600)?.as_ref())?;
59 println!("{date_time}");
60 println!("{date_time:#?}");
61
62 // Project the date time to another time zone
63 let other_date_time = date_time.project(TimeZone::fixed(3600)?.as_ref())?;
64 println!("{other_date_time}");
65 println!("{other_date_time:#?}");
66
67 // Create a new date time from a Unix time with nanoseconds and a time zone (2000-01-01T00:00:00.123456789Z)
68 let another_date_time = DateTime::from_timespec(946684800, 123_456_789, TimeZone::fixed(86400)?.as_ref())?;
69 println!("{another_date_time}");
70 println!("{another_date_time:#?}");
71
72 // Get the corresponding UTC Unix times with nanoseconds
73 println!("{:?}", (utc_date_time.unix_time(), utc_date_time.nanoseconds()));
74 println!("{:?}", (other_utc_date_time.unix_time(), other_utc_date_time.nanoseconds()));
75 println!("{:?}", (date_time.unix_time(), date_time.nanoseconds()));
76 println!("{:?}", (other_date_time.unix_time(), other_date_time.nanoseconds()));
77
78 // Nanoseconds are always added towards the future
79 let neg_utc_date_time = UtcDateTime::from_timespec(-1, 123_456_789)?;
80 println!("{neg_utc_date_time}");
81 println!("{}", neg_utc_date_time.total_nanoseconds());
82
83 // Get the current date time at the local time zone (UNIX only)
84 let time_zone_local = TimeZone::local()?;
85 println!("{:#?}", DateTime::now(time_zone_local.as_ref())?);
86
87 // Create a new date time with an UTC offset (2000-01-01T01:00:00.123456789+01:00)
88 println!("{:#?}", DateTime::new(2000, 1, 1, 1, 0, 0, 123_456_789, LocalTimeType::with_ut_offset(3600)?)?);
89
90 //
91 // Find the possible date times corresponding to a date, a time and a time zone
92 //
93 let time_zone = TimeZone::from_posix_tz("CET-1CEST,M3.5.0,M10.5.0/3")?;
94
95 // Found date time is unique
96 let found_date_times = DateTime::find(2000, 1, 1, 0, 0, 0, 0, time_zone.as_ref())?;
97 println!("{found_date_times:#?}");
98 println!("{:#?}", found_date_times.unique());
99 println!("{:#?}", found_date_times.earliest());
100 println!("{:#?}", found_date_times.latest());
101
102 // Found date time was skipped by a forward transition
103 let found_date_times = DateTime::find(2000, 3, 26, 2, 30, 0, 0, time_zone.as_ref())?;
104 println!("{found_date_times:#?}");
105 println!("{:#?}", found_date_times.unique());
106 println!("{:#?}", found_date_times.earliest());
107 println!("{:#?}", found_date_times.latest());
108
109 // Found date time is ambiguous because of a backward transition
110 let found_date_times = DateTime::find(2000, 10, 29, 2, 30, 0, 0, time_zone.as_ref())?;
111 println!("{found_date_times:#?}");
112 println!("{:#?}", found_date_times.unique());
113 println!("{:#?}", found_date_times.earliest());
114 println!("{:#?}", found_date_times.latest());
115 }
116
117 Ok(())
118}
Sourcepub fn from_posix_tz(tz_string: &str) -> Result<Self, Error>
Available on crate feature std
only.
pub fn from_posix_tz(tz_string: &str) -> Result<Self, Error>
std
only.Construct a time zone from a POSIX TZ string, as described in the POSIX documentation of the TZ
environment variable.
Examples found in repository?
examples/time.rs (line 30)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::{DateTime, LocalTimeType, TimeZone, UtcDateTime};
5
6 //
7 // TimeZone
8 //
9
10 // 2000-01-01T00:00:00Z
11 let unix_time = 946684800;
12
13 // Get UTC time zone
14 let time_zone_utc = TimeZone::utc();
15 println!("{:?}", time_zone_utc.find_local_time_type(unix_time)?);
16
17 // Get fixed time zone at GMT-1
18 let time_zone_fixed = TimeZone::fixed(-3600)?;
19 println!("{:?}", time_zone_fixed.find_local_time_type(unix_time)?.ut_offset());
20
21 // Get local time zone (UNIX only)
22 let time_zone_local = TimeZone::local()?;
23 println!("{:?}", time_zone_local.find_local_time_type(unix_time)?.ut_offset());
24
25 // Get the current local time type
26 println!("{:?}", time_zone_local.find_current_local_time_type()?);
27
28 // Get time zone from a TZ string:
29 // From an absolute file
30 let _ = TimeZone::from_posix_tz("/usr/share/zoneinfo/Pacific/Auckland");
31 // From a file relative to the system timezone directory
32 let _ = TimeZone::from_posix_tz("Pacific/Auckland");
33 // From a time zone description
34 TimeZone::from_posix_tz("HST10")?;
35 TimeZone::from_posix_tz("<-03>3")?;
36 TimeZone::from_posix_tz("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0")?;
37 // Use a leading colon to force searching for a corresponding file
38 let _ = TimeZone::from_posix_tz(":UTC");
39
40 //
41 // DateTime
42 //
43
44 // Get the current UTC date time
45 println!("{:?}", UtcDateTime::now()?);
46
47 // Create a new UTC date time (2000-01-01T00:00:00.123456789Z)
48 let utc_date_time = UtcDateTime::new(2000, 1, 1, 0, 0, 0, 123_456_789)?;
49 println!("{utc_date_time}");
50 println!("{utc_date_time:?}");
51
52 // Create a new UTC date time from a Unix time with nanoseconds (2000-01-01T00:00:00.123456789Z)
53 let other_utc_date_time = UtcDateTime::from_timespec(946684800, 123_456_789)?;
54 println!("{other_utc_date_time}");
55 println!("{other_utc_date_time:?}");
56
57 // Project the UTC date time to a time zone
58 let date_time = utc_date_time.project(TimeZone::fixed(-3600)?.as_ref())?;
59 println!("{date_time}");
60 println!("{date_time:#?}");
61
62 // Project the date time to another time zone
63 let other_date_time = date_time.project(TimeZone::fixed(3600)?.as_ref())?;
64 println!("{other_date_time}");
65 println!("{other_date_time:#?}");
66
67 // Create a new date time from a Unix time with nanoseconds and a time zone (2000-01-01T00:00:00.123456789Z)
68 let another_date_time = DateTime::from_timespec(946684800, 123_456_789, TimeZone::fixed(86400)?.as_ref())?;
69 println!("{another_date_time}");
70 println!("{another_date_time:#?}");
71
72 // Get the corresponding UTC Unix times with nanoseconds
73 println!("{:?}", (utc_date_time.unix_time(), utc_date_time.nanoseconds()));
74 println!("{:?}", (other_utc_date_time.unix_time(), other_utc_date_time.nanoseconds()));
75 println!("{:?}", (date_time.unix_time(), date_time.nanoseconds()));
76 println!("{:?}", (other_date_time.unix_time(), other_date_time.nanoseconds()));
77
78 // Nanoseconds are always added towards the future
79 let neg_utc_date_time = UtcDateTime::from_timespec(-1, 123_456_789)?;
80 println!("{neg_utc_date_time}");
81 println!("{}", neg_utc_date_time.total_nanoseconds());
82
83 // Get the current date time at the local time zone (UNIX only)
84 let time_zone_local = TimeZone::local()?;
85 println!("{:#?}", DateTime::now(time_zone_local.as_ref())?);
86
87 // Create a new date time with an UTC offset (2000-01-01T01:00:00.123456789+01:00)
88 println!("{:#?}", DateTime::new(2000, 1, 1, 1, 0, 0, 123_456_789, LocalTimeType::with_ut_offset(3600)?)?);
89
90 //
91 // Find the possible date times corresponding to a date, a time and a time zone
92 //
93 let time_zone = TimeZone::from_posix_tz("CET-1CEST,M3.5.0,M10.5.0/3")?;
94
95 // Found date time is unique
96 let found_date_times = DateTime::find(2000, 1, 1, 0, 0, 0, 0, time_zone.as_ref())?;
97 println!("{found_date_times:#?}");
98 println!("{:#?}", found_date_times.unique());
99 println!("{:#?}", found_date_times.earliest());
100 println!("{:#?}", found_date_times.latest());
101
102 // Found date time was skipped by a forward transition
103 let found_date_times = DateTime::find(2000, 3, 26, 2, 30, 0, 0, time_zone.as_ref())?;
104 println!("{found_date_times:#?}");
105 println!("{:#?}", found_date_times.unique());
106 println!("{:#?}", found_date_times.earliest());
107 println!("{:#?}", found_date_times.latest());
108
109 // Found date time is ambiguous because of a backward transition
110 let found_date_times = DateTime::find(2000, 10, 29, 2, 30, 0, 0, time_zone.as_ref())?;
111 println!("{found_date_times:#?}");
112 println!("{:#?}", found_date_times.unique());
113 println!("{:#?}", found_date_times.earliest());
114 println!("{:#?}", found_date_times.latest());
115 }
116
117 Ok(())
118}
Sourcepub fn find_current_local_time_type(&self) -> Result<&LocalTimeType, TzError>
Available on crate feature std
only.
pub fn find_current_local_time_type(&self) -> Result<&LocalTimeType, TzError>
std
only.Find the current local time type associated to the time zone
Examples found in repository?
examples/settings.rs (line 10)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::TimeZoneSettings;
5
6 const TIME_ZONE_SETTINGS: TimeZoneSettings<'static> =
7 TimeZoneSettings::new(&["/usr/share/zoneinfo", "/share/zoneinfo", "/etc/zoneinfo"], |path| Ok(std::fs::read(path)?));
8
9 let time_zone_local = TIME_ZONE_SETTINGS.parse_local()?;
10 println!("{:?}", time_zone_local.find_current_local_time_type()?);
11 }
12
13 Ok(())
14}
More examples
examples/time.rs (line 26)
1fn main() -> Result<(), tz::Error> {
2 #[cfg(feature = "std")]
3 {
4 use tz::{DateTime, LocalTimeType, TimeZone, UtcDateTime};
5
6 //
7 // TimeZone
8 //
9
10 // 2000-01-01T00:00:00Z
11 let unix_time = 946684800;
12
13 // Get UTC time zone
14 let time_zone_utc = TimeZone::utc();
15 println!("{:?}", time_zone_utc.find_local_time_type(unix_time)?);
16
17 // Get fixed time zone at GMT-1
18 let time_zone_fixed = TimeZone::fixed(-3600)?;
19 println!("{:?}", time_zone_fixed.find_local_time_type(unix_time)?.ut_offset());
20
21 // Get local time zone (UNIX only)
22 let time_zone_local = TimeZone::local()?;
23 println!("{:?}", time_zone_local.find_local_time_type(unix_time)?.ut_offset());
24
25 // Get the current local time type
26 println!("{:?}", time_zone_local.find_current_local_time_type()?);
27
28 // Get time zone from a TZ string:
29 // From an absolute file
30 let _ = TimeZone::from_posix_tz("/usr/share/zoneinfo/Pacific/Auckland");
31 // From a file relative to the system timezone directory
32 let _ = TimeZone::from_posix_tz("Pacific/Auckland");
33 // From a time zone description
34 TimeZone::from_posix_tz("HST10")?;
35 TimeZone::from_posix_tz("<-03>3")?;
36 TimeZone::from_posix_tz("NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0")?;
37 // Use a leading colon to force searching for a corresponding file
38 let _ = TimeZone::from_posix_tz(":UTC");
39
40 //
41 // DateTime
42 //
43
44 // Get the current UTC date time
45 println!("{:?}", UtcDateTime::now()?);
46
47 // Create a new UTC date time (2000-01-01T00:00:00.123456789Z)
48 let utc_date_time = UtcDateTime::new(2000, 1, 1, 0, 0, 0, 123_456_789)?;
49 println!("{utc_date_time}");
50 println!("{utc_date_time:?}");
51
52 // Create a new UTC date time from a Unix time with nanoseconds (2000-01-01T00:00:00.123456789Z)
53 let other_utc_date_time = UtcDateTime::from_timespec(946684800, 123_456_789)?;
54 println!("{other_utc_date_time}");
55 println!("{other_utc_date_time:?}");
56
57 // Project the UTC date time to a time zone
58 let date_time = utc_date_time.project(TimeZone::fixed(-3600)?.as_ref())?;
59 println!("{date_time}");
60 println!("{date_time:#?}");
61
62 // Project the date time to another time zone
63 let other_date_time = date_time.project(TimeZone::fixed(3600)?.as_ref())?;
64 println!("{other_date_time}");
65 println!("{other_date_time:#?}");
66
67 // Create a new date time from a Unix time with nanoseconds and a time zone (2000-01-01T00:00:00.123456789Z)
68 let another_date_time = DateTime::from_timespec(946684800, 123_456_789, TimeZone::fixed(86400)?.as_ref())?;
69 println!("{another_date_time}");
70 println!("{another_date_time:#?}");
71
72 // Get the corresponding UTC Unix times with nanoseconds
73 println!("{:?}", (utc_date_time.unix_time(), utc_date_time.nanoseconds()));
74 println!("{:?}", (other_utc_date_time.unix_time(), other_utc_date_time.nanoseconds()));
75 println!("{:?}", (date_time.unix_time(), date_time.nanoseconds()));
76 println!("{:?}", (other_date_time.unix_time(), other_date_time.nanoseconds()));
77
78 // Nanoseconds are always added towards the future
79 let neg_utc_date_time = UtcDateTime::from_timespec(-1, 123_456_789)?;
80 println!("{neg_utc_date_time}");
81 println!("{}", neg_utc_date_time.total_nanoseconds());
82
83 // Get the current date time at the local time zone (UNIX only)
84 let time_zone_local = TimeZone::local()?;
85 println!("{:#?}", DateTime::now(time_zone_local.as_ref())?);
86
87 // Create a new date time with an UTC offset (2000-01-01T01:00:00.123456789+01:00)
88 println!("{:#?}", DateTime::new(2000, 1, 1, 1, 0, 0, 123_456_789, LocalTimeType::with_ut_offset(3600)?)?);
89
90 //
91 // Find the possible date times corresponding to a date, a time and a time zone
92 //
93 let time_zone = TimeZone::from_posix_tz("CET-1CEST,M3.5.0,M10.5.0/3")?;
94
95 // Found date time is unique
96 let found_date_times = DateTime::find(2000, 1, 1, 0, 0, 0, 0, time_zone.as_ref())?;
97 println!("{found_date_times:#?}");
98 println!("{:#?}", found_date_times.unique());
99 println!("{:#?}", found_date_times.earliest());
100 println!("{:#?}", found_date_times.latest());
101
102 // Found date time was skipped by a forward transition
103 let found_date_times = DateTime::find(2000, 3, 26, 2, 30, 0, 0, time_zone.as_ref())?;
104 println!("{found_date_times:#?}");
105 println!("{:#?}", found_date_times.unique());
106 println!("{:#?}", found_date_times.earliest());
107 println!("{:#?}", found_date_times.latest());
108
109 // Found date time is ambiguous because of a backward transition
110 let found_date_times = DateTime::find(2000, 10, 29, 2, 30, 0, 0, time_zone.as_ref())?;
111 println!("{found_date_times:#?}");
112 println!("{:#?}", found_date_times.unique());
113 println!("{:#?}", found_date_times.earliest());
114 println!("{:#?}", found_date_times.latest());
115 }
116
117 Ok(())
118}
Trait Implementations§
impl Eq for TimeZone
impl StructuralPartialEq for TimeZone
Auto Trait Implementations§
impl Freeze for TimeZone
impl RefUnwindSafe for TimeZone
impl Send for TimeZone
impl Sync for TimeZone
impl Unpin for TimeZone
impl UnwindSafe for TimeZone
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more