ASP.NET 服务器上的时间转换(夏令时)
本文关键字:转换 夏令时 时间 NET 服务器 ASP | 更新日期: 2023-09-27 18:36:11
首先,让我简要介绍一下我们的应用程序!(请不要立即被我的大帖子关闭,它非常简单的场景,我只是描述性的! 我们有一个 ASP.NET 网站,主要是 C#,它充当我们特许经营范围内所有商店的店面。
每家商店可能位于不同的时区,但我们需要在我们的网站上注明商店是营业还是关闭。
服务器有一个数据库,其中包含指示不同商店的不同时间表的行,可以在我们的 asp.net 网站上显示。
在我的数据库中,我有保存位置偏移量的列和行,并以 UTC 格式存储小时数。例;
- 位置 ID: 21
- 时区偏移量: -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;
}
感谢您抽出宝贵时间查看我的解决方案!任何"抬头"或"禁忌"也将不胜感激!
冒昧地重构。 并不是说我会完全按照这样做所有事情,因为我可能会对数据库中的打开和关闭值进行非规范化,但是......在这里。。。
//I have loaded a TimeSchedule object by id
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(schedule.LocationId);
// Get the UTC time
DateTime utcNow = DateTime.UtcNow;
//I then apply the location offset hour to the server utc time.
DateTime currentUtcTime = DateTime.UtcNow;
DateTime locationTime = TimeZoneInfo.ConvertTimeFromUtc(currentUtcTime, tzi);
// 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 = currentUtcTime.TimeOfDay - schedule.sundayOpen;
TimeSpan differenceCloseTimeSun = schedule.sundayClose - currentUtcTime.TimeOfDay;
if (differenceOpenTimeSun >= zeroDifference && differenceCloseTimeSun > zeroDifference)
{
imgSign.ImageUrl = "~/SiteImages/open.jpg";
}
else
{
imgSign.ImageUrl = "~/SiteImages/closed.jpg";
}
break;
}
以下代码行处理时区并获取本地时间:
// 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);
但是,此代码仅处理添加时区偏移值,而不处理 DST。
因此,如果您想获得DST,则有两种选择:
- 在
- 数据库中为地点的每个 DST 周期使用一列,并执行逻辑来查找该区域的当前日期是否在 DST 期间,并相应地更改时间的 DST。
如果您使用的是 .NET 3.5 及更高版本,则可以遵循此选项:
DateTime eastern = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Eastern Standard Time");
TimeZoneInfo 类仅在 .NET 3.5 及更高版本中可用。
一些建议和意见...
查看 C# TimeZoneInfo 类。 http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx
它包含一些方法,可以使您的时区转换准确。 例如,与其将 TimeZoneOffset 存储在数据库中,不如存储 TimeZoneId。 然后使用...
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById(string id);
然后而不是..
locationTime = utcNow.Add(locationOffSetHour);
你可以做...
DateTime locationTime = ConvertTimeFromUtc(utcNow, tzi) ;
以获取本地时间。 这将为您提供更准确的偏移量,因为它支持夏令时。
查看所有其他代码,并查看 TimeZoneInfo 类是否包含可以使用的属性和方法,而不是滚动自己的属性和方法。
您也不需要初始化一个值,然后立即覆盖它...
// Change this...
DateTime utcNow = new DateTime();
utcNow = DateTime.UtcNow;
// to this...
DateTime utcNow = DateTime.UtcNow;
// and this...
TimeSpan differenceOpenTimeSun = new TimeSpan();
differenceOpenTimeSun = locationTime.TimeOfDay - schedule.sundayOpen;
// to this...
TimeSpan differenceOpenTimeSun = locationTime.TimeOfDay - schedule.sundayOpen;
// etc...
您说您将一天中的时间以 UTC 格式存储在数据库中。 我假设您正在将其转换为此代码之外的本地时间,因为您正在这样做......
differenceCloseTimeSun = schedule.sundayClose - locationTime.TimeOfDay
您可以考虑对这两个值使用 UTC 时间,这样就不必在计算差值之前进行转换。
您可能仍然需要进行时区转换以确定星期几,但您不必进行转换来确定商店是营业还是关闭。
您并没有真正使用标准命名约定。 不是必需的,但我建议Microsoft标准作为起点。 使人们在 StackOverflow :) 上提问时更容易阅读代码
http://msdn.microsoft.com/en-us/library/ff926074.aspx
希望这对你有帮助。