cxx_qt_lib/core/
qtimezone.rs

1// SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
2// SPDX-FileContributor: Andrew Hayzen <andrew.hayzen@kdab.com>
3//
4// SPDX-License-Identifier: MIT OR Apache-2.0
5use std::fmt;
6
7#[cxx::bridge]
8mod ffi {
9    unsafe extern "C++" {
10        include!("cxx-qt-lib/qbytearray.h");
11        type QByteArray = crate::QByteArray;
12        include!("cxx-qt-lib/qdatetime.h");
13        type QDateTime = crate::QDateTime;
14        include!("cxx-qt-lib/qlist.h");
15        type QList_QByteArray = crate::QList<crate::QByteArray>;
16        include!("cxx-qt-lib/qstring.h");
17        type QString = crate::QString;
18
19        include!("cxx-qt-lib/qtimezone.h");
20        /// The QTimeZone class converts between UTC and local time in a specific time zone.
21        //
22        // QTimeZone only has a copy-constructor and not a move-constructor, which means that the following is true
23        // "When the move constructor is not implicitly declared or explicitly supplied, expressions
24        // that otherwise would have invoked the move constructor may instead invoke a copy constructor."
25        //
26        // Therefore the internal QSharedDataPointer is incremented causing a memory leak, so use an opaque type.
27        type QTimeZone;
28
29        /// Returns the time zone abbreviation at the given atDateTime. The abbreviation may change depending on DST or even historical events.
30        fn abbreviation(self: &QTimeZone, atDateTime: &QDateTime) -> QString;
31
32        /// Returns any comment for the time zone.
33        fn comment(self: &QTimeZone) -> QString;
34
35        /// Returns the daylight-saving time offset at the given atDateTime,
36        /// i.e. the number of seconds to add to the standard time offset to obtain the local daylight-saving time.
37        #[rust_name = "daylight_time_offset"]
38        fn daylightTimeOffset(self: &QTimeZone, atDateTime: &QDateTime) -> i32;
39
40        /// Returns true if the time zone has practiced daylight-saving at any time.
41        #[rust_name = "has_daylight_time"]
42        fn hasDaylightTime(self: &QTimeZone) -> bool;
43
44        /// Returns true if the system backend supports obtaining transitions.
45        #[rust_name = "has_transitions"]
46        fn hasTransitions(self: &QTimeZone) -> bool;
47
48        /// Returns the IANA ID for the time zone.
49        fn id(self: &QTimeZone) -> QByteArray;
50
51        /// Returns true if daylight-saving was in effect at the given atDateTime.
52        #[rust_name = "is_daylight_time"]
53        fn isDaylightTime(self: &QTimeZone, atDateTime: &QDateTime) -> bool;
54
55        /// Returns true if this time zone is valid.
56        #[rust_name = "is_valid"]
57        fn isValid(self: &QTimeZone) -> bool;
58
59        /// Returns the total effective offset at the given atDateTime, i.e. the number of seconds to add to UTC to obtain the local time.
60        /// This includes any DST offset that may be in effect, i.e. it is the sum of standardTimeOffset() and daylightTimeOffset() for the given datetime.
61        #[rust_name = "offset_from_utc"]
62        fn offsetFromUtc(self: &QTimeZone, atDateTime: &QDateTime) -> i32;
63
64        /// Returns the standard time offset at the given atDateTime, i.e. the number of seconds to add to UTC to obtain the local Standard Time.
65        /// This excludes any DST offset that may be in effect.
66        #[rust_name = "standard_time_offset"]
67        fn standardTimeOffset(self: &QTimeZone, atDateTime: &QDateTime) -> i32;
68    }
69
70    #[namespace = "rust::cxxqtlib1"]
71    unsafe extern "C++" {
72        #[doc(hidden)]
73        #[rust_name = "qtimezone_available_time_zone_ids"]
74        fn qtimezoneAvailableTimeZoneIds() -> QList_QByteArray;
75        #[doc(hidden)]
76        #[rust_name = "qtimezone_clone"]
77        fn qtimezoneClone(timezone: &QTimeZone) -> UniquePtr<QTimeZone>;
78        #[doc(hidden)]
79        #[rust_name = "qtimezone_default"]
80        fn qtimezoneDefault() -> UniquePtr<QTimeZone>;
81        #[doc(hidden)]
82        #[rust_name = "qtimezone_from_offset_seconds"]
83        fn qtimezoneFromOffsetSeconds(offset_seconds: i32) -> UniquePtr<QTimeZone>;
84        #[doc(hidden)]
85        #[rust_name = "qtimezone_from_iana"]
86        fn qtimezoneFromIana(iana_id: &QByteArray) -> UniquePtr<QTimeZone>;
87        #[doc(hidden)]
88        #[rust_name = "qtimezone_system_time_zone"]
89        fn qtimezoneSystemTimeZone() -> UniquePtr<QTimeZone>;
90        #[doc(hidden)]
91        #[rust_name = "qtimezone_system_time_zone_id"]
92        fn qtimezoneSystemTimeZoneId() -> QByteArray;
93        #[doc(hidden)]
94        #[rust_name = "qtimezone_utc"]
95        fn qtimezoneUtc() -> UniquePtr<QTimeZone>;
96    }
97
98    #[namespace = "rust::cxxqtlib1"]
99    unsafe extern "C++" {
100        include!("cxx-qt-lib/common.h");
101
102        #[doc(hidden)]
103        #[rust_name = "qtimezone_eq"]
104        fn operatorEq(a: &QTimeZone, b: &QTimeZone) -> bool;
105        #[doc(hidden)]
106        #[rust_name = "qtimezone_to_qstring"]
107        fn toQString(value: &QTimeZone) -> QString;
108    }
109
110    // QTimeZone only has a copy-constructor and not a move-constructor, which means that the following is true
111    // "When the move constructor is not implicitly declared or explicitly supplied, expressions
112    // that otherwise would have invoked the move constructor may instead invoke a copy constructor."
113    //
114    // Therefore the internal QSharedDataPointer is incremented causing a memory leak, so use an opaque type.
115    impl UniquePtr<QTimeZone> {}
116}
117
118pub use ffi::QTimeZone;
119
120impl QTimeZone {
121    /// Returns a list of all available IANA time zone IDs on this system.
122    pub fn available_time_zone_ids() -> ffi::QList_QByteArray {
123        ffi::qtimezone_available_time_zone_ids()
124    }
125
126    /// Creates an instance of a time zone with the requested Offset from UTC of offsetSeconds.
127    pub fn from_offset_seconds(offset_seconds: i32) -> cxx::UniquePtr<Self> {
128        ffi::qtimezone_from_offset_seconds(offset_seconds)
129    }
130
131    /// Creates an instance of the requested time zone ianaId.
132    pub fn from_iana(iana_id: &ffi::QByteArray) -> cxx::UniquePtr<Self> {
133        ffi::qtimezone_from_iana(iana_id)
134    }
135
136    /// Create a null/invalid time zone instance.
137    pub fn new() -> cxx::UniquePtr<Self> {
138        ffi::qtimezone_default()
139    }
140
141    /// Returns a QTimeZone object that refers to the local system time, as specified by systemTimeZoneId().
142    pub fn system_time_zone() -> cxx::UniquePtr<Self> {
143        ffi::qtimezone_system_time_zone()
144    }
145
146    /// Returns the current system time zone IANA ID.
147    pub fn system_time_zone_id() -> ffi::QByteArray {
148        ffi::qtimezone_system_time_zone_id()
149    }
150
151    /// Copy constructor, create a copy of the QTimeZone.
152    pub fn to_owned(&self) -> cxx::UniquePtr<Self> {
153        ffi::qtimezone_clone(self)
154    }
155
156    /// Returns a QTimeZone object that refers to UTC (Universal Time Coordinated).
157    pub fn utc() -> cxx::UniquePtr<Self> {
158        ffi::qtimezone_utc()
159    }
160}
161
162impl std::cmp::PartialEq for QTimeZone {
163    fn eq(&self, other: &Self) -> bool {
164        ffi::qtimezone_eq(self, other)
165    }
166}
167
168impl std::cmp::Eq for QTimeZone {}
169
170impl fmt::Display for QTimeZone {
171    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
172        write!(f, "{}", ffi::qtimezone_to_qstring(self))
173    }
174}
175
176impl fmt::Debug for QTimeZone {
177    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
178        write!(f, "{self}")
179    }
180}