Skip to content

Commit fc0db10

Browse files
committed
Auto merge of #3523 - saethlin:localtime_r-env, r=RalfJung
Use the interpreted program's TZ variable in localtime_r This requires a bit of wiring and a new dependency, but the tests should correctly pass now regardless of what the host's time zone is. Fixes #3522
2 parents 8d600fa + 92715f5 commit fc0db10

File tree

6 files changed

+153
-151
lines changed

6 files changed

+153
-151
lines changed

Cargo.lock

Lines changed: 76 additions & 125 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ smallvec = "1.7"
2424
aes = { version = "0.8.3", features = ["hazmat"] }
2525
measureme = "11"
2626
ctrlc = "3.2.5"
27-
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
27+
chrono = { version = "0.4.38", default-features = false }
28+
chrono-tz = "0.9"
2829
directories = "5"
2930

3031
# Copied from `compiler/rustc/Cargo.toml`.

src/shims/env.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::OsString;
1+
use std::ffi::{OsStr, OsString};
22

33
use rustc_data_structures::fx::FxHashMap;
44

@@ -99,4 +99,15 @@ impl<'tcx> EnvVars<'tcx> {
9999
}
100100

101101
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir, 'tcx> {}
102-
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {}
102+
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
103+
/// Try to get an environment variable from the interpreted program's environment. This is
104+
/// useful for implementing shims which are documented to read from the environment.
105+
fn get_env_var(&mut self, name: &OsStr) -> InterpResult<'tcx, Option<OsString>> {
106+
let this = self.eval_context_ref();
107+
match &this.machine.env_vars {
108+
EnvVars::Uninit => return Ok(None),
109+
EnvVars::Unix(vars) => vars.get(this, name),
110+
EnvVars::Windows(vars) => vars.get(name),
111+
}
112+
}
113+
}

src/shims/time.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use std::ffi::OsString;
1+
use std::ffi::{OsStr, OsString};
22
use std::fmt::Write;
3+
use std::str::FromStr;
34
use std::time::{Duration, SystemTime};
45

5-
use chrono::{DateTime, Datelike, Local, Timelike, Utc};
6+
use chrono::{DateTime, Datelike, Offset, Timelike, Utc};
7+
use chrono_tz::Tz;
68

79
use crate::concurrency::thread::MachineCallback;
810
use crate::*;
@@ -136,8 +138,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
136138
.unwrap();
137139
let dt_utc: DateTime<Utc> =
138140
DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp");
141+
142+
// Figure out what time zone is in use
143+
let tz = this.get_env_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
144+
let tz = match tz.into_string() {
145+
Ok(tz) => Tz::from_str(&tz).unwrap_or(Tz::UTC),
146+
_ => Tz::UTC,
147+
};
148+
139149
// Convert that to local time, then return the broken-down time value.
140-
let dt: DateTime<Local> = DateTime::from(dt_utc);
150+
let dt: DateTime<Tz> = dt_utc.with_timezone(&tz);
141151

142152
// This value is always set to -1, because there is no way to know if dst is in effect with
143153
// chrono crate yet.
@@ -146,17 +156,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
146156

147157
// tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08.
148158
// This may not be consistent with libc::localtime_r's result.
149-
let offset_in_second = Local::now().offset().local_minus_utc();
150-
let tm_gmtoff = offset_in_second;
159+
let offset_in_seconds = dt.offset().fix().local_minus_utc();
160+
let tm_gmtoff = offset_in_seconds;
151161
let mut tm_zone = String::new();
152-
if offset_in_second < 0 {
162+
if offset_in_seconds < 0 {
153163
tm_zone.push('-');
154164
} else {
155165
tm_zone.push('+');
156166
}
157-
let offset_hour = offset_in_second.abs() / 3600;
167+
let offset_hour = offset_in_seconds.abs() / 3600;
158168
write!(tm_zone, "{:02}", offset_hour).unwrap();
159-
let offset_min = (offset_in_second.abs() % 3600) / 60;
169+
let offset_min = (offset_in_seconds.abs() % 3600) / 60;
160170
if offset_min != 0 {
161171
write!(tm_zone, "{:02}", offset_min).unwrap();
162172
}

0 commit comments

Comments
 (0)