Ratna
Ratna

Reputation: 2319

how can i calculate different timezone (following daylight saving) from a given time

I have a online book selling website, the admin and physical bookstore is situated in India, but the server is hosted on USA server. The admin has demanded to create a report of successful orders and mail him twice a day at 11:00 am and 4:00 pm.

At 11:00am he should get the list of orders placed between 4:00pm(previous day) and 11:am (same day)

At 4:00 pm he should get the order list placed between 11:00 am (same day) and 4:00pm (same day)

For Implementing it I created a table which holds a records for this mapping like if the page is being called at n time then the report should be generated between x and z time.

In the page load i am taking current time and and getting the from and to time. The code being used is as follows

    DateTime dt = DateTime.Now;//getting server time
    DateTime gmttime = DateTime.Now.ToUniversalTime();//getting gmt time
    DateTime indiantime = gmttime.AddHours(5).AddMinutes(30);//get india time(is always constant dont follows daylight saving)
    TimeSpan diff = dt-indiantime; // calculating the difference

    string runH = indiantime.ToString("%h"); // geting the hour component of india time
    string runTT = indiantime.ToString("tt"); //getting ap/pm component of india time
    string indiantimeformat = "dd/MM/yyyy h:m tt";
    CultureInfo c = System.Globalization.CultureInfo.InvariantCulture;
    string q = "SELECT frmH,frmTT,infrmprevious,toH,toTT,intoprevious FROM successfullordermailtiming WHERE runH = '"+runH+"' AND runTT = '"+runTT+"'";  //quering the database for getting from and when
    DataTable ddt = dtu.Table(q);
    if (ddt.Rows.Count>0)
    {
         object[] ob = ddt.Rows[0].ItemArray;
         string frmH= ob[0].ToString();
         string frmTT=ob[1].ToString();
         string infrmprevious=ob[2].ToString();

         string toH =ob[3].ToString();
         string toTT =ob[4].ToString();
         string intoprevious = ob[5].ToString();
         DateTime indianfrom = indiantime;
         if (infrmprevious == "1")
         {
              indianfrom = indiantime.AddDays(-1); // if previous the substract 1 day
         }
         DateTime indianto = indiantime;
         if (intoprevious == "1")
         {
              indianto = indianto.AddDays(-1);
         }
         //make indian frm
         string indianfrm = indianfrom.ToString("dd/MM/yyyy") + " " + frmH + ":0 " + frmTT; //making from india time
         string indiantoo = indianto.ToString("dd/MM/yyyy") + " " + toH + ":0 " + toTT; //making to india time
         indianfrom = DateTime.ParseExact(indianfrm, indiantimeformat, System.Globalization.CultureInfo.InvariantCulture);
         indianto = DateTime.ParseExact(indiantoo, indiantimeformat, System.Globalization.CultureInfo.InvariantCulture);

         DateTime serverfrm = indianfrom + diff; //calculating frm servertime
         DateTime serverto = indianto + diff;  //calculating to server time

This code was working correctly till the server place adjusted there for daylight saving (Denver (MDT) ) on around 12 april.

Please tell me what i am getting wrong ? I have assumed since GMT time is always constant i calculate gmt time and then india time then from server time I substract india time(difference). I make the india time frm and to component from data base then add the calculate difference again to get the equivalent server time.. Is my logic wrong. I read about day light saving but since my country dont follow it i find it hard what it means in real terms (It says a day can be of 23 hours or 25 hours also). Please correct me.

Upvotes: 1

Views: 1521

Answers (1)

Freelancer
Freelancer

Reputation: 9064

You can refer following code:

        var varLondon = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");//Returns Timezone based on particular ID, ID can be related to pasific timezone, Indian TimeZone,etc.
        var varGoogleplex = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
        var now = DateTimeOffset.UtcNow; //Gives you local clock time for your currrent time zone
        TimeSpan TSLondonOffset = varLondon.GetUtcOffset(now); //will return you timespan of current time zone and specified one.
        TimeSpan TSGoogleplexOffset = varGoogleplex.GetUtcOffset(now);

Hope its helpful.

Upvotes: 1

Related Questions