1

My company has support staff that are available from 7am to 7pm CST, and we try to reflect that on our website by showing either a button to initiate a chat, or a message saying that they are not available. The support staff are located in Central time, but the servers that our site is hosted on could be in any time zone. (I work remotely and when I run the project locally on my machine, it's in Eastern time for example)

Right now there is a bug where this says you are outside of the time range at 6pm CST, which is an hour too early. This appears to be because my time range in CST actually spans 2 days in UTC time (1pm until 1am the next day), and i am basingmy times off of the UTC date. I know this is what's causing the problem, but I'm not sure of the best way to resolve this.

Here's the code, converted from a function that returns a boolean into a console app to log out eh values.

View the code below, or use it here: https://dotnetfiddle.net/Hzhv8n

//=================================================================================================
//Get hours & days of operation
//These normally come from a config file, but hardcoding them here for demo
int availableChatStartHour = 13; //7am CST in UTC time
int availableChatDuration = 12;  //12 hours from 7am CST is 7pm CST
string[] availableDaysForChat = "monday,tuesday,wednesday,thursday,friday".ToUpper().Split(',');
//=================================================================================================

//The current timezone-agnostic time.
var now = DateTime.UtcNow;

//Convert times to a date object
//-------------------------------------------------
//I THINK THIS IS WHERE MY BUG IS
var startTime = new DateTime(now.Year, now.Month, now.Day, availableChatStartHour, 0, 0, DateTimeKind.Utc);
//-------------------------------------------------
var endTime = startTime.AddHours(availableChatDuration);

//Company HQ is located in the central time zone
//The central time zone can experience daylight saving time.
//We need to determine if DST is currently active or not in CST
var cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var cstTime = TimeZoneInfo.ConvertTimeFromUtc(now, cstZone);
var isDstActive = cstZone.IsDaylightSavingTime(cstTime);

//If DST is active, then we need to subtract an hour from the start and end times.
if (isDstActive)
{
    startTime = startTime.AddHours(-1);
    endTime = endTime.AddHours(-1);
}

//Determine if the day of the week (in CST time) is available for chat
var isDayAvilable = availableDaysForChat.Contains(cstTime.DayOfWeek.ToString().ToUpper());

//Make sure the time of day is within the acceptable range
var isAfterStartTime = now > startTime;
var isBeforeEndTime = now < endTime;

//Now take everything into account and see if the chat is available
//return isDayAvilable && isAfterStartTime && isBeforeEndTime;


Console.WriteLine("NOW:   "+ now);
Console.WriteLine("START: "+ startTime);
Console.WriteLine("END:   "+ endTime);
Console.WriteLine("-------------------------------------");
Console.WriteLine("Day Available:       "+isDayAvilable);
Console.WriteLine("Is After Start Time: "+ isAfterStartTime);
Console.WriteLine("Is Before End Time:  "+ isBeforeEndTime);
Console.WriteLine("-------------------------------------");
Console.WriteLine("FINAL RESULT:        "+ (isDayAvilable && isAfterStartTime && isBeforeEndTime));

Any ideas on how to make this work as it should?

5
  • Do you really mean they're available 6am-6pmCST, or 6am-6pm Central Time?
    – Jon Skeet
    Jan 16, 2015 at 21:06
  • @JonSkeet I mean 7am until 7pm in the Central Standard Timezone. (The company is located in the Florida panhandle)
    – Chris Barr
    Jan 16, 2015 at 21:10
  • There's no such thing as a "Central Standard Timezone". There's the Central time zone, which observes Central Standard Time in the winter, and Central Daylight Time in the summer. I'm 99.9% sure that's what you mean...
    – Jon Skeet
    Jan 16, 2015 at 21:12
  • @JonSkeet ok, then yes. Sorry. I assumed wrong since it's called "Central Standard Time" in FindSystemTimeZoneById("Central Standard Time") - I just added the word "zone" on the end.
    – Chris Barr
    Jan 16, 2015 at 21:14
  • Yes, the Windows time zone IDs are really unfortunately named. It's better to use the rather more standard time zone IDs maintained by IANA, but you don't need to here.
    – Jon Skeet
    Jan 16, 2015 at 21:17

1 Answer 1

3

You're doing far more than you need to. Personally I'd use my Noda Time project, but all of this can be done fairly easily with the BCL, using TimeZoneInfo.ConvertTime:

var zone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
var centralTime = TimeZoneInfo.ConvertTime(DateTime.UtcNow, zone);
if (centralTime.Hour >= 7 && centralTime.Hour < 21 &&
    centralTime.DayOfWeek >= DayOfWeek.Monday &&
    centralTime.DayOfWeek <= DayOfWeek.Friday)
{
    // Yes, you have support staff
}
else
{
    // Nope, let the answerphone handle it
}
4
  • Excellent, thanks so much. This is a lot simpler and easier to understand then what I had before.
    – Chris Barr
    Jan 19, 2015 at 13:32
  • @ChrisBarr: I would avoid using the string representations - you could easily create a List<DayOfWeek>.
    – Jon Skeet
    Jan 19, 2015 at 13:48
  • The days are set in a web.config file so it can be updated quickly if needed. Then again... I doubt that will need to be updated in actuality. Monday-Friday is pretty standard. Good idea.
    – Chris Barr
    Jan 19, 2015 at 15:16
  • @ChrisBarr: Having it in a config file seems reasonable - but I'd parse that to a List<DayOfWeek> (i.e. string to enum) at startup rather than converting from enum to string later. That way you can spot typos on startup instead of finding you mysteriously don't get any calls on "Monady" :)
    – Jon Skeet
    Jan 19, 2015 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.