当前位置: 首页 > 知识库问答 >
问题:

时区缩写

扶誉
2023-03-14

TimeZoneInfo不提供给定时区的缩写或简称。做到这一点的唯一好方法是拥有一个字典,它将缩写映射到timezone.idstandardnamedaylightname属性。然而,我搜索的缩写列表的所有来源都有不同的时区名称,即与Windows中的不同。

如何在.NET中向用户显示全名、id或任何其他名称?我不想要UtcOffset而是时区缩写-Pacific的PST,Universal的UTC,EST-East Standard等。是否有任何C#兼容的列表或数据库,其中包含所有可能的时区及其缩写,并与timeZoneInfo.getsystemtimezone()提供的时区及其缩写兼容?

共有1个答案

闻人宇定
2023-03-14

更新的答案

我最初的回应如下,并且仍然有效。但是现在有一个更简单的方法,使用TimeZoneNames库。从Nuget安装后,您可以执行以下操作:

string tzid = theTimeZoneInfo.Id;                // example: "Eastern Standard time"
string lang = CultureInfo.CurrentCulture.Name;   // example: "en-US"
var abbreviations = TZNames.GetAbbreviationsForTimeZone(tzid, lang);

生成的对象将具有类似于以下内容的属性:

abbreviations.Generic == "ET"
abbreviations.Standard == "EST"
abbreviations.Daylight == "EDT"
// starting with a .Net TimeZoneInfo
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");

// You need to resolve to a specific instant in time - a noda Instant
// For illustrative purposes, I'll start from a regular .Net UTC DateTime
var dateTime = DateTime.UtcNow;
var instant = Instant.FromDateTimeUtc(dateTime);

// note that if we really wanted to just get the current instant,
// it's better and easier to use the following:
// var instant = SystemClock.Instance.Now;


// Now let's map the Windows time zone to an IANA/Olson time zone,
// using the CLDR mappings embedded in NodaTime.  This will use
// the *primary* mapping from the CLDR - that is, the ones marked
// as "territory 001".

// we need the NodaTime tzdb source.  In NodaTime 1.1.0+:
var tzdbSource = TzdbDateTimeZoneSource.Default;

// in previous NodaTime releases:
// var tzdbSource = new TzdbDateTimeZoneSource("NodaTime.TimeZones.Tzdb");

// map to the appropriate IANA/Olson tzid
var tzid = tzdbSource.MapTimeZoneId(timeZoneInfo);

// get a DateTimeZone from that id
var dateTimeZone = DateTimeZoneProviders.Tzdb[tzid];


// Finally, let's figure out what the abbreviation is
// for the instant and zone we have.

// now get a ZoneInterval for the zone and the instant
var zoneInterval = dateTimeZone.GetZoneInterval(instant);

// finally, you can get the correct time zone abbreviation
var abbreviation = zoneInterval.Name;

// abbreviation will be either PST or PDT depending
// on what instant was provided
Debug.WriteLine(abbreviation);
 类似资料:
  • 从“GMT夏令时”或任何其他可用的Windows时区信息有什么方法可以到达BST吗?

  • 问题内容: 我正在使用此下拉列表按UTC偏移量存储时区: 使用PHP,我实际上没有最简单的选项将它们转换为时区缩写,我唯一以编程方式进行选择的方法是对约400个时区缩写进行排序。有人知道每个时区是什么以及夏时制进行时它们是什么的下拉列表吗?(我假设我需要手动定义两个列表) 编辑:将每个时区的此列表解析为单个缩写,但是它们没有“受欢迎”的缩写。 我的新清单 Code: 问题答案: 什么是“标准”时区

  • 我正在使用以下下拉列表为应用程序存储时区: 使用PHP,我没有最简单的选项来将这些转换为时区缩写,我唯一的选项是通过编程来完成,通过一个大约400个时区缩写的列表进行排序。有没有人知道下拉列表中的每一个时区是什么,以及夏令时是什么?(我假设我需要手动定义这两个列表) 编辑:将此列表解析为每个时区的一个缩写,但它们不是“流行”的。 我的新列表 代码:

  • 我使用下面的下拉列表按UTC偏移量存储应用程序的时区: 使用PHP时,我并没有最简单的方法将这些转换为时区缩写,我唯一的编程方法就是对大约400个时区缩写列表进行排序。有人知道这个下拉列表中的每个时区是什么吗,以及夏令时是什么吗?(我假设我需要手动定义这两个列表) 编辑:将这个列表解析为每个时区的一个缩写,但它们不是“流行”的。

  • 在我参与的几个项目中,我们使用了 YYYY-MM-DDT hh: mm: s UTC 例如 2017-01-01T12:00:00UTC 作为时间格式,并声明它(不正确?)成为ISO-8601的兼容子集。 很明显,ISO-8601包括和,作为指定与UTC的偏移量以及时区的法定方式(忽略夏时制)。 很明显,W3C和大多数采用ISO-8601(子集)的其他组织更喜欢使用(和)。 我正在寻找熟悉或拥有I

  • 上一章讲的东西比较多,完全理解会有点难,所以这一章来点容易的。我们已经学习了如何定义本地缓冲区的映射和设置选项,现在以同样的方式来学习本地缓冲区的缩写。 打开你的foo和bar这两个文件,切换到foo,然后执行下面的命令: :::vim :iabbrev <buffer> --- &mdash; 在文件foo下进入插入模式输入下面的文本: :::text Hello --- world. Vi