Skip to content

Commit

Permalink
Remove use of localtime_r, and all dependent callsights
Browse files Browse the repository at this point in the history
  • Loading branch information
kflansburg committed Oct 18, 2021
1 parent 4eeedcf commit ef9ae30
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@
#![cfg_attr(feature = "bench", feature(test))] // lib stability features as per RFC #507
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(dead_code)]
// #![deny(dead_code)]
// lints are added all the time, we test on 1.13
#![allow(unknown_lints)]
#![cfg_attr(not(any(feature = "std", test)), no_std)]
Expand Down
54 changes: 27 additions & 27 deletions src/offset/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,16 @@ fn datetime_to_timespec(d: &NaiveDateTime, local: bool) -> sys::Timespec {
pub struct Local;

impl Local {
/// Returns a `Date` which corresponds to the current date.
pub fn today() -> Date<Local> {
Local::now().date()
}
// /// Returns a `Date` which corresponds to the current date.
// pub fn today() -> Date<Local> {
// Local::now().date()
// }

/// Returns a `DateTime` which corresponds to the current date.
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
pub fn now() -> DateTime<Local> {
tm_to_datetime(Timespec::now().local())
}
// /// Returns a `DateTime` which corresponds to the current date.
// #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
// pub fn now() -> DateTime<Local> {
// tm_to_datetime(Timespec::now().local())
// }

/// Returns a `DateTime` which corresponds to the current date.
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
Expand Down Expand Up @@ -156,17 +156,17 @@ impl TimeZone for Local {
LocalResult::Single(DateTime::from_utc(local, offset))
}

#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
let timespec = datetime_to_timespec(local, true);
// #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
// fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
// let timespec = datetime_to_timespec(local, true);

// datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
let mut tm = timespec.local();
assert_eq!(tm.tm_nsec, 0);
tm.tm_nsec = local.nanosecond() as i32;
// // datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
// let mut tm = timespec.local();
// assert_eq!(tm.tm_nsec, 0);
// tm.tm_nsec = local.nanosecond() as i32;

LocalResult::Single(tm_to_datetime(tm))
}
// LocalResult::Single(tm_to_datetime(tm))
// }

fn from_utc_date(&self, utc: &NaiveDate) -> Date<Local> {
let midnight = self.from_utc_datetime(&utc.and_hms(0, 0, 0));
Expand All @@ -180,17 +180,17 @@ impl TimeZone for Local {
DateTime::from_utc(*utc, offset)
}

#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
let timespec = datetime_to_timespec(utc, false);
// #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
// fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
// let timespec = datetime_to_timespec(utc, false);

// datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
let mut tm = timespec.local();
assert_eq!(tm.tm_nsec, 0);
tm.tm_nsec = utc.nanosecond() as i32;
// // datetime_to_timespec completely ignores leap seconds, so we need to adjust for them
// let mut tm = timespec.local();
// assert_eq!(tm.tm_nsec, 0);
// tm.tm_nsec = utc.nanosecond() as i32;

tm_to_datetime(tm)
}
// tm_to_datetime(tm)
// }
}

#[cfg(test)]
Expand Down
38 changes: 19 additions & 19 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ impl Timespec {
Timespec { sec: st.as_secs() as i64, nsec: st.subsec_nanos() as i32 }
}

/// Converts this timespec into the system's local time.
pub fn local(self) -> Tm {
let mut tm = Tm {
tm_sec: 0,
tm_min: 0,
tm_hour: 0,
tm_mday: 0,
tm_mon: 0,
tm_year: 0,
tm_wday: 0,
tm_yday: 0,
tm_isdst: 0,
tm_utcoff: 0,
tm_nsec: 0,
};
inner::time_to_local_tm(self.sec, &mut tm);
tm.tm_nsec = self.nsec;
tm
}
// /// Converts this timespec into the system's local time.
// pub fn local(self) -> Tm {
// let mut tm = Tm {
// tm_sec: 0,
// tm_min: 0,
// tm_hour: 0,
// tm_mday: 0,
// tm_mon: 0,
// tm_year: 0,
// tm_wday: 0,
// tm_yday: 0,
// tm_isdst: 0,
// tm_utcoff: 0,
// tm_nsec: 0,
// };
// inner::time_to_local_tm(self.sec, &mut tm);
// tm.tm_nsec = self.nsec;
// tm
// }
}

/// Holds a calendar date and time broken down into its components (year, month,
Expand Down
50 changes: 25 additions & 25 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,31 @@ unsafe fn timegm(tm: *mut libc::tm) -> time_t {
ret
}

pub fn time_to_local_tm(sec: i64, tm: &mut Tm) {
unsafe {
let sec = sec as time_t;
let mut out = mem::zeroed();
if libc::localtime_r(&sec, &mut out).is_null() {
panic!("localtime_r failed: {}", io::Error::last_os_error());
}
#[cfg(any(target_os = "solaris", target_os = "illumos"))]
let gmtoff = {
tzset();
// < 0 means we don't know; assume we're not in DST.
if out.tm_isdst == 0 {
// timezone is seconds west of UTC, tm_gmtoff is seconds east
-timezone
} else if out.tm_isdst > 0 {
-altzone
} else {
-timezone
}
};
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
let gmtoff = out.tm_gmtoff;
tm_to_rust_tm(&out, gmtoff as i32, tm);
}
}
// pub fn time_to_local_tm(sec: i64, tm: &mut Tm) {
// unsafe {
// let sec = sec as time_t;
// let mut out = mem::zeroed();
// if libc::localtime_r(&sec, &mut out).is_null() {
// panic!("localtime_r failed: {}", io::Error::last_os_error());
// }
// #[cfg(any(target_os = "solaris", target_os = "illumos"))]
// let gmtoff = {
// tzset();
// // < 0 means we don't know; assume we're not in DST.
// if out.tm_isdst == 0 {
// // timezone is seconds west of UTC, tm_gmtoff is seconds east
// -timezone
// } else if out.tm_isdst > 0 {
// -altzone
// } else {
// -timezone
// }
// };
// #[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
// let gmtoff = out.tm_gmtoff;
// tm_to_rust_tm(&out, gmtoff as i32, tm);
// }
// }

pub fn utc_tm_to_time(rust_tm: &Tm) -> i64 {
#[cfg(not(any(
Expand Down

0 comments on commit ef9ae30

Please sign in to comment.