Skip to content

About

Provides an IANA time zone identifier from latitude and longitude coordinates.

Resources

Stars

255 stars

Watchers

19 watching

Forks

Repository files navigation

GeoTimeZone NuGet Version

Provides an IANA time zone identifier from latitude and longitude coordinates.

Installation

  • Add the GeoTimeZone NuGet package to your project.
  • Import the GeoTimeZone namespace where needed.

Compatibility

As of version 7.0.0, GeoTimeZone works with all of the following:

  • .NET 5 and newer, with dedicated targets for .NET 8, .NET 9, and .NET 10
  • .NET Core 2.0 or greater
  • .NET Framework 4.6.2 and greater

The package also includes .NET Standard 2.0 and 2.1 targets for compatible platforms.

.NET Framework versions less than 4.6.2 are no longer supported.

Example Usage

string tz = TimeZoneLookup.GetTimeZone(50.4372, -3.5559).Result;  // "Europe/London"

Initialization and Custom Data

By default, the embedded data is selected automatically on the first lookup. No explicit initialization is required. To load the index and geometry directory during application startup instead, call:

TimeZoneLookup.Initialize();

To override the embedded data, supply a compatible GeoTimeZoneData.zip produced by this project's data builder before any lookup or initialization:

using (var data = File.OpenRead("GeoTimeZoneData.zip"))
{
    await TimeZoneLookup.InitializeAsync(data);
}

string tz = TimeZoneLookup.GetTimeZone(50.4372, -3.5559).Result;

The synchronous equivalent is:

using (var data = File.OpenRead("GeoTimeZoneData.zip"))
{
    TimeZoneLookup.Initialize(data);
}

The stream overloads read from the current position, support nonseekable streams, and leave the stream open. The library retains its own copy of the internally compressed geometry archive, so the caller can close the stream afterward. Polygon components are still cached on demand.

Custom data is fully validated before it is selected. Failed or canceled loads allow a retry. InitializeAsync accepts an optional CancellationToken; await it before making lookups. Once initialization has started, supplying custom data throws InvalidOperationException. Repeated calls to the parameterless Initialize() are safe.

Usage Notes

This library returns IANA time zone IDs. If you need a Windows time zone ID, pass the return value into the TimeZoneConverter library's TZConvert.IanaToWindows method, or to TZConvert.GetTimeZoneInfo to get a TimeZoneInfo object in a platform-neutral manner.

This library uses the time zone border definitions from the Timezone Boundary Builder project, which in-turn derive from Open Street Map. As some international borders are the subject of dispute, the results may or may not align with your worldview. Use at your own risk.

Acknowledgements

Huge thank you to the following people:

  • Evan Siroky, who tirelessly maintains the Time Zone Boundary Builder project, which we use for our source data.
  • Eric Muller, who authored the original tz_world data set (now deprecated in favor of TBB).
  • Simon Bartlett, who contributed all the polygon indexing and lookup bits to this library.
  • Sharon Lourduraj, who wrote GeoHash-net that we used for our original implementation.
  • David Troy, who wrote Geohash-js that Sharon later ported to .NET
  • Nick Johnson, who's excellent blog post has been an inspiration to this project and so many others!
  • Jonas Nyrup, who has helped with performance optimizations.

License

This library is provided free of charge, under the terms of the MIT license.

About

Provides an IANA time zone identifier from latitude and longitude coordinates.

Resources

Stars

255 stars

Watchers

19 watching

Forks

Releases

Packages

Used by

Contributors

Languages