Skip to content

Commit

Permalink
fix empty result around borders
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn committed Feb 16, 2025
1 parent 35feeb2 commit 3f4de05
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT"
name = "tzf-rs"
readme = "README.md"
repository = "https://github.com/ringsaturn/tzf-rs"
version = "0.4.11"
version = "0.4.12"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
32 changes: 18 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,19 +407,9 @@ impl DefaultFinder {
// It's not a bug but a limitation of the simplified algorithm.
//
// To handle this, auto shift the point a little bit to find the nearest timezone.
for &dx in &[0.0, -0.01, 0.01, -0.02, 0.02] {
for &dy in &[0.0, -0.01, 0.01, -0.02, 0.02] {
let dlng = dx + lng;
let dlat = dy + lat;
let fuzzy_name = self.fuzzy_finder.get_tz_name(dlng, dlat);
if !fuzzy_name.is_empty() {
return fuzzy_name;
}
let name = self.finder.get_tz_name(dlng, dlat);
if !name.is_empty() {
return name;
}
}
let res = self.get_tz_names(lng, lat);
if !res.is_empty() {
return res.first().unwrap();
}
""
}
Expand All @@ -431,7 +421,21 @@ impl DefaultFinder {
/// ```
#[must_use]
pub fn get_tz_names(&self, lng: f64, lat: f64) -> Vec<&str> {
self.finder.get_tz_names(lng, lat)
for &dx in &[0.0, -0.01, 0.01, -0.02, 0.02] {
for &dy in &[0.0, -0.01, 0.01, -0.02, 0.02] {
let dlng = dx + lng;
let dlat = dy + lat;
let fuzzy_names = self.fuzzy_finder.get_tz_names(dlng, dlat);
if !fuzzy_names.is_empty() {
return fuzzy_names;
}
let names = self.finder.get_tz_names(dlng, dlat);
if !names.is_empty() {
return names;
}
}
}
Vec::new() // Return empty vector if no timezone is found
}

/// Returns all time zone names as a `Vec<&str>`.
Expand Down
21 changes: 21 additions & 0 deletions tests/basic_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#[cfg(test)]
mod tests {
use tzf_rs::DefaultFinder;
use tzf_rs::Finder;

#[test]
Expand All @@ -18,5 +19,25 @@ mod tests {
assert_eq!(finder.get_tz_name(-73.7729, 38.3530), "Etc/GMT+5");
assert_eq!(finder.get_tz_name(114.1594, 22.3173), "Asia/Hong_Kong");
assert_eq!(finder.get_tz_name(9.8198, 27.5775), "Africa/Tripoli");

// lng: 8.61231565, lat: 47.66148548

assert_eq!(finder.get_tz_name(8.61280918, 47.66097966) != "", true);
assert_eq!(finder.get_tz_name(8.61231565, 47.66148548) != "", true);
}

#[test]
fn no_empty() {
let finder = DefaultFinder::new();
// ensure not empty
assert_eq!(
finder.get_tz_name(8.61231564999999932297, 47.66148548000000317870) != "",
true
);
assert_eq!(
finder.get_tz_name(8.61280917999999928725, 47.66097966000000241138) != "",
true
);
assert_eq!(finder.get_tz_name(8.61231565, 47.66148548) != "", true);
}
}

0 comments on commit 3f4de05

Please sign in to comment.