Ruby / Rails: convert int to time OR get time from integer?
Use Time.at:
t = Time.at(i)
How do I convert this Time integer to a Date in Ruby?
Use Time.at
for this:
t = Time.at(i)
Convert number to date in Ruby
DateTime.strptime('1512387277084', '%Q')
#⇒ #<DateTime: 2017-12-04T11:34:37+00:00
# ((2458092j,41677s,84000000n),+0s,2299161j)>
DateTime
formatting.
Create TimeWithZone object from integer (unix epoch time with millisecond precision) and with specified zone (string)
Assuming that the timestamp is in milliseconds, then 1586653140000 is
Epoch: 1586653140
GMT: Sunday, April 12, 2020 12:59:00 AM
PDT: Saturday, April 11, 2020 17:59:00 PM -in time zone America/Los Angeles
These are just 3 different ways to refer to a specific point in time around the world.
Sat, 11 Apr 2020 20:59:00 PDT -07:00 and 2020-04-11 20:59:00 -0400 each refer to different points in time and not the same as epoch(1586653140)
Since the Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), it wouldn't make sense to take 1586653140
and only change the time zone without adding the zone's offset because now you are talking about another point in time.
To get the right "translation" from the epoch to any time zone you could just do
Time.zone = "GMT"
Time.zone.at(1586653140)
=> Sun, 12 Apr 2020 00:59:00 GMT +00:00
Time.zone = "America/Los_Angeles"
Time.zone.at(1586653140)
=> Sat, 11 Apr 2020 17:59:00 PDT -07:00
When working with dates in time zones in rails it is important to only use functions that take the set time zone into account:
DON’T USE
- Time.now
- Date.today
- Date.today.to_time
- Time.parse("2015-07-04 17:05:37")
- Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z")
DO USE
- Time.current
- 2.hours.ago
- Time.zone.today
- Date.current
- 1.day.from_now
- Time.zone.parse("2015-07-04 17:05:37")
- Time.strptime(string, "%Y-%m-%dT%H:%M:%S%z").in_time_zone
Also keep in mind that in a Rails app, we have three different time zones:
- system time,
- application time, and
- database time.
This post by thoughtbot explains things clearly.
Rails – Convert time duration to integer
You can calculate the seconds as below :
require 'time'
t = Time.parse("00:00:14.01")
sec = t.hour*3600 + t.min*60 + t.sec
sec # => 14
Important method sets here are - Time#hour
, Time#min
, Time#sec
.
As @ChrisHeald suggested -
t = Time.parse("00:00:14.01") - Time.parse("00:00:00")
Related Topics
Difference Between Plugins and Ruby Gems
When to Use Curly Braces VS Parenthesis in Expect Rspec Method
Ruby Dropped in Netbeans 7,How to Use It in Netbeans7
Ruby on Rails: Debugging Rake Tasks
Apply Method to Each Elements in Array/Enumerable
How to Create an Array in Ruby with Default Values
Rmagick Remove White Background from Image and Make It Transparent
How to Convert from a String to Object Attribute Name
How to Compare a String Against Multiple Other Strings
Hashes of Hashes Idiom in Ruby
How to Call an Older Version of a Gem from the Commandline
Rails 3 Additional Session Configuration Options (Key, Expires_After, Secure)
Can Bundler Show Me Which Gems in Gemfile Have Newer Versions (Eg. Dry-Run of Bundle Update)
How to Sort in Ruby/Rails on Two Fields
How to Remove a Substring After a Certain Character in a String Using Ruby