Add datetime()
and datetime_utc()
functions (#2167)
This commit is contained in:
parent
1c3c1dd3c0
commit
0e8f660d6d
10
README.md
10
README.md
@ -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
|
||||
|
@ -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),
|
||||
@ -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'-')
|
||||
|
27
tests/datetime.rs
Normal file
27
tests/datetime.rs
Normal file
@ -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();
|
||||
}
|
@ -47,6 +47,7 @@ mod completions;
|
||||
mod conditional;
|
||||
mod confirm;
|
||||
mod constants;
|
||||
mod datetime;
|
||||
mod delimiters;
|
||||
mod directories;
|
||||
mod dotenv;
|
||||
|
Loading…
Reference in New Issue
Block a user