讓我先給大家簡單介紹一下我們的應用程序! (請不要立即關閉我的大帖子,它很簡單的場景,我只是描述!)我們有一個ASP.NET網站,主要是C#,它充當我們所有商店的商店前臺特許經營權。服務器上的ASP.NET時間轉換(夏令時)
每家商店可能位於不同的時區,但我們需要在我們的網站上指明商店是開放還是關閉。
服務器有一個DB,其中包含的行可以指示不同商店的不同時間表,可以在我們的asp.net網站上顯示。
在我的數據庫中,我有保存位置偏移量的列和行,並以UTC存儲小時。例;
- LocationID:21
- 的timezoneoffset:-5:00
- SundayOpen:15:45
- SundayClose: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;
}
感謝您抽出時間來看看我的解決方案!任何「單挑」或「禁止」都將不勝感激!在你的程序代碼
感謝一大堆。 TimeZoneInfo類正是我正在尋找的。我認爲.NET可能比我更好地處理DST。雖然,schedule.LocationID不是時區,但我想我可以調整它以適應。在我嘗試完成後會更新你! – clamchoda
對不起,花了我很長時間纔回到這裏,我的盤子上有一堆。這正是我正在尋找的,謝謝。 – clamchoda