1
- use std:: ffi:: OsString ;
1
+ use std:: ffi:: { OsStr , OsString } ;
2
2
use std:: fmt:: Write ;
3
+ use std:: str:: FromStr ;
3
4
use std:: time:: { Duration , SystemTime } ;
4
5
5
- use chrono:: { DateTime , Datelike , Local , Timelike , Utc } ;
6
+ use chrono:: { DateTime , Datelike , Offset , Timelike , Utc } ;
7
+ use chrono_tz:: Tz ;
6
8
7
9
use crate :: concurrency:: thread:: MachineCallback ;
8
10
use crate :: * ;
@@ -136,8 +138,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
136
138
. unwrap ( ) ;
137
139
let dt_utc: DateTime < Utc > =
138
140
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
+
139
149
// 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 ) ;
141
151
142
152
// This value is always set to -1, because there is no way to know if dst is in effect with
143
153
// chrono crate yet.
@@ -146,17 +156,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
146
156
147
157
// tm_zone represents the timezone value in the form of: +0730, +08, -0730 or -08.
148
158
// 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 ;
151
161
let mut tm_zone = String :: new ( ) ;
152
- if offset_in_second < 0 {
162
+ if offset_in_seconds < 0 {
153
163
tm_zone. push ( '-' ) ;
154
164
} else {
155
165
tm_zone. push ( '+' ) ;
156
166
}
157
- let offset_hour = offset_in_second . abs ( ) / 3600 ;
167
+ let offset_hour = offset_in_seconds . abs ( ) / 3600 ;
158
168
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 ;
160
170
if offset_min != 0 {
161
171
write ! ( tm_zone, "{:02}" , offset_min) . unwrap ( ) ;
162
172
}
0 commit comments