libtz-sys

Rust FFI interface for IANA's libtz.

This provides an equivalent of glibc's localtime_r() function (and related functions). The difference is that this library has been compiled such that the getenv("TZ") call uses Rust's std::env::var_os() which protects it from std::env::set_var() races which could otherwise cause segfaults.

Aside from that it should be a drop in replacement for glibc. It will read the tzdata files that the system has installed to calculate things like leap seconds and daylight saving time.

Usage

Add this to your Cargo.toml:

toml [dependencies] libtz-sys = "0.1.0"

Example

```rust use std::ffi::{CString,CStr}; use std::mem::MaybeUninit; use libtzsys::{tzalloc, localtimerz, mktime_z, tzfree, TimeT, Tm};

let tzname = CString::new("America/NewYork").unwrap(); let tz = unsafe { tzalloc(tzname.asptr()) }; if tz == std::ptr::nullmut() { return Err(std::io::Error::lastoserror()); } let time: TimeT = 127810800; let mut tm = MaybeUninit::::uninit(); let ret = unsafe { localtimerz(tz, &time, tm.asmutptr()) }; if ret == std::ptr::nullmut() { return Err(std::io::Error::lastoserror()); } let tm = unsafe { tm.assumeinit() }; let zone: &str = unsafe { CStr::fromptr(tm.tmzone).tostr().expect("correct utf8") }; asserteq!((tm.tmsec, tm.tmmin, tm.tmhour, tm.tmmday, tm.tmmon), (0, 0, 3, 19, 0, )); asserteq!((tm.tmyear, tm.tmwday, tm.tmyday, tm.tmisdst, tm.tm_gmtoff, zone), (74, 6, 18, 1, -14400, "EDT"));

let timeagain = unsafe { mktimez(tz, &tm) }; // Round trip if timeagain == -1 { // Didn't work (errno is not reliably set in this case) } else { asserteq!(time_again, time); } unsafe { tzfree(tz) };

Ok(())

```

License

The Rust code is distributed under the MIT license.

The libtz code is mostly public domain with a couple files using the BSD-3 clause license.

See LICENSE.md for details.