This is a super easy way to ensure that timestamps/datetimes etc render correctly according to the timezone that the object is in.
In our usage we have a time_zone
attribute on our models that stores the time zone in a format that we can find with active support.
Example American Samoa
, for more examples run bundle exec rake time:zones:all
in your rails project.
We weren't aiming to display the datetime in a format relative to the user, but rather just display that the time was in X time zone
# lets assume we have an attribute called start_time
class Foo < ApplicationRecord
include TimeZones
time_zone_aware :start_time
end
foo = Foo.new(start_time: "2020-07-15 14:02:52", time_zone: "Pretoria")
foo.start_time.to_s # 2020-07-15 14:02:52 +0200
foo.time_zone = "Bogota"
foo.start_time.to_s # 2020-07-15 14:02:52 -0500
Maybe don't call
super()
twice?Also, what if someone passes a
String
to the assignment method?