DateTime, Timestamp, Time and Date in Rails

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

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 3 - 9.08

This week's freshest news, updates and guides from the Ruby and Rails programming
09 August 2019   393

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

As always, we have prepared some great material for you. In this issue, you will learn, for example, how to migrate your Docker\Kubernetes Ruby app today, how to use Ruby unique method to remove duplicates, watch video on how to remove conditionals with Ruby's Array wrap method and many other things.

Guides

  • Don’t change the signature of Sidekiq jobs running in production

Author brings a lot of arguments to the statement, given in the header.

  • Fullstaq Ruby: First impressions, and how to migrate your Docker/Kubernetes Ruby apps today

Short, but interesting tutorial, which covers one of the aspects of Fullstaq Ruby.

  • How to Use The Ruby Uniq Method To Remove Duplicates

 Using uniq with a block provides even more power; great guide for the beginners.

Videos

  • Removing conditionals with Ruby's Array wrap method

  • Dev Quick Tips - Using Ruby's Reduce method to create a Hash of "Banana Bunch" Arrays

Article

  • Tales of the Ruby Grimoire - Part One - The Grimoire

As author of this programming fairytale explains, this is the first of many tales of the legendary Ruby Grimoire, a great and terrible book of Ruby dark magics

  • Introducing the ‘aws-rails-provisioner’ gem developer preview

This gem is designed to help you define and deploy containerized Rails apps on AWS Fargate

Updates

  • Jekyll 4.0.0.pre.beta1 Released 

Pre beta1 of new major version of the solution, that allows to transform text in static websites and blogs

  • Ciao: An HTTP Monitoring Tool, Built on Rails 

Webapp that checks HTTP endpoints and can send notifications when something bad happens

  • Hightop: A Shortcut for Group Count Queries

Adds a top method to Enumberable so works with both plain old arrays or ActiveRecord.

Podcast

  • RR 424: Documenting Your Code

David Kumira (Screencaster of Drifting Ruby, Panelist on Ruby Rogues), Nate Hopkins (CodeFund) and Andrew Mason (Full stack Ruby on Rails developer) talks why documenting your Ruby (and not only Ruby) code is valuable.