Let me start off by giving you all a brief run down of our application! (Please don't be instantly turned off by my large post, its quite simple scenario, I was just descriptive!) We have an ASP.NET web site, mostly C#, which acts as a store front for all of the stores within our franchise.
Each store could be in a different timezone, but we need to indicate on our site if the store is open or closed.
The server has a DB which hold rows that indicate different time schedule for different stores which could be presented with on our asp.net website.
In my database, I have columns and rows which hold the Location offset, and store hours in UTC. Example;
- LocationID: 21
- TimeZoneOffSet: -5:00
- SundayOpen: 15:45
- SundayClose: 16:20
I have come up with a way to determine on the server if the location is open or not. I am having a tough time determining if it will work with Daylight Savings Time. My question is, does this account for Daylight Savings Time, and have I gone about this scenario correctly, as I have not dealth with Time like this?
Here's what I do in my server side code;
//Here is the important parts of my class that I use to hold store schedule from the DB
public class TimeSchedule
{
public TimeSpan locationOffset { get; set; }
public TimeSpan sundayOpen { get; set; }
public TimeSpan sundayClose { get; set; }
public TimeSchedule()
{
locationOffset = new TimeSpan();
sundayOpen = new TimeSpan();
sundayClose = new TimeSpan();
}
}
//I have loaded a TimeSchedule object by id
TimeSchedule schedule = location.getTimeScheduleByLocationID(21);
// Get the UTC time
DateTime utcNow = new DateTime();
utcNow = DateTime.UtcNow;
//Get the offset value that we stored in our schedule object
TimeSpan locationOffSetHour = schedule.locationOffset;
//I then apply the location offset hour to the server utc time.
DateTime locationTime = new DateTime();
locationTime = utcNow.Add(locationOffSetHour);
// Get the day of the week from our locationTime that we off setted from UTC
string LocationWeekDay = locationTime.DayOfWeek.ToString();
// Now for each case of week day, I check to see if the difference in time is >= 0
// If so I assume the store is open
TimeSpan zeroDifference = new TimeSpan(0, 0, 0);
// This switch case just gets the difference in time according to LocationTime (that we offset'd from UTC) and stored time for the location on the server.
// Then verifies that the location is open for that day
switch (LocationWeekDay)
{
case "Sunday":
// Verify that location is open, turn on open sign
TimeSpan differenceOpenTimeSun = new TimeSpan();
differenceOpenTimeSun = locationTime.TimeOfDay - schedule.sundayOpen;
TimeSpan differenceCloseTimeSun = new TimeSpan();
differenceCloseTimeSun = schedule.sundayClose - locationTime.TimeOfDay;
if (differenceOpenTimeSun >= zeroDifference && differenceCloseTimeSun > zeroDifference)
{
imgSign.ImageUrl = "~/SiteImages/open.jpg";
}
else
{
imgSign.ImageUrl = "~/SiteImages/closed.jpg";
}
break;
}
Thank you for taking the time to look at my solution! Any "Heads-up" or "no-no's" would be greatly appreciated as well!