在Ruby on Rails中,DateTime,Timestamp,Time和Date之间有什么区别? [英] In Ruby on Rails, what's the difference between DateTime, Timestamp, Time and Date?
问题描述
如果只是因为绝对数量众多的选择,Ruby和Rails总是让我失望的是,我没有任何想法应该选择。
当我使用Rails并查看ActiveRecord数据类型时,我可以找到以下
:datetime,:timestamp,:time,and:date
不知道差异是什么或者陷阱潜伏在哪里。
有什么区别?你用什么?
(PS我正在使用Rails3)
在ActiveRecord中的不同的日期/时间格式之间与Rails和与您使用的任何数据库无关的所有功能。
以MySQL为例(如果没有其他原因是因为它最受欢迎),您有 DATE
, DATETIME
, TIME
和 TIMESTAMP
列数据类型;就像你有 CHAR
, VARCHAR
, FLOAT
和 INTEGER
。
所以,你问,有什么区别?那么其中一些是不言自明的。 DATE
仅存储一个日期, TIME
只存储一天的时间,而 DATETIME
存储两者。
DATETIME
和 TIMESTAMP
有点更微妙:
只存储自1970-01-01以来的秒数,它使用4个字节。 DATETIME
格式化为 YYYY-MM-DD HH:MM:SS
。有效范围从1000年到9999年(以及之间的一切),而当您从数据库获取时, TIMESTAMP
看起来类似,它是真的只是一个 unix时间戳的前沿,其有效范围从1970年到2038年。这里的区别在于数据库引擎内的各种内置功能是存储空间,因为 DATETIME
存储年,月,日,时,秒中的每个数字,由于$ code> TIMESTAMP
您可以阅读更多关于MySQL 这里的时间格式之间的差异/ a>。
最后,归结于您需要的日期/时间列,您需要在1970年以前或之后存储日期和时间2038?使用 DATETIME
你需要担心数据库的大小,你在那个计时器?使用 TIMESTAMP
。你只需要存储日期吗?使用 DATE
。你只需要储存一次吗?使用 TIME
。
所有这一切, Rails实际上为您做出了这些决定。 :timestamp
和:datetime
将默认为 DATETIME
而:date
和:time
对应于 DATE
和 TIME
。
这意味着在Rails中,您只需要决定是否需要存储日期,时间或两者。
In my experience, getting dates/times right when programming is always fraught with danger and difficulity.
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.
When I'm using Rails and looking at ActiveRecord datatypes I can find the following
: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. I'm using Rails3)
The difference between different date/time formats in ActiveRecord have little to do with Rails and everything to do with whatever database you're using.
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
.
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.
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.
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. Do you need to store dates and times before 1970 or after 2038? Use DATETIME
. Do you need to worry about database size and you're within that timerange? Use TIMESTAMP
. Do you only need to store a date? Use DATE
. Do you only need to store a time? Use 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.
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屋!