Every real-time application needs to deal with Date and Time.In rails, Time
, Date
and DateTime
classes reside inside ruby library, Date and Time inheriting the Object
class. DateTime
inherits from Date
class. Current Time, Date and both can be easily accessible using instance methods of these above class methods.
[45] pry(main)> Date.superclass => Object [46] pry(main)> Time.superclass => Object [47] pry(main)> DateTime.superclass => Date
Accessing current date, time and both in rails.
[25] pry(main)> DateTime.now => Fri, 06 Oct 2017 18:06:50 +0000 [26] pry(main)> Date.today => Fri, 06 Oct 2017 [27] pry(main)> Time.now => 2017-10-06 18:07:07 +0000
How does Postgres store DateTime using rails…?
Postgres always stores Datetime
data in utc format
. When a sql query is made, the datetime format is converted into utc time, if necessary, before sql query is applied to database. For example, we make a sql query with IST
timings, the datetime is converted to utc and sql query is applied.
d = DateTime.now.in_time_zone("Asia/Kolkata") => Sat, 07 Oct 2017 00:38:26 IST +05:30
[57] pry(main)> d => Sat, 07 Oct 2017 00:38:40 IST +05:30 [58] pry(main)> Booking.where('created_at > ?', d).count (1.4ms) SELECT COUNT(*) FROM "bookings" WHERE (created_at > '2017-10-06 19:08:40.011480') D, [2017-10-06T19:10:37.734437 #1] DEBUG -- : (1.4ms) SELECT COUNT(*) FROM "bookings" WHERE (created_at > '2017-10-06 19:08:40.011480') D, [2017-10-06T19:10:37.734724 #1] DEBUG -- : (1.4ms) SELECT COUNT(*) FROM "bookings" WHERE (created_at > '2017-10-06 19:08:40.011480') => 3
Observe that we gave a sql query for datetime with IST timezone (07 Oct 2017 00:38:40 IST +05:30)
, but the query was made using UTC format (2017-10-06 19:08:40.011480)
in postgres.
Dealing with TimeZone
Rails would give utc time by default. In order to have IST or EST datetime, configuration at config/application.rb needs to be changed respectively.
config.time_zone = "Asia/Kolkata" config.time_zone = "Eastern Time (US & Canada)"
Using in_time_zone(timezone)
gives DateTime from timezone specified.
DateTime.now.in_time_zone('Asia/Kolkata') => Sat, 07 Oct 2017 01:22:11 IST +05:30 DateTime.now.in_time_zone('Eastern Time (US & Canada)') => Fri, 06 Oct 2017 15:53:20 EDT -04:00
Strftime
Formats date according to the directives in the given format string. The directives begins with a percent (%)
character. Any text not listed as a directive will be passed through to the output string.
Syntax : strftime( format )
DateTime.now => 2017-10-06 19:29:38 UTC
t.strftime("%H") => "19" # Hour of the time in 24 hour clock format t.strftime("%I") => "07" # Hour of the time in 12 hour clock format t.strftime("%M") => "29" # Minutes of the time t.strftime("%S") => "38" # Seconds of the time t.strftime("%Y") => "2017" # Year of the time t.strftime("%m") => "10" # month of the time t.strftime("%d") => "06" # day of month of the time t.strftime("%w") => "5" # day of week of the time t.strftime("%a") => "Fri" # name of week day in short form of the t.strftime("%A") => "Friday" # week day in full form of the time t.strftime("%b") => "Oct" # month in short form of the time t.strftime("%B") => "October" # month in full form of the time t.strftime("%y") => "17" # year without century of the time t.strftime("%Z") => "UTC" # Time Zone of the time t.strftime("%p") => "PM" # AM / PM of the time