确定某个时区的时间是否在范围内 [英] Determine if the time an a certain timezone is within a range

查看:73
本文介绍了确定某个时区的时间是否在范围内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司的支持人员在CST上午7点至晚上7点可用,我们尝试通过显示一个按钮来发起聊天或显示一条消息,指出他们不可用,以在我们的网站上反映出来.支持人员位于中部时间,但是托管我们网站的服务器可以位于任何时区.(我是远程工作,当我在计算机上本地运行项目时,例如是东部时间)

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)

现在有一个错误,表明您不在CST下午6点的时间范围内,这还为时过早一个小时.这似乎是因为我在CST的时间范围实际上是UTC时间的2天(从下午1点到第二天凌晨1点),而我的时间是UTC日期的基础.我知道这是导致问题的原因,但是我不确定解决此问题的最佳方法.

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.

这是从一个将布尔值返回到控制台应用程序以注销eh值的函数转换的代码.

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

查看以下代码,或在此处使用它: https://dotnetfiddle.net/Hzhv8n

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?

推荐答案

您所做的超出了您的需要.我个人会使用我的 Noda Time 项目,但是使用 可以轻松完成所有这些操作BCL,使用 TimeZoneInfo.ConvertTime :

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
}

这篇关于确定某个时区的时间是否在范围内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆