Comparing two different timezone timespans using NodaTime - c#

I have a requirement which I'm getting a little confused about. I started using NodaTime which I think is the best way to go.
I have two users, User1 and User2 both in two different timezones. They are available to meet between 2pm and 5pm for example, in their local timezones. If User2 has an offset of +2 hours from User1, then the overlap is just 1 hour. What I want to get the number of hours overlap (the actual time for User1 and User2 would be a bonus.)
All I have got so far is:
var user1TimeZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(user1timezone);
var user2TimeZone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(user2timeZone);
Any thoughts on how I should even start tackling this problem?
Thanks,

Firstly, be aware that it could change each day: don't treat a time zone as a fixed offset.
Secondly, be aware that the local time specified (for each of start/end) may not even happen, or may happen twice. Work out how you want to handle ambiguous and skipped times.
For any particular day, I would just convert they users' start/end times to Instant (via ZonedDateTime) and then you can find the overlap. This does assume that any overlap happens on the same day, however... that isn't the case in reality. I'm having a meeting soon where one of the attendees is in New Zealand - it's March 14th here, but March 15th there. Accounting for that is rather trickier...
Here's code for the relatively simple case though:
using NodaTime;
using System;
class Test
{
static void Main()
{
// My availability: 4pm-7pm in London
var jon = new Availability(
DateTimeZoneProviders.Tzdb["Europe/London"],
new LocalTime(16, 0, 0),
new LocalTime(19, 0, 0));
// My friend Richard's availability: 12pm-4pm in New York
var richard = new Availability(
DateTimeZoneProviders.Tzdb["America/New_York"],
new LocalTime(12, 0, 0),
new LocalTime(16, 0, 0));
// Let's look through all of March 2017...
var startDate = new LocalDate(2017, 3, 1);
var endDate = new LocalDate(2017, 4, 1);
for (LocalDate date = startDate; date < endDate; date = date.PlusDays(1))
{
var overlap = GetAvailableOverlap(date, jon, richard);
Console.WriteLine($"{date:yyyy-MM-dd}: {overlap:HH:mm}");
}
}
static Duration GetAvailableOverlap(
LocalDate date,
Availability avail1,
Availability avail2)
{
// TODO: Check that the rules of InZoneLeniently are what you want.
// Be careful, as you could end up with an end before a start...
var start1 = (date + avail1.Start).InZoneLeniently(avail1.Zone);
var end1 = (date + avail1.End).InZoneLeniently(avail1.Zone);
var start2 = (date + avail2.Start).InZoneLeniently(avail2.Zone);
var end2 = (date + avail2.End).InZoneLeniently(avail2.Zone);
var latestStart = Instant.Max(start1.ToInstant(), start2.ToInstant());
var earliestEnd = Instant.Min(end1.ToInstant(), end2.ToInstant());
// Never return a negative duration... return zero of there's no overlap.
// Noda Time should have Duration.Max really...
var overlap = earliestEnd - latestStart;
return overlap < Duration.Zero ? Duration.Zero : overlap;
}
}
public sealed class Availability
{
public DateTimeZone Zone { get; }
public LocalTime Start { get; }
public LocalTime End { get; }
public Availability(DateTimeZone zone, LocalTime start, LocalTime end)
{
Zone = zone;
Start = start;
End = end;
}
}

If you have a server where you do that, you have to send UTC and then compare it. When you get the time on the client side you have to convert it into local. It means, that when first user wants to arrange a meeting, he sends his time into UTC to server, then when second user gets this time, he will convert it into his local time.
// First user sends UTC.
DateTime firstUserTime = DateTime.UtcNow;
// Second user gets time in his time zone.
DateTime secondUserTime = firstUserTime.ToLocalTime();

Related

NodaTime - Calculate duration between two LocalTime variables that span midnight

I have a requirement to calculate the duration between two times that may span midnight.
The use case is to allow the user to set up shift plans e.g. “09:00 to 17:00” or “22:00 to 06:00” and calculate the contracted time
For “09:00 to 17:00” I can use:
LocalTime inAt = new LocalTime(9, 0);
LocalTime outAt = new LocalTime(17, 0);
var period = Period.Between(inAt, outAt);
Which results in 8 hours, the answer I am looking for.
For “22:00 to 06:00” the period returns 16 hours (regardless of the order of the parameters).
LocalTime inAt = new LocalTime(22, 0);
LocalTime outAt = new LocalTime(6, 0);
var period = Period.Between(inAt, outAt);
var period2 = Period.Between(outAt, inAt);
I am guessing that this is related to daylight saving time, unless you know the dates you cannot be sure that the answer will always the 8 hours. If the clocks go forward it would be 7, backwards would be 9.
How can I ensure that no matter what LocalTime values are used the period would disregard any daylight savings? Should I use LocalDateTime with an arbitrary date such as 2021-01-01?
Also, am I correct in using Period or should I be using Duration?
Update
This seems to work however I am still wondering if there is an eaiser way of doing it?
LocalTime inAt = new LocalTime(22, 0);
LocalTime outAt = new LocalTime(6, 0);
var period = Period.Between(inAt, outAt, PeriodUnits.Ticks);
LocalTime? midnightAdjustedTime = null;
if (period.Ticks < 0)
{
midnightAdjustedTime = LocalTime.Midnight + period;
}
var duration = Duration.FromTicks(midnightAdjustedTime?.TickOfDay ?? period.Ticks);
This has nothing to do with daylight savings - it can't do, given that everything is in terms of LocalTime. It's about negative periods.
For “22:00 to 06:00” the period returns 16 hours (regardless of the order of the parameters)
No, it doesn't. One returns 16 hours, the other returns -16 hours:
using NodaTime;
var start = new LocalTime(22, 0);
var end = new LocalTime(6, 0);
var startToEnd = Period.Between(start, end);
var endToStart = Period.Between(end, start);
Console.WriteLine(startToEnd);
Console.WriteLine(endToStart);
Output:
PT-16H
PT16H
It's not clear to me how you came to the conclusion that both returned the same value, but the fact that they don't return the same value is crucial to fixing the problem. The simple approach is just to add 1 day (24 hours) if the period is negative. The code you've got is almost right, but it can be done much simpler - and without using the Ticks unit at all:
// Note: Period.ToDuration() isn't "generally safe" due to variable lengths
// of months etc, but is okay here.
var duration = startToEnd.ToDuration();
if (duration < Duration.Zero)
{
duration += Duration.FromDays(1);
}
Console.WriteLine(duration);

How to check if between two points in time C#

I'm setting up a scheduling system for one of my projects and one thing in particular that I need to do is allow for multiple windows to be present within each day. A window would represent two points in time, the start and the end.
I am not sure just how I should approach this issue. I can do this in a very hacky way but I would rather know how to do it right, so that I can be satisfied that my code is as it should be.
What I'm currently attempting to do is as seen here:
public class ScheduleWindow
{
public string Name;
public DateTime EndTime;
public DateTime StartTime;
}
I have a name id for my schedule, but for this that is irrelevant.
I have a date in time at which the window will start.
I have a date in time at which the window will end.
The intent for the following method is to add a window to a schedule. I want the schedule to represent my day, so I'm using the current year, month and day and then setting the hours and minutes to the points in time that I would like this window to be active.
public void AddWindow(string name, int startHour, int endHour, int startMinute, int endMinute)
{
var year = DateTime.Now.Year;
var month = DateTime.Now.Month;
var day = DateTime.Now.Day;
var startTime = new DateTime(year: year, month: month, day: day, hour: startHour, minute: startMinute, second: 0, millisecond: 0);
var endTime = new DateTime(year: year, month: month, day: day, hour: endHour, minute: endMinute, second: 0, millisecond: 0);
var window = new ScheduleWindow()
{
EndTime = endTime,
StartTime = startTime,
Name = name
};
_scheduleWindows.Add(window);
}
So now we're to the root of my issue.
I am actually completely unsure of how to check if we are within that time window.
`public bool WindowIsActive()
{
foreach (var window in _scheduleWindows)
{
...
//if any window is currently active, return true
}
}`
I've been fiddling here with this code for some time now, and any help would be super appreciated. If anyone can give me some pointers to perhaps a solution that would work better, that would be awesome!
The goal is to check and see if any window is currently active. Currently, I have no clue how.
I imagine it's look something like this
public bool WindowIsActive()
{
foreach (var window in _scheduleWindows)
{
if (DateTime.Now >= window.StartTime && DateTime.Now <= window.EndTime)
{
return true;
}
}
}
This works because DateTime implements the GreaterThanOrEqual and LessThanOrEqual operators.
Two things to consider about this answer:
This code assumes EndTime is later than StartTime.
If you care about timezones, you should use DateTimeOffset instead.
You can use < and > operators to compare DateTimes.
[edit: I realized that you just wanted to compare the time of day - i.e. disregarding the month and year - you'd use the TimeOfDay property of DateTime]
var timeOfDay = DateTime.Now.TimeOfDay; //this is a TimeSpan type
if(timeOfDay > window.StartTime.TimeOfDay && timeOfDay < window.EndTime.TimeOfDay)
{
//time is within the time window.
}

How to check if DateTime is within a specific range

I'm developing a video rental application using C# winforms, and came across a problem I can't seem to write up or find the solution to.
The program needs to check the current date and number of days passed and also the range between them.
If the current Date is less than or equal to the date specified, it will not calculate the penalty cost.
Otherwise if the Date today has already passed the date specified, it will calculate the penalty cost multiplied by the number of days that has passed between them.
Here's the sample code I have playing with the idea:
DateTime db = DateTime.Parse(dateBeforeString);
DateTime dt = DateTime.Now;
var dateDiff = (dt - db);
double totalDays = dateDiff.TotalDays;
int totalPenalty = initialPenaltyInt*(int)Convert.ToInt64(totalDays);
int totalCost = totalPenalty + rentalCostInt;
if(DateTime.Now != db)
{
//do stuff here to:
//check if current day is less than the one on the database
//set total penalty to zero
}
else if(DateTime.Now > db)
{
//otherwise calculate the total penalty cost multipled by the number of days passed since a specific date
}
Simplistic, but might help you progress, hopefully:
public class Penalties
{
// What about this choice of "int" (vs. decimal)?
public virtual int ComputeOverdueDaysPenalty(int penaltyPerOverdueDay, DateTime dueDate)
{
// Work only with year, month, day, to drop time info and ignore time zone
dueDate = new DateTime(dueDate.Year, dueDate.Month, dueDate.Day);
var now = DateTime.Now;
now = new DateTime(now.Year, now.Month, now.Day);
return now > dueDate ? (int)now.Subtract(dueDate).TotalDays * penaltyPerOverdueDay : 0;
}
}
class Program
{
static void Main(string[] args)
{
var penalties = new Penalties();
var now = DateTime.Now;
// due = today
// should print 0
Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, new DateTime(now.Year, now.Month, now.Day)));
// due = today plus 1
var dueDate = now.AddDays(1);
// should print 0 again
Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));
// due = today minus 1
dueDate = dueDate.Subtract(new TimeSpan(48, 0, 0));
// should print 1234
Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));
// due = today minus 2
dueDate = dueDate.Subtract(new TimeSpan(24, 0, 0));
// should print 2468
Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));
dueDate = DateTime.Parse("2016-10-02");
// should print 12340, as of 10/12/2016
Console.WriteLine(penalties.ComputeOverdueDaysPenalty(1234, dueDate));
Console.ReadKey();
}
}
Just a remark:
I find it a bit odd you've settled for using the int type in that context, btw.
If your "penalty" units are in fact some currency, the recommended data type for that is decimal, in most use cases.
'Hope this helps.

Getting Daylight Savings Time Start and End in NodaTime

How can I get the starting and ending dates for Daylight Savings Time using Noda Time?
The function below accomplishes this task but it is horribly unwieldy and is begging for a simpler solution.
/// <summary>
/// Gets the start and end of daylight savings time in a given time zone
/// </summary>
/// <param name="tz">The time zone in question</param>
/// <returns>A tuple indicating the start and end of DST</returns>
/// <remarks>Assumes this zone has daylight savings time</remarks>
private Tuple<LocalDateTime, LocalDateTime> GetZoneStartAndEnd(DateTimeZone tz)
{
int thisYear = TimeUtils.SystemLocalDateTime.Year; // Get the year of the current LocalDateTime
// Get January 1, midnight, of this year and next year.
var yearStart = new LocalDateTime(thisYear, 1, 1, 0, 0).InZoneLeniently(tz).ToInstant();
var yearEnd = new LocalDateTime(thisYear + 1, 1, 1, 0, 0).InZoneLeniently(tz).ToInstant();
// Get the intervals that we experience in this year
var intervals = tz.GetZoneIntervals(yearStart, yearEnd).ToArray();
// Assuming we are in a US-like daylight savings scheme,
// we should see three intervals:
// 1. The interval that January 1st sits in
// 2. At some point, daylight savings will start.
// 3. At some point, daylight savings will stop.
if (intervals.Length == 1)
throw new Exception("This time zone does not use daylight savings time");
if (intervals.Length != 3)
throw new Exception("The daylight savings scheme in this time zone is unexpected.");
return new Tuple<LocalDateTime,LocalDateTime>(intervals[1].IsoLocalStart, intervals[1].IsoLocalEnd);
}
There's not a single built-in function that I am aware of, but the data is all there, so you can certainly create your own.
You're on the right track with what you've shown, but there are a few things to consider:
Normally people are interested in the end points of the intervals. By returning the start and stop of only the middle interval, you are likely getting values different than you expect. For example, if you use one of the US time zones, such as "America/Los_Angeles", your function returns the transitions as 3/9/2014 3:00:00 AM and 11/2/2014 2:00:00 AM, where you are probably expecting 2:00 AM for both.
Time zones south of the equator that use DST will start it towards the end of the year, and end it towards the beginning of the next year. So sometimes the items in the tuple might be reversed from what you expect them to be.
There are quite a lot of time zones that don't use daylight saving time, so throwing an exception isn't the best idea.
There are at least two time zones that presently have four transitions in a single year ("Africa/Casablanca" and "Africa/Cairo") - having a "break" in their DST periods for Ramadan. And occasionally, there are non-DST-related transitions, such as when Samoa changed its standard offset in 2011, which gave it three transitions in a single year.
Taking all of this into account, it would seem better to return a list of single transition points, rather than a tuple of pairs of transitions.
Also, this is minor, but it would be better form to not bind the method to the system clock at all. The year can easily be passed by parameter. Then you can use this method for non-current years if necessary.
public IEnumerable<LocalDateTime> GetDaylightSavingTransitions(DateTimeZone timeZone, int year)
{
var yearStart = new LocalDateTime(year, 1, 1, 0, 0).InZoneLeniently(timeZone).ToInstant();
var yearEnd = new LocalDateTime(year + 1, 1, 1, 0, 0).InZoneLeniently(timeZone).ToInstant();
var intervals = timeZone.GetZoneIntervals(yearStart, yearEnd);
return intervals.Select(x => x.IsoLocalEnd).Where(x => x.Year == year);
}
Also note at the end, it's important to filter just the values that are in the current year because the intervals may very well extend into the following year, or go on indefinitely.
This snippet code also help you to check a time is in daylightsavingstime or not
public static bool IsDaylightSavingsTime(this DateTimeOffset dateTimeOffset)
{
var timezone = "Europe/London"; //https://nodatime.org/TimeZones
ZonedDateTime timeInZone = dateTimeOffset.DateTime.InZone(timezone);
var instant = timeInZone.ToInstant();
var zoneInterval = timeInZone.Zone.GetZoneInterval(instant);
return zoneInterval.Savings != Offset.Zero;
}
how to use it
var testDate = DateTimeOffset.Now;
var isDst = testDate.IsDaylightSavingsTime();
Depend on your situation, you can modify it a bit

DateTime.AddYear, AddMonth, AddDay, AddHour, AddMinute doesn't add?

I have the following code:
DateTime endTime = new DateTime(01, 01, 01, 00, 00, 00);
endTime = endTime.AddYears(currentYear - 1);
endTime = endTime.AddMonths(currentMonth - 1);
endTime = endTime.AddDays(currentDay - 1);
hourToWaitTo = Convert.ToInt32(txtboxHourToWaitTo.Text);
minuteToWaitTo = Convert.ToInt32(txtboxMinuteToWaitTo.Text);
endTime = endTime.AddHours(hourToWaitTo);
endTime = endTime.AddMinutes(minuteToWaitTo);
But it doesn't add anything to endTime
EDIT1:
I set currentYear, currentMonth and currentDay like this:
int currentYear = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
int currentMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
int currentDay = Convert.ToInt32(DateTime.Now.ToString("dd"));
hourToWaitTo and minuteToWaitTo is set by user in a textbox.
I want the user to set a time (e.g. 12:25) for the computer to shutdown at, and I also want a countdown to say how many hours:minutes:seconds left till shutdown. I have managed to do all of this, but i couldn't fix the above mentioned endTime problem.
SOLUTION:
The solution to this problem is very simple:
DateTime endTime = new DateTime(currentYear, currentMonth, currentDay, hourToWaitTo, minuteToWaitTo, 0);
I tried to do this earlier, but for some reason I was getting an error. To set those variables above I used:
int currentYear = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
int currentMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
int currentDay = Convert.ToInt32(DateTime.Now.ToString("dd"));
and
int minuteToWaitTo = Convert.ToInt32(txtboxMinuteToWaitTo.Text);
int hourToWaitTo = Convert.ToInt32(txtboxHourToWaitTo.Text);
Thank you all for your help.
This is not a direct answer to your question - the code you've posted looks okay so there must be something else going on - but I'm wondering why you don't just do something like:
hourToWaitTo = Convert.ToInt32(txtboxHourToWaitTo.Text);
minuteToWaitTo = Convert.ToInt32(txtboxMinuteToWaitTo.Text);
DateTime endTime = new DateTime(currentYear, currentMonth, currentDay, hourToWaitTo, minuteToWaitTo, 0);
Code like this should be abolished:
int currentYear = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
int currentMonth = Convert.ToInt32(DateTime.Now.ToString("MM"));
int currentDay = Convert.ToInt32(DateTime.Now.ToString("dd"));
You are checking the system clock three times, pulling out partial values, serializing to a string, parsing that string, and then using each part. Lots of work for nothing.
All you really need is:
DateTime endTime = DateTime.Today.AddHours(hourToWaitTo)
.AddMinutes(minuteToWaitTo);
You should consider the kind of the dates you are working with. When you construct a DateTime using the constructors, you are getting a .Kind of Unspecified unless you specifically tell it what kind of date you want. It's more appropriate in your scenario to be working with a local date, which you will get with DateTime.Today or DateTime.Now.
Also be aware that since you are asking the user for a local time, but allowing them to enter the time components, that time may be invalid or ambiguous. This happens during daylight savings time transitions. You can validate the user input with TimeZoneInfo.Local.IsInvalidTime() or TimeZoneInfo.Local.IsAmbiguousTime(). In the case of ambiguous time, you will need to ask your user "Before or after the daylight savings transition?" or something similar.
And finally, if there's any chance that the user is NOT in the same timezone as the computer in question, then you have a lot of more work to do. You should consider using DateTimeOffset instead, and you will need to capture the intended offset or timezone of the shutdown. Another approach would be to convert the time to the UTC time of the shutdown event. Review this article for more details and best practices.
new DateTime(year,month,day,hour,minute,0)
?

Categories

Resources