From 51649272bd388f14685a47cfe805bd9cdb602240 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 15 Mar 2023 12:44:22 +0530 Subject: perf: do not depend on iana-time-zone (#18088) Chrono's `clock` feature pulls in `iana-time-zone` which links to macOS core_foundation. This PR itself is not enough to get rid of CoreFoundation. Removal depends on getting rid of security framework, see #18071 --- cli/util/mod.rs | 1 + cli/util/time.rs | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 cli/util/time.rs (limited to 'cli/util') diff --git a/cli/util/mod.rs b/cli/util/mod.rs index 515bc64e9..c3133cc10 100644 --- a/cli/util/mod.rs +++ b/cli/util/mod.rs @@ -12,6 +12,7 @@ pub mod logger; pub mod path; pub mod progress_bar; pub mod text_encoding; +pub mod time; pub mod unix; pub mod v8; pub mod windows; diff --git a/cli/util/time.rs b/cli/util/time.rs new file mode 100644 index 000000000..9c5b48b57 --- /dev/null +++ b/cli/util/time.rs @@ -0,0 +1,22 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +/// Identical to chrono::Utc::now() but without the system "clock" +/// feature flag. +/// +/// The "clock" feature flag pulls in the "iana-time-zone" crate +/// which links to macOS's "CoreFoundation" framework which increases +/// startup time for the CLI. +/// +/// You can simply include this file in your project using +/// `include!("path/to/cli/util/time.rs"))` and use it +/// as a drop-in replacement for chrono::Utc::now(). +pub fn utc_now() -> chrono::DateTime { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("system time before Unix epoch"); + let naive = chrono::NaiveDateTime::from_timestamp( + now.as_secs() as i64, + now.subsec_nanos(), + ); + chrono::DateTime::from_utc(naive, chrono::Utc) +} -- cgit v1.2.3