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

在这一点上,Ruby和Rails总是让我避而远之,如果只是因为有太多的选择;我一直不知道该选哪个。

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

:datetime、:timestamp、:time、:date

而且不知道有什么不同,也不知道陷阱在哪里。

有什么不同?你用它们做什么?

(附注:我使用Rails3)


当前回答

这是我找到的一个非常棒而精确的解释。

TIMESTAMP used to track changes of records, and update every time when the record is changed. DATETIME used to store specific and static value which is not affected by any changes in records. TIMESTAMP also affected by different TIME ZONE related setting. DATETIME is constant. TIMESTAMP internally converted a current time zone to UTC for storage, and during retrieval convert the back to the current time zone. DATETIME can not do this. TIMESTAMP is 4 bytes and DATETIME is 8 bytes. TIMESTAMP supported range: ‘1970-01-01 00:00:01′ UTC to ‘2038-01-19 03:14:07′ UTC DATETIME supported range: ‘1000-01-01 00:00:00′ to ‘9999-12-31 23:59:59′

来源:https://www.dbrnd.com/2015/09/difference-between-datetime-and-timestamp-in-mysql/: ~:文本= DATETIME % 20 vs % 20时间戳% 3,DATETIME % 20是% 20常数。

也……

表具有不同的列“date”类型和相应的rails迁移类型,具体取决于数据库

其他回答

这是我找到的一个非常棒而精确的解释。

TIMESTAMP used to track changes of records, and update every time when the record is changed. DATETIME used to store specific and static value which is not affected by any changes in records. TIMESTAMP also affected by different TIME ZONE related setting. DATETIME is constant. TIMESTAMP internally converted a current time zone to UTC for storage, and during retrieval convert the back to the current time zone. DATETIME can not do this. TIMESTAMP is 4 bytes and DATETIME is 8 bytes. TIMESTAMP supported range: ‘1970-01-01 00:00:01′ UTC to ‘2038-01-19 03:14:07′ UTC DATETIME supported range: ‘1000-01-01 00:00:00′ to ‘9999-12-31 23:59:59′

来源:https://www.dbrnd.com/2015/09/difference-between-datetime-and-timestamp-in-mysql/: ~:文本= DATETIME % 20 vs % 20时间戳% 3,DATETIME % 20是% 20常数。

也……

表具有不同的列“date”类型和相应的rails迁移类型,具体取决于数据库

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

以MySQL为例(如果没有其他原因,因为它是最流行的),你有DATE, DATETIME, TIME和TIMESTAMP列数据类型;就像你有CHAR, VARCHAR, FLOAT和INTEGER一样。

所以,你会问,有什么不同?有些是不言自明的。DATE只存储日期,TIME只存储一天中的某个时间,而DATETIME两者都存储。

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中不同时间格式的区别。

最后,它归结为你需要你的日期/时间列做什么:

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

说了这么多,Rails实际上为您做出了其中的一些决定。:timestamp和:datetime都默认为datetime,而:date和:time分别对应date和time。

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

:datetime(8字节) 存储日期和时间格式的YYYY-MM-DD HH:MM:SS 对于birth_date这样的列很有用 :时间戳(4字节) 1970-01-01以来的存储秒数 对于updated_at、created_at等列很有用 :date(3字节) 店日期 :time(3字节) 商店的时间