zoukankan      html  css  js  c++  java
  • .NET时区相关操作

    TimeZoneInfo

    TimeZoneInfo是一个关于时区的静态类,可以使得我们方便的操作与时区相关的信息
    对于一个开放于全球的网站或服务,在时间上的显示是一个问题,因为各个国家都会有所谓的时差,好在 .Net提供了 TimeZoneInfo 类来解决这个问题。
    TimeZoneInfo 类的方法可用于将一个时区的时间转换为其他任意时区的相应时间

    静态成员

    • CreateCustomTimeZone()使用应用程序提供的数据创建自定义时区。 CreateCustomTimeZone 方法创建在本地系统注册表中未定义的时区。然后可使用 ToSerializedString 属性将时区对象的信息保存为字符串,该字符串可以以应用程序可访问的某种格式存储。使用 FromSerializedString 方法可将序列化字符串转换回 TimeZoneInfo 对象
    • FindSystemTimeZoneById()根据时区的标识符实例化在系统注册表中定义的时区。
    • FromSerializedString()反序列化一个字符串值,以重新创建先前已序列化的 TimeZoneInfo 对象。
    • GetSystemTimeZones()返回 TimeZoneInfo 对象的可枚举的 ReadOnlyCollection<(Of <(T>)>),它表示本地系统上可用的所有时区。
    • Local 属性:实例化表示本地时区的 TimeZoneInfo 对象。
    • Utc 属性:实例化表示 UTC 时区的 TimeZoneInfo 对象。

    案例

    TimeZoneInfo 的使用很简单,一开始利用 FindSystemTimeZoneById 找到对应的 TimeZoneInfo 对象,就可以利用该对象来转换时间。下列程序是示范如何将 UTC Time 转为东京的当地间时。

    TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
    Console.WriteLine(string.Format("UTC Time:{0}", time.ToString()));
    Console.WriteLine(string.Format("Tokyo Time:{0}", TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, timeZoneInfo)));
    

    或许有人会问,该如何知道各个地区所对应的 id,其实利用 TimeZoneInfo.GetSystemtimeZones 就可以了。

    var list =  TimeZoneInfo.GetSystemTimeZones();
                foreach (var timeZoneInfo in list)
                {
                    Console.WriteLine(timeZoneInfo.DisplayName);
                    Console.WriteLine(timeZoneInfo.Id);
                }
    

    时区类型

    Windows时区,在Windows平台实现,例:"China Standard Time"
    Olson(奥尔森)时区,在Linux,Mac,IOS,Andorid,JavaScript,Java,PHP和许多其他平台上广泛实现,例:"Asia/Shanghai"
    两种类型可以互转,需要用Windows时区与国际时区的映射表来确认当前国际时区id
    通过olson时区获取TimeZoneInfo:

    /// <summary>
    /// Converts an Olson time zone ID to a Windows time zone ID.
    /// </summary>
    /// <param name="olsonTimeZoneId">An Olson time zone ID. See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html. </param>
    /// <returns>
    /// The TimeZoneInfo corresponding to the Olson time zone ID, 
    /// or null if you passed in an invalid Olson time zone ID.
    /// </returns>
    /// <remarks>
    /// See http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/zone_tzid.html
    /// </remarks>
    public static TimeZoneInfo OlsonTimeZoneToTimeZoneInfo(string olsonTimeZoneId)
    {
        var olsonWindowsTimes = new Dictionary<string, string>()
        {
            { "Africa/Bangui", "W. Central Africa Standard Time" },
            { "Africa/Cairo", "Egypt Standard Time" },
            { "Africa/Casablanca", "Morocco Standard Time" },
            { "Africa/Harare", "South Africa Standard Time" },
            { "Africa/Johannesburg", "South Africa Standard Time" },
            { "Africa/Lagos", "W. Central Africa Standard Time" },
            { "Africa/Monrovia", "Greenwich Standard Time" },
            { "Africa/Nairobi", "E. Africa Standard Time" },
            { "Africa/Windhoek", "Namibia Standard Time" },
            { "America/Anchorage", "Alaskan Standard Time" },
            { "America/Argentina/San_Juan", "Argentina Standard Time" },
            { "America/Asuncion", "Paraguay Standard Time" },
            { "America/Bahia", "Bahia Standard Time" },
            { "America/Bogota", "SA Pacific Standard Time" },
            { "America/Buenos_Aires", "Argentina Standard Time" },
            { "America/Caracas", "Venezuela Standard Time" },
            { "America/Cayenne", "SA Eastern Standard Time" },
            { "America/Chicago", "Central Standard Time" },
            { "America/Chihuahua", "Mountain Standard Time (Mexico)" },
            { "America/Cuiaba", "Central Brazilian Standard Time" },
            { "America/Denver", "Mountain Standard Time" },
            { "America/Fortaleza", "SA Eastern Standard Time" },
            { "America/Godthab", "Greenland Standard Time" },
            { "America/Guatemala", "Central America Standard Time" },
            { "America/Halifax", "Atlantic Standard Time" },
            { "America/Indianapolis", "US Eastern Standard Time" },
            { "America/Indiana/Indianapolis", "US Eastern Standard Time" },
            { "America/La_Paz", "SA Western Standard Time" },
            { "America/Los_Angeles", "Pacific Standard Time" },
            { "America/Mexico_City", "Mexico Standard Time" },
            { "America/Montevideo", "Montevideo Standard Time" },
            { "America/New_York", "Eastern Standard Time" },
            { "America/Noronha", "UTC-02" },
            { "America/Phoenix", "US Mountain Standard Time" },
            { "America/Regina", "Canada Central Standard Time" },
            { "America/Santa_Isabel", "Pacific Standard Time (Mexico)" },
            { "America/Santiago", "Pacific SA Standard Time" },
            { "America/Sao_Paulo", "E. South America Standard Time" },
            { "America/St_Johns", "Newfoundland Standard Time" },
            { "America/Tijuana", "Pacific Standard Time" },
            { "Antarctica/McMurdo", "New Zealand Standard Time" },
            { "Atlantic/South_Georgia", "UTC-02" },
            { "Asia/Almaty", "Central Asia Standard Time" },
            { "Asia/Amman", "Jordan Standard Time" },
            { "Asia/Baghdad", "Arabic Standard Time" },
            { "Asia/Baku", "Azerbaijan Standard Time" },
            { "Asia/Bangkok", "SE Asia Standard Time" },
            { "Asia/Beirut", "Middle East Standard Time" },
            { "Asia/Calcutta", "India Standard Time" },
            { "Asia/Colombo", "Sri Lanka Standard Time" },
            { "Asia/Damascus", "Syria Standard Time" },
            { "Asia/Dhaka", "Bangladesh Standard Time" },
            { "Asia/Dubai", "Arabian Standard Time" },
            { "Asia/Irkutsk", "North Asia East Standard Time" },
            { "Asia/Jerusalem", "Israel Standard Time" },
            { "Asia/Kabul", "Afghanistan Standard Time" },
            { "Asia/Kamchatka", "Kamchatka Standard Time" },
            { "Asia/Karachi", "Pakistan Standard Time" },
            { "Asia/Katmandu", "Nepal Standard Time" },
            { "Asia/Kolkata", "India Standard Time" },
            { "Asia/Krasnoyarsk", "North Asia Standard Time" },
            { "Asia/Kuala_Lumpur", "Singapore Standard Time" },
            { "Asia/Kuwait", "Arab Standard Time" },
            { "Asia/Magadan", "Magadan Standard Time" },
            { "Asia/Muscat", "Arabian Standard Time" },
            { "Asia/Novosibirsk", "N. Central Asia Standard Time" },
            { "Asia/Oral", "West Asia Standard Time" },
            { "Asia/Rangoon", "Myanmar Standard Time" },
            { "Asia/Riyadh", "Arab Standard Time" },
            { "Asia/Seoul", "Korea Standard Time" },
            { "Asia/Shanghai", "China Standard Time" },
            { "Asia/Singapore", "Singapore Standard Time" },
            { "Asia/Taipei", "Taipei Standard Time" },
            { "Asia/Tashkent", "West Asia Standard Time" },
            { "Asia/Tbilisi", "Georgian Standard Time" },
            { "Asia/Tehran", "Iran Standard Time" },
            { "Asia/Tokyo", "Tokyo Standard Time" },
            { "Asia/Ulaanbaatar", "Ulaanbaatar Standard Time" },
            { "Asia/Vladivostok", "Vladivostok Standard Time" },
            { "Asia/Yakutsk", "Yakutsk Standard Time" },
            { "Asia/Yekaterinburg", "Ekaterinburg Standard Time" },
            { "Asia/Yerevan", "Armenian Standard Time" },
            { "Atlantic/Azores", "Azores Standard Time" },
            { "Atlantic/Cape_Verde", "Cape Verde Standard Time" },
            { "Atlantic/Reykjavik", "Greenwich Standard Time" },
            { "Australia/Adelaide", "Cen. Australia Standard Time" },
            { "Australia/Brisbane", "E. Australia Standard Time" },
            { "Australia/Darwin", "AUS Central Standard Time" },
            { "Australia/Hobart", "Tasmania Standard Time" },
            { "Australia/Perth", "W. Australia Standard Time" },
            { "Australia/Sydney", "AUS Eastern Standard Time" },
            { "Etc/GMT", "UTC" },
            { "Etc/GMT+11", "UTC-11" },
            { "Etc/GMT+12", "Dateline Standard Time" },
            { "Etc/GMT+2", "UTC-02" },
            { "Etc/GMT-12", "UTC+12" },
            { "Europe/Amsterdam", "W. Europe Standard Time" },
            { "Europe/Athens", "GTB Standard Time" },
            { "Europe/Belgrade", "Central Europe Standard Time" },
            { "Europe/Berlin", "W. Europe Standard Time" },
            { "Europe/Brussels", "Romance Standard Time" },
            { "Europe/Budapest", "Central Europe Standard Time" },
            { "Europe/Dublin", "GMT Standard Time" },
            { "Europe/Helsinki", "FLE Standard Time" },
            { "Europe/Istanbul", "GTB Standard Time" },
            { "Europe/Kiev", "FLE Standard Time" },
            { "Europe/London", "GMT Standard Time" },
            { "Europe/Minsk", "E. Europe Standard Time" },
            { "Europe/Moscow", "Russian Standard Time" },
            { "Europe/Paris", "Romance Standard Time" },
            { "Europe/Sarajevo", "Central European Standard Time" },
            { "Europe/Warsaw", "Central European Standard Time" },
            { "Indian/Mauritius", "Mauritius Standard Time" },
            { "Pacific/Apia", "Samoa Standard Time" },
            { "Pacific/Auckland", "New Zealand Standard Time" },
            { "Pacific/Fiji", "Fiji Standard Time" },
            { "Pacific/Guadalcanal", "Central Pacific Standard Time" },
            { "Pacific/Guam", "West Pacific Standard Time" },
            { "Pacific/Honolulu", "Hawaiian Standard Time" },
            { "Pacific/Pago_Pago", "UTC-11" },
            { "Pacific/Port_Moresby", "West Pacific Standard Time" },
            { "Pacific/Tongatapu", "Tonga Standard Time" }
        };
    
        var windowsTimeZoneId = default(string);
        var windowsTimeZone = default(TimeZoneInfo);
        if (olsonWindowsTimes.TryGetValue(olsonTimeZoneId, out windowsTimeZoneId))
        {
            try { windowsTimeZone = TimeZoneInfo.FindSystemTimeZoneById(windowsTimeZoneId); }
            catch (TimeZoneNotFoundException) { }
            catch (InvalidTimeZoneException) { }
        }
        return windowsTimeZone;
    }
    

    夏令时:

    夏令时是指某个时区在春季将时间向前移动(通常为一小时),然后在秋季向后移动的时间段。
    并非每个时区都会转换一小时。例如, Australia/Lord_Howe 时区仅偏移30分钟

    参考:
    https://www.cnblogs.com/wuxiaoqian726/archive/2011/03/19/1988931.html

  • 相关阅读:
    JDOJ 2430: 组合数取模
    USACO Dual Palindromes
    洛谷 P1365 WJMZBMR打osu! / Easy
    洛谷 P6154 游走
    洛谷 P1654 OSU!
    洛谷 P3802 小魔女帕琪
    洛谷 P1784 数独
    POJ King Arthur's Birthday Celebration
    洛谷 P4550 收集邮票
    洛谷 SP1026 FAVDICE
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/14145694.html
Copyright © 2011-2022 走看看