DateTime, Timestamp, Time and Date in Rails

Learn about key differenece between DateTime, Timestamp, Time and Date in Rails
31 October 2017   12514

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 DATEDATETIMETIME and TIMESTAMP column data types; just as you have CHARVARCHARFLOATand INTEGER.

So, main differences: 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 DATETIMEstores 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.

Ruby/RoR News Digest 21 - 27.03

Learn why and how to host Rails app with ElasticBeanstalnk, how to do multi-step forms in Rails, listed to Sorbet podcast and more
27 March 2020   945

Greetings! I hope your week went great! Here's new Ruby news digest.

Learn about RuboCoping with legacy, how to secure SSH keys in use, listed to podcast about Sidekiq and check other interesting things in this podcast.

Guides

  • Why and How to Host your Rails 6 App with AWS ElasticBeanstalk and RDS

Tutorial of using one of AWS’s most mature services to deploy a Rails app onto AWS

  • RuboCoping with legacy:Bring your Ruby code up to Standard

This guide is providing you with a "standard" approach of using RuboCop

  • How to do multi-step forms in Rails

This tutorial will teach you how to create multi-step forsm gem-free

  • Securing SSH keys in use

Ruby-powered digging around in SSH keys here. Learn about SSH securing, Ruby-powered

Podcast

  • Sorbet: Typed Ruby with Dmitry Petrashko

Dmitry from Stripe talks about Sorbet, type checker for Ruby

  • Rails with Jason - 037 - Server Infrastructure, systemd, and the Business Side of Sidekiq with Mike Perham

Long talk by Mike Perham of Sidekiq about the business side of Sidekiq, systemd, and server infrastructure in general

Updates

  • Webpacker 5.0 released

Major version of super popular Rails solution that makes it easy to use the webpack to manage application-like JavaScript in Rails brings a lot of changes and updates, like multply files per entry

  • pgsync

Allows to sync data from one Postgres database to another

  • render_async

This gem allows to speed up rendering Rails pages

  • Authlogic

Ruby authentification solution