1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//! Time and date conversion routines.
//!
//! This code is highly experimental.

use alloc::ffi::CString;
use core::cell::OnceCell;
use core::ptr::{self, null_mut};
use errno::{set_errno, Errno};
use libc::{c_char, c_int, c_long, time_t, tm};
use std::collections::HashSet;
use std::sync::{Mutex, MutexGuard};
use tz::error::{TzError, TzFileError, TzStringError};
use tz::timezone::TransitionRule;
use tz::{DateTime, LocalTimeType, TimeZone};

// These things aren't really thread-safe, but they're implementing C ABIs and
// the C rule is, it's up to the user to ensure that none of the bad things
// happen.
struct SyncTm(tm);
unsafe impl Sync for SyncTm {}

// `tzname`, `timezone`, and `daylight` are guarded by `TIMEZONE_LOCK`.
struct SyncTzName([*mut c_char; 2]);
unsafe impl Sync for SyncTzName {}

// Hold `TIMEZONE_LOCK` when updating `tzname`, `timezone`, and `daylight`.
static TIMEZONE_LOCK: Mutex<(Option<CString>, Option<CString>)> = Mutex::new((None, None));
#[no_mangle]
static mut tzname: SyncTzName = SyncTzName([null_mut(), null_mut()]);
#[no_mangle]
static mut timezone: c_long = 0;
#[no_mangle]
static mut daylight: c_int = 0;

// Name storage for the `tm_zone` field.
static TIMEZONE_NAMES: Mutex<OnceCell<HashSet<CString>>> = Mutex::new(OnceCell::new());

#[no_mangle]
unsafe extern "C" fn gmtime(time: *const time_t) -> *mut tm {
    libc!(libc::gmtime(time));

    static mut TM: SyncTm = SyncTm(blank_tm());
    gmtime_r(time, &mut TM.0)
}

#[no_mangle]
unsafe extern "C" fn gmtime_r(time: *const time_t, result: *mut tm) -> *mut tm {
    libc!(libc::gmtime_r(time, result));

    let utc_time_type = LocalTimeType::utc();

    let date_time = match DateTime::from_timespec_and_local((*time).into(), 0, utc_time_type) {
        Ok(date_time) => date_time,
        Err(_err) => {
            set_errno(Errno(libc::EIO));
            return null_mut();
        }
    };

    ptr::write(result, date_time_to_tm(date_time));

    result
}

#[no_mangle]
unsafe extern "C" fn localtime(time: *const time_t) -> *mut tm {
    libc!(libc::localtime(time));

    static mut TM: SyncTm = SyncTm(blank_tm());
    localtime_r(time, &mut TM.0)
}

#[no_mangle]
unsafe extern "C" fn localtime_r(time: *const time_t, result: *mut tm) -> *mut tm {
    libc!(libc::localtime_r(time, result));

    let time_zone = match time_zone() {
        Ok(time_zone) => time_zone,
        Err(err) => {
            set_errno(tz_error_to_errno(err));
            return null_mut();
        }
    };

    let local_time_type = match time_zone.find_local_time_type((*time).into()) {
        Ok(local_time_type) => local_time_type,
        Err(_err) => {
            set_errno(Errno(libc::EIO));
            return null_mut();
        }
    };

    let date_time = match DateTime::from_timespec_and_local((*time).into(), 0, *local_time_type) {
        Ok(date_time) => date_time,
        Err(_err) => {
            set_errno(Errno(libc::EIO));
            return null_mut();
        }
    };

    ptr::write(result, date_time_to_tm(date_time));

    result
}

fn tm_to_date_time(tm: &tm, time_zone: &TimeZone) -> Result<DateTime, Errno> {
    let tm_year = tm.tm_year;
    let tm_mon: u8 = match tm.tm_mon.try_into() {
        Ok(tm_mon) => tm_mon,
        Err(_err) => return Err(Errno(libc::EOVERFLOW)),
    };
    let tm_mday = match tm.tm_mday.try_into() {
        Ok(tm_mday) => tm_mday,
        Err(_err) => return Err(Errno(libc::EOVERFLOW)),
    };
    let tm_hour = match tm.tm_hour.try_into() {
        Ok(tm_hour) => tm_hour,
        Err(_err) => return Err(Errno(libc::EOVERFLOW)),
    };
    let tm_min = match tm.tm_min.try_into() {
        Ok(tm_min) => tm_min,
        Err(_err) => return Err(Errno(libc::EOVERFLOW)),
    };
    let tm_sec = match tm.tm_sec.try_into() {
        Ok(tm_sec) => tm_sec,
        Err(_err) => return Err(Errno(libc::EOVERFLOW)),
    };

    let utc;
    let (std_time_type, dst_time_type) = if let Some(extra_rule) = time_zone.as_ref().extra_rule() {
        match extra_rule {
            TransitionRule::Fixed(local_time_type) => (local_time_type, None),
            TransitionRule::Alternate(alternate_time_type) => {
                (alternate_time_type.std(), Some(alternate_time_type.dst()))
            }
        }
    } else {
        utc = LocalTimeType::utc();
        (&utc, None)
    };

    let date_time = match tm.tm_isdst {
        tm_isdst if tm_isdst < 0 => {
            match DateTime::find(
                tm_year + 1900,
                tm_mon + 1,
                tm_mday,
                tm_hour,
                tm_min,
                tm_sec,
                0,
                time_zone.as_ref(),
            ) {
                Ok(found) => {
                    match found.unique() {
                        Some(date_time) => date_time,
                        None => {
                            // It's not clear what we should do here, so for
                            // now just be conservative.
                            return Err(Errno(libc::EIO));
                        }
                    }
                }
                Err(_err) => return Err(Errno(libc::EIO)),
            }
        }
        tm_isdst => {
            let time_type = if tm_isdst > 0 {
                dst_time_type.unwrap_or(std_time_type)
            } else {
                std_time_type
            };
            match DateTime::new(
                tm_year + 1900,
                tm_mon + 1,
                tm_mday,
                tm_hour,
                tm_min,
                tm_sec,
                0,
                *time_type,
            ) {
                Ok(date_time) => date_time,
                Err(_err) => return Err(Errno(libc::EIO)),
            }
        }
    };

    Ok(date_time)
}

#[no_mangle]
unsafe extern "C" fn mktime(tm: *mut tm) -> time_t {
    libc!(libc::mktime(tm));

    let mut lock = TIMEZONE_LOCK.lock().unwrap();

    clear_timezone(&mut lock);

    let time_zone = match time_zone() {
        Ok(time_zone) => time_zone,
        Err(err) => {
            set_errno(tz_error_to_errno(err));
            return -1;
        }
    };

    let utc;
    let (std_time_type, dst_time_type) = if let Some(extra_rule) = time_zone.as_ref().extra_rule() {
        match extra_rule {
            TransitionRule::Fixed(local_time_type) => (local_time_type, None),
            TransitionRule::Alternate(alternate_time_type) => {
                (alternate_time_type.std(), Some(alternate_time_type.dst()))
            }
        }
    } else {
        utc = LocalTimeType::utc();
        (&utc, None)
    };

    let date_time = match tm_to_date_time(&*tm, &time_zone) {
        Ok(date_time) => date_time,
        Err(errno) => {
            set_errno(errno);
            return -1;
        }
    };

    let unix_time = match date_time.unix_time().try_into() {
        Ok(unix_time) => unix_time,
        Err(_err) => {
            set_errno(Errno(libc::EOVERFLOW));
            -1
        }
    };

    ptr::write(tm, date_time_to_tm(date_time));

    set_timezone(&mut lock, std_time_type, dst_time_type);

    unix_time
}

fn date_time_to_tm(date_time: DateTime) -> tm {
    let local_time_type = date_time.local_time_type();

    let tm_zone = {
        let mut timezone_names = TIMEZONE_NAMES.lock().unwrap();
        timezone_names.get_or_init(HashSet::new);
        let cstr = CString::new(local_time_type.time_zone_designation()).unwrap();
        // TODO: Use `get_or_insert` when `hash_set_entry` is stabilized.
        timezone_names.get_mut().unwrap().insert(cstr.clone());
        timezone_names.get().unwrap().get(&cstr).unwrap().as_ptr()
    };

    tm {
        tm_year: date_time.year() - 1900,
        tm_mon: (date_time.month() - 1).into(),
        tm_mday: date_time.month_day().into(),
        tm_hour: date_time.hour().into(),
        tm_min: date_time.minute().into(),
        tm_sec: date_time.second().into(),
        tm_wday: date_time.week_day().into(),
        tm_yday: date_time.year_day().into(),
        tm_isdst: if local_time_type.is_dst() { 1 } else { 0 },
        tm_gmtoff: local_time_type.ut_offset().into(),
        tm_zone,
    }
}

const fn blank_tm() -> tm {
    tm {
        tm_year: 0,
        tm_mon: 0,
        tm_mday: 0,
        tm_hour: 0,
        tm_min: 0,
        tm_sec: 0,
        tm_wday: 0,
        tm_yday: 0,
        tm_isdst: -1,
        tm_gmtoff: 0,
        tm_zone: null_mut(),
    }
}

fn tz_error_to_errno(err: TzError) -> Errno {
    match err {
        TzError::IoError(err) => Errno(err.raw_os_error().unwrap_or(libc::EIO)),
        TzError::TzFileError(TzFileError::IoError(err)) => {
            Errno(err.raw_os_error().unwrap_or(libc::EIO))
        }
        TzError::TzStringError(TzStringError::IoError(err)) => {
            Errno(err.raw_os_error().unwrap_or(libc::EIO))
        }
        _ => Errno(libc::EIO),
    }
}

#[no_mangle]
unsafe extern "C" fn timegm(tm: *mut tm) -> time_t {
    libc!(libc::timegm(tm));

    let time_zone_utc = TimeZone::utc();

    let date_time = match tm_to_date_time(&*tm, &time_zone_utc) {
        Ok(date_time) => date_time,
        Err(errno) => {
            set_errno(errno);
            return -1;
        }
    };

    let unix_time = match date_time.unix_time().try_into() {
        Ok(unix_time) => unix_time,
        Err(_err) => {
            set_errno(Errno(libc::EOVERFLOW));
            -1
        }
    };

    unix_time
}

#[no_mangle]
unsafe extern "C" fn timelocal(tm: *mut tm) -> time_t {
    //libc!(libc::timelocal(tm)); // TODO: upstream

    let time_zone = match time_zone() {
        Ok(time_zone) => time_zone,
        Err(err) => {
            set_errno(tz_error_to_errno(err));
            return -1;
        }
    };

    let date_time = match tm_to_date_time(&*tm, &time_zone) {
        Ok(date_time) => date_time,
        Err(errno) => {
            set_errno(errno);
            return -1;
        }
    };

    let unix_time = match date_time.unix_time().try_into() {
        Ok(unix_time) => unix_time,
        Err(_err) => {
            set_errno(Errno(libc::EOVERFLOW));
            -1
        }
    };

    unix_time
}

#[no_mangle]
unsafe extern "C" fn tzset() {
    //libc!(libc::tzset()); // TODO: upstream

    let mut lock = TIMEZONE_LOCK.lock().unwrap();

    clear_timezone(&mut lock);

    let time_zone = match time_zone() {
        Ok(time_zone) => time_zone,
        Err(_) => return,
    };

    if let Some(extra_rule) = time_zone.as_ref().extra_rule() {
        match extra_rule {
            TransitionRule::Fixed(local_time_type) => {
                set_timezone(&mut lock, local_time_type, None);
            }
            TransitionRule::Alternate(alternate_time_type) => {
                let std = alternate_time_type.std();
                let dst = alternate_time_type.dst();
                set_timezone(&mut lock, std, Some(dst));
            }
        }
    }
}

fn time_zone() -> Result<TimeZone, TzError> {
    match std::env::var("TZ") {
        Ok(tz) => TimeZone::from_posix_tz(&tz),
        Err(_) => TimeZone::local(),
    }
}

unsafe fn set_timezone(
    guard: &mut MutexGuard<'_, (Option<CString>, Option<CString>)>,
    std: &LocalTimeType,
    dst: Option<&LocalTimeType>,
) {
    guard.0 = Some(CString::new(std.time_zone_designation()).unwrap());
    tzname.0[0] = guard.0.as_ref().unwrap().as_ptr().cast_mut();

    match dst {
        Some(dst) => {
            guard.1 = Some(CString::new(dst.time_zone_designation()).unwrap());
            tzname.0[1] = guard.1.as_ref().unwrap().as_ptr().cast_mut();
            daylight = 1;
        }
        None => {
            guard.1 = None;
            tzname.0[1] = guard.0.as_ref().unwrap().as_ptr().cast_mut();
            daylight = 0;
        }
    }

    let ut_offset = std.ut_offset();
    timezone = -c_long::from(ut_offset);
}

unsafe fn clear_timezone(guard: &mut MutexGuard<'_, (Option<CString>, Option<CString>)>) {
    guard.0 = None;
    guard.1 = None;
    tzname.0[0] = null_mut();
    tzname.0[1] = null_mut();
    timezone = 0;
    daylight = 0;
}