c# - ASP.NET Time Conversion on server (Daylight Savings) -


let me start off giving brief run down of our application! (please don't instantly turned off large post, quite simple scenario, descriptive!) have asp.net web site, c#, acts store front of stores within our franchise.

each store in different timezone, need indicate on our site if store open or closed.

the server has db hold rows indicate different time schedule different stores presented on our asp.net website.

in database, have columns , rows hold location offset, , store hours in utc. example;

  • locationid: 21
  • timezoneoffset: -5:00
  • sundayopen: 15:45
  • sundayclose: 16:20

i have come way determine on server if location open or not. having tough time determining if work daylight savings time. question is, account daylight savings time, , have gone scenario correctly, have not dealth time this?

here's in server side code;

//here important parts of class use hold store schedule 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 timeschedule object id timeschedule schedule = location.gettimeschedulebylocationid(21);  // utc time datetime utcnow = new datetime(); utcnow = datetime.utcnow;  //get offset value stored in our schedule object timespan locationoffsethour = schedule.locationoffset;  //i apply location offset hour server utc time. datetime locationtime = new datetime(); locationtime = utcnow.add(locationoffsethour);   // day of week our locationtime off setted utc  string locationweekday = locationtime.dayofweek.tostring();  // each case of week day, check see if difference in time >= 0 // if assume store open     timespan zerodifference = new timespan(0, 0, 0);      // switch case gets difference in time according locationtime (that offset'd utc) , stored time location on server.    // verifies location open day      switch (locationweekday)       {           case "sunday":               // verify location 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; } 

thank taking time @ solution! "heads-up" or "no-no's" appreciated well!

took liberty refactor. not saying since denormalize open , close values in database but...here is...

//i have loaded timeschedule object id  timezoneinfo tzi = timezoneinfo.findsystemtimezonebyid(schedule.locationid);   // utc time  datetime utcnow = datetime.utcnow;   //i apply location offset hour server utc time.  datetime currentutctime = datetime.utcnow;  datetime locationtime = timezoneinfo.converttimefromutc(currentutctime, tzi);  // day of week our locationtime off setted utc  string locationweekday = locationtime.dayofweek.tostring();   // each case of week day, check see if difference in time >= 0  // if assume store open  timespan zerodifference = new timespan(0, 0, 0);   // switch case gets difference in time according locationtime (that offset'd utc) , stored time location on server.  // verifies location open day    switch (locationweekday)  {       case "sunday":             // verify location 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;   

}


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -