“Surviving user timezones in Rails” 可以翻译为 “在Rails中处理用户时区”。

huangapple go评论68阅读模式
英文:

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:

我应该:

  1. Keep setting these dates relative to my time, eg starts_at set in CEST and interpret them into the client time, eg client_starts_at. However this is confusing since, we usually input starts_at relative to the client's time and not to our own.

  2. 继续相对于我的时间设置这些日期,例如,将starts_at设置为CEST并将其解释为客户的时间,例如,client_starts_at。但是这很令人困惑,因为通常我们是根据客户的时间输入starts_at,而不是根据我们自己的时间。

  3. 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.

  4. 进行某种类型的getter/setter操作,以便能够按用户的时间输入日期。这可以避免大部分问题,因为所有的事情最终都会转换为UTC。

  5. Configure the model or controller to take into account the timezone field of the Event and thereby set every record starts_at into the correct timezone and then interpret the time zone for us into local_starts_at.

  6. 配置模型或控制器以考虑Eventtimezone字段,并因此将每个记录的starts_at设置为正确的时区,然后为我们解释成local_starts_at

  7. Perhaps i'd be better off separating starts_at into two fields separating date from time?

  8. 也许我最好将starts_at分成两个字段,分别是datetime

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:

  1. Keep setting these dates relative to my time, eg starts_at set in CEST and
    interpret them into the client time, eg client_starts_at. However this is confusing since, we usually input starts_at relative to the client's time and not to our own.
  2. 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.
  3. Configure the model or controller to take into account the timezone field
    of the Event and thereby set every record starts_at into the
    correct timezone and then interpret the time zone for us into
    local_starts_at.
  4. Perhaps i'd be better off separating starts_at into two fields separating date from 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. 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

答案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

huangapple
  • 本文由 发表于 2023年5月10日 19:41:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定