私たちのアプリケーションの簡単な概要を説明することから始めましょう! (非常に単純なシナリオです。説明を書いただけです!) 私たちは、ASP.NET Web サイトを持っています。ほとんどが C# で、私たちのすべての店舗の店頭として機能します。フランチャイズ。
各店舗は異なるタイムゾーンにある可能性がありますが、店舗が開いているか閉じているかをサイトに表示する必要があります.
サーバーには、asp.net Web サイトで表示できるさまざまな店舗のさまざまなタイム スケジュールを示す行を保持する DB があります。
私のデータベースには、場所のオフセットを保持する列と行があり、UTC で時間を格納しています。例;
- ロケーションID: 21
- TimeZoneOffSet: -5:00
- 日曜開場: 15:45
- 日曜定休日: 16:20
その場所が開いているかどうかをサーバー上で判断する方法を考え出しました。サマータイムで動作するかどうかを判断するのに苦労しています. 私の質問は、これは夏時間を考慮しているのか、そしてこのような時間を扱っていないので、このシナリオを正しく行ったのでしょうか?
サーバー側のコードで行うことは次のとおりです。
//これは、DB からストア スケジュールを保持するために使用するクラスの重要な部分です。
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;
}
私の解決策を見てくれてありがとう!「ヘッズアップ」または「ノーノー」も大歓迎です!