How to determine if now(utc) is within the range of the given days of the week and times of the day in ISO 8601 format

我是研究僧i 提交于 2021-01-27 13:20:33

问题


I run into this question of how to determine if DateTime.UtcNow (e.g. 2018-01-01T20:00:00Z) falls within the given range of days and times that are in another timezone. There are no specific dates given, just the days of the week, and the time of the day. The given time is in ISO 8601 standard format.

To simplify this question, it can be how to check if a UTC time is within business hours in China.

For example, the given day and time range is given by someone form China in time zone +08:00, it can be: FromDayOfWeek = "Friday", FromTimeOfDay = "17:00:00+8:00", ToDayOfWeek = "Monday", ToTimeOfWeek = "08:00:00+8:00". I need to determine if "now" in China is sometime between the given range (Friday 17:00:00+8:00 - Monday 08:00:00+8:00).

I'm stuck at how to convert the DateTime and get the day of the week in that local time, since 2018-01-01T20:00:00Z is Monday in UK, but at the same time, since China is +08:00, it is already Tuesday in China.

My approach:

// parse the time to get the zone first (+08:00)
TimeSpan ts = TimeSpan.Parse("-08:00");

// Create a custom time zone since the time zone id is not given, and cannot be searched by SearchTimeZoneById
TimeZoneInfo tzi = TimeZoneInfo.CreateCustomTimeZone(zoneId, ts, displayName, standardName);
DateTime localDateTime = TimeZoneInfo.ConvertTime(Date.UtcNow, tzi);

String localDay = localDateTime.DayOfWeek;

// Determine if localDay is between FromDayOfWeek and ToDayOfWeek
// cast the days to integers from 1 (Monday) to 7 (Sunday)  
// create an array of days in integar days = [5, 6, 7, 1]
// if days.contains(localDays), check the times
...

Can anyone suggest some better solutions? I am not sure if mine works, and there are holes in how to deal with Day Light Saving time, since the zone will change, and how to check the time range. I am new to C#, any suggestions of libraries I can use is great!


回答1:


Instead of converting both start and end times from UTC, just convert the other time into UTC

    TimeZoneInfo chinaTimeZone = TimeZoneInfo.CreateCustomTimeZone(zoneID, TimeSpan.Parse("-08:00"), displayName, standardName);

    DateTime FromTime = new DateTime(2018, 0, 19, 13, 0, 0); // year, month, day, hour, minute, second : Friday 1pm
    DateTime ToTime = new DateTime(2018, 0, 21, 1, 0, 0); // year, month, day, hour, minute, second : Monday 1am

    DateTime nowinUTC = DateTime.UtcNow;
    DateTime nowInChina = TimeZoneInfo.ConvertTimeFromUtc(nowinUTC, chinaTimeZone);

    if(FromTime< nowInChina && ToTime> nowInChina)
    {
        // Time is within the from and two times
    }



回答2:


Per the comments on @Moffen's answer, you only want to check if Now is within a specific DayOfWeek range:

public void CheckAll(List<SomeClass> spans)
{
    var chinaTZ = TimeZoneInfo.CreateCustomTimeZone(zoneID, TimeSpan.Parse("-08:00"), displayName, standardName);

    var nowInChina = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, chinaTZ);

    foreach ( var span in spans )
    {
        if (InRange(nowInChina, span.startDay, span.endDay))
            // Do something on success 
            // Check for valid times here
            ;
        else
            // Do something on Failure
            ;
    }
}
public bool InRange(DateTime dateToCheck, DayOfWeek startDay, DayOfWeek endDay)
{
    // Initialise as one day prior because first action in loop is to increment current
    var current = (int)startDay - 1;

    do
    {
        // Move to next day, wrap back to Sunday if went past Saturday
        current = (current + 1) % 7;

        if (dateToCheck.DayOfWeek == (DayOfWeek)current)
            return true;

    } while (current != (int)endDay);

    return false;
}


来源:https://stackoverflow.com/questions/50421152/how-to-determine-if-nowutc-is-within-the-range-of-the-given-days-of-the-week-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!