How to check if between two points in time C# - 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.
}

Related

How to set DateTime to start/end of the day?

I want to calculate the start DateTime and end DateTime of the current week. First of all I created a class holding both information
internal class ReportTimeSpan
{
public DateTime From { get; set; }
public DateTime To { get; set; }
}
After that this is my calculation
public ReportTimeSpan GetTimeSpanForThisWeek()
{
int amountOfDays = GetAmountOfWeekDays();
int currentDayIndex = GetCurrentWeekDayIndex();
DateTime weekStart = DateTime.Now.AddDays(-currentDayIndex);
int differenceCurrentDayIndexToLastDayIndex = amountOfDays - currentDayIndex;
DateTime weekEnd = DateTime.Now.AddDays(differenceCurrentDayIndexToLastDayIndex);
return new ReportTimeSpan()
{
From = weekStart,
To = weekEnd
};
}
private int GetAmountOfWeekDays()
{
string[] dayNames = Enum.GetNames(typeof(DayOfWeek));
return dayNames.Length;
}
private int GetCurrentWeekDayIndex()
{
DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek;
return (int)dayOfWeek;
}
}
The date of both values is correct, but the time is wrong.
The variable weekStart should have a time of "00:00:00"
The variable weekEnd should have a time of "23:59:59" (not sure about that)
Are there any methods I can use for this? (I don't want to use external packages)
I expect you want something like this:
weekStart = DateTime.Now.AddDays(-currentDayIndex).Date;
As Tim notes, you can simplify this to:
weekStart = DateTime.Today.AddDays(-currentDayIndex);
.Date will remove the time component, so you're just left with the date and a 00:00:00 time. .Today will return today's date without a time component.
For weekEnd, we should add the number of days in the week to weekStart, and then step back 1 tick to take it back into the previous day:
weekEnd = weekStart.AddDays(7).AddTicks(-1);
You could also use .AddMilliseconds(-1), .AddSeconds(-1), or whatever amount you require to safely be inside the previous day (some databases will have less than tick precision, etc.).
If you have some reason for using GetAmountOfWeekDays() then substitute 7 in the above with GetAmountOfWeekDays().
Depending on what you're using this for, you might be better off with an inclusive weekStart and an exclusive nextWeekStart comparison:
weekStart = DateTime.Now.AddDays(-currentDayIndex).Date;
nextWeekStart = weekStart.AddDays(7);
bool isInWeek = someDate >= weekStart && somedate < nextWeekStart;
weekStart = weekStart.Date;
weekEnd = weekEnd.AddHours(23).AddMinutes(59).AddSeconds(59).AddMilliseconds(999);
OR
weekStart = weekStart.Date;
weekEnd = weekEnd.AddHours(24).AddMilliseconds(-1);
OR
weekStart = weekStart.Date;
weekEnd = new DateTime(weekEnd .Year, weekEnd .Month, weekEnd .Day, 23, 59, 59);

Comparing two different timezone timespans using NodaTime

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();

Determine if a date is a "work day off"

At my work, we are on the 9/80 plan where we get every other Friday off. We've got a small program that shows a DevExpress.Scheduler control and I'd like to color our "Friday's off" a different color. What I need to know is how do I know if a date is one of our Friday's off? The Friday's off will always be every other week (in other words, we don't skip a week due to a holiday or something like that). I have the date of our First Friday off of the year, so I think I can use that somehow...I can also get the date from the scheduler as it's drawn so I have something to compare to.
DateTime dtFirstFridayOff = new DateTime(2011, 1, 1);
DateTime dtCellDate = Convert.ToDateTime(e.Cell.Value);
Now I'm a bit lost as to how to check if dtCellDate is a Friday off.
public static bool IsDateMultipleDays(DateTime originalDate, int numberOfDays, DateTime potentialDate)
{
var original = originalDate.Date; // to make sure that it doesn't have a time portion
var potential = potentialDate.Date;
var difference = potential - original;
return (int)difference.TotalDays % numberOfDays == 0;
}
Then you'd call it like this:
IsDateMultipleDays(dtFirstFridayOff, 14, dtCellDate)
Try this:
bool IsFridayOff(DateTime dt)
{
if (dt.DayOfWeek != DayOfWeek.Friday)
{
return false;
}
DateTime dtFirstFridayOff = new DateTime(2011, 1, 1);
TimeSpan span = dtFirstFridayOff - dt.Date;
return (int) span.TotalDays%14 == 0;
}

how to get no of days and nights between two dates using c#

i am doing a project on cab services.in this rate is different for day and night.
in the form only journey start date and end date is selected.based on this i have to calculate the no of days and nights.
here i am confused how to calculate the no of days and night.
thanks in advance.
private List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
{
if (StartingDate > EndingDate)
{
return null;
}
List<DateTime> rv = new List<DateTime>();
DateTime tmpDate = StartingDate;
do
{
rv.Add(tmpDate);
tmpDate = tmpDate.AddDays(1);
} while (tmpDate <= EndingDate);
return rv;
}
To view this code in action, copy and paste the following code into SnippetCompiler:
DateTime StartingDate = DateTime.Parse("02/25/2007");
DateTime EndingDate = DateTime.Parse("03/06/2007");
foreach (DateTime date in GetDateRange(StartingDate,EndingDate))
{
WL(date.ToShortDateString());
}
Sample output :
2/25/2007
2/26/2007
2/27/2007
2/28/2007
3/1/2007
3/2/2007
3/3/2007
3/4/2007
3/5/2007
3/6/2007
Use the Subtract method to get the difference, which is a TimeSpan value. Example:
TimeSpan diff = SecondDate.Subtract(FirstDate);
You can get the length of the time span for example in hours:
double hours = diff.TotalHours;
I'm not sure which time unit "days and nights" could be interpreted as, though. Perhaps days?
double days = diff.TotalDays;
DateTime dt1,dt2;
//...
TimeSpan period = dt1 - dt2;
int days = period.Days;
It sounds like a very long Cab journey that takes days and nights!
I think you need to define what a day and a night is more clearly in order to get your perfect answer. You also need to think about what impact Daylight Saving Time has on your calculations.
If say:
a day was the period from 6am to 6pm
the night was the rest - from 6pm to 6am
and you wanted to really count hours rather than days
In this case then a calculation would require you to:
iterate a currentDateTime from the startDateTime to the endDateTime
choose the increment in the currentDateTime so that it jumps to the next time barrier (6am, 6pm or the endDateTime)
within each loop, then add to your cumulative calculation of numDayHours or numNightHours so far.
Note that:
you could make this calculation quicker by counting whole days along the way
you need to be very careful about the time zone you are calculating in (I just hope that your taxi doesn't cross time zone boundaries!)
you need to be very careful about local time changes - especially "daylight savings time" type changes - the duration from 6pm to 6am is not always 12 hours!
Some pseudo code:
var numDayHours = 0.0;
var numNightHours = 0.0;
var current = startDateTime;
while (current < endDateTime)
{
next_hop = calculate_next_hop (current, endDateTime);
// select next date time
switch (next_hop.hop_type)
{
case HopType.night_time_hop:
numNightHours += next_hop.num_hours;
break;
case HopType.day_time_hop:
numDayHours += next_hop.num_hours;
break;
}
current = next_hop.EndDateTime;
}
// and here is the result
double numDays = numDayHours / 12.0;
double numHours = numNightHours / 12.0;

How to Handle Actual Time with Durations in C#?

What's the preferred approach to compare a complete DateTime instance with an hour, minute, and second which represents an actual time of day, with the ability to operate over those triplets (eg add hours, minutes seconds..)?
My current approach is something like
DateTime startHour = new DateTime(1900,1,1,12,25,43);
DateTime endHour = new DateTime(1900,1,1,13,45,32);
// I need to, say, know if a complete DateTime instance
// is later than startHour plus 15 minutes
DateTime now = DateTime.Now();
startHour = startHour.addMinutes(15);
if (now.CompareTo(new DateTime(now.Year, now.Month, now.Day, startHour.Hour,
startHour.Minute, startHour.Second)) > 0)
{
//I can do something now
}
This is very cumbersome and even failure prone. TimeSpans are not a solution as far as I can see, because they represent spans and aren't bound by the 24 hours limit (a TimeSpan of 56 hours 34 minutes is valid.)
What's the preferred approach for this type of calculations?
It's not at all clear what you mean by "is greater than startHour"... but taking
TimeSpan startHour = new TimeSpan(12, 25, 43);
if (endHour.TimeOfDay > startHour)
{
...
}
... works pretty simply.
By all means add argument checking to make sure that you don't specify a value for startHour which is < 0 or > 23 hours, but that's all pretty easy.
.NET's date and time API is quite primitive (even in 3.5) compared with, say, Joda Time - but in this particular case I think it's not too bad.
A little hint - .NET supports arithmetic operations on DateTime objects, and returns a TimeSpan object. Thus, you can do the following:
DateTime fromDate = ....
DateTime toDate = ....
TimeSpan diff = toDate - fromDate;
and you can expand this to:
DateTime fromDate = DateTime.Now;
DateTime toDate = DateTime.Now.addMinutes(x);
if ((toDate - fromDate).TotalMinutes > 15) {
...
}
You should use TimeSpan for startHour and endHour.
When comparing with now, you should "convert" them to a full DateTime or get the Time with DateTime.TimeOfDay as mentioned by Jon Skeet.
TimeSpan startHour = new TimeSpan(12, 25, 43);
DateTime now = DateTime.Now;
if (now.CompareTo(DateTime.Today.Add(startHour)) > 0) {
//...
}
or
TimeSpan startHour = new TimeSpan(12, 25, 43);
DateTime now = DateTime.Now;
if (now.TimeOfDay.CompareTo(startHour) > 0) {
//...
}
So you're only interested in the time component of the date.
if(DateTime.Now.TimeOfDay > startHour.TimeOfDay)
{
// do stuff
}
What's wrong with doing this?

Categories

Resources