rustpython-host_env 0.6.0

Host OS API abstractions for RustPython
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#[cfg(unix)]
use alloc::ffi::CString;
use core::time::Duration;
use std::time::{SystemTime, SystemTimeError, UNIX_EPOCH};

#[cfg(target_env = "msvc")]
use alloc::string::String;

pub const SEC_TO_MS: i64 = 1000;
pub const MS_TO_US: i64 = 1000;
pub const SEC_TO_US: i64 = SEC_TO_MS * MS_TO_US;
pub const US_TO_NS: i64 = 1000;
pub const MS_TO_NS: i64 = MS_TO_US * US_TO_NS;
pub const SEC_TO_NS: i64 = SEC_TO_MS * MS_TO_NS;
pub const NS_TO_MS: i64 = 1000 * 1000;
pub const NS_TO_US: i64 = 1000;

/// Access to the C runtime's `tzset` / `timezone` / `altzone` / `daylight` / `tzname`
/// globals used by Python's `time` module.
///
/// Not available under MSVC (which exposes these only via the
/// `_get_tzname`-style helpers) or on `wasm32` (no libc tz state).
#[cfg(all(not(target_env = "msvc"), not(target_arch = "wasm32")))]
pub mod tz {
    unsafe extern "C" {
        #[cfg(not(target_os = "freebsd"))]
        #[link_name = "daylight"]
        static c_daylight: core::ffi::c_int;
        #[link_name = "timezone"]
        static c_timezone: core::ffi::c_long;
        // Set by `build.rs` when `time.h` exposes `altzone` (CPython `HAVE_ALTZONE`).
        #[cfg(has_altzone)]
        #[link_name = "altzone"]
        static c_altzone: core::ffi::c_long;
        #[link_name = "tzname"]
        static c_tzname: [*const core::ffi::c_char; 2];
        #[link_name = "tzset"]
        fn c_tzset();
    }

    pub fn tzset() {
        unsafe { c_tzset() }
    }

    #[must_use]
    pub fn timezone() -> core::ffi::c_long {
        unsafe { c_timezone }
    }

    /// DST offset west of UTC in seconds, matching CPython's `time.altzone`.
    ///
    /// Uses the C `altzone` global when available; otherwise falls back to
    /// `timezone - 3600` (same as CPython without `HAVE_ALTZONE`).
    #[must_use]
    pub fn altzone() -> core::ffi::c_long {
        #[cfg(has_altzone)]
        {
            unsafe { c_altzone }
        }
        #[cfg(not(has_altzone))]
        {
            timezone() - 3600
        }
    }

    #[cfg(not(target_os = "freebsd"))]
    #[must_use]
    pub fn daylight() -> core::ffi::c_int {
        unsafe { c_daylight }
    }

    /// Snapshot of `tzname[0]` / `tzname[1]` as owned `String`s.
    /// Reads the C globals once and copies the bytes out so callers don't
    /// have to handle the raw pointers themselves.
    #[must_use]
    pub fn tzname_strings() -> (String, String) {
        unsafe fn to_str(s: *const core::ffi::c_char) -> String {
            unsafe { core::ffi::CStr::from_ptr(s) }
                .to_string_lossy()
                .into_owned()
        }
        unsafe { (to_str(c_tzname[0]), to_str(c_tzname[1])) }
    }
}

pub fn duration_since_system_now() -> Result<Duration, SystemTimeError> {
    SystemTime::now().duration_since(UNIX_EPOCH)
}

#[cfg(unix)]
pub type TimeT = libc::time_t;

#[cfg(windows)]
pub type TimeT = libc::time_t;

#[cfg(unix)]
#[derive(Clone, Copy, Debug)]
pub struct ProcessTimes {
    pub user: f64,
    pub system: f64,
    pub children_user: f64,
    pub children_system: f64,
    pub elapsed: f64,
}

#[cfg(unix)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn current_time_t() -> TimeT {
    unsafe { libc::time(core::ptr::null_mut()) }
}

#[cfg(unix)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn gmtime_from_timestamp(when: TimeT) -> Option<libc::tm> {
    let mut out = core::mem::MaybeUninit::<libc::tm>::uninit();
    let ret = unsafe { libc::gmtime_r(&when, out.as_mut_ptr()) };
    (!ret.is_null()).then(|| unsafe { out.assume_init() })
}

#[cfg(unix)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn localtime_from_timestamp(when: TimeT) -> Option<libc::tm> {
    let mut out = core::mem::MaybeUninit::<libc::tm>::uninit();
    let ret = unsafe { libc::localtime_r(&when, out.as_mut_ptr()) };
    (!ret.is_null()).then(|| unsafe { out.assume_init() })
}

#[cfg(unix)]
pub fn mktime(tm: &mut libc::tm) -> TimeT {
    unsafe { libc::mktime(tm) }
}

#[cfg(windows)]
unsafe extern "C" {
    fn _gmtime64_s(tm: *mut libc::tm, time: *const libc::time_t) -> libc::c_int;
    fn _localtime64_s(tm: *mut libc::tm, time: *const libc::time_t) -> libc::c_int;
    #[link_name = "_mktime64"]
    fn c_mktime(tm: *mut libc::tm) -> libc::time_t;
}

#[cfg(windows)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn current_time_t() -> TimeT {
    unsafe { libc::time(core::ptr::null_mut()) }
}

#[cfg(windows)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn gmtime_from_timestamp(when: TimeT) -> Option<libc::tm> {
    let mut out = core::mem::MaybeUninit::<libc::tm>::uninit();
    let err = unsafe { _gmtime64_s(out.as_mut_ptr(), &when) };
    (err == 0).then(|| unsafe { out.assume_init() })
}

#[cfg(windows)]
#[cfg_attr(target_env = "musl", allow(deprecated))]
pub fn localtime_from_timestamp(when: TimeT) -> Option<libc::tm> {
    let mut out = core::mem::MaybeUninit::<libc::tm>::uninit();
    let err = unsafe { _localtime64_s(out.as_mut_ptr(), &when) };
    (err == 0).then(|| unsafe { out.assume_init() })
}

#[cfg(windows)]
pub fn mktime(tm: &mut libc::tm) -> TimeT {
    unsafe { crate::suppress_iph!(c_mktime(tm)) }
}

#[cfg(any(unix, windows, target_os = "wasi"))]
pub fn strerror(errno: i32) -> String {
    unsafe { core::ffi::CStr::from_ptr(libc::strerror(errno)) }
        .to_string_lossy()
        .into_owned()
}

#[cfg(unix)]
pub fn nix_errno_display(errno: i32) -> String {
    nix::errno::Errno::from_raw(errno).to_string()
}

#[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))]
pub fn getloadavg() -> std::io::Result<[f64; 3]> {
    let mut loadavg = [0f64; 3];
    let ok = unsafe { libc::getloadavg(loadavg.as_mut_ptr(), 3) };
    if ok != 3 {
        Err(std::io::Error::last_os_error())
    } else {
        Ok(loadavg)
    }
}

#[cfg(unix)]
pub fn waitstatus_to_exitcode(status: libc::c_int) -> Option<i32> {
    if libc::WIFEXITED(status) {
        return Some(libc::WEXITSTATUS(status));
    }
    if libc::WIFSIGNALED(status) {
        return Some(-libc::WTERMSIG(status));
    }
    None
}

#[cfg(any(unix, all(target_arch = "wasm32", target_os = "emscripten")))]
pub fn process_times() -> std::io::Result<ProcessTimes> {
    let mut t = libc::tms {
        tms_utime: 0,
        tms_stime: 0,
        tms_cutime: 0,
        tms_cstime: 0,
    };

    let tick_for_second = unsafe { libc::sysconf(libc::_SC_CLK_TCK) };
    if tick_for_second <= 0 {
        return Err(std::io::Error::last_os_error());
    }
    let tick_for_second = tick_for_second as f64;
    let c = unsafe { libc::times(&mut t as *mut _) };
    if c == (-1i8) as libc::clock_t {
        return Err(std::io::Error::last_os_error());
    }

    Ok(ProcessTimes {
        user: t.tms_utime as f64 / tick_for_second,
        system: t.tms_stime as f64 / tick_for_second,
        children_user: t.tms_cutime as f64 / tick_for_second,
        children_system: t.tms_cstime as f64 / tick_for_second,
        elapsed: c as f64 / tick_for_second,
    })
}

#[cfg(any(unix, target_os = "wasi"))]
#[derive(Copy, Clone, Debug)]
// WASI libc represents clockid_t as an opaque pointer type without Eq or PartialEq.
#[cfg_attr(unix, derive(Eq, PartialEq))]
pub struct ClockId(libc::clockid_t);

#[cfg(any(unix, target_os = "wasi"))]
impl ClockId {
    pub const fn from_raw(raw: libc::clockid_t) -> Self {
        Self(raw)
    }

    pub const fn as_raw(self) -> libc::clockid_t {
        self.0
    }

    pub const CLOCK_MONOTONIC: Self = Self(libc::CLOCK_MONOTONIC);
    pub const CLOCK_REALTIME: Self = Self(libc::CLOCK_REALTIME);

    #[cfg(not(any(
        target_os = "illumos",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "openbsd",
        target_os = "wasi",
    )))]
    pub const CLOCK_PROCESS_CPUTIME_ID: Self = Self(libc::CLOCK_PROCESS_CPUTIME_ID);

    #[cfg(not(any(
        target_os = "illumos",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "openbsd",
        target_os = "redox",
        target_os = "wasi",
    )))]
    pub const CLOCK_THREAD_CPUTIME_ID: Self = Self(libc::CLOCK_THREAD_CPUTIME_ID);

    #[cfg(target_vendor = "apple")]
    pub const CLOCK_UPTIME_RAW: Self = Self(libc::CLOCK_UPTIME_RAW);
}

#[cfg(unix)]
pub use libc::{CLOCK_MONOTONIC, CLOCK_REALTIME};

#[cfg(target_os = "solaris")]
pub use libc::CLOCK_HIGHRES;

#[cfg(any(target_os = "linux", target_vendor = "apple"))]
pub use libc::CLOCK_MONOTONIC_RAW;

#[cfg(all(
    unix,
    not(any(
        target_os = "illumos",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "openbsd",
    ))
))]
pub use libc::CLOCK_PROCESS_CPUTIME_ID;

#[cfg(all(
    unix,
    not(any(
        target_os = "illumos",
        target_os = "netbsd",
        target_os = "solaris",
        target_os = "openbsd",
        target_os = "redox",
    ))
))]
pub use libc::CLOCK_THREAD_CPUTIME_ID;

#[cfg(target_os = "linux")]
pub use libc::{CLOCK_BOOTTIME, CLOCK_TAI};

#[cfg(target_vendor = "apple")]
pub use libc::{CLOCK_MONOTONIC_RAW_APPROX, CLOCK_UPTIME_RAW, CLOCK_UPTIME_RAW_APPROX};

#[cfg(any(target_os = "freebsd", target_os = "openbsd", target_os = "dragonfly"))]
pub use libc::{CLOCK_PROF, CLOCK_UPTIME};

#[cfg(unix)]
fn nix_clock_id(id: ClockId) -> nix::time::ClockId {
    nix::time::ClockId::from_raw(id.as_raw())
}

#[cfg(unix)]
pub fn clock_gettime(id: ClockId) -> std::io::Result<Duration> {
    nix::time::clock_gettime(nix_clock_id(id))
        .map(Duration::from)
        .map_err(std::io::Error::from)
}

#[cfg(target_os = "wasi")]
pub fn clock_gettime(id: ClockId) -> std::io::Result<Duration> {
    let mut ts = core::mem::MaybeUninit::<libc::timespec>::uninit();

    let ret = unsafe { libc::clock_gettime(id.as_raw(), ts.as_mut_ptr()) };
    if ret != 0 {
        return Err(std::io::Error::last_os_error());
    }

    let ts = unsafe { ts.assume_init() };

    Ok(Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32))
}

#[cfg(all(unix, not(target_os = "redox")))]
pub fn clock_getres(id: ClockId) -> std::io::Result<Duration> {
    nix::time::clock_getres(nix_clock_id(id))
        .map(Duration::from)
        .map_err(std::io::Error::from)
}

#[cfg(target_os = "wasi")]
pub fn clock_getres(id: ClockId) -> std::io::Result<Duration> {
    let mut ts = core::mem::MaybeUninit::<libc::timespec>::uninit();
    let ret = unsafe { libc::clock_getres(id.as_raw(), ts.as_mut_ptr()) };
    if ret != 0 {
        return Err(std::io::Error::last_os_error());
    }
    let ts = unsafe { ts.assume_init() };
    Ok(Duration::new(ts.tv_sec as u64, ts.tv_nsec as u32))
}

#[cfg(all(unix, not(target_os = "redox"), not(target_vendor = "apple")))]
pub fn clock_settime(id: ClockId, time: Duration) -> std::io::Result<()> {
    let ts = nix::sys::time::TimeSpec::from(time);
    nix::time::clock_settime(nix_clock_id(id), ts)
        .map(drop)
        .map_err(std::io::Error::from)
}

#[cfg(all(unix, not(target_os = "redox"), target_os = "macos"))]
pub fn clock_settime(id: ClockId, time: Duration) -> std::io::Result<()> {
    let ts = nix::sys::time::TimeSpec::from(time);
    let ret = unsafe { libc::clock_settime(id.as_raw(), ts.as_ref()) };
    if ret != 0 {
        Err(std::io::Error::last_os_error())
    } else {
        Ok(())
    }
}

#[cfg(unix)]
pub fn nanosleep(duration: Duration) -> std::io::Result<()> {
    let ts = nix::sys::time::TimeSpec::from(duration);
    let ret = unsafe { libc::nanosleep(ts.as_ref(), core::ptr::null_mut()) };
    if ret != 0 {
        Err(std::io::Error::last_os_error())
    } else {
        Ok(())
    }
}

#[cfg(target_os = "solaris")]
pub fn gethrvtime_duration() -> Duration {
    Duration::from_nanos(unsafe { libc::gethrvtime() })
}

#[cfg(windows)]
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Debug)]
pub struct WindowsTimeZoneInfo {
    pub bias: i32,
    pub standard_bias: i32,
    pub daylight_bias: i32,
    pub standard_name: String,
    pub daylight_name: String,
}

#[cfg(windows)]
#[cfg(not(target_arch = "wasm32"))]
fn decode_tz_name(name: &[u16]) -> String {
    widestring::decode_utf16_lossy(name.iter().copied())
        .take_while(|&c| c != '\0')
        .collect()
}

#[cfg(windows)]
#[cfg(not(target_arch = "wasm32"))]
#[must_use]
pub fn get_tz_info() -> WindowsTimeZoneInfo {
    let mut info = unsafe { core::mem::zeroed() };
    unsafe { windows_sys::Win32::System::Time::GetTimeZoneInformation(&mut info) };
    WindowsTimeZoneInfo {
        bias: info.Bias as i32,
        standard_bias: info.StandardBias as i32,
        daylight_bias: info.DaylightBias as i32,
        standard_name: decode_tz_name(&info.StandardName),
        daylight_name: decode_tz_name(&info.DaylightName),
    }
}

#[cfg(windows)]
fn u64_from_filetime(time: windows_sys::Win32::Foundation::FILETIME) -> u64 {
    u64::from(time.dwLowDateTime) | (u64::from(time.dwHighDateTime) << 32)
}

#[cfg(windows)]
#[derive(Clone, Copy, Debug)]
pub struct ProcessTimes100ns {
    pub user: u64,
    pub system: u64,
}

#[cfg(windows)]
pub fn query_performance_frequency() -> Option<i64> {
    let mut freq = core::mem::MaybeUninit::uninit();
    (unsafe {
        windows_sys::Win32::System::Performance::QueryPerformanceFrequency(freq.as_mut_ptr())
    } != 0)
        .then(|| unsafe { freq.assume_init() })
}

#[cfg(windows)]
pub fn query_performance_counter() -> i64 {
    let mut counter = core::mem::MaybeUninit::uninit();
    unsafe {
        windows_sys::Win32::System::Performance::QueryPerformanceCounter(counter.as_mut_ptr());
        counter.assume_init()
    }
}

#[cfg(windows)]
pub fn get_system_time_adjustment() -> Option<u32> {
    let mut time_adjustment = core::mem::MaybeUninit::uninit();
    let mut time_increment = core::mem::MaybeUninit::uninit();
    let mut is_time_adjustment_disabled = core::mem::MaybeUninit::uninit();
    (unsafe {
        windows_sys::Win32::System::SystemInformation::GetSystemTimeAdjustment(
            time_adjustment.as_mut_ptr(),
            time_increment.as_mut_ptr(),
            is_time_adjustment_disabled.as_mut_ptr(),
        )
    } != 0)
        .then(|| unsafe { time_increment.assume_init() })
}

#[cfg(windows)]
pub fn tick_count64() -> u64 {
    unsafe { windows_sys::Win32::System::SystemInformation::GetTickCount64() }
}

#[cfg(windows)]
pub fn get_thread_time_100ns() -> Option<u64> {
    let mut creation_time = core::mem::MaybeUninit::uninit();
    let mut exit_time = core::mem::MaybeUninit::uninit();
    let mut kernel_time = core::mem::MaybeUninit::uninit();
    let mut user_time = core::mem::MaybeUninit::uninit();
    (unsafe {
        windows_sys::Win32::System::Threading::GetThreadTimes(
            windows_sys::Win32::System::Threading::GetCurrentThread(),
            creation_time.as_mut_ptr(),
            exit_time.as_mut_ptr(),
            kernel_time.as_mut_ptr(),
            user_time.as_mut_ptr(),
        )
    } != 0)
        .then(|| unsafe {
            u64_from_filetime(kernel_time.assume_init())
                + u64_from_filetime(user_time.assume_init())
        })
}

#[cfg(windows)]
pub fn get_process_time_100ns() -> Option<u64> {
    get_process_times_100ns().map(|times| times.user + times.system)
}

#[cfg(windows)]
pub fn get_process_times_100ns() -> Option<ProcessTimes100ns> {
    let mut creation_time = core::mem::MaybeUninit::uninit();
    let mut exit_time = core::mem::MaybeUninit::uninit();
    let mut kernel_time = core::mem::MaybeUninit::uninit();
    let mut user_time = core::mem::MaybeUninit::uninit();
    (unsafe {
        windows_sys::Win32::System::Threading::GetProcessTimes(
            windows_sys::Win32::System::Threading::GetCurrentProcess(),
            creation_time.as_mut_ptr(),
            exit_time.as_mut_ptr(),
            kernel_time.as_mut_ptr(),
            user_time.as_mut_ptr(),
        )
    } != 0)
        .then(|| unsafe {
            ProcessTimes100ns {
                user: u64_from_filetime(user_time.assume_init()),
                system: u64_from_filetime(kernel_time.assume_init()),
            }
        })
}

#[cfg(any(unix, windows))]
pub fn asctime_from_tm(tm: &libc::tm) -> String {
    const WDAY_NAME: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    const MON_NAME: [&str; 12] = [
        "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
    ];
    format!(
        "{} {}{:>3} {:02}:{:02}:{:02} {}",
        WDAY_NAME[tm.tm_wday as usize],
        MON_NAME[tm.tm_mon as usize],
        tm.tm_mday,
        tm.tm_hour,
        tm.tm_min,
        tm.tm_sec,
        tm.tm_year + 1900
    )
}

#[cfg(any(unix, windows))]
#[derive(Clone, Debug)]
pub struct CheckedTm {
    pub tm: libc::tm,
    #[cfg(unix)]
    pub zone: Option<CString>,
}

#[cfg(any(unix, windows))]
#[derive(Clone, Debug)]
pub struct CheckedTmParts {
    pub year: i64,
    pub tm_mon: i32,
    pub tm_mday: i32,
    pub tm_hour: i32,
    pub tm_min: i32,
    pub tm_sec: i32,
    pub tm_wday: i32,
    pub tm_yday: i32,
    pub tm_isdst: i32,
    #[cfg(unix)]
    pub zone: Option<String>,
    #[cfg(unix)]
    pub gmtoff: Option<i64>,
}

#[cfg(any(unix, windows))]
#[derive(Clone, Copy, Debug)]
pub struct MktimeTmParts {
    pub year: i32,
    pub tm_sec: i32,
    pub tm_min: i32,
    pub tm_hour: i32,
    pub tm_mday: i32,
    pub tm_mon: i32,
    pub tm_yday: i32,
    pub tm_isdst: i32,
}

#[cfg(any(unix, windows))]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CheckedTmError {
    YearOutOfRange,
    MonthOutOfRange,
    DayOfMonthOutOfRange,
    HourOutOfRange,
    MinuteOutOfRange,
    SecondsOutOfRange,
    DayOfWeekOutOfRange,
    DayOfYearOutOfRange,
    EmbeddedNul,
}

#[cfg(any(unix, windows))]
pub fn checked_tm_from_parts(parts: CheckedTmParts) -> Result<CheckedTm, CheckedTmError> {
    if parts.year < i64::from(i32::MIN) + 1900 || parts.year > i64::from(i32::MAX) {
        return Err(CheckedTmError::YearOutOfRange);
    }

    let mut tm: libc::tm = unsafe { core::mem::zeroed() };
    tm.tm_year = parts.year as i32 - 1900;
    tm.tm_mon = parts.tm_mon;
    tm.tm_mday = parts.tm_mday;
    tm.tm_hour = parts.tm_hour;
    tm.tm_min = parts.tm_min;
    tm.tm_sec = parts.tm_sec;
    tm.tm_wday = parts.tm_wday;
    tm.tm_yday = parts.tm_yday;
    tm.tm_isdst = parts.tm_isdst;

    if tm.tm_mon == -1 {
        tm.tm_mon = 0;
    } else if !(0..=11).contains(&tm.tm_mon) {
        return Err(CheckedTmError::MonthOutOfRange);
    }
    if tm.tm_mday == 0 {
        tm.tm_mday = 1;
    } else if !(0..=31).contains(&tm.tm_mday) {
        return Err(CheckedTmError::DayOfMonthOutOfRange);
    }
    if !(0..=23).contains(&tm.tm_hour) {
        return Err(CheckedTmError::HourOutOfRange);
    }
    if !(0..=59).contains(&tm.tm_min) {
        return Err(CheckedTmError::MinuteOutOfRange);
    }
    if !(0..=61).contains(&tm.tm_sec) {
        return Err(CheckedTmError::SecondsOutOfRange);
    }
    if tm.tm_wday < 0 {
        return Err(CheckedTmError::DayOfWeekOutOfRange);
    }
    if tm.tm_yday == -1 {
        tm.tm_yday = 0;
    } else if !(0..=365).contains(&tm.tm_yday) {
        return Err(CheckedTmError::DayOfYearOutOfRange);
    }

    #[cfg(unix)]
    {
        let zone = match parts.zone {
            Some(zone) => Some(CString::new(zone).map_err(|_| CheckedTmError::EmbeddedNul)?),
            None => None,
        };
        if let Some(zone) = &zone {
            tm.tm_zone = zone.as_ptr().cast_mut();
        }
        if let Some(gmtoff) = parts.gmtoff {
            tm.tm_gmtoff = gmtoff as _;
        }
        Ok(CheckedTm { tm, zone })
    }
    #[cfg(windows)]
    {
        Ok(CheckedTm { tm })
    }
}

#[cfg(any(unix, windows))]
pub fn mktime_tm_from_parts(parts: MktimeTmParts) -> Result<libc::tm, CheckedTmError> {
    if parts.year < i32::MIN + 1900 {
        return Err(CheckedTmError::YearOutOfRange);
    }
    let mut tm: libc::tm = unsafe { core::mem::zeroed() };
    tm.tm_sec = parts.tm_sec;
    tm.tm_min = parts.tm_min;
    tm.tm_hour = parts.tm_hour;
    tm.tm_mday = parts.tm_mday;
    tm.tm_mon = parts.tm_mon - 1;
    tm.tm_year = parts.year - 1900;
    tm.tm_wday = -1;
    tm.tm_yday = parts.tm_yday - 1;
    tm.tm_isdst = parts.tm_isdst;
    Ok(tm)
}

#[cfg(unix)]
pub fn strftime_ascii(fmt: &str, tm: &libc::tm) -> Result<String, CheckedTmError> {
    let fmt_c = CString::new(fmt).map_err(|_| CheckedTmError::EmbeddedNul)?;
    let mut size = 1024usize;
    let max_scale = 256usize.saturating_mul(fmt.len().max(1));
    loop {
        let mut out = vec![0u8; size];
        let written = unsafe {
            libc::strftime(
                out.as_mut_ptr().cast(),
                out.len(),
                fmt_c.as_ptr(),
                tm as *const libc::tm,
            )
        };
        if written > 0 || size >= max_scale {
            return Ok(String::from_utf8_lossy(&out[..written]).into_owned());
        }
        size = size.saturating_mul(2);
    }
}

#[cfg(windows)]
unsafe extern "C" {
    fn wcsftime(
        s: *mut libc::wchar_t,
        max: libc::size_t,
        format: *const libc::wchar_t,
        tm: *const libc::tm,
    ) -> libc::size_t;
}

#[cfg(windows)]
pub fn strftime_ascii(fmt: &str, tm: &libc::tm) -> Result<String, CheckedTmError> {
    let fmt_wide = widestring::WideCString::from_str(fmt)
        .map_err(|_| CheckedTmError::EmbeddedNul)?
        .into_vec_with_nul();
    let mut size = 1024usize;
    let max_scale = 256usize.saturating_mul(fmt.len().max(1));
    loop {
        let mut out = vec![0u16; size];
        let written = unsafe {
            crate::suppress_iph!(wcsftime(
                out.as_mut_ptr(),
                out.len(),
                fmt_wide.as_ptr(),
                tm as *const libc::tm,
            ))
        };
        if written > 0 || size >= max_scale {
            return Ok(String::from_utf16_lossy(&out[..written]));
        }
        size = size.saturating_mul(2);
    }
}