迷失在日期和时区 [英] Lost in dates and timezones

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

问题描述

我的工作存储会议与他们的开始和结束日期的应用程序。到现在为止,我正在制定在比利时和我的服务器是在法国,所以一切都在同一个时区,没有任何问题。但今天,我在旧金山,我的服务器是在法国,我发现我有一个bug。

I'm working on an application that stores conferences with their start and end date. Up until now, I was developing in Belgium and my server is in France, so everything is in the same timezone, no problem. But today, I'm in San Francisco, my server is in France and I noticed I have a bug.

我是从一个Flex客户端设置日期根据客户当地的时区,也就是今天GMT-8,我(动作自动调整的日期显示。我的服务器上的Hibernate和MySQL在法国(GMT + 1运行)。因此,当我看着我的数据库使用phpMyAdmin,我看到设置为2010-06-07 00:00:01,但在我的Flex客户端会显示一个日期2010-06-06十五点00分01秒。

I'm setting dates from a Flex client (ActionScript automatically adapts date display according to client local timezone, which is GMT-8 for me today. My server runs on Hibernate and MySQL in France (GMT+1). So when I look at my database using phpMyAdmin, I see a date set to "2010-06-07 00:00:01" but in my Flex client it displays "2010-06-06 15:00:01".

最后,我想的是,日期显示在事件,这是我将它设置为日期的本地时区。所以,当我在比利时和我设置一个事件的开始日期为2010-06-07 00:00:01我想找回这种方式。

Ultimately, what I want is that the dates are displayed in the local timezone of the event, which is the date I set it to. So when I'm in Belgium and I set the start date of an event to be "2010-06-07 00:00:01" I want to retrieve it that way.

不过,我迷路了,什么层适应什么。是时区存储在MySQL的DATETIME列(我不能看到它在MySQL的)?请问Hibernate的任何东西给它,当它转移到java.lang.Date具有时区信息?而最终,什么是解决这个烂摊子的最佳方法是什么?

But I'm lost as to what layer adapts what. Is timezone stored in DATETIME MySQL columns (I can't see it in MySQL)? Does Hibernate to anything to it when it transfers it to java.lang.Date that has Timezone information? And ultimately, what is the best way to solve this mess?

推荐答案

这确实是一个烂摊子。我有writen <一href="http://stackoverflow.com/questions/2532729/daylight-saving-time-and-timezone-best-practices/2533378#2533378">something它之前。

It's truly a mess. I've writen something about it before.

在你的情况,我建议:每个会议都有(概念)输入本地DATETIME的启动时间,而且还具有时区 (时区可以是隐含在它的位置,或者存储 明确地沿着其他数据)。 这在概念上是正确的:如果用户说:本次会议 开始于上午11点,他表示目前在该时钟 它是城市的标志11:00 AM(即使明天GOVERMENT决定改变这个国家GMT偏移量)。 这也比较容易实现,因为Java和MySQL都不是很聪明的约 时区。只要坚持(在方案=这个跨pretation:日期时间为会议活动的地方,他们不会再present一个时间点在时间的物理连续线,而是一个公民的概念(想想。它作为-almost-字符串)。缺点,当然,是它将更加困难的事时间计算,特别内的数据块中。

In your case, I'd advise: each conference has (conceptually) a start-time of type "LOCAL DATETIME", and also has a timezone (the timezone can either be implicit in its location, or stored explicitly along the other data). That is conceptually correct: if the user says "The conference starts at 11:00 am", he means "at the moment at which the clocks it that city marks 11:00 am" (even if tomorrow the goverment decides to change the country GMT offset). It's also relatively simple to implement, as Java and Mysql are not very clever about timezones. Just stick (in your scenario= to this interpretation: Datetimes for conferences events are LOCAL, they do not represent a point in time in the physical continuum line of time, but a civic concept. (Think of it as -almost- a string). The drawback, of course, is that it will more difficult to do time calculations, specially inside the DB.

您只需要将其转换为一个物理时间,如果你需要计算,例如,间隔从现在到该事件(如果你需要从事件上升报警,或区分事件过去在未来,等)。因为现在是一个物理时间的概念。在这种情况下,你必须自己做转换。如果你需要作出规定计算的数据库里面,你可能要储存GMT偏移量(比如,以分钟计)在DB明确。

You'll only need to convert it to a "physical time" if you need to compute, for example, the interval from now to that event (if you need to rise an alarm, or distinguish events in the past from events in the future, etc). Because "now" is a physical time concept. In that case, you must do the conversion yourself. If you need to make that calculation inside the DB, you'll probably have to store the GMT-offset (say, in minutes) in the DB explicitly.

这篇关于迷失在日期和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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