미국 우편 번호를 시간대에 매핑
사용자가 앱에 등록 할 때 국가 데이터베이스에 대해 유효성을 검사 할 때 우편 번호를 유추 할 수 있습니다. 이 우편 번호에서 시간대를 추측 할 수있는 가장 좋은 방법은 무엇입니까?
우리는 명시 적으로 요청해야하는 데이터의 양을 최소화하려고 노력하고 있습니다. 최선의 추측이 잘못된 경우 나중에 수동으로 시간대를 설정할 수 있습니다. 나는 우편 번호가 미국 이외의 시간대를 알아내는 데 도움이되지 않는다는 것을 알고 있지만,이 경우 어쨌든 수동으로 물어봐야 할 것이고 우리는 상관없이 주로 미국과 거래합니다.
많은 우편 번호 데이터베이스를 찾았고 지금까지 몇 개만 시간대 정보를 포함하고 있지만 이것 처럼 무료가 아닌 것은 . 이를 위해 서비스 구독료를 반드시 지불해야하는 경우에는 그만한 가치가 없으며 사용자에게 명시 적으로 요청해야합니다.
언어는 필요에 따라 변환 할 수 있으므로 특별히 관련이 없지만 PHP와 MySQL을 사용하고 있습니다.
시간 오프셋과 DST 참여가 포함 된 무료 zip 데이터베이스 를 방금 찾았습니다 . 저는 Erik J의 대답을 좋아합니다. 오프셋이 아닌 실제 시간대를 선택하는 데 도움이되기 때문입니다 (규칙에 대해 완전히 확신 할 수 없기 때문입니다).하지만 저는 이것부터 시작하여 시도해 보도록하겠습니다. 오프셋 / dst 구성을 기반으로 최상의 시간대 일치를 찾습니다. 나는 온전한 테스트로 zip 정보에서 얻은 것을 확인하기 위해 Development 4.0의 답변의 간단한 버전을 설정하려고 할 수 있다고 생각합니다. 내가 바라는 것만 큼 간단하지는 않지만 조합을 통해 사용자의 시간대를 90 % 이상 확신 할 수 있습니다.
대부분의 주가 정확히 하나의 시간대에 있습니다 (몇 가지 예외가 있음). 대부분의 우편 번호는 주 경계를 넘지 않습니다 (몇 가지 예외가 있지만).
이러한 사실을 결합하여 zip 당 시간대 목록을 신속하게 만들 수 있습니다.
다음 은 주별 우편 번호 범위 목록과 시간 대별 주 목록입니다 .
우편 번호의 경계를 확인 하고이 링크 또는 Google 어스를 사용하여 시간대지도와 비교하여 시간대로 구분 된 주에 대한 시간대에 우편 번호 를 매핑 할 수 있습니다.
미국 외 국가의 대부분은 아마도 정확히 하나의 시간대에있을 것입니다. 미국 이외의 상위 N 명의 방문자가 어디에서 왔는지 확인하고 시간대 만 검색 할 수 있습니다.
우편 번호와 시간대를 상호 참조하는 오픈 소스 (MIT 라이선스) MySQL 데이터베이스 테이블을 만들어 sourceforge.net에 업로드했습니다.
http://sourceforge.net/projects/zip2timezone/files/
4 개 위치에서 제공됩니다 (기본 Yahoo PlaceFinder API-@Chris N 감사합니다).
자세한 내용과 지침은 README 파일을 참조하십시오.
원한다면 브라우저에 Josh Fraser가 여기에 멋진 글을 써 놓았 음을 요청하여 시간대에 대한 느낌을 얻을 수도 있습니다.
var rightNow = new Date();
var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
var temp = jan1.toGMTString();
var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
두 번째로 알아야 할 것은 해당 위치가 일광 절약 시간 (DST)을 준수하는지 여부입니다. DST는 항상 여름에 관찰되기 때문에 1 월의 두 날짜 간의 시간 오프셋을 6 월의 두 날짜 간의 시간 오프셋과 비교할 수 있습니다. 오프셋이 다르면 위치가 DST를 준수한다는 것을 압니다. 오프셋이 같으면 위치가 DST를 준수하지 않는다는 것을 압니다.
var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
temp = june1.toGMTString();
var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
var dst;
if (std_time_offset == daylight_time_offset) {
dst = "0"; // daylight savings time is NOT observed
} else {
dst = "1"; // daylight savings time is observed
}
이것에 대한 모든 크레딧은 Josh Fraser에게 있습니다.
이것은 미국 이외의 고객에게 도움이 될 수 있으며 우편 접근 방식을 보완 할 수 있습니다.
다음은 자바 스크립트에서 시간대를 얻는 방법에 대한 질문입니다.
Google에는 이에 대한 특정 API가 있습니다 : https://developers.google.com/maps/documentation/timezone/
예 : https://maps.googleapis.com/maps/api/timezone/json?location=40.704822,-74.0137431×tamp=0
{
dstOffset: 0,
rawOffset: -18000,
status: "OK",
timeZoneId: "America/New_York",
timeZoneName: "Eastern Standard Time"
}
쿼리 문자열에 유닉스 타임 스탬프가 필요합니다. 반환 된 응답 timeZoneName
에서 타임 스탬프를 기준으로 일광 절약 시간을 고려한 timeZoneId
것으로 보이며은 시간대에 대한 DST 독립적 인 이름입니다.
필자가 사용하기 위해 Python timestamp=0
에서 timeZoneId
값을 전달 하고 사용하여 pytz 에서 tz_info
객체를 가져 오면 이를 사용하여 내 코드에서 특정 날짜 시간을 지역화 할 수 있습니다.
I believe for PHP similarly you can find "America/New_York" in http://pecl.php.net/package/timezonedb
Ruby gem to convert zip code to timezone: https://github.com/Katlean/TZip (forked from https://github.com/farski/TZip).
> ActiveSupport::TimeZone.find_by_zipcode('90029')
=> "Pacific Time (US & Canada)"
It's fast, small, and has no external dependencies, but keep in mind that zip codes just don't map perfectly to timezones.
I recently made a Ruby hash of all the zipcodes in the US and their associated timezones that you could use if you're interested. Should be pretty easy to reverse engineer for your purposes.
I've been working on this problem for my own site and feel like I've come up with a pretty good solution
1) Assign time zones to all states with only one timezone (most states)
and then either
2a) use the js solution (Javascript/PHP and timezones) for the remaining states
or
2b) use a database like the one linked above by @Doug.
This way, you can find tz cheaply (and highly accurately!) for the majority of your users and then use one of the other, more expensive methods to get it for the rest of the states.
Not super elegant, but seemed better to me than using js or a database for each and every user.
In addition to Doug Kavendek answer. One could use the following approach to get closer to tz_database.
- Download [Free Zip Code Latitude and Longitude Database]
- Download [A shapefile of the TZ timezones of the world]
- Use any free library for shapefile querying (e.g. .NET Easy GIS .NET, LGPL).
var shapeFile = new ShapeFile(shapeFilePath); var shapeIndex = shapeFile.GetShapeIndexContainingPoint(new PointD(long, lat), 0D); var attrValues = shapeFile.GetAttributeFieldValues(shapeIndex); var timeZoneId = attrValues[0];
P.S. Can't insert all the links :( So please use search.
See these links for the Olson database:
- http://en.wikipedia.org/wiki/Tz_database
- ftp://ftp.iana.org/tz/tz-link.html (yes,
ftp
)http://www.twinsun.com/tz/tz-link.htm - ftp://ftp.iana.org/tz/releases
ftp://elsie.nci.nih.gov/pub/ - https://iana.org/time-zones
I maintain a zipcode-to-timezone API called Zipgenius which uses the data provided by @chriv provided above. Feel free to use it for your own needs.
Also note, that if you happen to be using Yahoo geocoding service you can have timezone information returned to you by setting the correct flag.
http://developer.yahoo.com/geo/placefinder/guide/requests.html#flags-parameter
This will download a yaml file that will map all timezones to an array of their zipcodes:
curl https://gist.githubusercontent.com/anonymous/01bf19b21da3424f6418/raw/0d69a384f55c6f68244ddaa07e0c2272b44cb1de/timezones_to_zipcodes.yml > timezones_to_zipcodes.yml
e.g.
"Eastern Time (US & Canada)" => ["00100", "00101", "00102", "00103", "00104", ...]
"Central Time (US & Canada)" => ["35000", "35001", "35002", "35003", "35004", ...]
etc...
If you prefer the shortened timezones, you can run this one:
curl https://gist.githubusercontent.com/anonymous/4e04970131ca82945080/raw/e85876daf39a823e54d17a79258b170d0a33dac0/timezones_to_zipcodes_short.yml > timezones_to_zipcodes.yml
e.g.
"EDT" => ["00100", "00101", "00102", "00103", "00104", ...]
"CDT" => ["35000", "35001", "35002", "35003", "35004", ...]
etc...
There's actually a great Google API for this. It takes in a location and returns the timezone for that location. Should be simple enough to create a bash or python script to get the results for each address in a CSV file or database then save the timezone information.
https://developers.google.com/maps/documentation/timezone/start
Request Endpoint:
https://maps.googleapis.com/maps/api/timezone/json?location=38.908133,-77.047119×tamp=1458000000&key=YOUR_API_KEY
Response:
{
"dstOffset" : 3600,
"rawOffset" : -18000,
"status" : "OK",
"timeZoneId" : "America/New_York",
"timeZoneName" : "Eastern Daylight Time"
}
참고URL : https://stackoverflow.com/questions/2456324/mapping-us-zip-code-to-time-zone
'code' 카테고리의 다른 글
기능과 레이블의 차이점은 무엇입니까? (0) | 2020.10.09 |
---|---|
람다 함수 및 매개 변수의 범위? (0) | 2020.10.09 |
Windows 7에 32 비트 및 64 비트 Java를 모두 설치할 수 있습니까? (0) | 2020.10.08 |
Vim에서 위치 목록과 Quickfix 목록의 차이점은 무엇입니까 (0) | 2020.10.08 |
Zepto와 jQuery 2의 차이점은 무엇입니까? (0) | 2020.10.08 |