让我首先简要介绍一下我们的应用程序!(请不要被我的大帖子立即关闭,它的场景非常简单,我只是描述性的!)我们有一个 ASP.NET 网站,主要是 C#,它充当我们所有商店的店面特许经营权。
每家商店可能位于不同的时区,但我们需要在我们的网站上注明商店是营业还是关闭。
服务器有一个数据库,其中包含指示不同商店的不同时间安排的行,这些行可以显示在我们的 asp.net 网站上。
在我的数据库中,我有保存位置偏移量的列和行,并以 UTC 存储时间。例子;
- 位置编号:21
- 时区偏移:-5:00
- 周日开放时间:15:45
- 周日关闭:16:20
我想出了一种方法来确定服务器上的位置是否打开。我很难确定它是否适用于夏令时。我的问题是,这是否考虑了夏令时,我是否正确地处理了这种情况,因为我没有像这样处理时间?
这是我在服务器端代码中所做的;
//这是我用来从数据库保存存储计划的课程的重要部分
public class TimeSchedule
{
public TimeSpan locationOffset { get; set; }
public TimeSpan sundayOpen { get; set; }
public TimeSpan sundayClose { get; set; }
public TimeSchedule()
{
locationOffset = new TimeSpan();
sundayOpen = new TimeSpan();
sundayClose = new TimeSpan();
}
}
//I have loaded a TimeSchedule object by id
TimeSchedule schedule = location.getTimeScheduleByLocationID(21);
// Get the UTC time
DateTime utcNow = new DateTime();
utcNow = DateTime.UtcNow;
//Get the offset value that we stored in our schedule object
TimeSpan locationOffSetHour = schedule.locationOffset;
//I then apply the location offset hour to the server utc time.
DateTime locationTime = new DateTime();
locationTime = utcNow.Add(locationOffSetHour);
// Get the day of the week from our locationTime that we off setted from UTC
string LocationWeekDay = locationTime.DayOfWeek.ToString();
// Now for each case of week day, I check to see if the difference in time is >= 0
// If so I assume the store is open
TimeSpan zeroDifference = new TimeSpan(0, 0, 0);
// This switch case just gets the difference in time according to LocationTime (that we offset'd from UTC) and stored time for the location on the server.
// Then verifies that the location is open for that day
switch (LocationWeekDay)
{
case "Sunday":
// Verify that location is open, turn on open sign
TimeSpan differenceOpenTimeSun = new TimeSpan();
differenceOpenTimeSun = locationTime.TimeOfDay - schedule.sundayOpen;
TimeSpan differenceCloseTimeSun = new TimeSpan();
differenceCloseTimeSun = schedule.sundayClose - locationTime.TimeOfDay;
if (differenceOpenTimeSun >= zeroDifference && differenceCloseTimeSun > zeroDifference)
{
imgSign.ImageUrl = "~/SiteImages/open.jpg";
}
else
{
imgSign.ImageUrl = "~/SiteImages/closed.jpg";
}
break;
}
感谢您花时间查看我的解决方案!任何“提醒”或“禁止”也将不胜感激!