-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stdlib): Add get_vector_timezone function (#671)
* 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
1 parent
a3a6a71
commit cc62e0c
Showing
7 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters