在我的数据库中存储时区信息的最佳方法是什么?

时间:2022-11-02 20:07:59

I have a asp.net-mvc web site that i took over and there is a page page where people enter information and times (including local timezone). The database is persisting a start time, an end time and a timezone as it feels a bit flaky to me. here is the code to get the list to choose from:

我有一个asp.net-mvc网站,我接手了这个网站,那里有一个页面,人们输入信息和时间(包括本地时区)。这个数据库保存了一个开始时间、一个结束时间和一个时区,我觉得有点不太可靠。下面是要从列表中选择的代码:

  static private IEnumerable<SelectListItem> GetTimeZones(string selected)
    {
        var timeZoneNames = TimeZoneInfo.GetSystemTimeZones()
            .Where(tz => tz.DisplayName.Contains("London")
                         || tz.DisplayName.Contains("*")
                         || tz.DisplayName.Contains("Mumbai")
                         || tz.DisplayName.Contains("Eastern Time"))
            .ConvertAll(tz => tz.DisplayName).ToList();

        var items = new List<SelectListItem>();
        foreach (var item in timeZoneNames)
        {
            var slItem = new SelectListItem();
            slItem.Text = item;
            slItem.Value = item;
            slItem.Selected = item == selected;
            items.Add(slItem);
        }

        return items;
    }

So its simply storing the full string that is returned from TimeZoneInfo.GetSystemTimeZones()

它只是存储从TimeZoneInfo.GetSystemTimeZones()返回的完整字符串

Are there any flaws with this approach? I am a bit concerned reading here that this comes locally from the machine as what if different machines have different settings. Also, trying to figure out if everything is listed as UTC or GMT, etc . . Any better suggestions? For example, should i do the conversion to UTC on the website and normalize all of the times in the database?

这种方法有什么缺点吗?我有点担心在这里阅读,这是本地的机器,如果不同的机器有不同的设置。另外,试着弄清楚是否所有的东西都被列出为UTC或GMT,等等。什么更好的建议吗?例如,我是否应该在网站上转换到UTC,并在数据库中实现所有时间的规范化?

1 个解决方案

#1


19  

You should store the ID returned by TimeZoneInfo.Id. That's the identifier - the string which is meant to identify the time zone. Using the display name is a really bad idea, as that can vary by culture and is less likely to be stable.

您应该存储TimeZoneInfo.Id返回的ID。这就是标识符——用来标识时区的字符串。使用显示名称是一个非常糟糕的主意,因为这可能因文化而异,并且不太可能是稳定的。

You can then use TimeZoneInfo.FindSystemTimeZoneById to retrieve the zone from the ID.

然后可以使用TimeZoneInfo。FindSystemTimeZoneById从ID检索区域。

Admittedly I prefer TZDB for time zone handling, but that's a different matter :)

诚然,我更喜欢TZDB用于时区处理,但那是另一回事:)

#1


19  

You should store the ID returned by TimeZoneInfo.Id. That's the identifier - the string which is meant to identify the time zone. Using the display name is a really bad idea, as that can vary by culture and is less likely to be stable.

您应该存储TimeZoneInfo.Id返回的ID。这就是标识符——用来标识时区的字符串。使用显示名称是一个非常糟糕的主意,因为这可能因文化而异,并且不太可能是稳定的。

You can then use TimeZoneInfo.FindSystemTimeZoneById to retrieve the zone from the ID.

然后可以使用TimeZoneInfo。FindSystemTimeZoneById从ID检索区域。

Admittedly I prefer TZDB for time zone handling, but that's a different matter :)

诚然,我更喜欢TZDB用于时区处理,但那是另一回事:)