Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor date_time #121

Merged
merged 3 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "shadow-rs"
version = "0.17.1"
version = "0.18.0"
authors = ["baoyachi <[email protected]>"]
edition = "2021"
description = "A build-time information stored in your rust project"
Expand Down Expand Up @@ -36,5 +36,8 @@ document-features = { version = "0.2", optional = true }
[features]
default = ["git2", "tzdb"]

[dev-dependencies]
regex = "1.7.0"

[workspace]
members = ["example_shadow", "example_shadow_hook"]
13 changes: 10 additions & 3 deletions src/data_time.rs → src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub enum DateTime {
Utc(OffsetDateTime),
}

pub fn now_data_time() -> DateTime {
// Enable reproducibility for uses of `now_data_time` by respecting the
pub fn now_date_time() -> DateTime {
// Enable reproducibility for uses of `now_date_time` by respecting the
// `SOURCE_DATE_EPOCH` env variable.
//
// https://reproducible-builds.org/docs/source-date-epoch/
Expand Down Expand Up @@ -108,11 +108,12 @@ impl Format for OffsetDateTime {
#[cfg(test)]
mod tests {
use super::*;
use regex::Regex;

#[test]
fn test_source_date_epoch() {
std::env::set_var("SOURCE_DATE_EPOCH", "1628080443");
let time = now_data_time();
let time = now_date_time();
assert_eq!(time.human_format(), "2021-08-04 12:34:03 +00:00");
}

Expand All @@ -122,6 +123,12 @@ mod tests {
#[cfg(unix)]
assert!(!std::fs::read("/etc/localtime").unwrap().is_empty());

let regex = Regex::new(
r#"^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{2}:[0-9]{2}:[0-9]{2}\s[+][0-9]{2}:[0-9]{2}"#,
)
.unwrap();
assert!(regex.is_match(&time));

println!("local now:{}", time); // 2022-07-14 00:40:05 +08:00
assert_eq!(time.len(), 26);
}
Expand Down
4 changes: 2 additions & 2 deletions src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::build::*;
use crate::data_time::now_data_time;
use crate::date_time::now_date_time;
use crate::env::dep_source_replace::filter_cargo_tree;
use crate::err::SdResult;
use crate::Format;
Expand Down Expand Up @@ -282,7 +282,7 @@ const BUILD_RUST_CHANNEL: ShadowConst = "BUILD_RUST_CHANNEL";

pub fn build_time(project: &mut Project) {
// Enable reproducible builds: https://reproducible-builds.org/docs/source-date-epoch/
let time = now_data_time();
let time = now_date_time();
project.map.insert(
BUILD_TIME,
ConstVal {
Expand Down
2 changes: 1 addition & 1 deletion src/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::build::{ConstType, ConstVal, ShadowConst};
use crate::ci::CiType;
use crate::data_time::DateTime;
use crate::date_time::DateTime;
use crate::err::*;
use crate::Format;
use std::collections::BTreeMap;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@

mod build;
mod ci;
mod data_time;
mod date_time;
mod env;
mod err;
mod gen_const;
Expand All @@ -167,7 +167,7 @@ use env::*;
use git::*;

use crate::ci::CiType;
pub use crate::data_time::DateTime;
pub use crate::date_time::DateTime;
pub use const_format::*;
use std::collections::BTreeMap;
use std::env as std_env;
Expand Down