Skip to content

Commit

Permalink
Add datetime() and datetime_utc() functions (#2167)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Jun 15, 2024
1 parent 1c3c1dd commit 0e8f660
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,16 @@ which will halt execution.
characters. For example, `choose('64', HEX)` will generate a random
64-character lowercase hex string.

#### Datetime

- `datetime(format)`<sup>master</sup> - Return local time with `format`.
- `datetime_utc(format)`<sup>master</sup> - Return UTC time with `format`.

The arguments to `datetime` and `datetime_utc` are `strftime`-style format
strings, see the
[`chrono` library docs](https://docs.rs/chrono/latest/chrono/format/strftime/index.html)
for details.

#### Semantic Versions

- `semver_matches(version, requirement)`<sup>1.16.0</sup> - Check whether a
Expand Down
10 changes: 10 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"config_local_directory" => Nullary(|_| dir("local config", dirs::config_local_dir)),
"data_directory" => Nullary(|_| dir("data", dirs::data_dir)),
"data_local_directory" => Nullary(|_| dir("local data", dirs::data_local_dir)),
"datetime" => Unary(datetime),
"datetime_utc" => Unary(datetime_utc),
"encode_uri_component" => Unary(encode_uri_component),
"env" => UnaryOpt(env),
"env_var" => Unary(env_var),
Expand Down Expand Up @@ -235,6 +237,14 @@ fn dir(name: &'static str, f: fn() -> Option<PathBuf>) -> FunctionResult {
}
}

fn datetime(_context: Context, format: &str) -> FunctionResult {
Ok(chrono::Local::now().format(format).to_string())
}

fn datetime_utc(_context: Context, format: &str) -> FunctionResult {
Ok(chrono::Utc::now().format(format).to_string())
}

fn encode_uri_component(_context: Context, s: &str) -> FunctionResult {
static PERCENT_ENCODE: percent_encoding::AsciiSet = percent_encoding::NON_ALPHANUMERIC
.remove(b'-')
Expand Down
27 changes: 27 additions & 0 deletions tests/datetime.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use super::*;

#[test]
fn datetime() {
Test::new()
.justfile(
"
x := datetime('%Y-%m-%d %z')
",
)
.args(["--eval", "x"])
.stdout_regex(r"\d\d\d\d-\d\d-\d\d [+-]\d\d\d\d")
.run();
}

#[test]
fn datetime_utc() {
Test::new()
.justfile(
"
x := datetime_utc('%Y-%m-%d %Z')
",
)
.args(["--eval", "x"])
.stdout_regex(r"\d\d\d\d-\d\d-\d\d UTC")
.run();
}
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod completions;
mod conditional;
mod confirm;
mod constants;
mod datetime;
mod delimiters;
mod directories;
mod dotenv;
Expand Down

0 comments on commit 0e8f660

Please sign in to comment.