我是新手Noda Time,我基本上想比较日期是否已过期。就我而言,我有一个对象,它date被创建,由 a 表示LocalDate,并且它的数量months作为 an 有效int,所以我想做一个简单的:
if ( Now > (dateCreated + validMonths) ) expired = true;
Run Code Online (Sandbox Code Playgroud)
但我在 Noda Time 文档中找不到获取当前日期的正确方法(它们仅显示如何获取当前时间SystemClock.Instance.Now)以及处理时间比较的正确方法。
例如,如果今天是 2015 年 1 月 1 日,文档创建于 2014 年 12 月 1 日,并且有效期为一个月,那么今天它的一个月有效期将到期。
我怀念诸如isBefore()和之类的方法isAfter()来比较日期和时间。< > 运算符的简单重载也可能非常有帮助。
编辑:
1 - 抱歉,有< >运算符可以比较日期。
2 - 我使用此代码解决了我的问题(尚未测试!):
...
LocalDate dateNow = this.clock.Now.InZone(DateTimeZoneProviders.Tzdb.GetSystemDefault()).LocalDateTime.Date;
LocalDate dateExpiration = DataASO.PlusMonths(validity);
return (dateNow < dateExpiration);
Run Code Online (Sandbox Code Playgroud) 我正在开发一项全球调度服务,该服务使用不同时区的物理位置。这些时区必须与每个位置一起保存在数据库中。问题是,它们如何最好地存储?
我们目前使用自定义时区表,它将自定义整数 ID 映射到 Microsoft 时区字符串标识符。我希望改为存储 IANA 时区标识符。我们的数据库是一个 SQL Server,使用 Entity Framework 6 在 C# 中访问。我们使用 NodaTime 处理时间。解决方案必须与所有这些技术配合良好。
我看到了两种不同的方法:
第一种解决方案可能是最简单的,因为它很容易允许使用新的标识符并将数据紧密地保持在一起。但是,它确实有使用大量空间的缺点。
第二种解决方案要求我们在每次需要时区时加入时区表 - 这很常见 - 但需要很少的空间。如果需要,必须将新的时区标识符添加到该表中。它还引入了这些神奇的整数 ID(使用的外键),它们可能会被误认为是众所周知的标识符(我们目前有这个问题,其中 ID 已移出数据库并移入代码字典中,而不是数据库中使用)桌子)。
在我写这篇文章时,我想知道是否有可能为 SQL Server 创建一个自定义时区 UDT,其中时区可以作为字符串标识符保存和加载,但可以更有效地存储在用户中- 隐藏格式。
我正在使用 WikiPedia 中的一些值,这些值在某些情况下是数百万年前的(例如月球的形成,WikiData 报告其正在形成:“-4527000000-01-01T00:00:00Z”。在其他情况下,我'我只是看看可能是公元前 10000 年等的年份,仍然表示为“-10000-01-01T00:00:00Z”。
据我所知,测试 NodaTime 并阅读文档,不支持这些古老的年份(或者我忽略了一些东西)。例如,这失败了:
OffsetDateTime defaultValue = new OffsetDateTime(new LocalDateTime(77, 1, 1, 0, 0), Offset.Zero);
var pattern = OffsetDateTimePattern.Create("yyyy-MM-ddTHH:mm:ss'Z'", CultureInfo.InvariantCulture, defaultValue);
string P = "-1000-01-25T20:34:25Z";
var result = pattern.Parse(P);
NodaTime.Text.UnparsableValueException: The value string includes a negative value where only a non-negative one is allowed. Value being parsed: '^-1000-01-25T20:34:25Z'. (^ indicates error position.)
at NodaTime.Text.ParseResult`1.GetValueOrThrow() in C:\Users\jon\Test\Projects\nodatime\build\releasebuild\src\NodaTime\Text\ParseResult.cs:line 81
at TryNodaTime.Program.Main(String[] args
Run Code Online (Sandbox Code Playgroud)
顺便说一句,使用 sing 这个字符串可以正常工作: string P = "1000-01-25T20:34:25Z";
我希望我只是忽略了一些明显的处理非常大的旧日期/年份的事情。其他例子包括老城市顺便说一句,它们的确切日期早在 0 年之前。
关于如何处理这些问题的指导将不胜感激。
编辑:发现这个例子,它要求我首先手动检测年份,如果它是负数,然后使用 LocalDate,但是,这里没有解决更大的问题,因为它在 -9999 以下失败: …
我在 ASP.NET Core 项目和包含两个Instant属性的模型中使用 Noda Time 。
public class Template
{
//... redacted bits
public Instant CreateDt { get; set; }
public Instant LastmodDt { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
数据库中的信息正如我所期望的“开箱即用”。但是通过 API取回值会返回空对象属性,如下所示:
{
//... redacted bits
"createDt": {},
"lastmodDt": {}
}
Run Code Online (Sandbox Code Playgroud)
我如何取回我输入的相同日期,或者至少可以输入一些数据?
FWIW 我使用 ASP.NET Core 3.1,默认 System.Text.Json(不是 JSON.NET)。
编辑:删除了与问题无关的关于 PostgreSQL 的虚假注释。
天然气日定义为欧洲标准时间 24 小时的时间范围,从 UTC 5:00 开始,到次日 UTC 5:00 结束。在欧洲夏令时期间,它从 4:00 UTC 开始,到次日 4:00 UTC 结束(请参阅维基百科或ACER了解英文解释)。
我需要在应用程序中使用天然气日才能执行以下操作:
在我看来,“加油日”应该像时区一样可用,然后我可以在我的应用程序中使用它,但尝试使用DateTime或DateTimeOffset让我完全不知道我应该做什么这项工作。
谁能指出我必须做什么才能进行上面解释的计算的正确方向?是否有一个库可以让这件事变得更容易一些?例如,我已经研究过NodaTime,但我在其文档中找不到任何可以让我更轻松地解决此任务的内容。
我有一个要求,管理员将为该国家/地区列表选择一些用户countries List和Time for Alert用户.
让我们说管理员24/07/2014 09:00 AM在国家/地区中选择India,Malaysia and Canada,他们需要根据这些警报获取警报,their TimeZone并且每个国家/地区的用户都需要获取警报User's Local 9 AM Time.
所以,我只有他们country Codes喜欢IN,MY,CA
所以,我想到了他们的TimeZones和基于的计算Server Time.
例如:我的网站服务器位于Canada.所以,我认为心计Time to Alert的India基础上,India TimeZone和save the Time in Db.所以我的窗口服务将在印度时间运行,推动警报.
但为此,我需要在Db中以不同的时间保存多个记录.
因此,为了获得TimeZone基于CountryCode的内容,我使用了NodaTime
var CountryInfo = (from location in TzdbDateTimeZoneSource.Default.ZoneLocations
where location.CountryCode.Equals(CountryCode,
StringComparison.OrdinalIgnoreCase)
select new { location.ZoneId, location.CountryName })
.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
我TimeZoneID从这个查询得到了. …
我在项目中使用本机Datetime和Noda Time库LocalDate.LocalDate主要用于计算,而Datetime用于其他地方.
有人可以给我一个简单的方法来转换本地和nodatime日期字段?
我目前正在使用以下方法将Datetime字段转换为Noda LocalDate.
LocalDate endDate = new LocalDate(startDate.Year, startDate.Month, startDate.Day);
Run Code Online (Sandbox Code Playgroud) 假设我有以下日期,时间和时区: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.
如果时间不明确,即发生两次,我想要早期映射.
我试图了解我们所遇到的这个时区问题.我们希望以UTC格式存储所有日期时间,然后将日期时间转换为用户时区.
我们决定使用Nodatime,因为它似乎是正确的方法.但是我们遇到了一些问题
这就是我们将datetime转换为UTC的方法(注意:我现在硬编码了usersTimeZone)
public static DateTime ConvertLocaltoUTC(this DateTime dateTime)
{
LocalDateTime localDateTime = LocalDateTime.FromDateTime(dateTime);
IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
var usersTimezoneId = "Europe/Copenhagen";
var usersTimezone = timeZoneProvider[usersTimezoneId];
var zonedDbDateTime = usersTimezone.AtLeniently(localDateTime);
var returnThis = zonedDbDateTime.ToDateTimeUtc();
return zonedDbDateTime.ToDateTimeUtc();
}
Run Code Online (Sandbox Code Playgroud)
以下是我们如何将其转换回来的方式
public static DateTime ConvertUTCtoLocal(this DateTime dateTime)
{
Instant instant = Instant.FromDateTimeUtc(dateTime);
IDateTimeZoneProvider timeZoneProvider = DateTimeZoneProviders.Tzdb;
var usersTimezoneId = "Europe/Copenhagen"; //just an example
var usersTimezone = timeZoneProvider[usersTimezoneId];
var usersZonedDateTime = instant.InZone(usersTimezone);
return usersZonedDateTime.ToDateTimeUnspecified();
}
Run Code Online (Sandbox Code Playgroud)
但是当我们将它转换回本地时,它会引发异常:
参数异常:Instant.FromDateTimeUtc的DateTime.Kind无效
在"ConvertUTCtoLocal()"的第一行,日期时间的一个例子可能是:"9/18/2017 5:28:46 PM" - 是的,这是通过ConvertLocalToUTC方法.
我提供了错误的格式,或者我在这里做错了什么?
I have a ZonedDateTime which I have created using a DateTimeOffset and a timezone id (string), and I also have a DateTimeZone.
The particular case I have is that I need to change the Zone of the ZonedDateTime without actually converting the time (i.e. i have 18:00 UTC-4:00, i want to change this to 18:00 UTC-8 without converting the 18:00 accordingly)
The only pertinent function I'm finding is .WithZone(DateTimeZone) but this seems to convert my time based on the …