如何确定 now(utc) 是否在 ISO 8601 格式的给定星期几和一天中的时间范围内
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
我 运行 进入这个问题,即如何确定 DateTime.UtcNow
(例如 2018-01-01T20:00:00Z)是否在另一个时区的给定日期和时间范围内.没有给出具体日期,只有星期几和一天中的时间。给定时间采用 ISO 8601 标准格式。
为了简化这个问题,可以是如何检查UTC时间是否在中国的工作时间内。
比如给定的日期和时间范围是中国人在+08:00时区给出的,可以是:FromDayOfWeek = "Friday", FromTimeOfDay = "17:00:00+8:00", ToDayOfWeek = "Monday", ToTimeOfWeek = "08:00:00+8:00".
我需要判断"now"在中国是不是在给定范围之间的某个时间(星期五 17:00:00+8:00 - 星期一 08:00:00+8:00)。
我被困在如何转换 DateTime 并获取当地时间的星期几,因为 2018-01-01T20:00:00Z 在英国是星期一,但与此同时,因为中国是+08:00,在中国已经是星期二了。
我的做法:
// 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
...
任何人都可以提出一些更好的解决方案吗?我不确定我的是否有效,并且在如何处理夏令时方面存在漏洞,因为区域会改变,以及如何检查时间范围。我是 C# 的新手,任何我可以使用的库的建议都很棒!
不要将开始时间和结束时间都转换为 UTC,只需将另一个时间转换为 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
}
根据@Moffen 的评论,您只想检查 Now
是否在特定的 DayOfWeek
范围内:
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;
}
我 运行 进入这个问题,即如何确定 DateTime.UtcNow
(例如 2018-01-01T20:00:00Z)是否在另一个时区的给定日期和时间范围内.没有给出具体日期,只有星期几和一天中的时间。给定时间采用 ISO 8601 标准格式。
为了简化这个问题,可以是如何检查UTC时间是否在中国的工作时间内。
比如给定的日期和时间范围是中国人在+08:00时区给出的,可以是:FromDayOfWeek = "Friday", FromTimeOfDay = "17:00:00+8:00", ToDayOfWeek = "Monday", ToTimeOfWeek = "08:00:00+8:00".
我需要判断"now"在中国是不是在给定范围之间的某个时间(星期五 17:00:00+8:00 - 星期一 08:00:00+8:00)。
我被困在如何转换 DateTime 并获取当地时间的星期几,因为 2018-01-01T20:00:00Z 在英国是星期一,但与此同时,因为中国是+08:00,在中国已经是星期二了。
我的做法:
// 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
...
任何人都可以提出一些更好的解决方案吗?我不确定我的是否有效,并且在如何处理夏令时方面存在漏洞,因为区域会改变,以及如何检查时间范围。我是 C# 的新手,任何我可以使用的库的建议都很棒!
不要将开始时间和结束时间都转换为 UTC,只需将另一个时间转换为 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
}
根据@Moffen Now
是否在特定的 DayOfWeek
范围内:
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;
}