1

我有一个在线图书销售网站,管理员和实体书店位于印度,但服务器托管在美国服务器上。管理员要求创建成功订单报告,并在每天上午 11:00 和下午 4:00 给他发送两次邮件。

在上午 11:00,他应该得到下午 4:00(前一天)和上午 11:0(同一天)之间的订单列表

在下午 4:00,他应该在上午 11:00(同一天)和下午 4:00(同一天)之间获得订单列表

为了实现它,我创建了一个表,该表包含此映射的记录,例如如果在 n 时间调用页面,则应在 x 和 z 时间之间生成报告。

在页面加载中,我正在获取当前时间并获取从时间到到时间。正在使用的代码如下

    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.

4

1 回答 1

1

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.

于 2013-04-17T05:27:25.933 回答