build-time-0.1.3/.cargo_vcs_info.json0000644000000001360000000000100130700ustar { "git": { "sha1": "ac84210949079d8a080d1e1f1ad63444c0332881" }, "path_in_vcs": "" }build-time-0.1.3/.github/dependabot.yml000064400000000000000000000003201046102023000160430ustar 00000000000000version: 2 updates: - package-ecosystem: cargo directory: "/" schedule: interval: daily open-pull-requests-limit: 10 commit-message: prefix: ":arrow_up: " include: "scope" build-time-0.1.3/.github/workflows/test.yml000064400000000000000000000030271046102023000167610ustar 00000000000000name: Tests and Lints on: push: pull_request: jobs: check: name: Check runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v2 - name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true - name: Run cargo check uses: actions-rs/cargo@v1 with: command: check args: --all-features test: name: Test Suite runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v2 - name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true - name: Run cargo test uses: actions-rs/cargo@v1 with: command: test args: --all-features lints: name: Lints runs-on: ubuntu-latest steps: - name: Checkout sources uses: actions/checkout@v2 - name: Install stable toolchain uses: actions-rs/toolchain@v1 with: profile: minimal toolchain: stable override: true components: rustfmt, clippy - name: Run cargo fmt uses: actions-rs/cargo@v1 with: command: fmt args: --all -- --check - name: Run cargo clippy uses: actions-rs/cargo@v1 with: command: clippy args: -- -W clippy::nursery -D warnings build-time-0.1.3/.gitignore000064400000000000000000000000231046102023000136430ustar 00000000000000/target Cargo.lock build-time-0.1.3/Cargo.toml0000644000000023620000000000100110710ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "build-time" version = "0.1.3" authors = ["AlephAlpha "] description = "Simple proc-macros to generate build timestamp string literals." homepage = "https://github.com/AlephAlpha/build-time" readme = "README.md" keywords = [ "build", "proc_macro", "timestamp", "tool", ] categories = [ "date-and-time", "development-tools", "rust-patterns", ] license = "MIT" repository = "https://github.com/AlephAlpha/build-time" [lib] proc-macro = true [dependencies.chrono] version = "0.4.20" features = ["clock"] default-features = false [dependencies.once_cell] version = "1.17" [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" [dependencies.syn] version = "2.0" features = ["parsing"] build-time-0.1.3/Cargo.toml.orig000064400000000000000000000014261046102023000145520ustar 00000000000000[package] authors = ["AlephAlpha "] categories = ["date-and-time", "development-tools", "rust-patterns"] description = "Simple proc-macros to generate build timestamp string literals." edition = "2018" homepage = "https://github.com/AlephAlpha/build-time" keywords = ["build", "proc_macro", "timestamp", "tool"] license = "MIT" name = "build-time" readme = "README.md" repository = "https://github.com/AlephAlpha/build-time" version = "0.1.3" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [lib] proc-macro = true [dependencies] chrono = { version = "0.4.20", default-features = false, features = ["clock"] } once_cell = "1.17" proc-macro2 = "1.0" quote = "1.0" syn = { version = "2.0", features = ["parsing"] } build-time-0.1.3/README.md000064400000000000000000000017451046102023000131460ustar 00000000000000# build-time Simple proc-macros to generate build timestamp string literals. Based on Jasen Borisov's [build_timestamp](https://crates.io/crates/build_timestamp) crate. Two function like procedures are provided: `build_time_utc` and `build_time_local`. They take an optional [`strftime`](https://docs.rs/chrono/0.4/chrono/format/strftime/index.html) date and time format string as input, and return a string literal. If the input is empty, they will return a string literal in [RFC 3339 date and time format](https://en.wikipedia.org/wiki/ISO_8601#RFCs), e.g., `"2021-05-29T06:55:50.418437046+00:00"`. Requires Rust 1.45+ because these macros are used in expression positions. ## Usage ```rust use build_time::{build_time_utc, build_time_local}; // Returns the UTC build timestamp in RFC3339 date and time format. let utc_build_time = build_time_utc!(); // Returns the local build timestamp in the specified format. let local_build_time = build_time_local!("%Y-%m-%dT%H:%M:%S%.f%:z"); ```build-time-0.1.3/src/lib.rs000064400000000000000000000067131046102023000135720ustar 00000000000000/*! # build-time Simple proc-macros to generate build timestamp string literals. Based on Jasen Borisov's [build_timestamp](https://crates.io/crates/build_timestamp) crate. Two function like procedures are provided: `build_time_utc` and `build_time_local`. They take an optional [`strftime`](https://docs.rs/chrono/0.4/chrono/format/strftime/index.html) date and time format string as input, and return a string literal. If the input is empty, they will return a string literal in [RFC 3339 date and time format](https://en.wikipedia.org/wiki/ISO_8601#RFCs), e.g., `"2021-05-29T06:55:50.418437046+00:00"`. Requires Rust 1.45+ because these macros are used in expression positions. ## Usage ```rust use build_time::{build_time_utc, build_time_local}; // Returns the UTC build timestamp in RFC3339 date and time format. let utc_build_time = build_time_utc!(); // Returns the local build timestamp in the specified format. let local_build_time = build_time_local!("%Y-%m-%dT%H:%M:%S%.f%:z"); ``` */ use chrono::{DateTime, Local, Utc}; use once_cell::sync::Lazy; use proc_macro::TokenStream; use proc_macro2::Span; use quote::quote; use syn::{parse_macro_input, LitStr}; static BUILD_TIME: Lazy> = Lazy::new(Utc::now); /// Build time in UTC. /// /// It takes an optional [`strftime`](https://docs.rs/chrono/0.4/chrono/format/strftime/index.html) /// date and time format string as input, and returns a string literal. /// If the input is empty, it will return a string literal in /// [RFC 3339 date and time format](https://en.wikipedia.org/wiki/ISO_8601#RFCs), /// e.g., `"2021-05-29T06:55:50.418437046+00:00"`. /// /// # Example /// /// ```rust /// use build_time::build_time_utc; /// /// // Returns the UTC build timestamp in RFC3339 date and time format. /// let build_time_rfc3339 = build_time_utc!(); /// /// // Returns the UTC build timestamp in the specified format. /// let build_time_formatted = build_time_utc!("%Y-%m-%dT%H:%M:%S%.f%:z"); /// ``` #[proc_macro] pub fn build_time_utc(input: TokenStream) -> TokenStream { let time_str = if input.is_empty() { BUILD_TIME.to_rfc3339() } else { let format = parse_macro_input!(input as LitStr); BUILD_TIME.format(&format.value()).to_string() }; let lit = LitStr::new(&time_str, Span::call_site()); quote!(#lit).into() } /// Build time in the local timescale. /// /// It takes an optional [`strftime`](https://docs.rs/chrono/0.4/chrono/format/strftime/index.html) /// date and time format string as input, and returns a string literal. /// If the input is empty, it will return a string literal in /// [RFC 3339 date and time format](https://en.wikipedia.org/wiki/ISO_8601#RFCs), /// e.g., `"2021-05-29T06:55:50.418437046+00:00"`. /// /// # Example /// /// ```rust /// use build_time::build_time_local; /// /// // Returns the local build timestamp in RFC3339 date and time format. /// let build_time_rfc3339 = build_time_local!(); /// /// // Returns the local build timestamp in the specified format. /// let build_time_formatted = build_time_local!("%Y-%m-%dT%H:%M:%S%.f%:z"); /// ``` #[proc_macro] pub fn build_time_local(input: TokenStream) -> TokenStream { let local_time = BUILD_TIME.with_timezone(&Local); let time_str = if input.is_empty() { local_time.to_rfc3339() } else { let format = parse_macro_input!(input as LitStr); local_time.format(&format.value()).to_string() }; let lit = LitStr::new(&time_str, Span::call_site()); quote!(#lit).into() } build-time-0.1.3/tests/test.rs000064400000000000000000000016641046102023000143560ustar 00000000000000use build_time::{build_time_local, build_time_utc}; use chrono::DateTime; use std::{thread::sleep, time::Duration}; #[test] fn call_twice() { let utc = build_time_utc!(); let local = build_time_local!(); sleep(Duration::from_secs(1)); let utc1 = build_time_utc!(); let local1 = build_time_local!(); assert_eq!(utc, utc1); assert_eq!(local, local1); } #[test] fn local_utc_match() { let utc = DateTime::parse_from_rfc3339(build_time_utc!()).unwrap(); let local = DateTime::parse_from_rfc3339(build_time_local!()).unwrap(); assert_eq!(utc, local); } #[test] fn strftime_format() { let utc_rfc3339 = build_time_utc!(); let local_rfc3339 = build_time_local!(); let utc_formatted = build_time_utc!("%Y-%m-%dT%H:%M:%S%.f%:z"); let local_formatted = build_time_local!("%Y-%m-%dT%H:%M:%S%.f%:z"); assert_eq!(utc_rfc3339, utc_formatted); assert_eq!(local_rfc3339, local_formatted); }