duannan3959 2013-04-17 20:39
浏览 48
已采纳

通过他们的lat / long / city获取用户当前时间

I'm using http://ipinfodb.com/ api in order to get users lat/long... I have users timezone in the format of an array. The array returns the following information...

array(11) { 
     ["statusCode"]=> string(2) "OK"... 
     ["countryCode"]=> string(2) "CA" 
     ["countryName"]=> string(6) "CANADA" 
     ["regionName"]=> string(16) "BRITISH COLUMBIA" 
     ["cityName"]=> string(8) "VANCOUVER" ....
     ["timeZone"]=> string(6) "-07:00" 
}

With the information from the array, how would i find the users current time when i have their timezone in the format of -07:00, 04:00... etc etc

  • 写回答

1条回答 默认 最新

  • dongqian0763 2013-04-17 21:25
    关注

    A "time zone" is NOT an offset! It bothers me that so many APIs make this common mistake. Don't be fooled by the fact that the return field is called "timeZone".

    A "time zone" is an region that can have one or more offsets throughout the year. You can resolve an offset from a time zone and a date, but you cannot go the other direction! A list of TZDB time zones is here.

    The specific time zone would be America/Vancouver - not because your lat/lon matched the city of Vancouver, but because Vancouver happens to be the reference city for the time zone of your location. Not every city has its own time zone id, so you can't make any assumptions.

    -07:00 is the current offset for the America/Vancouver time zone. It happens to be the daylight savings offset. When daylight savings ends, it will change to -08:00. It would appear that the particular web service you are using does not reveal this information.

    There are several other questions on StackOverflow that already answer how to resolve an actual time zone from a lat/lon. There are web services you can call, or you can use your own database. Here are some links:

    I'm sure there are more. I was tempted to close this as a duplicate, but I hadn't seen ipinfodb.com referenced before.

    If you are certain that your API returns the current offset, and you just want the user's current time, then as Jonathon pointed out in the question comments, you can just apply the offset to the current UTC time and you'll know what the time is in that location. But you'll have to do this every time. You can't save that offset and use it again later. For that, you need the full time zone reference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染