I'm building an app which has a database of businesses and they have attributes like the opening/closing times, e.g. 9:00 – 18:00
. Also there is a whole lot of other object which has some time/date info associated.
Those objects are located in different time zones, and the app generally doesn't care where exactly they are. The site editors and the end users of the app are also in different time zones.
And I'm looking for a very simple thing — I want the date/time object submitted by an editor to be stored exactly as it was submitted and then the end users should see exactly the same date/time value.
And everything is good in the JavaScript-land, Postgres is also happy to save whatever datetime I give it, but Rails tries to apply its Timezone logic everywhere — and I don't want it to be neither UTC, neither local, neither any other time zone — I want to just store the raw value — and it seems that Ruby (and Rails) just don't have such a simple concept at all.
I don't really want to store date/times as numbers or strings, neither I don't want to use ruby Time objects as it seems that they always have a concept of the timezone.
So I wonder — is there any ruby library to handle date/times without zones?
Thank you
Rails adds a time zone for all DateTime
columns and starting in Rails 5.1, Time
columns are also time zone aware. They will be saved as UTC in the database by default and then, when called, returned to you in UTC or to a time zone you specified in application.rb
. So, there isn't really a way out of time zones being involved in the storing a DateTime
or Time
, but you can display these values however you want.
You can use the strftime method for changing how your DateTime
and Time
objects are displayed and use this helpful site for playing around with it.
Alternatively, if you don't care about using any of the methods associated with DateTime
or Time
, you can just save this information as strings. I would suggest embracing the default UTC behavior though and not worrying too much about it since if everything is always saved as UTC and you don't have a time zone set in application.rb
, then you can count on always getting the same values returned to you when you need them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With