C#时区计算问题 [英] C# TimeZone Calculation Problem

查看:163
本文介绍了C#时区计算问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在处理在不同的时区贷款服务器应用程序。
服务器操作使用UTC,计划任务将在服务器上启动一个进程@ 08:00当地时间并选择在处理该时区的贷款。服务器必须检查每个贷款则设置超时,将触发在特定当地时间一定的贷款(调整夏令),因为参数说17:30第二天,就需要计算UTC等效时间再锻炼的小时,分钟和安培数;秒,将需要经过的timout必达之前。

I have a server application that is processing loans that are in different timezones. 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.

我觉得是翻译的超时业务需求成一个可配置的设置的问题。这一要求可能会改变至17:30当天或17:30在接下来的一周。我需要一个解决方案(如果有),将与在其中达到超时周期夏令变化存在的应对。

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.

推荐答案

所以,你只需要一个本地时间转换在一个特定的时间段成UTC时间(这是适合超时等)?

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

使用的 TimeZoneInfo.ConvertTimeToUtc ,传递当地的日期/时间为的DateTime ,以及相关的的TimeZoneInfo

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

这是值得意识到,一些本地时间可能不存在,或者可能会出现两次,围绕DST更改。对于大多数时区这种情况发生在早晨,当地时间早,所以也不会影响您的业务规则 - 但它是值得意识到的。您可以检测到这种 TimeZoneInfo.IsAmbiguousTime TimeZoneInfo.IsInvalidTime 如果你需要 - 尽管你需要工作出你发现它,当然以后做什么。

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.

这篇关于C#时区计算问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆