I'm writing a test app to reproduce some data writing scenario - a task that runs each hour.
For that purpose I need to know how many times it will execute - how many hours there are on a given day. I tried to do that like this: Period.Between(localDateTimeStart.Date, localDateTimeStart.Date.PlusDays(1), PeriodUnits.Hours)
but it gives me ArgumentException saying Units contains time units: Hours
. What would be the idiomatic way to do that in NodaTime? Or is my approach wrong?
Between two local date/time values, there will always be 24 hours. It sounds like you're interested in a date in a particular zone, in which case you want:
var startOfDay = zone.AtStartOfDay(date);
var startOfNextDay = zone.AtStartOfDay(date.PlusDays(1));
var duration = startOfNextDay.ToInstant() - startOfDay.ToInstant();
var hours = duration.Ticks / NodaConstants.TicksPerHour;
(In Noda Time 2.0, Duration
has a TotalHours
property making this easier. That would return a double
value, whereas here hours
is a long
due to the types of the operands. Be aware that it could lose information if the time zone has a transition of (say) half an hour.)
See more on this question at Stackoverflow