Skip to content

Commit

Permalink
chore: remove usage of chrono::Utc::now() (#20995)
Browse files Browse the repository at this point in the history
Remove usage of Chrono's clock feature which pulls in iana-time-zone ->
core-foundation
  • Loading branch information
littledivy authored Oct 30, 2023
1 parent 02cc37e commit 1acef75
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 56 deletions.
50 changes: 0 additions & 50 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ brotli = "3.3.4"
bytes = "1.4.0"
cache_control = "=0.2.0"
cbc = { version = "=0.1.2", features = ["alloc"] }
chrono = { version = "0.4", default-features = false, features = ["std", "serde", "clock"] }
# Note: Do not use the "clock" feature of chrono, as it links us to CoreFoundation on macOS.
# Instead use util::time::utc_now()
chrono = { version = "0.4", default-features = false, features = ["std", "serde"] }
console_static_text = "=0.8.1"
data-url = "=0.3.0"
data-encoding = "2.3.3"
Expand Down
5 changes: 3 additions & 2 deletions cli/tools/jupyter/jupyter_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Copyright 2020 The Evcxr Authors. MIT license.

use bytes::Bytes;
use chrono::Utc;
use data_encoding::HEXLOWER;
use deno_core::anyhow::anyhow;
use deno_core::anyhow::bail;
Expand All @@ -15,6 +14,8 @@ use ring::hmac;
use std::fmt;
use uuid::Uuid;

use crate::util::time::utc_now;

pub(crate) struct Connection<S> {
pub(crate) socket: S,
/// Will be None if our key was empty (digest authentication disabled).
Expand Down Expand Up @@ -177,7 +178,7 @@ impl JupyterMessage {
header["msg_type"] = serde_json::Value::String(msg_type.to_owned());
header["username"] = serde_json::Value::String("kernel".to_owned());
header["msg_id"] = serde_json::Value::String(Uuid::new_v4().to_string());
header["date"] = serde_json::Value::String(Utc::now().to_rfc3339());
header["date"] = serde_json::Value::String(utc_now().to_rfc3339());

JupyterMessage {
zmq_identities: Vec::new(),
Expand Down
4 changes: 2 additions & 2 deletions ext/kv/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod interface;
mod proto;
pub mod remote;
pub mod sqlite;
mod time;

use std::borrow::Cow;
use std::cell::RefCell;
Expand All @@ -14,7 +15,6 @@ use std::rc::Rc;

use base64::prelude::BASE64_URL_SAFE;
use base64::Engine;
use chrono::Utc;
use codec::decode_key;
use codec::encode_key;
use deno_core::anyhow::Context;
Expand Down Expand Up @@ -610,7 +610,7 @@ async fn op_kv_atomic_write<DBH>(
where
DBH: DatabaseHandler + 'static,
{
let current_timestamp = Utc::now().timestamp_millis() as u64;
let current_timestamp = time::utc_now().timestamp_millis() as u64;
let db = {
let state = state.borrow();
let resource =
Expand Down
2 changes: 1 addition & 1 deletion ext/kv/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ async fn metadata_refresh_task(
metadata
.expires_at
.timestamp_millis()
.saturating_sub(Utc::now().timestamp_millis()),
.saturating_sub(crate::time::utc_now().timestamp_millis()),
)
.unwrap_or_default();

Expand Down
19 changes: 19 additions & 0 deletions ext/kv/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// 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.
pub fn utc_now() -> chrono::DateTime<chrono::Utc> {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system time before Unix epoch");
let naive = chrono::NaiveDateTime::from_timestamp_opt(
now.as_secs() as i64,
now.subsec_nanos(),
)
.unwrap();
chrono::DateTime::from_naive_utc_and_offset(naive, chrono::Utc)
}

0 comments on commit 1acef75

Please sign in to comment.