I'm using this quite a bit to convert Unix timestamps that I receive from a third-party API, into UTC datetimes:
Time.at(1386549840).utc.to_datetime
How can I create a custom method to keep from repeating that so much? I use this primarily in my models and some background job workers.
Something like:
1386549840.convert_to_utc
This happens to be in a Rails 4 app using Ruby 2.0, FWIW.
Write a method yourself to do it:
class Fixnum
def convert_to_utc
Time.at(self).utc.to_datetime
end
end
Classes in Ruby are open for extension.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.