| 1 |
//! Get the system's UTC offset on Unix. |
| 2 |
|
| 3 |
use core::mem::MaybeUninit; |
| 4 |
|
| 5 |
use crate::util::local_offset::{self, Soundness}; |
| 6 |
use crate::{OffsetDateTime, UtcOffset}; |
| 7 |
|
| 8 |
/// Whether the operating system has a thread-safe environment. This allows bypassing the check for |
| 9 |
/// if the process is multi-threaded. |
| 10 |
// This is the same value as `cfg!(target_os = "x")`. |
| 11 |
// Use byte-strings to work around current limitations of const eval. |
| 12 |
const OS_HAS_THREAD_SAFE_ENVIRONMENT: bool = match std::env::consts::OS.as_bytes() { |
| 13 |
// https://github.com/illumos/illumos-gate/blob/0fb96ba1f1ce26ff8b286f8f928769a6afcb00a6/usr/src/lib/libc/port/gen/getenv.c |
| 14 |
b"illumos" |
| 15 |
// https://github.com/NetBSD/src/blob/f45028636a44111bc4af44d460924958a4460844/lib/libc/stdlib/getenv.c |
| 16 |
// https://github.com/NetBSD/src/blob/f45028636a44111bc4af44d460924958a4460844/lib/libc/stdlib/setenv.c |
| 17 |
| b"netbsd" |
| 18 |
=> true, |
| 19 |
_ => false, |
| 20 |
}; |
| 21 |
|
| 22 |
/// Convert the given Unix timestamp to a `libc::tm`. Returns `None` on any error. |
| 23 |
/// |
| 24 |
/// # Safety |
| 25 |
/// |
| 26 |
/// This method must only be called when the process is single-threaded. |
| 27 |
/// |
| 28 |
/// This method will remain `unsafe` until `std::env::set_var` is deprecated or has its behavior |
| 29 |
/// altered. This method is, on its own, safe. It is the presence of a safe, unsound way to set |
| 30 |
/// environment variables that makes it unsafe. |
| 31 |
unsafe fn timestamp_to_tm(timestamp: i64) -> Option<libc::tm> { |
| 32 |
extern "C" { |
| 33 |
#[cfg_attr(target_os = "netbsd", link_name = "__tzset50")] |
| 34 |
fn tzset(); |
| 35 |
} |
| 36 |
|
| 37 |
// The exact type of `timestamp` beforehand can vary, so this conversion is necessary. |
| 38 |
#[allow(clippy::useless_conversion)] |
| 39 |
let timestamp = timestamp.try_into().ok()?; |
| 40 |
|
| 41 |
let mut tm = MaybeUninit::uninit(); |
| 42 |
|
| 43 |
// Update timezone information from system. `localtime_r` does not do this for us. |
| 44 |
// |
| 45 |
// Safety: tzset is thread-safe. |
| 46 |
unsafe { tzset() }; |
| 47 |
|
| 48 |
// Safety: We are calling a system API, which mutates the `tm` variable. If a null |
| 49 |
// pointer is returned, an error occurred. |
| 50 |
let tm_ptr = unsafe { libc::localtime_r(×tamp, tm.as_mut_ptr()) }; |
| 51 |
|
| 52 |
if tm_ptr.is_null() { |
| 53 |
None |
| 54 |
} else { |
| 55 |
// Safety: The value was initialized, as we no longer have a null pointer. |
| 56 |
Some(unsafe { tm.assume_init() }) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/// Convert a `libc::tm` to a `UtcOffset`. Returns `None` on any error. |
| 61 |
// This is available to any target known to have the `tm_gmtoff` extension. |
| 62 |
#[cfg(any( |
| 63 |
target_os = "redox", |
| 64 |
target_os = "linux", |
| 65 |
target_os = "l4re", |
| 66 |
target_os = "android", |
| 67 |
target_os = "emscripten", |
| 68 |
target_os = "macos", |
| 69 |
target_os = "ios", |
| 70 |
target_os = "watchos", |
| 71 |
target_os = "freebsd", |
| 72 |
target_os = "dragonfly", |
| 73 |
target_os = "openbsd", |
| 74 |
target_os = "netbsd", |
| 75 |
target_os = "haiku", |
| 76 |
))] |
| 77 |
fn tm_to_offset(_unix_timestamp: i64, tm: libc::tm) -> Option<UtcOffset> { |
| 78 |
let seconds = tm.tm_gmtoff.try_into().ok()?; |
| 79 |
UtcOffset::from_whole_seconds(seconds).ok() |
| 80 |
} |
| 81 |
|
| 82 |
/// Convert a `libc::tm` to a `UtcOffset`. Returns `None` on any error. |
| 83 |
/// |
| 84 |
/// This method can return an incorrect value, as it only approximates the `tm_gmtoff` field. The |
| 85 |
/// reason for this is that daylight saving time does not start on the same date every year, nor are |
| 86 |
/// the rules for daylight saving time the same for every year. This implementation assumes 1970 is |
| 87 |
/// equivalent to every other year, which is not always the case. |
| 88 |
#[cfg(not(any( |
| 89 |
target_os = "redox", |
| 90 |
target_os = "linux", |
| 91 |
target_os = "l4re", |
| 92 |
target_os = "android", |
| 93 |
target_os = "emscripten", |
| 94 |
target_os = "macos", |
| 95 |
target_os = "ios", |
| 96 |
target_os = "watchos", |
| 97 |
target_os = "freebsd", |
| 98 |
target_os = "dragonfly", |
| 99 |
target_os = "openbsd", |
| 100 |
target_os = "netbsd", |
| 101 |
target_os = "haiku", |
| 102 |
)))] |
| 103 |
fn tm_to_offset(unix_timestamp: i64, tm: libc::tm) -> Option<UtcOffset> { |
| 104 |
use crate::Date; |
| 105 |
|
| 106 |
let mut tm = tm; |
| 107 |
if tm.tm_sec == 60 { |
| 108 |
// Leap seconds are not currently supported. |
| 109 |
tm.tm_sec = 59; |
| 110 |
} |
| 111 |
|
| 112 |
let local_timestamp = |
| 113 |
Date::from_ordinal_date(1900 + tm.tm_year, u16::try_from(tm.tm_yday).ok()? + 1) |
| 114 |
.ok()? |
| 115 |
.with_hms( |
| 116 |
tm.tm_hour.try_into().ok()?, |
| 117 |
tm.tm_min.try_into().ok()?, |
| 118 |
tm.tm_sec.try_into().ok()?, |
| 119 |
) |
| 120 |
.ok()? |
| 121 |
.assume_utc() |
| 122 |
.unix_timestamp(); |
| 123 |
|
| 124 |
let diff_secs = (local_timestamp - unix_timestamp).try_into().ok()?; |
| 125 |
|
| 126 |
UtcOffset::from_whole_seconds(diff_secs).ok() |
| 127 |
} |
| 128 |
|
| 129 |
/// Obtain the system's UTC offset. |
| 130 |
pub(super) fn local_offset_at(datetime: OffsetDateTime) -> Option<UtcOffset> { |
| 131 |
// Continue to obtaining the UTC offset if and only if the call is sound or the user has |
| 132 |
// explicitly opted out of soundness. |
| 133 |
// |
| 134 |
// Soundness can be guaranteed either by knowledge of the operating system or knowledge that the |
| 135 |
// process is single-threaded. If the process is single-threaded, then the environment cannot |
| 136 |
// be mutated by a different thread in the process while execution of this function is taking |
| 137 |
// place, which can cause a segmentation fault by dereferencing a dangling pointer. |
| 138 |
// |
| 139 |
// If the `num_threads` crate is incapable of determining the number of running threads, then |
| 140 |
// we conservatively return `None` to avoid a soundness bug. |
| 141 |
|
| 142 |
if OS_HAS_THREAD_SAFE_ENVIRONMENT |
| 143 |
|| local_offset::get_soundness() == Soundness::Unsound |
| 144 |
|| num_threads::is_single_threaded() == Some(true) |
| 145 |
{ |
| 146 |
let unix_timestamp = datetime.unix_timestamp(); |
| 147 |
// Safety: We have just confirmed that the process is single-threaded or the user has |
| 148 |
// explicitly opted out of soundness. |
| 149 |
let tm = unsafe { timestamp_to_tm(unix_timestamp) }?; |
| 150 |
tm_to_offset(unix_timestamp, tm) |
| 151 |
} else { |
| 152 |
None |
| 153 |
} |
| 154 |
} |