在 Ruby on Rails 中,DateTime、Timestamp、Time 和 Date 有什么区别? [英] In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and 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 为例(如果没有其他原因,因为它最流行),您有 DATE
、DATETIME
、TIME
和 TIMESTAMP
列数据类型;就像你有 CHAR
、VARCHAR
、FLOAT
和 INTEGER
.
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.
DATETIME
和 TIMESTAMP
之间的区别有点微妙: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
分别对应DATE
和TIME
.
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屋!