gpt4 book ai didi

.net - 为什么 new DateTimeOffset(DateTime.Today).AddDays(4).Offset 和 new DateTimeOffset(DateTime.Today.AddDays(4)).Offset 返回不同的值?

转载 作者:行者123 更新时间:2023-12-05 08:33:12 26 4
gpt4 key购买 nike

我目前在 UTC+1 时区。 TimeZoneInfo.Local.BaseUtcOffset 返回 +1。

new DateTimeOffset(DateTime.Today).AddDays(4).Offset 返回 +1。

但是 new DateTimeOffset(DateTime.Today.AddDays(4)).Offset 返回 +2!

更有趣的是,直到加上四天的偏移量是相同的。从四天开始调用 new DateTimeOffset(DateTime.Today.AddDays(x)).Offset 返回 +2 偏移量。为什么?

//Same offset +1
var offset2 = new DateTimeOffset(DateTime.Today).AddDays(7).Offset;
var offset22 = new DateTimeOffset(DateTime.Today.AddDays(7)).Offset;

//Same offset +1
var offset3 = new DateTimeOffset(DateTime.Today).AddDays(3).Offset;
var offset32 = new DateTimeOffset(DateTime.Today.AddDays(3)).Offset;

//Different offset +1 and +2
var offset4 = new DateTimeOffset(DateTime.Today).AddDays(4).Offset;
var offset42 = new DateTimeOffset(DateTime.Today.AddDays(4)).Offset;

//Different offset +1 and +2
var offset5 = new DateTimeOffset(DateTime.Today).AddDays(5).Offset;
var offset52 = new DateTimeOffset(DateTime.Today.AddDays(5)).Offset;

最佳答案

比我的评论更认真的回答...

来自 MSDN:

Because a DateTimeOffset object does not represent the date and time in a specific time zone, the AddDays method does not consider a particular time zone's adjustment rules when it performs date and time arithmetic.

因此

DateTimeOffset(DateTime.Today).AddDays(5)

不更改原始日期的时区(今天 = CET = UTC+1)

还有

DateTime.Today.AddDays(5)

给出 5 天后的本地时间 (CEST = UTC+2)。

所以

DateTimeOffset(DateTime.Today.AddDays(5))

将给出 CEST 偏移量 (UTC+2) 的时间

关于.net - 为什么 new DateTimeOffset(DateTime.Today).AddDays(4).Offset 和 new DateTimeOffset(DateTime.Today.AddDays(4)).Offset 返回不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42976786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com