gpt4 book ai didi

c# - DateTimeOffset 同日比较

转载 作者:行者123 更新时间:2023-11-30 13:56:32 26 4
gpt4 key购买 nike

我遇到了以下要求的有趣问题:测试进程是否在同一天运行,如果没有运行进程。日期存储为 DataTimeOffset。

我最初的做法是:

  1. 将这两个值都转换为 UTC,因为这些日期可能是在不同的时区创建的并且具有不同的偏移量。
  2. 查看每个值的日期值。这是在转换为 UTC 之后完成的,因为 Date 方法忽略了偏移量。

这在大多数情况下都有效,但我遇到了一个逻辑会失败的情况。如果其中一个值的时间接近前一天/第二天,那么在转换为 UTC 时它会更改日期。如果其他值没有也转换为前一天/第二天的时间,则日期比较失败。

所以我最终采用以下逻辑来包含该场景:

public static bool SameDate(DateTimeOffset first, DateTimeOffset second)
{
bool returnValue = false;
DateTime firstAdjusted = first.ToUniversalTime().Date;
DateTime secondAdjusted = second.ToUniversalTime().Date;

// If date is now a day ahead after conversion, than add/deduct a day to other date if that date hasn't advanced
if (first.Date < firstAdjusted.Date && second.Date == secondAdjusted.Date)
secondAdjusted = secondAdjusted.Date.AddDays(1);
if (first.Date > firstAdjusted.Date && second.Date == secondAdjusted.Date)
secondAdjusted = secondAdjusted.Date.AddDays(-1);

if (second.Date < secondAdjusted.Date && first.Date == firstAdjusted.Date)
firstAdjusted = firstAdjusted.Date.AddDays(1);
if (second.Date > secondAdjusted.Date && first.Date == firstAdjusted.Date)
firstAdjusted = firstAdjusted.Date.AddDays(-1);

if (DateTime.Compare(firstAdjusted, secondAdjusted) == 0)
returnValue = true;

return returnValue;
}

这是现在通过的未通过的单元测试:

 [TestMethod()]
public void SameDateTest()
{
DateTimeOffset current = DateTimeOffset.Now;
DateTimeOffset first = current;
DateTimeOffset second = current;

// 23 hours later, next day, with negative offset (EST) -- First rolls over
first = new DateTimeOffset(2014, 1, 1, 19, 0, 0, new TimeSpan(-5, 0, 0));
second = new DateTimeOffset(2014, 1, 2, 18, 0, 0, new TimeSpan(-5, 0, 0));
Assert.IsFalse(Common.SameDate(first, second));

// 23 hours earlier, next day, with postive offset -- First rollovers
first = new DateTimeOffset(2014, 1, 1, 4, 0, 0, new TimeSpan(5, 0, 0));
second = new DateTimeOffset(2014, 1, 2, 5, 0, 0, new TimeSpan(5, 0, 0));
Assert.IsFalse(Common.SameDate(first, second));

// 23 hours later, next day, with negative offset (EST) -- Second rolls over
first = new DateTimeOffset(2014, 1, 2, 18, 0, 0, new TimeSpan(-5, 0, 0));
second = new DateTimeOffset(2014, 1, 1, 19, 0, 0, new TimeSpan(-5, 0, 0));
Assert.IsFalse(Common.SameDate(first, second));

// 23 hours earlier, next day, with postive offset -- Second rolls over
first = new DateTimeOffset(2014, 1, 2, 5, 0, 0, new TimeSpan(5, 0, 0));
second = new DateTimeOffset(2014, 1, 1, 4, 0, 0, new TimeSpan(5, 0, 0));
Assert.IsFalse(Common.SameDate(first, second));
}

我的直觉是,有一种比基于其他值递增/递减更简洁的方法。有更好的方法吗?

主要标准:

  1. 将两个日期调整为具有相同的偏移量。
  2. 仅当第一个和第二个日期都发生在同一日历日而不是在 24 小时内时才返回真。

最佳答案

调整其中一个日期为两个日期的差异:

public static bool SameDate(DateTimeOffset first, DateTimeOffset second)
{
bool returnValue = false;
DateTime firstAdjusted = first.ToUniversalTime().Date;
DateTime secondAdjusted = second.ToUniversalTime().Date;

// calculate the total diference between the dates
int diff = first.Date.CompareTo(firstAdjusted) - second.Date.CompareTo(secondAdjusted);
// the firstAdjusted date is corected for the difference in BOTH dates.
firstAdjusted = firstAdjusted.AddDays(diff);

if (DateTime.Compare(firstAdjusted, secondAdjusted) == 0)
returnValue = true;

return returnValue;
}

在这个函数中,我假设偏移量永远不会超过 24 小时。 IE 日期与其调整日期之间的差异不会是两天或更多天。如果不是这种情况,那么您可以使用 time span比较。

关于c# - DateTimeOffset 同日比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27093065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com