显示时区缩写 [英] display time zone abbreviation

查看:154
本文介绍了显示时区缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用.net Framework 2.0,并且想显示时区缩写.阿拉伯标准时间为AST,但下面的代码显示了完整的时区名称
请帮忙

Hi,

I am using .net framework 2.0 and i want to display time zone abbreviation eg. Arabian Standard Time as AST but my code below displays the full timezone name
Please help

<div style="float:right"><asp:Label ID="Label1" runat="server" ><%=DateTime.Now.ToLongDateString()+" "+DateTime.Now.ToLongTimeString()+TimeZone.CurrentTimeZone.StandardName%></asp:Label></div>

推荐答案

首先,使用类System.TimeZoneInfo代替System.TimeZone,如上一个MSDN帮助页面中所建议的那样.
您当前的时区由System.TimeZoneInfo.Local返回;相同时区的名称由属性DaylightNameDisplayNameIdStandardName返回.

请参阅 http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx [^ ] .

哪个名称代表缩写形式!没有人!System.TimeZone一样.为什么?

我认为这是可以理解的.查看时区的所有缩写名称列表:
http://www.timeanddate.com/library/abbreviations/timezones/ [缩写形式对识别没有用,因此应尽量避免使用缩写形式,因为它们会引起混淆.即使使用缩写形式的名称,也应使用UTC表示法,以免产生歧义.

众所周知,.NET不支持缩写形式.我认为-有充分的理由.

有一个旧的W3C RFC822标准
http://www.w3.org/Protocols/rfc822/#z28 [ ^ ](1977年),描述了几个缩写,涵盖了只有我们;并且此子集不会造成歧义.

关于各种黑客的不同讨论,这些黑客试图通过自动缩写标准名称来创建缩写名称.我不想引用这样的代码;您可以轻松找到它们.没有什么可以保证它们匹配已知的缩写名称.可以使用上面引用的表,使用System.TimeZoneInfo.GetSystemTimeZones列出所有时区,手动键入缩写名称,以System.Collections.Generic.Dictionary<System.TimeZoneInfo, string>的形式创建查找表; (由于上述歧义,因此不能相反)将其填充一次并保留在磁盘上.在运行时,您可以从文件中填充字典,并使用它来按真实"时区查找缩写名称.我不建议浪费时间.

—SA
First, using class System.TimeZoneInfo instead of System.TimeZone as it is recommended in the MSDN help page of the last one.

You current time zone is returned by System.TimeZoneInfo.Local; the names of the same time zone are returned by the properties DaylightName, DisplayName, Id and StandardName.

See http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx[^].

Which of the names represent the abbreviated form! No one! Same thing as with System.TimeZone. Why?

I think it is understandable. Look at the list of all abbreviated names of the time zones:
http://www.timeanddate.com/library/abbreviations/timezones/[^].

Your can see that there is no one-to-one correspondence between abbreviations and time zones. More exactly, more than one time zone can be mapped onto the same abbreviated form; for example, "AMT" means UTC+4 (Armenia Time) and UTC-4 (Amazon Time) at the same time. Same thing with "EDT" and other abbreviations. On other words, abbreviated forms are useless for identification purposes, so their use should be best avoided as they can cause confusion. Even if you use abbreviated form of the name, it should be accompanied by UTC notation to avoid ambiguity.

It is known that .NET does not support abbreviated forms; I think — for a good reason.

There is an old W3C RFC822 standard http://www.w3.org/Protocols/rfc822/#z28[^] (1977) which described several abbreviations which cover only US; and this subset does not create ambiguity.

There are different discussion on various hacks which attempt to create the abbreviated name by automatic abbreviation of the standard names. I don''t want to reference such codes; you can easily find them. Nothing can guarantee they match knows abbreviated names. One can use the table referenced above, list all time zones using System.TimeZoneInfo.GetSystemTimeZones, manually type abbreviated names, create a look-up table in the form of System.Collections.Generic.Dictionary<System.TimeZoneInfo, string>; (not the other way around, due to ambiguity described above), populate it once and persist on the disk. During run-time, you can populate the dictionary from the file and use it to find abbreviated name by the "real" time zone. I would not recommend to waste time on it.

—SA


不幸的是,我没有一种简单的内置方法可以做到这一点.但是,您可以自己整理一些东西.这是一个示例:


Unfortunately, there is no easy built-in way of doing this that I know of. However, you could put something together yourself. Here''s an example:


public static class zoneShort
    {
        public static string Convertion(this TimeZoneInfo Source)
        {
            var Map = new Dictionary<string, string>()
            {
            {"india standard time","ist"},
            {"eastern standard time","est"},
            {"mountain standard time","mst"},
            {"central standard time","cst"},
            {"pacific standard time","pst"}
            //etc...
            };
            return Map[Source.Id.ToLower()].ToUpper();
        }
    }



用途如下:



use as follows :

string zoneAbbreviation = TimeZoneInfo.Local.Convertion();


这篇关于显示时区缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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