英文:
Surviving user timezones in Rails
问题
I'm trying to play nice with TimeZones in Rails.
我正在尝试在Rails中与时区合作。
I have a model called Event
which has the datetime field starts_at
and string field timezone
.
我有一个名为Event
的模型,其中包含了starts_at
日期时间字段和timezone
字符串字段。
I'm however just a bit confused as to the best practice for managing these dates as I am working with the date of these Events which are quite different from the application's timezone.
然而,我对于如何管理这些日期的最佳实践有些困惑,因为我正在处理与应用程序时区非常不同的这些事件的日期。
By default rails is using the timezone of my application which is CEST and so by default when I save a date for an Event it uses this timezone.
默认情况下,Rails使用我的应用程序的时区,即CEST,因此默认情况下,当我保存事件的日期时,它使用这个时区。
However 99% of the time we think of the date in terms of the client for whom we set the timezone on the model.
然而,99%的情况下,我们是根据客户的时区来考虑日期的。
So the default of setting it in our timezone is not great.
因此,将其设置为我们的时区的默认设置不是很好。
Should I:
我应该:
-
Keep setting these dates relative to my time, eg
starts_at
set in CEST and interpret them into the client time, egclient_starts_at
. However this is confusing since, we usually inputstarts_at
relative to the client's time and not to our own. -
继续相对于我的时间设置这些日期,例如,将
starts_at
设置为CEST并将其解释为客户的时间,例如,client_starts_at
。但是这很令人困惑,因为通常我们是根据客户的时间输入starts_at
,而不是根据我们自己的时间。 -
Do some kind of getter/setter to be able to input dates as the users time. Which avoids most of the issue as all things go into UTC anyway.
-
进行某种类型的getter/setter操作,以便能够按用户的时间输入日期。这可以避免大部分问题,因为所有的事情最终都会转换为UTC。
-
Configure the model or controller to take into account the
timezone
field of theEvent
and thereby set every recordstarts_at
into the correct timezone and then interpret the time zone for us intolocal_starts_at
. -
配置模型或控制器以考虑
Event
的timezone
字段,并因此将每个记录的starts_at
设置为正确的时区,然后为我们解释成local_starts_at
。 -
Perhaps i'd be better off separating
starts_at
into two fields separatingdate
fromtime
? -
也许我最好将
starts_at
分成两个字段,分别是date
和time
?
The option 3 or 4 seem better but does anyone know of a resource as this would seem to be a very common problem.
选项3或4似乎更好,但是否有人知道有关此问题的资源,因为这似乎是一个非常常见的问题。
Also I'm not sure if I should be managing these issues on the model level or dealing with it in the controller.
而且我不确定是否应该在模型级别管理这些问题,还是在控制器中处理它。
Thanks,
谢谢,
https://thoughtbot.com/blog/its-about-time-zones
https://thoughtbot.com/blog/its-about-time-zones
英文:
I'm trying to play nice with TimeZones in Rails.
I have a model called Event
which has the datetime field starts_at
and string field timezone
. I'm however just a bit confused as to the best practice for managing these dates as I am working with the date of these Events which are quite different from the application's timezone.
By default rails is using the timezone of my application which is CEST and so by default when I save a date for an Event it uses this timezone. However 99% of the time we think of the date in terms of the client for whom we set the timezone on the model. So the default of setting it in our timezone is not great.
Should I:
- Keep setting these dates relative to my time, eg
starts_at
set in CEST and
interpret them into the client time, egclient_starts_at
. However this is confusing since, we usually inputstarts_at
relative to the client's time and not to our own. - Do some kind of getter/setter to be able to input dates as the users time. Which avoids most of the issue as all things go into UTC anyway.
- Configure the model or controller to take into account the
timezone
field
of theEvent
and thereby set every recordstarts_at
into the
correct timezone and then interpret the time zone for us into
local_starts_at
. - Perhaps i'd be better off separating
starts_at
into two fields separatingdate
fromtime
?
The option 3 or 4 seem better but does anyone know of a resource as this would seem to be a very common problem. Also I'm not sure if I should be managing these issues on the model level or dealing with it in the controller.
Thanks,
答案1
得分: 1
你可以使用 in_time_zone
。
def client_starts_at
starts_at.in_time_zone(timezone)
end
无论它是如何存储的,你都会得到正确的时区。
你甚至可以重写基础方法(不推荐):
def starts_at
read_attribute(:starts_at).in_time_zone(timezone)
end
英文:
You can use in_time_zone
.
def client_starts_at
starts_at.in_time_zone(timezone)
end
No matter how it was stored, you'll get the right timezone.
You can even override the base method (not recommended):
def starts_at
read_attribute(:starts_at).in_time_zone(timezone)
end
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论