How to get timezone from airport code (IATA/FAA)

后端 未结 5 1947
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 04:25

I am trying to make a PHP function that returns the UTC timezone for a given airport code (IATA/FAA).

What the function should do is something like this:

<         


        
相关标签:
5条回答
  • 2020-12-14 04:47

    I would recommend the FAA's "NASR" database, which is a 56-day dataset provided by the FAA. The database is a set of raw text files using fixed-width ASCII columns. Format description files are provided that show what each column is.

    One of the files in that dataset is APT.txt which contains a time zone field for each airport; see the format description for APT.txt to find the column offsets. It may sound intimidating but it's very easy to parse.

    Note also that this is the "official" raw navigation data product offered by the FAA. It's "from the horse's mouth" so to speak.

    [EDIT: This database only covers US airports; see comments below.]

    0 讨论(0)
  • 2020-12-14 04:51

    You are confusing a "time zone" with a "time zone offset". They are not the same thing. You can't just ask for the offset at a location - you also would need to know the specific time in question. It's invalid to ask "What's the time zone offset for LAX" - because in the winter it will be -8 while in the summer it will be -7.

    You can ask "what is the offset at this location right now", but that might give you a different answer depending on when you ask. So the date and time have to be part of the input.

    What you really need to know instead is that LAX corresponds to the IANA time zone of America/Los_Angeles. These time zone codes are understood by the PHP date/time API, and many other systems. (.NET Windows users can convert these to Microsoft Windows time zones using my TimeZoneConverter library.)

    To do this, you need to take the latitude and longitude of each airport (which are available from OpenFlights and many other places), and use one of the methods described here to lookup the IANA time zone for those coordinates.

    I've already written code for this in C#, which you can find here. The output is included in a .csv file, which you can parse and use in any language you like. It includes both IANA and Microsoft time zones.

    Be sure to also read the timezone tag wiki, especially the parts on the IANA database, and the section titled "Time Zone != Offset".

    Also note that while the lat/lon coordinates in the OpenFlights database are great, the time zone data in that file is not very accurate, so going through the process I described is important. When it comes to the specifics of time zones, you should only trust the IANA database and make sure you keep updated.

    0 讨论(0)
  • 2020-12-14 04:53

    Hmmm..perhaps you can make a converter yourself - it should be simple enough with a small database table mapping airport codes to timezones.

    I found the following link containing a CSV file of over 5,000 airport codes and their timezone relative to UTC.

    http://openflights.org/data.html

    You can import the CSV from that link into your own database and then have your code work around the timezones and airports in that table.

    0 讨论(0)
  • 2020-12-14 04:55

    I've managed to find a solution to the issue. Through the flightstats.com API it is possible to get a free but limited access to a complete airport database: https://developer.flightstats.com/api-docs/airports/v1

    The API returns all active/inactive airports in the following format:

    {
       "fs": "LAX",
       "iata": "LAX",
       "icao": "KLAX",
       "faa": "LAX",
       "name": "Los Angeles International Airport",
       "street1": "One World Way",
       "street2": "",
       "city": "Los Angeles",
       "cityCode": "LAX",
       "stateCode": "CA",
       "postalCode": "90045-5803",
       "countryCode": "US",
       "countryName": "United States",
       "regionName": "North America",
       "timeZoneRegionName": "America/Los_Angeles",
       "weatherZone": "CAZ041",
       "localTime": "2014-06-20T06:00:50.439",
       "utcOffsetHours": -7,
       "latitude": 33.943399,
       "longitude": -118.408279,
       "elevationFeet": 126,
       "classification": 1,
       "active": true,
       "delayIndexUrl": "https://api.flightstats.com/flex/delayindex/rest/v1/json/airports/LAX?codeType=fs",
       "weatherUrl": "https://api.flightstats.com/flex/weather/rest/v1/json/all/LAX?codeType=fs"
    }
    

    This was exactly the data I needed to be able to make my function:

    echo getTimezoneFromAirportCode("LAX"); // -7
    

    The data is available through the following GET request:

    https://api.flightstats.com/flex/airports/rest/v1/json/all?appId=[appId]&appKey=[appKey]
    

    [appId] and [appKey] will be provided after creating a free flightstats.com developer account here: https://developer.flightstats.com/signup

    0 讨论(0)
  • 2020-12-14 04:57

    Seeing as the question is still open I'd like to point to my very own timezone map files. For your particular purpose, the IATA tzmap seems perfect:

    https://raw.github.com/hroptatyr/dateutils/tzmaps/iata.tzmap

    Obviously, you'd have to snarf the offset in question (it depends on a date as it might change over time) from the zoneinfo files, e.g. with zdump(1).

    0 讨论(0)
提交回复
热议问题