[英]How get correct time zone offset on daylight saving in C#
I am struggling to get a correct datetimeoffset when switching from winter to summer time. 从冬季切换到夏季时,我正努力获得正确的datetimeoffset。
What I am trying to do is to send a request to an API, and the parameters are two dates with following format:" 2018-03-01T01:00:00+01:00
" I have created two Datetimeoffset in Paris TimeZone (my PC is in the UK but the API is a french service), here is how I did this: 我想做的是向API发送请求,参数是两个日期,格式如下:“ 2018-03-01T01:00:00+01:00
”我在巴黎TimeZone中创建了两个Datetimeoffset(我PC在英国,但API是法国服务),这是我的操作方式:
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
DateTime dt = new DateTime(2018,03,01,00,00,00);
dt = DateTime.SpecifyKind(dt, DateTimeKind.Local);
DateTimeOffset startingDate = new DateTimeOffset(dt);
startingDate = TimeZoneInfo.ConvertTime(startingDate, timeZoneInfo);
DateTimeOffset endingDate = new DateTimeOffset();
for (int i = 0;i<700;i++)
{
startingDate = startingDate.AddMonths(i);
endingDate = startingDate.AddMonths(1);
IRestResponse myquery= getAPIresult("", "", startingDate.ToString("yyyy-MM-ddTHH:mm:sszzzzzz"), endingDate.ToString("yyyy-MM-ddTHH:mm:sszzzzzz"));
When executing the code, I get " 2018-03-01T01:00:00+01:00
" for startingDate, which is what I expect. 执行代码时,我对startingDate收到“ 2018-03-01T01:00:00+01:00
”,这是我期望的。
But I get " 2018-04-01T01:00:00+01:00
" for endingDate, which is strange as the 31st of March is the daylight saving in France, so the Paris timeZone such from GMT+01 to GMT+02. 但是我得到的EndingDate为“ 2018-04-01T01 2018-04-01T01:00:00+01:00
”,这很奇怪,因为3月31日是法国的夏令时,因此巴黎的时区从GMT + 01到GMT + 02。
So I should get " 2018-04-01T01:00:00+02:00
" for ending date. 所以我应该得到“ 2018-04-01T01:00:00+02:00
”作为结束日期。
If you could help me on this, I would really be thankfull. 如果您能在这方面帮助我,我将非常感激。
You must call TimeZoneInfo.ConvertTime
within the loop, such that every value is re-evaluated against the time zone. 您必须在循环内调用TimeZoneInfo.ConvertTime
,以便针对时区重新评估每个值。 (The DateTimeOffset
caries only the offset, not the time zone.) ( DateTimeOffset
仅显示偏移量,而不显示时区。)
Also, your logic of .AddMonths(i)
is in error, as you are mutating the startingDate
in the loop. 还有,你的逻辑.AddMonths(i)
是错误的,因为你是变异的startingDate
的循环。 You can either use .AddMonths(1)
, or you can hold the original starting date in a separate variable that doesn't mutate. 您可以使用.AddMonths(1)
,也可以将原始开始日期保存在一个不会变异的单独变量中。
The simplest fix is thus: 因此,最简单的解决方法是:
startingDate = TimeZoneInfo.ConvertTime(startingDate.AddMonths(1), timeZoneInfo);
endingDate = TimeZoneInfo.ConvertTime(startingDate.AddMonths(1), timeZoneInfo);
Additionally, you might want to think about why you're introducing DateTimeKind.Local
in the starting date at all. 另外,您可能想考虑为什么在开始日期中全部引入DateTimeKind.Local
。 It should be irrelevant for this task. 这与这项任务无关。 Consider perhaps instead: 考虑一下吧:
DateTime dt = new DateTime(2018, 3, 1, 0, 0, 0);
TimeSpan offset = timeZoneInfo.GetUtcOffset(dt);
DateTimeOffset startingDate = new DateTimeOffset(dt, offset);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.