我有一个销售网站的在线图书,管理和实体书店位于印度,但服务器托管在美国服务器上。 管理员要求创建成功订单的报告,并在上午11:00和下午4:00每天发送两次邮件。
上午11:00他应该在下午4:00(前一天)和上午11点(同一天)之间获得订单清单
下午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时间然后印度时间然后从服务器时间I减去印度时间(差异)。我创建印度时间frm和数据库中的组件然后再次添加计算差异以获得等效的服务器时间..我的逻辑是错误的。我读到了关于日光节约的信息,但由于我的国家不遵守它,我发现它的实际意义很难(它说一天也可能是23小时或25小时)。请纠正我。
答案 0 :(得分: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);
希望它有所帮助。