将日期时间偏移应用于美国东部时区
我有一个生成 vCal 和 iCal 文件以将事件导入 Outlook 和 iCal 的页面。这些邮件客户端(Outlook 和 iCal)将采用 UTC 格式的日期,因为它们知道用户的偏移量并会处理输入正确的时间。我们为其编写此代码的客户位于美国东部时区,我也是。他们的主机位于中部时区(比东部时区晚 1 小时)。我想让代码在没有任何硬编码偏移的情况下处理这个问题。我的代码当前从我们的 CMS 获取日期时间,并将其转换为 Outlook/iCal 的 UTC。这意味着 CMS 中的日期时间值是相对于托管位置的:
// Start Date Time
sb.AppendFormat("DTSTART:{0}\n", thisEvent.StartDate.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
当我在本地运行此值时,一切都很好,因为 CMS 中的日期时间基于我们客户的东部时区,我也是如此,所以过程是东部->世界标准时间 ->进入 Outlook,然后进入 UTC ->东方,一切都好。当我部署到中部的服务器时,CMS 的日期时间与东部相差一个小时。如何在转换为 UTC 之前自动获取与东部的偏移量并将其添加进去?我显然需要它来处理标准时间(EST)和夏令时(EDT)。我想以编程方式执行此操作,而不需要硬编码值,因此无论您位于哪个时区,它总是可以正常工作。即,如果我将此代码交付给印度的某人进行开发,它应该自动处理他们的本地服务器的偏移量和进行相应调整。
我需要做这样的事情,在转换为 UTC 之前对东部时间应用偏移量:
sb.AppendFormat("DTSTART:{0}\n", thisEvent.StartDate.ApplyEasternOffset().ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
抱歉,如果这是之前讨论过的主题,我只是不确定找到这样的问题的最佳术语。提前致谢。
I have a page that generates vCal and iCal files to import events into Outlook and iCal. These mail clients (Outlook & iCal) will take in the dates in UTC since they know the user's offset and will handle putting in the right times. Our client for which we're writing this code is based out of the USA Eastern time zone and so am I. Their host is in the Central time zone (1 hour behind Eastern). I'd like to make the code handle this without any hard-coded offsets. My code currently get's the DateTime from our CMS and converts to UTC for Outlook/iCal. That means the DateTime value from the CMS is relative to the hosting location:
// Start Date Time
sb.AppendFormat("DTSTART:{0}\n", thisEvent.StartDate.ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
When I run this on my local, it's all fine, because the DateTime in the CMS is based on our client's Eastern time zone and so am I, so the process is Eastern -> UTC -> into Outlook which then goes UTC -> Eastern, and everything is good. When I deploy to the server in Central, the DateTime from the CMS is an hour off from Eastern. How can I automatically get the offset from Eastern and add it in before I convert to UTC? I obviously need this to handle both standard time (EST) and daylight time (EDT). I'd like to do it programatically without hard-coded values so it always works correctly no matter what time zone you're in. I.e. if I deliver this code to someone for development in India, it should automatically handle their local server's offset and adjust accordingly.
I need to do something like this where I apply an offset to Eastern time before converting to UTC:
sb.AppendFormat("DTSTART:{0}\n", thisEvent.StartDate.ApplyEasternOffset().ToUniversalTime().ToString("yyyyMMddTHHmmssZ"));
Sorry if this is a topic that's been discussed before, I'm just not sure of the best terms to find a question like this. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 TimeZoneInfo 类。它有方法获取任何时区的 UTC 偏移量。
http://msdn.microsoft.com/en-us/library/ system.timezoneinfo.aspx
另外,请查看之前的帖子:如何使用 TimeZoneInfo 在夏令时期间获取当地时间?
下面是一个显示从太平洋时间到东部时间转换的示例:
显示 4/4/2011 12:05 :31 当我的本地时钟显示 9:05:31 时。
Check out the TimeZoneInfo class. It has methods to get UTC offsets for any timezone.
http://msdn.microsoft.com/en-us/library/system.timezoneinfo.aspx
Also, check out this previous post: How to use TimeZoneInfo to get local time during Daylight Savings Time?
Here's an example showing a conversion from Pacfic to Eastern:
Shows 4/4/2011 12:05:31 when my local clock shows 9:05:31.
您可以使用 TimeZoneInfo 类。
看看这个示例:
You can use TimeZoneInfo Class.
Take a look at this sample: