如何用野田时间找到昨天的开始和结束?

How to find the the start and end of a yesterday with Noda Time?

我正在尝试使用 Noda Time 从昨天开始计算 first/last 毫秒(这可能也适用于其他日期),我想我已经接近了,但不确定我是否做对了.

如果我可以将 yesterday.AtMidnight() 转换为 Ticks,那么我可以将一天中的 ticks 数加上减 1 来得到结束时间。这是否有意义?根据我的要求,这是 Noda Time 最正确的用法吗?

谢谢你,斯蒂芬

//Come up with the begin and end parameters in milliseconds for a Oracle query

//Assumption is that this code will run from Midnight to 4AM
//Assumption is this application code will run on servers in the Midwest USA
//Assumption is the database servers are on the West coast USA


Instant now = SystemClock.Instance.Now;
Duration duration = Duration.FromHours(24);
LocalDate yesterday = now.InUtc().Minus(duration).Date;

Console.WriteLine(yesterday.AtMidnight());

// add a day minus one tick

更新

Noda docs 显示 Period.Between 也可能有用,所以我会测试一下。

看起来应该不会比这更难:

public static void DefineYesterday( out LocalDateTime yesterdayStartOfDay , out LocalDateTime yesterdayEndOfDay )
{
  BclDateTimeZone tz         = NodaTime.TimeZones.BclDateTimeZone.ForSystemDefault() ;
  LocalDateTime   now        = SystemClock.Instance.Now.InZone(tz).LocalDateTime ;
  LocalDateTime   startOfDay = now.PlusTicks( - now.TickOfDay ) ;

  yesterdayStartOfDay = startOfDay.PlusDays(  -1 ) ;
  yesterdayEndOfDay   = startOfDay.PlusTicks( -1 ) ;

  return;
}