Skip to content

Commit

Permalink
chore: Add a generic current Utc time fn to c2pa-crypto (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten-adobe authored Nov 14, 2024
1 parent 60d39e5 commit 1e1553d
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ jobs:
- name: Install wasm-pack
run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh

- name: Run Wasm tests (c2pa-crypto)
run: wasm-pack test --chrome --headless
working-directory: ./internal/crypto

- name: Run Wasm tests (c2pa-status-tracker)
run: wasm-pack test --chrome --headless
working-directory: ./internal/status-tracker
Expand Down
18 changes: 18 additions & 0 deletions internal/crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]

[dependencies.chrono]
version = "0.4.38"
default-features = false
features = ["wasmbind"]

[target.'cfg(not(target_arch = "wasm32"))'.dependencies.chrono]
version = "0.4.38"
default-features = false
features = ["now", "wasmbind"]

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "0.2.83"
wasm-bindgen-futures = "0.4.31"
web-time = "1.1"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.31"
17 changes: 17 additions & 0 deletions internal/crypto/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# IMPORTANT: PR validation uses nightly version of
# rustfmt.

# Invoke by using:
#
# rustup toolchain add nightly
# cargo +nightly fmt

blank_lines_upper_bound = 1
empty_item_single_line = true
format_code_in_doc_comments = true
group_imports = "StdExternalCrate"
hex_literal_case = "Lower"
imports_granularity = "Crate"
reorder_impl_items = true
unstable_features = true
wrap_comments = true
14 changes: 14 additions & 0 deletions internal/crypto/src/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

pub(crate) mod time;
38 changes: 38 additions & 0 deletions internal/crypto/src/internal/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

#![allow(dead_code)] // TEMPORARY while building

use chrono::{DateTime, Utc};

/// Return the current time in UTC.
pub(crate) fn utc_now() -> DateTime<Utc> {
#[cfg(not(target_arch = "wasm32"))]
{
Utc::now()
}

#[cfg(target_arch = "wasm32")]
{
// Very unlikely that SystemTime would be before Unix epoch, but if so, cap it
// at 0.
let utc_duration = web_time::SystemTime::now()
.duration_since(web_time::UNIX_EPOCH)
.unwrap_or(std::time::Duration::ZERO);

let mut utc_now = chrono::DateTime::UNIX_EPOCH;
utc_now += utc_duration;

utc_now
}
}
31 changes: 23 additions & 8 deletions internal/crypto/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
/// This crate is a placeholder. More to come soon ...
pub fn does_nothing_yet() {}
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::unwrap_used)]
#![deny(missing_docs)]
#![deny(warnings)]
#![doc = include_str!("../README.md")]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg, doc_cfg_hide))]

pub(crate) mod internal;

#[cfg(test)]
mod tests {
#[test]
fn it_does_nothing() {
println!("Placeholder");
}
}
pub(crate) mod tests;
14 changes: 14 additions & 0 deletions internal/crypto/src/tests/internal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

mod time;
27 changes: 27 additions & 0 deletions internal/crypto/src/tests/internal/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test;

use crate::internal::time;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
#[test]
fn now() {
let time_now = time::utc_now();
let unix_ts = time_now.timestamp();
dbg!(&unix_ts);

assert!(unix_ts > 1731560000); // 2024-11-14T04:53:00Z
}
24 changes: 24 additions & 0 deletions internal/crypto/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.

// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

// Tests are grouped under this module so as to avoid
// having the test code itself included in coverage numbers.

#![allow(clippy::expect_used)]
#![allow(clippy::panic)]
#![allow(clippy::unwrap_used)]

#[cfg(target_arch = "wasm32")]
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);

mod internal;

0 comments on commit 1e1553d

Please sign in to comment.