0

让我首先简要介绍一下我们的应用程序!(请不要被我的大帖子立即关闭,它的场景非常简单,我只是描述性的!)我们有一个 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;
}

感谢您花时间查看我的解决方案!任何“提醒”或“禁止”也将不胜感激!

4

3 回答 3

2

冒昧地重构。并不是说我会完全像这样做所有事情,因为我可能会对数据库中的打开和关闭值进行非规范化,但是......在这里......

//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;  

}

于 2012-06-04T19:22:31.310 回答
1

程序中的以下代码行处理时区并获取本地时间:

// 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,您有两种选择:

  1. 在数据库中为每个地方的 DST 期间使用一列,并执行逻辑以查找该地区的当前日期是否在 DST 期间,并相应地在时间进行 DST 更改。
  2. 如果您使用的是 .NET 3.5 及更高版本,则可以遵循此选项:

    DateTime 东部 = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "东部标准时间");

    TimeZoneInfo类仅在 .NET 3.5 及更高版本中可用。

于 2012-06-04T18:33:52.107 回答
0

一些建议和意见...


查看 C# TimeZoneInfo 类。 http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx

它包含一些可以使您的时区转换准确的方法。例如,您可以存储 TimeZoneId,而不是将 TimeZoneOffset 存储在数据库中。然后用...

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

希望这对您有所帮助。

于 2012-06-04T19:04:08.480 回答