东部时间应该是 -4 而不是 -5 ?如何在我的应用程序中存储时区偏移量?
Eastern time should be -4 not -5 ? How can I store the timezone offset on my application?
I am creating an application that saves the dates on universal time. Later I change the dates according to the timezone that the user specifies.
To save the date I do:
var dateToSaveOnDatabase = DateTime.Now.ToUniversalTime();
If I go to google I verify the utc date to be:
Now on my app I let the user specify the timezone manually with a number. Lets say the user chooses -5 just like on my computer:**
That means that in my code I have
var userLocalTime = DateTime.Now.ToUniversalTime().AddHours(-5)
If I do that userLocalTime
ends up to be equal to 8:00 PM! The eastern time should be 9:00 PM NOT 8:00 PM :/ . If I google what is the eastern time I get:
So my question is why is the eastern time equal to -5 if in reality it is -4 hours away? I am storing the user time zone in a number because the application is used on c# and also on linux. How should I store the timezone in my application?
最好在您的应用程序中存储时区代码,然后进行相应调整。这样你就可以考虑夏令时和其他奇怪的偏移量(不是每个人都有一个小时的偏移量,在少数情况下有些是 30 或 45 分钟)。
在 C# 中使用时区代码从 UTC 转换为用户的时区非常简单,如下所示:
DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
I am creating an application that saves the dates on universal time. Later I change the dates according to the timezone that the user specifies.
To save the date I do:
var dateToSaveOnDatabase = DateTime.Now.ToUniversalTime();
If I go to google I verify the utc date to be:
Now on my app I let the user specify the timezone manually with a number. Lets say the user chooses -5 just like on my computer:**
That means that in my code I have
var userLocalTime = DateTime.Now.ToUniversalTime().AddHours(-5)
If I do that
userLocalTime
ends up to be equal to 8:00 PM! The eastern time should be 9:00 PM NOT 8:00 PM :/ . If I google what is the eastern time I get:
So my question is why is the eastern time equal to -5 if in reality it is -4 hours away? I am storing the user time zone in a number because the application is used on c# and also on linux. How should I store the timezone in my application?
最好在您的应用程序中存储时区代码,然后进行相应调整。这样你就可以考虑夏令时和其他奇怪的偏移量(不是每个人都有一个小时的偏移量,在少数情况下有些是 30 或 45 分钟)。
在 C# 中使用时区代码从 UTC 转换为用户的时区非常简单,如下所示:
DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);