如何转换日期为UTC正常,然后再转换回来? [英] How to convert a date to UTC properly and then convert it back?

查看:489
本文介绍了如何转换日期为UTC正常,然后再转换回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挣扎与转换日期时间为UTC的概念,所有的东西我不理解正确。

I'm struggling with converting DateTime to UTC, the concept and all, something I'm not understanding correctly.

当我得到一个日期时间字符串,说:2013年7月10日,我简单地做

When I get a date time string, say "7/10/2013", I simply do

  Convert.ToDateTime("7/10/2013").ToUniversalTime();

这会记录它在数据库中2013年7月10日的上午4点00分00秒。服务器位于U.S东海岸(-5)。当然,7月2013期间,DST仍在观察到,在这段时间,所以偏移量是-4,这种额外的4小时上午四点○○分00秒记录为UTC。

This will record it as "7/10/2013 4:00:00 AM" in the database. Server is located at U.S East Coast (-5). Of course, during July 2013, DST is still being observed, so offset during that time is -4, such the extra 4 hours 4:00:00 AM" recorded as UTC.

正如我在写这篇文章,这是2014年2月和DST实际上是没有,所以抵消现在的问题是-5。在我的应用程序,这就是补偿我选择在我的应用程序。

As I'm writing this post, it's Feb 2014 and DST is not in effect, so offset right now is -5. In my application, that's the offset I choose in my application.

如果我申请-5偏移7 / 10 / 2013 上午4点00分零零秒,该日期将是将7 / 09 / 2013 下午十一时00分00秒

If I apply -5 offset to "7/10/2013 4:00:00 AM", the date will be will "7/09/2013 11:00:00PM".

这是错误的,关闭的一天。

问题1

那么,如何做我正确的UTC时间转换回?意思是,当用户加载我的应用程序目前已经在2014年2月(与时区偏移-5目前),2013年7月10日上午4时零零分00秒应该还是2013年7月10日,而不是2013年7月9日。

How then do I properly convert the UTC time back? Meaning, when a user loads my application right now in Feb 2014 (with timezone offset -5 currently), 7/10/2013 4:00:00AM should be still 7/10/2013, NOT 7/09/2013.

什么困惑我的是,由于.ToUniversalTime()采用服务器DST考虑,是有一个硬一套世界时,这不是受服务器所在????

What confuses me is, since .ToUniversalTime() takes server DST into consideration, is there a hard set "universal time" that's not affected by locations of where the server is????

问题#2

会发生什么事,当我有两个西部和东部沿海的服务器,写入数据库?如何申请知道,如果被记录的UTC时间是根据东部或西部海岸?

What happens, when I have servers in both west and east coast, writing to the database? How can an application tell, if a UTC time being recorded is based on east or west coast?

基本上,又如何能在code说,2013年7月10日上午4时00分○○秒是在东海岸建立一个UTC时间(即表示2013年7月10日0点0分美国东海岸),而不是在西海岸的服务器(即表示这是2013年7月9日20:00:00 PM美国西海岸)?

basically, how then can the code tell, "7/10/2013 4:00:00 AM" is a UTC time created on the East coast (that indicates 7/10/2013 00:00:00AM for U.S East Coast) and not by a server on the west coast (that indicates it's 7/09/2013 20:00:00pm" for U.S West Coast)?

抱歉,如果这听起来很愚蠢。任何的建议是AP preciated它。

Sorry, if that sounds stupid. Any advice is appreciated it.

==========最后的编辑,我目前的解决方案===============

==========Final Edit, My current solution===============

MIMO的回答是有道理的。我被搞糊涂了两件事情。

MiMo's answer makes sense. I was confused with two things.

  1. 什么存储在数据库UTC时间意味着到服务器?
  2. 什么是服务器的时间,应用程序用户的关系?

我的应用,可以从不同的时区的用户使用和一些用户是在同一时区作为服务器,有些则不是。一些旅行和因此,即使他们在同一个时区的服务器,他们可能会在不同的timzone所有的时间着陆。我的应用程序允许他们选择他们在时区而这种反映适当的时间。

My application can be used by users from different timezone and some users are in the same timezone as the server, some are not. Some travels and so even if they were in the same timezone as the server, they might land in a different timzone all the time. My application allows them to choose which timezone they are in and such reflect the time appropriately.

本来,我只是得到了UTC时间从数据库中,并减去它的用户的时区偏移。由于米姆认为,这是错误的。之所以能在我的岗位上可见。

Originally, I simply get the UTC time out of the database and subtract user's timezone offset from it. As Mimo suggested, it's wrong. The reason can be seen in my post above.

我原来的解决方案是让服务器的时区偏移,现在并用它来添加/ UTC减,这是错误的了。由于在2013年7月10日,服务器的补偿,在当时是-4。眼下,在2014年2月,服务器区偏移量为-5。解决这个当然是使用.ToLocalTime()

My original solution was to get server's timezone offset right now and use that to add/subtract from UTC, and that's wrong too. As on 7/10/2013, server's offset at the time was -4. Right now, in Feb 2014, server timezone offset is -5. Solution to that is of course is to use .ToLocalTime()

在我深入挖掘米姆的关于如何使用TimeZone.ToLocalTime(建议),这里是我做过什么,暂时解决问题。

Before I dig deeper into Mimo's suggestion on how to use TimeZone.ToLocalTime(), here is what I did to temporarily fix the issue.

  1. 获取UTC日期从数据库中,并转换为.ToLocalTime,这是服务器表演。所以到了服务器,2013年7月10日上午04时00分00秒就2013年7月10日上午12:00:00。

  1. Get the UTC date out of the database and convert to .ToLocalTime, which is what the server shows. so to the server, 7/10/2013 4:00:00AM becomes 7/10/2013 12:00:00AM.

获取服务器时区偏移。它显示-5目前,因为它是在美国东海岸。

Get the server timezone offset. It shows -5 currently, since it's at U.S East Coast.

获取用户的时区偏移。对于西海岸,用户选择-8现在。对于东海岸,用户选择-5现在。

Get the user's timezone offset. For west coast, user choose -8 right now. For east coast, user choose -5 right now.

获取用户的时区和服务器的时区之间的差异。西海岸是-3。东海岸为0。

Get the difference between user's timezone and server's timezone. West coast is -3. East Coast is 0.

减去2013年7月10日上午12时00分○○秒的差别,所以西海岸有一个截止日期2013年7月9日21点00分零零秒PM和东海岸有一个截止日期2013年7月10日上午12:00:00。

Subtract the differences from 7/10/2013 12:00:00AM, so west coast has a due date 7/09/2013 21:00:00PM and east coast has a due date 7/10/2013 12:00:00AM.

正确的。

非常感谢球员。现在的时间挖成TimeZone.ToLocalTime(),看看我是否可以减少一步2-5。

Thanks a lot guys. Now time to dig into TimeZone.ToLocalTime() and see if I can reduce step 2-5.

推荐答案

您转换回本地时间使用 ToLocalTime()。将看到,日期/时间是在七月,所以用DST,因此它会通过4个小时,而不是转移由5

You convert back to local time using ToLocalTime(). It will see that the date/time is in July, so with DST, and hence it will shift by 4 hours and not by 5.

如果你有一个客户端(如Web浏览器)连接的服务器不是你最终要转换为客户的本地时间的​​日期/时间服务器。要做到这一点,最好的方法是使用的 TimeZone.ToLocalTime() :发送到送达时区的客户端是,然后直接转换成时区所有的时间。

If you have a client (e.g. Web browser) connecting to the server you will ultimately want the date/time converted to the local time of the client, not of the server. To do this the best way is to use TimeZone.ToLocalTime(): send to the served the time-zone the client is in and then convert directly to that time-zone all the time.

从不加/减小时 - 总是经过时区和使用TimeZoone.ToLocalTime()。增加/减少小时将无法正常工作时,DST参与。

NEVER add/subtract hours - always go through the time zone and use TimeZoone.ToLocalTime(). Adding/subtracting hours won't work when DST is involved.

请注意,这是不可能的,从一个浏览器内获得当前(当地)时间区。如果您的客户端是一个浏览器,你需要从某种配置的时区或已在用户输入。

Note that it is not possible to get the current (local) time-zone from inside a browser. If your client is a browser you need to get the time-zone from some sort of configuration or have the user enter it.

还要注意的是,一旦你开始处理不同的时区,你再也不用只是日期 - 你总是要处理完整的日期,时间:如果你带或松动的时间部分全部转换就无法工作了。

Note also that once you start to handle different time zones you no longer have just dates - you always have to deal with complete date-times: if you strip or loose the time part all the conversions won't work any more.

关于问题2:UTC时间是通用的,不基于任何特定的时间段,因此,一旦转换为UTC,你不必担心任何更多的服务器的时区

Concerning question 2: UTC time is universal, not based on any specific time zone, hence once you convert to UTC you don't have to worry any more about the time-zone of the servers.

这篇关于如何转换日期为UTC正常,然后再转换回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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