Skip to content

Commit

Permalink
Don't interpret GB as a timezone name (#129)
Browse files Browse the repository at this point in the history
Fixes "4 bytes -> GB" being an error saying you can't convert to a
timezone.

This timezone can be obtained in better ways, there's no reason it
should be like this.
  • Loading branch information
tiffany352 committed Mar 30, 2024
1 parent 349fd86 commit ba3a17d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion core/src/parsing/text_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ pub fn parse_query(iter: &mut Iter<'_>) -> Query {
Conversion::Expr(parse_eq(&mut old))
}
}
Token::Ident(ref s) if Tz::from_str(s).is_ok() => Conversion::Timezone(
Token::Ident(ref s) if is_valid_timezone(s) => Conversion::Timezone(
Tz::from_str(s).expect("Running from_str a second time failed"),
),
_ => Conversion::Expr(parse_eq(iter)),
Expand All @@ -845,6 +845,11 @@ pub fn parse_query(iter: &mut Iter<'_>) -> Query {
}
}

fn is_valid_timezone(s: &String) -> bool {
use std::str::FromStr;
s != "GB" && Tz::from_str(s).is_ok()
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
8 changes: 8 additions & 0 deletions core/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,14 @@ fn test_to_timezone() {
);
}

#[test]
fn test_gb_is_gigabytes() {
test(
"56kbps * 1 year -> GB",
"approx. 220.8984 gigabyte (information)",
)
}

#[test]
fn test_missing_base() {
test("3 -> base", "Expected decimal base, got eof");
Expand Down

0 comments on commit ba3a17d

Please sign in to comment.