foundation: remove optional chrono dependency (#666)

* foundation: remove optional chrono dependency

* Bump version numbers for semver-incompatible changes
This commit is contained in:
Dirkjan Ochtman
2024-04-08 12:20:45 +02:00
committed by GitHub
parent d7879eb353
commit 9721646053
8 changed files with 18 additions and 77 deletions
+1 -3
View File
@@ -3,7 +3,7 @@ name = "core-foundation"
description = "Bindings to Core Foundation for macOS"
homepage = "https://github.com/servo/core-foundation-rs"
repository = "https://github.com/servo/core-foundation-rs"
version = "0.9.4"
version = "0.10.0"
authors = ["The Servo Project Developers"]
license = "MIT OR Apache-2.0"
categories = ["os::macos-apis"]
@@ -17,7 +17,6 @@ version = "0.8.6"
[dependencies]
libc = "0.2"
chrono = { version = "0.4", optional = true }
uuid = { version = "0.5", optional = true }
[features]
@@ -25,7 +24,6 @@ default = ["link"]
mac_os_10_7_support = ["core-foundation-sys/mac_os_10_7_support"] # backwards compatibility
mac_os_10_8_features = ["core-foundation-sys/mac_os_10_8_features"] # enables new features
with-chrono = ["chrono"]
with-uuid = ["uuid"]
# Disable to manually link. Enabled by default.
link = ["core-foundation-sys/link"]
-56
View File
@@ -14,9 +14,6 @@ pub use core_foundation_sys::date::*;
use crate::base::TCFType;
#[cfg(feature = "with-chrono")]
use chrono::NaiveDateTime;
declare_TCFType! {
/// A date.
CFDate, CFDateRef
@@ -43,26 +40,6 @@ impl CFDate {
pub fn abs_time(&self) -> CFAbsoluteTime {
unsafe { CFDateGetAbsoluteTime(self.0) }
}
#[cfg(feature = "with-chrono")]
pub fn naive_utc(&self) -> NaiveDateTime {
let ts = unsafe { self.abs_time() + kCFAbsoluteTimeIntervalSince1970 };
let (secs, nanos) = if ts.is_sign_positive() {
(ts.trunc() as i64, ts.fract())
} else {
// nanoseconds can't be negative in NaiveDateTime
(ts.trunc() as i64 - 1, 1.0 - ts.fract().abs())
};
NaiveDateTime::from_timestamp(secs, (nanos * 1e9).floor() as u32)
}
#[cfg(feature = "with-chrono")]
pub fn from_naive_utc(time: NaiveDateTime) -> CFDate {
let secs = time.timestamp();
let nanos = time.timestamp_subsec_nanos();
let ts = unsafe { secs as f64 + (nanos as f64 / 1e9) - kCFAbsoluteTimeIntervalSince1970 };
CFDate::new(ts)
}
}
#[cfg(test)]
@@ -70,18 +47,6 @@ mod test {
use super::CFDate;
use std::cmp::Ordering;
#[cfg(feature = "with-chrono")]
use chrono::NaiveDateTime;
#[cfg(feature = "with-chrono")]
fn approx_eq(a: f64, b: f64) -> bool {
use std::f64;
let same_sign = a.is_sign_positive() == b.is_sign_positive();
let equal = ((a - b).abs() / f64::min(a.abs() + b.abs(), f64::MAX)) < f64::EPSILON;
same_sign && equal
}
#[test]
fn date_comparison() {
let now = CFDate::now();
@@ -97,25 +62,4 @@ mod test {
let same_time = CFDate::new(now.abs_time());
assert_eq!(now, same_time);
}
#[test]
#[cfg(feature = "with-chrono")]
fn date_chrono_conversion_positive() {
let date = CFDate::now();
let datetime = date.naive_utc();
let converted = CFDate::from_naive_utc(datetime);
assert!(approx_eq(date.abs_time(), converted.abs_time()));
}
#[test]
#[cfg(feature = "with-chrono")]
fn date_chrono_conversion_negative() {
use super::kCFAbsoluteTimeIntervalSince1970;
let ts = unsafe { kCFAbsoluteTimeIntervalSince1970 - 420.0 };
let date = CFDate::new(ts);
let datetime: NaiveDateTime = date.naive_utc();
let converted = CFDate::from_naive_utc(datetime);
assert!(approx_eq(date.abs_time(), converted.abs_time()));
}
}