【问题标题】:how can i calculate different timezone (following daylight saving) from a given time我如何从给定时间计算不同的时区(夏令时之后)
【发布时间】:2013-04-09 17:58:27
【问题描述】:

我有一个在线图书销售网站,管理员和实体书店位于印度,但服务器托管在美国服务器上。 管理员要求创建成功订单报告,并在每天上午 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

在服务器位置在 4 月 12 日左右调整为夏令时(丹佛(MDT))之前,此代码一直正常工作。

请告诉我我做错了什么?我假设由于 GMT 时间始终是恒定的,我计算 gmt 时间,然后计算印度时间,然后从服务器时间减去印度时间(差异)。我将印度时间从数据库中设置为 frm 和组件,然后再次添加计算差异以获得等效的服务器时间。我的逻辑是否错误。我读过关于夏令时的文章,但由于我的国家不遵循它,我发现它的实际含义很难(它说一天也可以是 23 小时或 25 小时)。请纠正我。

【问题讨论】:

  • 提到的时间(上午 11 点和下午 4 点)是在哪个时区,您如何将值存储在数据库中?
  • 上午 11 点和下午 4 点采用印度时间 (+5:30) 日期时间值仅存储在服务器时间中。

标签: c# asp.net datetime dst


【解决方案1】:

您可以参考以下代码:

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

希望对您有所帮助。

【讨论】:

  • 您能否通过评论来详细说明您的代码,我真的觉得日期时间很难理解。
猜你喜欢
  • 2017-07-11
  • 2011-07-25
  • 1970-01-01
  • 2012-12-18
  • 2023-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
相关资源
最近更新 更多