在 Ruby on Rails 中,DateTime、Timestamp、Time 和 Date 之间有什么区别? [英] In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?

查看:34
本文介绍了在 Ruby on Rails 中,DateTime、Timestamp、Time 和 Date 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的经验,在编程时获取正确的日期/时间总是充满危险和困难.

In my experience, getting dates/times right when programming is always fraught with danger and difficulity.

Ruby 和 Rails 在这个问题上总是让我望而却步,即使只是因为选项太多;我从来不知道我应该选择哪个.

Ruby and Rails have always eluded me on this one, if only due to the overwhelming number of options; I never have any idea which I should pick.

当我使用 Rails 并查看 ActiveRecord 数据类型时,我可以找到以下内容

When I'm using Rails and looking at ActiveRecord datatypes I can find the following

:datetime、:timestamp、:time 和:date

:datetime, :timestamp, :time, and :date

并且不知道有什么区别或陷阱潜伏在哪里.

and have no idea what the differences are or where the gotchas lurk.

有什么区别?你用它们做什么?

What's the difference? What do you use them for?

(P.S. 我使用的是 Rails3)

(P.S. I'm using Rails3)

推荐答案

ActiveRecord 中不同日期/时间格式之间的差异与 Rails 关系不大,而与您使用的任何数据库有关.

The difference between different date/time formats in ActiveRecord has little to do with Rails and everything to do with whatever database you're using.

以 MySQL 为例(如果没有其他原因,因为它最流行),你有 DATEDATETIMETIMETIMESTAMP 列数据类型;就像你有 CHARVARCHARFLOATINTEGER 一样.

Using MySQL as an example (if for no other reason because it's most popular), you have DATE, DATETIME, TIME and TIMESTAMP column data types; just as you have CHAR, VARCHAR, FLOAT and INTEGER.

所以,你问,有什么区别?嗯,其中一些是不言自明的.DATE 只存储日期,TIME 只存储一天中的时间,而 DATETIME 存储两者.

So, you ask, what's the difference? Well, some of them are self-explanatory. DATE only stores a date, TIME only stores a time of day, while DATETIME stores both.

DATETIMETIMESTAMP 之间的区别有点微妙:DATETIME 的格式为 YYYY-MM-DD HH:MM:SS.有效范围从 1000 年到 9999 年(以及介于两者之间的所有内容.虽然 TIMESTAMP 看起来与从数据库中获取时相似,但它实际上只是一个 unix 时间戳.它的有效范围从 1970 年到 2038 年.这里的区别,除了各种数据库引擎的内置函数,就是存储空间,因为DATETIME存储了年、月、日、时、分、秒的每一位数字,所以一共占用了8个字节.如TIMESTAMP 只存储自 1970-01-01 以来的秒数,它使用 4 个字节.

The difference between DATETIME and TIMESTAMP is a bit more subtle: DATETIME is formatted as YYYY-MM-DD HH:MM:SS. Valid ranges go from the year 1000 to the year 9999 (and everything in between. While TIMESTAMP looks similar when you fetch it from the database, it's really a just a front for a unix timestamp. Its valid range goes from 1970 to 2038. The difference here, aside from the various built-in functions within the database engine, is storage space. Because DATETIME stores every digit in the year, month day, hour, minute and second, it uses up a total of 8 bytes. As TIMESTAMP only stores the number of seconds since 1970-01-01, it uses 4 bytes.

您可以阅读有关 MySQL 中时间格式差异的更多信息 此处.

You can read more about the differences between time formats in MySQL here.

最后,归结为您需要日期/时间列执行的操作:

In the end, it comes down to what you need your date/time column to do:

  • 您是否需要存储 1970 年之前或 2038 年之后的日期和时间?=>使用 DATETIME.
  • 您是否需要担心数据库大小并且您在该时间范围内?=>使用 TIMESTAMP.
  • 您是否只需要存储日期?=>使用 DATE.
  • 你只需要存储一个时间吗?=>使用 TIME.

说了这么多,Rails 实际上为您做出了一些决定.:timestamp:datetime 都将默认为 DATETIME,而 :date:time分别对应DATETIME.

Having said all of this, Rails actually makes some of these decisions for you. Both :timestamp and :datetime will default to DATETIME, while :date and :time corresponds to DATE and TIME, respectively.

这意味着在 Rails 中,您只需决定是否需要存储日期、时间或两者.

This means that within Rails, you only have to decide whether you need to store date, time or both.

这篇关于在 Ruby on Rails 中,DateTime、Timestamp、Time 和 Date 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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