我什么时候应该在 ruby​​/rails 中使用 DateTime 与日期、时间字段? [英] When should i use DateTime vs date, time fields in ruby/rails?

查看:29
本文介绍了我什么时候应该在 ruby​​/rails 中使用 DateTime 与日期、时间字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有单独的 game_date 和 game_time 字段,并且由于时区问题,我花了很长时间将我的 DateTime.now 与串联的 DateTime 进行比较.我应该重新设计我的数据库以只使用 DateTime 吗?我有一个单独的时间字段,因为时间在某些时间点可能为 NULL.典型的做法是什么,另外,我应该如何解决以下时区的问题?

I currently have separate game_date and game_time fields and I am having a hell of a time comparing my DateTime.now to a concatenated DateTime because of time zone issues. Should I redesign my database to just use DateTime? I have a time field separately because the time can be NULL at some points in time. What is the typical practice, and also, how should I resolve my issue with the time zones below?

    now = DateTime.now
    @upcoming_games = []
    @past_games = []
    games.each do |game|
      game.game_time = DateTime.now if game.game_time.nil?
      dt = DateTime.parse("#{game.game_date}T#{game.game_time.strftime("%H:%M:00")}")
      if dt >= now
        @upcoming_games << game
      else
        @past_games << game
      end
    end

推荐答案

总体思路是使用 DateTime 作为时间的通用表示.您可能会感到困惑的是 Time 还包括一个日期组件,因为它是 UNIX time_t 自纪元以来秒概念的封装,或 MySQL 术语中的 UNIX_TIME() .

The general idea is to use a DateTime as a general purpose representation of time. Where you might be confused is that Time also includes a date component as it is an encapsulation of the UNIX time_t concept of seconds since epoch, or UNIX_TIME() in MySQL terms.

正如我在另一个答案中解释,Time 是比 DateTime 更有限的表示,只能表示截至 2038 年 1 月 18 日的日期和时间.DateTime 可以表示 4,712 BCE 以及未来 21,000 年.

As I explain in another answer, Time is a more limited representation than DateTime and can only represent dates and times up to Jan 18, 2038. A DateTime can represent 4,712 BCE as well as 21,000 years in the future.

如果您想要一个单独的字段来表示一天中的时间,这似乎是您在这里可能想要的,您应该创建一个单独的数字字段,如果需要这种精度,则表示午夜后的秒数,或者更方便的"HHMM"表示不关心 base-60 和 base-10 之间的差异.

If you want a separate field that represents time of day, which it seems like you might want here, you should create a single numerical field that represents either seconds past midnight if that kind of precision is required, or a more convenient "HHMM" representation that doesn't concern itself with the differences between base-60 and base-10.

另一种选择是有两个字段,一个是日期时间,一个是日期.如果您要创建没有特定时间的日历条目,请仅填充日期字段.如果有时间,则填充两者.

Another alternative is to have two fields, one being a DateTime and one being a Date. If you're creating a calendar entry with no particular time, populate only the Date field. If it has a time, populate both.

请记住,日期字段是用文字日期填充的,它本身与时区无关,因此如果它不是以用户的本地时间表示,这可能会导致问题.如果需要,DateTime 始终可以转换为用户的本地时间.

Remember that a date field is populated with a literal date and does not concern itself with time zones, so this can cause trouble if it is not expressed in the user's local time. A DateTime can always be converted to a user's local time if required.

这篇关于我什么时候应该在 ruby​​/rails 中使用 DateTime 与日期、时间字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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