# Datetime vs timestamp

**URL:** <https://www.ruby-forum.com/t/datetime-vs-timestamp/152203>\
**Category:** Rails\
**Created:** [October 31, 2008, 8:55am UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203 "2008-10-31T08:55:48Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 31, 2008, 8:55am UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/1 "2008-10-31T08:55:48Z")

</div>

ROR always converts :timestamp to :datetime. I’ve been googling and  
still havent found out why this is the case.

The mysql data type DATETIME takes up twice the amount of space as  
TIMESTAMP. What will break in my app if I change DATETIME to TIMESTAMP.  
Is this well documented somewhere?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 31, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/2 "2008-10-31T15:11:23Z")

</div>

Aryk G. wrote:

> ROR always converts :timestamp to :datetime. I’ve been googling and  
> still havent found out why this is the case.
> 
> The mysql data type DATETIME takes up twice the amount of space as  
> TIMESTAMP. What will break in my app if I change DATETIME to TIMESTAMP.  
> Is this well documented somewhere?

I assume you’re talking about migrations. So when you create a field  
such as

t.timestamp :my\_field

the column in the database is actually a datetime. Each database adapter  
uses a hash to convert what is used in the migration to what is used in  
the database. This is done with a method called native\_database\_types.  
For example, the Postgres adapter defines:

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

# in order to add or change the datatypes, this function

# must be overriden. Be careful, then, to not remove anything.

# That carries with it the warning that if Rails Core changes

# this function, this override will do away with those changes!

def native\_database\_types  
{  
:primary\_key =\> “serial primary key”,  
:string =\> { :name =\> “character varying”, :limit =\> 255 },  
:text =\> { :name =\> “text” },  
:integer =\> { :name =\> “integer” },  
:float =\> { :name =\> “float” },  
:decimal =\> { :name =\> “decimal” },  
:datetime =\> { :name =\> “timestamp” },  
:timestamp =\> { :name =\> “timestamp” },  
:time =\> { :name =\> “time” },  
:date =\> { :name =\> “date” },  
:binary =\> { :name =\> “bytea” },  
:boolean =\> { :name =\> “boolean” },  
:bigint =\> { :name =\> “int8” }  
}  
end  
end

This is actually my version to add support for bigints that I needed in  
a project. Notice that when :datetime or :timestamp is used, it becomes  
a timestamp in the database. If the MySQL adapter does not define this  
method explicitly, look at the abstract adapter. Also note the comment  
that I have in my overridden method. With great power comes great  
responsibility.

Peace.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 31, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/3 "2008-10-31T17:16:33Z")

</div>

In the case of MySQL there are significant differences between DATETIME  
and TIMESTAMP.

## From MySQL documentation:

The DATETIME type is used when you need values that contain both date  
and time information. MySQL retrieves and displays DATETIME values in  
‘YYYY-MM-DD HH:MM:SS’ format. The supported range is ‘1000-01-01  
00:00:00’ to ‘9999-12-31 23:59:59’.

## The TIMESTAMP data type has a range of ‘1970-01-01 00:00:01’ UTC to ‘2038-01-09 03:14:07’ UTC. It has varying properties, depending on the MySQL version and the SQL mode the server is running in. These properties are described later in this section.

It’s the limited range of TIMESTAMP that allows it to take up less  
storage than DATETIME.

Note: If you’re optimizing for disk space, date/time fields are not the  
place to start anyways.

ALSO:

The TIMESTAMP data type in MySQL will automatically update itself if the  
column is not explicitly set in an UPDATE statement:

Example:

Person  
| id | first\_name| last\_name | created\_at | updated\_at |  
| 1 | null | null | 2008-10-29 15:00 | 2008-10-29 15:00 |

Where created\_at and updated\_at were a TIMESTAMP not a DATETIME

UPDATE people SET id = 1, first\_name=“Robert”, last\_name = “Walker”;

| 1 | Robert | Walker | 2008-10-30 10:20:10 | 2008-10-30 10:20:10 |

This would be the result of the update. This is NOT what you would want  
or expect. updated\_at would have the correct value (even though it’s not  
specified in the update). However, created\_at would be wrong.

This is why Rails uses DATETIME and not TIMESTAMP. IMHO the only safe  
use of the TIMESTAMP data type is in columns that are not used directly  
by the application. They are good for “timestamping” a row in a  
database, but you must understand this self updating behavior.

Aryk G. wrote:

> ROR always converts :timestamp to :datetime. I’ve been googling and  
> still havent found out why this is the case.
> 
> The mysql data type DATETIME takes up twice the amount of space as  
> TIMESTAMP. What will break in my app if I change DATETIME to TIMESTAMP.  
> Is this well documented somewhere?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 31, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/4 "2008-10-31T18:48:36Z")

</div>

I haven’t seen anywhere in the mysql documentation that TIMESTAMP data  
type will automatically update itself.

Where is this?

It looks like timestamp HAS TO have a default value though, but I didn’t  
see anything about it not updating itself.

Can you point me to it?

Im wondering if anything will break in RoR between DATETIME and  
TIMESTAMP.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 31, 2008, 7:03pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/5 "2008-10-31T19:03:02Z")

</div>

Aryk G. wrote:

> Phillip,
> 
> I see that you always use timestamp instead of datetime. Have you  
> noticed any problems in Rails \>2. How confident are you that timestamp  
> and datetime can be used interchangeably?
> 
> I know that TIMESTAMP needs to have a default value, but besides that,  
> are there any issues?
> 
> Aryk

I’m using Postgres, not MySQL. Postgres has only the timestamp data type  
(for date/time columns), which is why the native\_database\_types method  
has both :datetime and :timestamp mapped to it. And that is not my code.  
I redefined the method only to add :bigint at the bottom.

Peace.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 31, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/6 "2008-10-31T20:07:03Z")

</div>

On Fri, Oct 31, 2008 at 10:48 AM, Aryk G.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I haven’t seen anywhere in the mysql documentation that TIMESTAMP data  
> type will automatically update itself.

[http://dev.mysql.com/doc/refman/5.0/en/timestamp.html](http://dev.mysql.com/doc/refman/5.0/en/timestamp.html)

–  
Hassan S. ------------------------ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [November 1, 2008, 12:03am UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/7 "2008-11-01T00:03:35Z")

</div>

Right, but that is not the reason why timestamps dont use TIMESTAMP. You  
can always create the column without auto update functionality,  
according to mysql’s website. You must always have a default value for a  
timestamp, so that might be a reason why it’s not used, but that doesnt  
seem like a big enough reason, IMO.

AFAIK, there is no reason why Rails must use DATETIME over TIMESTAMP  
except for the range of dates supported.

Does anyone have a reason for why Rails uses DATETIME over TIMESTAMP for  
timestamp fields?

I ran some tests and it looks like they behave the same within Rails.

Aryk

Hassan S. wrote:

> On Fri, Oct 31, 2008 at 10:48 AM, Aryk G.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I haven’t seen anywhere in the mysql documentation that TIMESTAMP data  
> > type will automatically update itself.
> 
> [http://dev.mysql.com/doc/refman/5.0/en/timestamp.html](http://dev.mysql.com/doc/refman/5.0/en/timestamp.html)
> 
> –  
> Hassan S. ------------------------ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 31, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/8 "2008-10-31T18:53:18Z")

</div>

Phillip,

I see that you always use timestamp instead of datetime. Have you  
noticed any problems in Rails \>2. How confident are you that timestamp  
and datetime can be used interchangeably?

I know that TIMESTAMP needs to have a default value, but besides that,  
are there any issues?

Aryk

Phillip K. wrote:

> For example, the Postgres adapter defines:
> 
> class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
> 
> # in order to add or change the datatypes, this function
> 
> # must be overriden. Be careful, then, to not remove anything.
> 
> # That carries with it the warning that if Rails Core changes
> 
> # this function, this override will do away with those changes!
> 
> def native\_database\_types  
> {  
> :primary\_key =\> “serial primary key”,  
> :string =\> { :name =\> “character varying”, :limit =\> 255 },  
> :text =\> { :name =\> “text” },  
> :integer =\> { :name =\> “integer” },  
> :float =\> { :name =\> “float” },  
> :decimal =\> { :name =\> “decimal” },  
> :datetime =\> { :name =\> “timestamp” },  
> :timestamp =\> { :name =\> “timestamp” },  
> :time =\> { :name =\> “time” },  
> :date =\> { :name =\> “date” },  
> :binary =\> { :name =\> “bytea” },  
> :boolean =\> { :name =\> “boolean” },  
> :bigint =\> { :name =\> “int8” }  
> }  
> end  
> end
> 
> This is actually my version to add support for bigints that I needed in  
> a project. Notice that when :datetime or :timestamp is used, it becomes  
> a timestamp in the database. If the MySQL adapter does not define this  
> method explicitly, look at the abstract adapter. Also note the comment  
> that I have in my overridden method. With great power comes great  
> responsibility.
> 
> Peace.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [November 3, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/9 "2008-11-03T18:28:07Z")

</div>

The only real benefit is that it takes up half the space in the database  
(8 bytes instead of 4). There are also some querying benefits which a  
mysql expert could get into (easier to do range queries with, etc…).

But yeah, if you have 100 mil rows in a table, using this will save you  
4 bytes, or 400 MB which I guess you could argue is neglible in the  
grand scheme of things, but if do it across your database, it could save  
you a couple gigs, which isn’t too bad.

Robert W. wrote:

> I stopped using TIMESTAMP due to the auto-updating that I was seeing in  
> a real application. However, that was way back in version 3.x of MySQL.  
> I did notice later that this behavior looks to have been changed. So I  
> suppose if you need date/times fields that don’t require values before  
> Jan 1, 1970 they are probably okay to use. But, I still don’t see much  
> benefit in doing so over just using DATTIME. It makes sense to me for  
> Rails to use DATETIME over TIMESTAMP. It’s simpler, and safer to do so.
> 
> Aryk G. wrote:
> 
> > Right, but that is not the reason why timestamps dont use TIMESTAMP. You  
> > can always create the column without auto update functionality,  
> > according to mysql’s website. You must always have a default value for a  
> > timestamp, so that might be a reason why it’s not used, but that doesnt  
> > seem like a big enough reason, IMO.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [November 2, 2008, 4:03am UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/10 "2008-11-02T04:03:57Z")

</div>

I stopped using TIMESTAMP due to the auto-updating that I was seeing in  
a real application. However, that was way back in version 3.x of MySQL.  
I did notice later that this behavior looks to have been changed. So I  
suppose if you need date/times fields that don’t require values before  
Jan 1, 1970 they are probably okay to use. But, I still don’t see much  
benefit in doing so over just using DATTIME. It makes sense to me for  
Rails to use DATETIME over TIMESTAMP. It’s simpler, and safer to do so.

Aryk G. wrote:

> Right, but that is not the reason why timestamps dont use TIMESTAMP. You  
> can always create the column without auto update functionality,  
> according to mysql’s website. You must always have a default value for a  
> timestamp, so that might be a reason why it’s not used, but that doesnt  
> seem like a big enough reason, IMO.

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [November 3, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/datetime-vs-timestamp/152203/11 "2008-11-03T18:28:45Z")

</div>

Woops, I meant 4 bytes instead of 8…its early…=) -Aryk
