开发者

C# TimeZone Calculation Problem

I have a server application that is processing loans that are in different t开发者_运维百科imezones. The server operates using UTC, a scheduled task will start a process on the server @ 08:00 Local time and select the loans in that time zone for processing. The server must check each loan then for certain loans set a timeout that will fire at a specific Local Time (adjusted for daylight savings), for argument say 17:30 the following day, it will need to calculate the UTC equivalent time then workout the number of Hours, Minutes & Seconds that will need to elapse before the timout will be reached.

The problem I think is translating business requirement for the timeout into a configurable setting. This requirement could change to 17:30 the same day or 17:30 the following week. I need a solution (if there is one) that would cope with a Daylight Savings Change occuring during the period where the timeout is reached.


So you just need to convert a local time in a particular time zone into a UTC time (which is appropriate for timeouts etc)?

Use TimeZoneInfo.ConvertTimeToUtc, passing in the local date/time as a DateTime, and the relevant TimeZoneInfo.

It's worth being aware that some local times may not exist, or may occur twice, around DST changes. For most time zones this happens early in the morning, local time, so it may not affect your business rules - but it's worth being aware of. You can detect this with TimeZoneInfo.IsAmbiguousTime and TimeZoneInfo.IsInvalidTime if you need to - although you'll need to work out what to do after you've detected it, of course.


If you know a local time, its timezone, and the required date you can always get that as Universal Time using TimeZoneInfo.ConvertTimeToUtc.

TimeZoneInfo is DST aware, so you should be able to diff the two universal (UTC) times you obtain this way (from current server's local time, and from target for timer expiry in loan timezone of interest) to determine the necessary timer interval.


Sounds simple enough - use DateTimeOffset.


I recommend against converting to UTC. When DST starts/ends is constantly changing and sometimes with little notice (Agentina once changed the statrt day to be just 12 days later). And it sounds like you want the process to run at 17:30 local time.

The only way to do this is to store the local time and the time zone. And note "local time" is not the time in that timezone, it's just a set time of day. Then when you need to calculate the next time to run, apply the timezone to the taime for the date you're running and get the UTC time of that next event.

So specific events - store in UTC. Reocurring events - store in local time & timezone.

More at http://blogs.windwardreports.com/davidt/2009/11/what-every-developer-should-know-about-time.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜