- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我有以下日期、时间和时区:2016-10-15, 1:00:00, America/Toronto
。
如何创建一个 ZonedDateTime
来表示指定区域中的确切日期和时间?
基本上,我需要一个 ZonedDateTime
对象来表示准确时区中的准确日期和时间。
如果时间被跳过,我想在新时间上加上小时的滴答声。示例:
如果从 00:00 跳到 1:00,并且我尝试获取区域中的时间 00:30,我希望结果是 1:30,而不仅仅是 1:00,这是第一次间隔。
如果从 00:00 跳到 1:45,并且我尝试获取该区域中的时间 00:20,我希望结果为 2:05。
如果时间不明确,i. e., 出现两次,我想要早期的映射。
最佳答案
您所描述的正是 Noda Time 2.0 中 LocalDateTime.InZoneLeniently
的行为。 (感谢 Matt Johnson's change :) 然而,由于它仍处于 alpha 阶段,这里是 1.3.2 的解决方案。基本上,您只需要一个合适的 ZoneLocalMappingResolver
,您可以使用 Resolvers
构建它.这是一个完整的示例。
using NodaTime.TimeZones;
using NodaTime.Text;
class Program
{
static void Main(string[] args)
{
// Paris went forward from UTC+1 to UTC+2
// at 2am local time on March 29th 2015, and back
// from UTC+2 to UTC+1 at 3am local time on October 25th 2015.
var zone = DateTimeZoneProviders.Tzdb["Europe/Paris"];
ResolveLocal(new LocalDateTime(2015, 3, 29, 2, 30, 0), zone);
ResolveLocal(new LocalDateTime(2015, 6, 19, 2, 30, 0), zone);
ResolveLocal(new LocalDateTime(2015, 10, 25, 2, 30, 0), zone);
}
static void ResolveLocal(LocalDateTime input, DateTimeZone zone)
{
// This can be cached in a static field; it's thread-safe.
var resolver = Resolvers.CreateMappingResolver(
Resolvers.ReturnEarlier, ShiftForward);
var result = input.InZone(zone, resolver);
Console.WriteLine("{0} => {1}", input, result);
}
static ZonedDateTime ShiftForward(
LocalDateTime local,
DateTimeZone zone,
ZoneInterval intervalBefore,
ZoneInterval intervalAfter)
{
var instant = new OffsetDateTime(local, intervalBefore.WallOffset)
.WithOffset(intervalAfter.WallOffset)
.ToInstant();
return new ZonedDateTime(instant, zone);
}
}
输出:
29/03/2015 02:30:00 => 2015-03-29T03:30:00 Europe/Paris (+02)
19/06/2015 02:30:00 => 2015-06-19T02:30:00 Europe/Paris (+02)
25/10/2015 02:30:00 => 2015-10-25T02:30:00 Europe/Paris (+02)
关于c# - Noda Time - 从 DateTime 和 TimeZoneId 创建 ZonedDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40184589/
我目前正在使用 RavenDB 来存储一个使用 Noda Time 的 Instant 类来存储日期的对象。 RavenDb 将值存储为 “到达时间”:{ “蜱”:13507658019037497
如果日期时间确实存在,有没有办法验证它? 这听起来可能很奇怪,但如果您将夏令时应用到某个区域,例如美国/纽约 3 月 12 日。2017 年凌晨 2 点到凌晨 3 点是他们的夏令时。从 2017 年
我从事的项目基本上是一个数据收集器。通过定位,将其视为具有应用程序层(OSI 第 7 层)解析/分析功能的 Wireshark(或等同物)可能会有所帮助。当前版本是一个遗留的 MFC 应用程序,已有
在使用 Noda Time 时,我试图将一些数据基于 WeekOfWeekYear, 我注意到 WeekOfWeekYear 属性在星期一更改,但我需要它在星期日开始(使用 ISO 日历)。这可能吗?
我试图用 C# 表示公司的成立日期。我偶尔有完整的一天、一个月和一年,但更多时候我只有一个月和一年,或者有时只有一年。 有没有一种方法可以在 NodaTime 中表示它,然后还允许它们之间进行比较?即
我正在尝试使用 Noda Time 设置一个简单的单元测试。我的代码是: public void ValidityPassedDate() { FakeClock fakeClock = ne
给定下面的代码片段,为什么最后四个输出周期相同?我希望这些行的天数部分是 4、3、2、1 而不是 4、4、4、4。这是一个错误还是我遗漏了一些明显的东西? (来晚了,累了,所以很可能是后者。)我用的是
我在使用 NodaTime 库时遇到了问题。我的目标:计算两个日期之间的年/月/日。所以,这是我的测试示例: private static void Main() {
我使用 Noda Time ,并具有以下代码: var pattern = ZonedDateTimePattern.CreateWithInvariantCulture( "yyyy-MM-
我正在尝试将 Windows 转换为 IANA 时区,下面是大部分时间都有效的代码。但是,当Windows时间id =“特克斯和凯科斯群岛”时,下面的代码返回NULL。我正在使用NODATIME版本1
假设 Instant 变量包含“4/8/2014 11:09:24 AM”,当我将此值传递给此方法时,它会给我一个输出“4/8/2014 12:09:24 PM”我 checkin 了一些时区计算器加
我正在为 .NET Core 开发,所以我使用的是 Noda Time 的不稳定的最新 alpha (2.0.0-alpha20160729)。正在使用 JSON 处理数据。 我将接受用户在出生日期字
我目前正尝试针对几个国家/地区本地化我的 windowsphone Time 应用程序。我正在使用 Noda Time,因为它对新手来说非常简单。我面临的问题是所有时区 ID 都是标准英语,我正在寻找
在 Noda Time 中获取系统时间作为 LocalDateTime 的惯用方法是什么?我能想到的最直接的方法是 var dt = DateTime.Now LocalDateTime system
我正在构建一个应用程序,它需要计算诸如“两个日期之间有多少周/月/年”之类的东西,并且我的所有用户都来自同一个时区。 我想知道,为此使用 LocalDateTime(因为我只使用一个时区)还是使用 Z
我在项目中同时使用 native Datetime 和 Noda Time 库 LocalDate。 LocalDate 主要用于计算,而 Datetime 用于其他地方。 有人可以给我一个简单的方法
假设我有 Noda Time LocalDateTime myLocalDateTime 和 Offset myOffset 变量。要生成 ISO8601 格式的 DateTimeOffset 字符串
我有一个在 OperationContract 参数中使用 Noda Time 类型(LocalDate 和 ZonedDateTime)的服务,但是当我尝试发送例如 LocalDate(1990,7
我正在使用 Noda Time 在 Xamarin Forms 中获取设备上的本地时区。我们使用下面的代码来返回它。 NodaTime.DateTimeZoneProviders.Tzdb.GetSy
Olson 和 Windows 时间 Id 之间的转换在 SO 上多次出现;许多人建议 Jon Skeet 的 Noda Time为了这个任务。 虽然 Google 代码页指出有一个函数可以在两者之间
我是一名优秀的程序员,十分优秀!