c# Timezones question
I implemented a country/region/city database 开发者_运维技巧for my site. Each city has a timezoneoffset stored as Varchar(32) in the format -06:00 , +09:00 etc. Now I'm not quite sure how to convert my UTC times which are stored in the database with those specific offsets. I know it's possible to convert using TimeZoneInfo.FindSystemTimeZoneById but that's not possible with the 'semi numeric' values I have.
Does anyone have an idea on how to go around this?
Thank you!
If you've got offsets, then you want DateTimeOffset
- you don't really need a time zone. On the other hand, you should be aware that what you've got won't accurately reflect local time, because a single offset is inadequate for most places due to daylight saving time.
I would suggest you store an actual time zone ID... and if you definitely want to store a single offset instead, I suggest you store the number of minutes in an integer field - there's no point in introducing a string representation which you just have to parse again.
You could use TimeZoneInfo.CreateCustomTimeZone()
for this:
DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo targetTimeZone = TimeZoneInfo.CreateCustomTimeZone("MyId", TimeSpan.FromHours(-6), "Somewhere", "Somewhere");
DateTime targetTime = TimeZoneInfo.ConvertTime(utcTime, targetTimeZone);
精彩评论