什么是存储在我的数据库时区信息的最佳方式是什么? [英] What is the best way to store timezone information in my DB?

查看:225
本文介绍了什么是存储在我的数据库时区信息的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net MVC的网站,我接手并没有人们输入信息和时间(包括本地时区)的网页页面。该数据库是一个持续的开始时间,结束时间和时区,因为它感觉有点片状我。这里是代码以获取列表中进行选择:

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:

  static private IEnumerable<SelectListItem> GetTimeZones(string selected)
    {
        var timeZoneNames = TimeZoneInfo.GetSystemTimeZones()
            .Where(tz => tz.DisplayName.Contains("London")
                         || tz.DisplayName.Contains("Hong Kong")
                         || 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;
    }



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

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

有没有使用这种方法什么破绽?我有点担心这里阅读的,这从机器自带的什么地方如果不同的机器有不同的设置。此外,试图如果一切被列为UTC或GMT,等弄清楚。 。什么更好的建议?例如,我应该做转换为UTC在网站上,规范各个时代的数据库?

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?

推荐答案

您应该存储由 TimeZoneInfo.Id 。这是该标识符 - 这是为了的确定的时区字符串。 。使用显示名称是一个非常糟糕的主意,因为这可以通过文化有所不同,不太可能是稳定的。

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.FindSystemTimeZoneById 检索从ID区域

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

诚然,我宁愿TZDB时区的处理,但是这是一个不同的问题:)

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

这篇关于什么是存储在我的数据库时区信息的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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