我怎么能确定由UTC时区偏移? [英] How can I determine a timezone by the UTC offset?

查看:170
本文介绍了我怎么能确定由UTC时区偏移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我有一个区时差(分钟),需要确定它的时区。我知道所有的数据不可用(例如,可能有几个时区与-240分钟偏移)而是一个最佳猜测是可以接受的。

I have a scenario where I have a timezone offset (in minutes) and need to determine the timezone for it. I know that all the data is not available (for example, there may be several timezones with an offset of -240 minutes) but a "best guess" is acceptable.

我第一遍是这样的:

foreach (var info in TimeZoneInfo.GetSystemTimeZones())
{
    if (info.BaseUtcOffset.TotalMinutes == timezoneOffset)
    {
         // do something here if this is a valid timezone
    }
}

这八九不离十工作,但我需要考虑夏令这是我扔了一些。我加了这个可怕的黑客:

This sorta works, but I need to account for daylight savings which is throwing me off somewhat. I added this terrible hack:

foreach (var info in TimeZoneInfo.GetSystemTimeZones())
{
    var extra = info.IsDaylightSavingTime(DateTime.Now) ? 60 : 0;
    if (info.BaseUtcOffset.TotalMinutes + extra == timezoneOffset)
    {
         // do something here if this is a valid timezone
    }
}

本作品不够好,因为我可以显示用户正确的时间为他们当夏令不影响和上午DST期间,约70%是正确的。还是......这是一些可怕的代码,以我的眼球。

This works "well enough" in that I can show the user the correct time for them when daylight savings is not in effect and am about 70% correct during DST. Still... this is some awful code to my eyeballs.

有没有更好的方法来做到这一点?更多的优雅将是一件好事,而且更准确会更好。

Is there a better way to do this? More elegance would be good, and more accuracy would be better still.

更新

Update

从技术上讲我有机会获得任何信息的JavaScript就可以的日期得到。我有我已经放在一个隐藏字段名为抵消的页面。我有一个jQuery函数与日期时间()填充偏移字段。使用getTimezoneOffset()。虽然我没有看到DateTime对象,这将有助于,也许这将打开其他渠道的想法上的任何东西。

Technically I have access to any information Javascript can get regarding the date. I have a page on which I've placed a hidden field called "offset". I have a JQuery function that populates the offset field with the DateTime().getTimezoneOffset(). While I don't see anything on the DateTime object that will help, perhaps this will open other avenues for ideas.

推荐答案

短回答:你不能

夏令时使它不可能。例如,有没有办法确定,单从UTC偏移量,在夏季亚利桑那州和加利福尼亚州之间的差异,或亚利桑那和新墨西哥冬季(因为亚利桑那不守DST)。

Daylight saving time make it impossible. For example, there is no way to determine, solely from UTC offset, the difference between Arizona and California in the summer, or Arizona and New Mexico in the winter (since Arizona does not observe DST).

有也是什么时间不同国家遵守DST的问题。例如,在美国DST启动较早,结束晚于欧洲。

There is also the issue of what time different countries observe DST. For example, in the US DST starts earlier and ends later than in Europe.

一个密切的猜测是可能的(即+/-一个小时),但如果您使用的是它显示的时间给用户,你将不可避免地显示错误的时间,其中的一些。

A close guess is possible (i.e. +/- an hour), but if you are using it to display time to users you will inevitably display the wrong time to some of them.

更新:从评论,它看起来像你的主要目标是显示在用户的本地时区的时间戳。如果这是你想做的事,你应该发送时间作为UTC时间戳记,然后只需重写它的用户是用JavaScript的浏览器。在他们没有启用的Javascript的情况下,他们仍然会看到可用的UTC时间戳。下面是我在这个问题,我在这个Greasemonkey的脚本。您可能需要调整,以满足您的需求。

Update: From the comments, it looks like your primary goal is to display a timestamp in the user's local timezone. If that is what you want to do, you should send the time as a UTC timestamp, and then just rewrite it on the user's browser with Javascript. In the case that they don't have Javascript enabled, they would still see a usable UTC timestamp. Here is a function I came up with in this question, which I used in this Greasemonkey script. You may want to tweak it to suit your needs.

//@param timestamp An ISO-8601 timestamp in the form YYYY-MM-DDTHH:MM:SS±HH:MM
//Note: Some other valid ISO-8601 timestamps are not accepted by this function
function parseISO8601(timestamp)
{
  var regex = new RegExp("^([\\d]{4})-([\\d]{2})-([\\d]{2})T([\\d]{2}):([\\d]{2}):([\\d]{2})([\\+\\-])([\\d]{2}):([\\d]{2})$");
  var matches = regex.exec(timestamp);
  if(matches != null)
  {
    var offset = parseInt(matches[8], 10) * 60 + parseInt(matches[9], 10);
    if(matches[7] == "-")
      offset = -offset;

    return new Date(
      Date.UTC(
        parseInt(matches[1], 10),
        parseInt(matches[2], 10) - 1,
        parseInt(matches[3], 10),
        parseInt(matches[4], 10),
        parseInt(matches[5], 10),
        parseInt(matches[6], 10)
      ) - offset*60*1000
    );
  }
  return null;
}

下面是我在我的博客使用一个函数来显示在一个解析时间戳用户的本地时区。同样,你可以把它调整到你想要的格式。

Here is a function I use on my blog to display a parsed timestamp in the user's local timezone. Again, you can tweak it to the format you want.

var weekDays = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
        "Thursday", "Friday", "Saturday");
var months = new Array("January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December");

function toLocalTime(date)
{
  var hour = date.getHours();
  var ampm = (hour < 12 ? "am" : "pm");
  hour = (hour + 11)%12 + 1;

  var minutes = date.getMinutes();
  if(minutes < 10)
    minutes = "0" + minutes;

  return weekDays[date.getDay()] + ", "
       + months[date.getMonth()] + " "
       + date.getDate()          + ", "
       + date.getFullYear()      + " at "
       + hour                    + ":"
       + minutes                 + " "
       + ampm;
}

这篇关于我怎么能确定由UTC时区偏移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆