Skip to content

Commit

Permalink
feat(stdlib): Add get_vector_timezone function (#671)
Browse files Browse the repository at this point in the history
* Add get_timezone_name to VRL stdlib

This function returns the name of the VRL timezone, or if
TimeZone::Local then it will attempt to get the timezone name from the
host OS. If that fails, then it will get the local time, and return the
formatted timezone offset (e.g., "+02:00")

Note that iana-time-zone was previously an indirect dependency, now it's
a direct dependency.

* Rename changelog.d fragment now that PR # is known
  • Loading branch information
klondikedragon committed Feb 5, 2024
1 parent a3a6a71 commit cc62e0c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 8 deletions.
15 changes: 8 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ exitcode = {version = "1", optional = true }
flate2 = { version = "1.0.28", default-features = false, features = ["default"], optional = true }
hex = { version = "0.4", optional = true }
hmac = { version = "0.12.1", optional = true }
iana-time-zone = "0.1.59"
indexmap = { version = "~2.2.2", default-features = false, features = ["std"], optional = true}
indoc = {version = "2.0.4", optional = true }
itertools = { version = "0.12.1", default-features = false, optional = true }
Expand Down
11 changes: 10 additions & 1 deletion LICENSE-3rdparty.csv
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,16 @@ web-sys,https://github.com/rustwasm/wasm-bindgen/tree/master/crates/web-sys,MIT
webbrowser,https://github.com/amodm/webbrowser-rs,MIT OR Apache-2.0,Amod Malviya @amodm
winapi,https://github.com/retep998/winapi-rs,MIT OR Apache-2.0,Peter Atashian <retep998@gmail.com>
winapi-util,https://github.com/BurntSushi/winapi-util,Unlicense OR MIT,Andrew Gallant <jamslam@gmail.com>
windows,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows-core,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows-sys,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows-targets,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows_aarch64_gnullvm,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows_aarch64_msvc,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows_i686_gnu,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows_i686_msvc,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows_x86_64_gnu,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows_x86_64_gnullvm,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
windows_x86_64_msvc,https://github.com/microsoft/windows-rs,MIT OR Apache-2.0,Microsoft
winnow,https://github.com/winnow-rs/winnow,MIT,The winnow Authors
woothee,https://github.com/woothee/woothee-rust,Apache-2.0,hhatto <hhatto.jp@gmail.com>
wyz,https://github.com/myrrlyn/wyz,MIT,myrrlyn <self@myrrlyn.dev>
Expand Down
10 changes: 10 additions & 0 deletions benches/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ criterion_group!(
get,
get_env_var,
get_hostname,
get_timezone_name,
includes,
int,
ip_aton,
Expand Down Expand Up @@ -578,6 +579,15 @@ bench_function! {
}
}

bench_function! {
get_timezone_name => vrl::stdlib::GetTimezoneName;

get {
args: func_args![],
want: Ok(vrl::stdlib::get_name_for_timezone(&vrl::compiler::TimeZone::Named(chrono_tz::Tz::UTC))),
}
}

bench_function! {
includes => vrl::stdlib::Includes;

Expand Down
3 changes: 3 additions & 0 deletions changelog.d/671.enhancement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add VRL function `get_timezone_name` to return the configured/resolved IANA timezone name.

authors: klondikedragon
74 changes: 74 additions & 0 deletions src/stdlib/get_timezone_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use crate::compiler::prelude::*;
use chrono::Local;
use std::borrow::Cow;

#[must_use]
pub fn get_name_for_timezone(tz: &TimeZone) -> Cow<'_, str> {
match tz {
TimeZone::Named(tz) => tz.name().into(),
TimeZone::Local => iana_time_zone::get_timezone()
.unwrap_or_else(|_| Local::now().offset().to_string())
.into(),
}
}

#[allow(clippy::unnecessary_wraps)]
fn get_timezone_name(ctx: &mut Context) -> Resolved {
Ok(get_name_for_timezone(ctx.timezone()).into())
}

#[derive(Clone, Copy, Debug)]
pub struct GetTimezoneName;

impl Function for GetTimezoneName {
fn identifier(&self) -> &'static str {
"get_timezone_name"
}

fn examples(&self) -> &'static [Example] {
&[Example {
title: "Get the VRL timezone name, or for 'local' the local timezone name or offset (e.g., -05:00)",
source: r#"get_timezone_name!() != """#,
result: Ok("true"),
}]
}

fn compile(
&self,
_state: &TypeState,
_ctx: &mut FunctionCompileContext,
_: ArgumentList,
) -> Compiled {
Ok(GetTimezoneNameFn.as_expr())
}
}

#[derive(Debug, Clone)]
struct GetTimezoneNameFn;

impl FunctionExpression for GetTimezoneNameFn {
fn resolve(&self, ctx: &mut Context) -> Resolved {
get_timezone_name(ctx)
}

fn type_def(&self, _: &TypeState) -> TypeDef {
TypeDef::bytes().fallible()
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::value;

test_function![
get_hostname => GetTimezoneName;

// the test harness always initializes the VRL timezone to UTC
utc {
args: func_args![],
want: Ok(value!(get_name_for_timezone(&TimeZone::Named(chrono_tz::Tz::UTC)))),
tdef: TypeDef::bytes().fallible(),
}
];
}
4 changes: 4 additions & 0 deletions src/stdlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ cfg_if::cfg_if! {
mod get;
mod get_env_var;
mod get_hostname;
mod get_timezone_name;
mod hmac;
mod includes;
mod integer;
Expand Down Expand Up @@ -251,6 +252,8 @@ cfg_if::cfg_if! {
pub use get::Get;
pub use get_env_var::GetEnvVar;
pub use get_hostname::GetHostname;
pub use get_timezone_name::GetTimezoneName;
pub use get_timezone_name::get_name_for_timezone;
pub use includes::Includes;
pub use integer::Integer;
pub use ip_aton::IpAton;
Expand Down Expand Up @@ -418,6 +421,7 @@ pub fn all() -> Vec<Box<dyn Function>> {
Box::new(Get),
Box::new(GetEnvVar),
Box::new(GetHostname),
Box::new(GetTimezoneName),
Box::new(Hmac),
Box::new(Includes),
Box::new(Integer),
Expand Down

0 comments on commit cc62e0c

Please sign in to comment.