簡體   English   中英

確定某個時區是否在某個范圍內的時間

[英]Determine if the time an a certain timezone is within a range

我的公司有支持人員,可在美國中部標准時間早上7點到晚上7點之間使用,我們會嘗試通過顯示發起聊天的按鈕或顯示他們不可用的消息在我們的網站上反映出來。 支持人員位於中部時間,但我們站點所在的服務器可以位於任何時區。 (我在遠程工作,當我在我的機器上本地運行項目時,例如在東部時間)

現在有一個錯誤,這表示你在中部標准時間下午6點之外的時間范圍之外,這是一個小時太早。 這似乎是因為我在CST的時間范圍實際上在UTC時間跨越2天(下午1點到次日凌晨1點),並且我基於UTC日期離開時間。 我知道這是導致問題的原因,但我不確定解決這個問題的最佳方法。

這是代碼,從一個返回布爾值的函數轉換為控制台應用程序以注銷eh值。

查看下面的代碼,或在此處使用: 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));

有關如何使其正常工作的任何想法?

你所做的遠遠超過你的需要。 就個人而言,我會使用我的Noda Time項目,但使用TimeZoneInfo.ConvertTime 可以很容易地使用BCL完成所有這些:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM