0

I need to be able to compute differences between two data/time values which represent instants in (potentially) two different geographic locations.

Each input value I have is a tuple of the following:

  • A date/time value with no timezone offset, for instance, 6:54 AM, 12/3/2005.
  • A latitude/longitude value (e.g., -33.8704, 151.1938) describing the location of the specific time.

From any combination of two input values, I need to compute the time difference (which may be minutes, days, years, etc.). To start with, I need to:

  • Normalise each of the input values into their relevant GMT (or Zulu) time. I imagine this can be done by converting lat/long values into a timezone. Once a related timezone is obtained, I then need to account for daylight savings differences according to the specified date to determine an accurate time.
  • Once the time values have been normalized, computing the difference is trivial.

Can anyone please suggest how I can:

  1. Convert a lat/long pair into a timezone, and
  2. Determine the daylight savings offset for a given timezone and a date.

I imagine I need some function for (1) and a database of values for (2), but I do not know where I can get these, or if they are freely available.

Other constraints:

  • I would like software in Java or .Net (as my software application can use plugins written in either), and
  • I really would like all processing to be local. I am aware that there exist several web services out there to compute the timezone for a lat/long pair (as discussed on this Stackoverflow question such as Geonames.org or DRTEngine), but given the amount of data I have, I do not want to rely on or hit third party web services for every data point I have, so I'd prefer 'free' software and access to all the right info locally.

UPDATE: Thanks to all those who provided answers so far. I am currently trying to tackle this problem by using the GeoNames data and performing a nearest-neighbor calculation to determine a timezone from a lat/long, and will focus on using Joda Time and the associated latest tz database for computing daylight savings offsets for particular dates.

Community
  • 1
  • 1
antonsyd
  • 5
  • 4
  • It's a pretty big assumption that the lat/long correspond to a particular timezone - what if someone's travelled to a different country but has left their device/computer set to their home timezone? – Damien_The_Unbeliever Mar 30 '11 at 07:24
  • 1
    @Damien_The_Unbeliever: I am not making an assumption. The data I am concerned with here are generated from software operating on stationary sensors which log observations in local time. It is only these data I am concerned with integrating with other data which have correct timezone information from other sources. – antonsyd Mar 30 '11 at 09:49

2 Answers2

2

Once you've got a time zone, you don't need to perform the DST checking yourself - the Java TimeZone, Joda Time DateTimeZone and .NET TimeZoneInfo classes will take care of that for you.

(If you're using Java, I'd strongly recommend using Joda Time instead of the built-in calendar classes.)

I don't know of any libraries to convert a lat/long to a time zone, I'm afraid. I'm sure there must be something available, but I don't know it offhand. Note that any such library is likely to give you a zoneinfo style name (e.g. "Europe/London") which isn't what the .NET TimeZoneInfo class uses, unfortunately. If Noda Time were ready, I'd suggest using that instead of .NET, but we haven't finished it yet :(

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks Jon. Looking at Joda time I've learnt about it's use of (and the existence of) the [tz database](http://www.twinsun.com/tz/tz-link.htm), which helps me answer my question (2) - fantastic! :) – antonsyd Mar 30 '11 at 09:43
1

Just a dumb question... are you sure that the timestamps you have aren't in GMT/Z/UTC already. That seems more logical if this data is from a single source, or even multiple similar sources. If so a fair bit of your problem just disappeared.

If the data is from a collection of static sites (i.e. weather stations) then you can use one of the suggested web services to look up the timezone and then cache the result locally. Admittedly, daylight savings may cause issues with this as the timezone would change dependent on the date.

Michael Rutherfurd
  • 13,815
  • 5
  • 29
  • 40
  • Date is in UTC, but Calendar has a TimeZone. Using Date globally is simpler if you can do that. – Peter Lawrey Mar 30 '11 at 08:20
  • 1
    Not a dumb question at all. Unfortunately, of all the raw data I am dealing with, I am sure that some subsets are expressed against local time (which were generated by certain software which has this behavior). It is these which I am hoping to incorporate into the whole (cleaned) data set. I am hoping for a software solution because I have many of these such data, although if worse comes to worst, I suppose I could always do it manually... :( – antonsyd Mar 30 '11 at 09:09