- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用这段代码将“东部时区”转换为“EST”。现在它显示“EDT”。您不会经常在某些地方看到那个缩写,并且想坚持使用“EST”。我如何使用 NodaTime 执行此操作?
public static string GetTimeZoneAbbr(string timeZone)
{
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(timeZone);
if (timeZoneInfo != null)
{
var dateTime = DateTime.UtcNow;
var instant = Instant.FromDateTimeUtc(dateTime);
var tzdbSource = TzdbDateTimeZoneSource.Default;
var tzid = tzdbSource.MapTimeZoneId(timeZoneInfo);
var dateTimeZone = DateTimeZoneProviders.Tzdb[tzid];
var zoneInterval = dateTimeZone.GetZoneInterval(instant);
return zoneInterval.Name;
}
return string.Empty;
}
最佳答案
更新
下面的答案描述了如何解析和使用 CLDR 数据。这很好,但我通过将所有这些都包含在一个库中使它变得更容易。参见 this StackOverflow answer , read my blog post ,看看the TimeZoneNames library .使用这个库比自己解析 CLDR 数据容易得多。
// You can pass either type of time zone identifier:
var tz = "America/New_York"; // IANA
var tz = "Eastern Standard Time"; // Windows
// You can get names or abbreviations for any language or locale
var names = TZNames.GetNamesForTimeZone(tz, "en-US");
var abbreviations = TZNames.GetAbbreviationsForTimeZone(tz, "en-US");
names.Generic == "Eastern Time"
names.Standard == "Eastern Standard Time"
names.Daylight == "Eastern Daylight Time"
abbreviations.Generic == "ET"
abbreviations.Standard == "EST"
abbreviations.Daylight == "EDT"
原始答案
我在问题评论中写了一些关于为什么显示缩写形式是完全有效的,但请允许我也回答问题。
以另一种方式重申您的问题,您希望从 Microsoft Windows 时区 ID 开始,并以代表整个时区的人类可读字符串结束,而不仅仅是有效的时区段。
您可以只给他们 TimeZoneInfo.DisplayName
,但这并不总是合适的。对于美国,您可能会得到 "(UTC-05:00) Eastern Time (US & Canada)
的显示名称,并且您可以去掉前导偏移量和括号以返回 “东部时间(美国和加拿大)”
。但这并不适用于所有时区,因为许多时区仅具有列出城市的显示名称,例如 "(UTC-04:00)乔治敦、拉巴斯、马瑙斯、圣胡安”
。
更好的方法是使用来自 Unicode CLDR Project 的数据. Noda Time 拥有此数据的部分,但不是您解决此特定问题所需的全部数据。所以我不能给你一个使用 Noda Time 的代码示例。但是,您可以对原始 CLDR 数据使用以下步骤来实现您的目标:
找到与 Windows 时区对应的 IANA 时区 ID,例如您已经在上面的代码中完成的,或者使用 CLDR Windows time zone mappings直接。
在 CLDR MetaZones file 中查找 IANA 时区.
在 CLDR 翻译之一中查找 MetaZone data files , 或 charts such as this one .使用 "generic-long"
或 "generic-short"
模式,以及您选择的语言,例如 "en"
表示英语.
因此,在您的情况下,从 “东部标准时间”
的 Windows TimeZoneInfo.Id
开始:
IANA 区域 = "America/New_York"
CLDR MetaZone = "America_Eastern"
generic-long [en] = “东部时间”
generic-short [en] = "ET"
请注意,并非每个 Windows 时区都可映射到 IANA 区域,并非每个元区域都有一个短名称,并且一些从未遵循夏令时的区域将只有一个标准名称而不是通用名称。
下面是一些 C# 代码,展示了如何遍历 CLDR 的 XML 数据以获得 TimeZoneInfo
对象的通用长名称。它假定您可以访问指定路径上的 CLDR 数据。下载 the latest core.zip并提取,然后将 basePath
指向该文件夹。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using System.Xml.XPath;
static Dictionary<TimeZoneInfo, string> GetCldrGenericLongNames(string basePath, string language)
{
// Set some file paths
string winZonePath = basePath + @"\common\supplemental\windowsZones.xml";
string metaZonePath = basePath + @"\common\supplemental\metaZones.xml";
string langDataPath = basePath + @"\common\main\" + language + ".xml";
// Make sure the files exist
if (!File.Exists(winZonePath) || !File.Exists(metaZonePath) || !File.Exists(langDataPath))
{
throw new FileNotFoundException("Could not find CLDR files with language '" + language + "'.");
}
// Load the data files
var xmlWinZones = XDocument.Load(winZonePath);
var xmlMetaZones = XDocument.Load(metaZonePath);
var xmlLangData = XDocument.Load(langDataPath);
// Prepare the results dictionary
var results = new Dictionary<TimeZoneInfo, string>();
// Loop for each Windows time zone
foreach (var timeZoneInfo in TimeZoneInfo.GetSystemTimeZones())
{
// Get the IANA zone from the Windows zone
string pathToMapZone = "/supplementalData/windowsZones/mapTimezones/mapZone" +
"[@territory='001' and @other='" + timeZoneInfo.Id + "']";
var mapZoneNode = xmlWinZones.XPathSelectElement(pathToMapZone);
if (mapZoneNode == null) continue;
string primaryIanaZone = mapZoneNode.Attribute("type").Value;
// Get the MetaZone from the IANA zone
string pathToMetaZone = "/supplementalData/metaZones/metazoneInfo/timezone[@type='" + primaryIanaZone + "']/usesMetazone";
var metaZoneNode = xmlMetaZones.XPathSelectElements(pathToMetaZone).LastOrDefault();
if (metaZoneNode == null) continue;
string metaZone = metaZoneNode.Attribute("mzone").Value;
// Get the generic name for the MetaZone
string pathToNames = "/ldml/dates/timeZoneNames/metazone[@type='" + metaZone + "']/long";
var nameNodes = xmlLangData.XPathSelectElement(pathToNames);
var genericNameNode = nameNodes.Element("generic");
var standardNameNode = nameNodes.Element("standard");
string name = genericNameNode != null
? genericNameNode.Value
: standardNameNode != null
? standardNameNode.Value
: null;
// If we have valid results, add to the dictionary
if (name != null)
{
results.Add(timeZoneInfo, name);
}
}
return results;
}
调用它会给你一个字典,然后你可以用它来查找。示例:
// load the data once an cache it in a static variable
const string basePath = @"C:\path\to\extracted\cldr\core";
private static readonly Dictionary<TimeZoneInfo, string> timeZoneNames =
GetCldrGenericLongNames(basePath, "en");
// then later access it like this
string tzname = timeZoneNames[yourTimeZoneInfoObject];
关于c# - 显示 EDT 而不是 EST 的时区转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890184/
在我的 Rails (3.0) 应用程序中,我使用助手 time_zone_select() 来使用时区。它生成时区名称,如“(GMT+01:00) Paris”... 但这些名称与 MySQL 中的
我正在解析格式为的日期: SimpleDateFormat formatter = new SimpleDateFormat("EE MMM dd HH:mm:ss Z yyyy"); 当我这样做的时
我在 Android 上使用 ACTION_TIMEZONE_CHANGED Intent 过滤器来响应时区更改。 我注意到 Jodas 当前时区此时没有更新,使用:DateTimeZone.getD
我有一个旧版 Windows 应用程序,它从数据库读取数据。其中一列是“TimeZoneInfoId”。在传统世界中,它是由另一个 Windows 应用程序编写的,因此它存储 Windows 字符串:
我是数据库开发新手,目前从事 MySQL 工作。我有一列包含美国的所有时区。我希望能够获取每行中每个时区的当前时间(只有时间,没有日期)。 时区数据如下:+05:00、-03:00 等等 这就是我尝试
我正在尝试了解 session 时区在 PostgreSQL 中的工作原理。 当我将 session 时区设置为“01:00”时,返回日期为“-01:00”。符号总是颠倒的,我不理解这种行为。 这怎么
我写入我的 mongoDB 数据库,例如开始日期,开始日期始终是一周中的星期一(我使用并且我在欧洲中部时区)。 "startDate" : ISODate("2014-12-28T23:00:00Z"
是否有详尽的 MySQL 时区列表? 似乎 MySQL 设置中 time_zone 的有效值取决于主机操作系统,但我一直无法找到可能值的列表。 我需要时间显示卡尔加里本地时间。 最佳答案 默认情况下,
我已经在 config/app.php 中配置了时区至 Europe/Lisbon . 如果我做 return date_default_timezone_get(); , 返回 Europe/Lis
我在整个脚本中广泛使用 unix 时间,通常我将时区设置如下: date_default_timezone_set('Europe/London'); 这一直工作正常,直到上周末时钟回退一个小时,我怀
我有一个字符串,2013-10-07T23:59:51.205-07:00,想将其转换为 Java 日期对象。我收到解析错误。 date = new SimpleDateFormat("yyyy-MM
如何更改日志文件名中的 log4j 时区? 我的 log4j.xml 文件:
在JAVA中,如何确定所有日期都作为GMT日期返回? 例如,即使我尝试使用GMT语言环境强制使用DateFormat,它也会应用某种逻辑来检索BST日期。 public static void mai
好吧,这是我遇到过的最奇怪的错误之一! 首先:我不是 Python 程序员,该脚本是由 friend 编写的(我认为他的大部分内容来自示例)。 该脚本的用途:将日历信息(CET 时间)从 XML 文件
我们有一个网站,目前在某个时间(由用户选择)运行拍卖,这一切都工作正常,因为当服务器到达该时间时,拍卖就会开始。然后我们必须添加时区,具体取决于用户居住的地方。然后,根据所选的下拉菜单,这将增加或减少
我陷入了这个逻辑。我正在开发一个消息调度项目,我的客户来自不同的国家。让我们考虑一下亚洲(不遵守夏令时)和美国/纽约(遵守夏令时)。 现在,我正在编写以下查询来获取距当前时间 10 分钟间隔内的时间表
我尝试返回特定时区的值,但我遇到了一些奇怪的响应行为: SELECT created_at AT TIME ZONE 'US/Pacific' - created_at, NOW() A
我正在尝试将不带时区值(例如“31.05.2015”)的字符串转换为 NSDate。 我的 DateFormatter 有什么问题?它返回日期 2015-05-31 22:00:00 +0000当我尝
我已经成功地将我的本地 tzinfo 导入到 mysql 中,我几乎已经了解了关于如何存储和使用这些数据的所有细节,除了一个字段。有一个名为 Transition_type_id 的数字字段(在表 t
为什么以下行返回的 TimeZone 显示不正确的时间: TimeZone.getTimeZone("America/Ottawa") 现在显示晚上 10:26 [亚特兰大时间现在是 6:26,Ott
我是一名优秀的程序员,十分优秀!