K
K
ksim_miloff2016-10-13 19:20:54
MongoDB
ksim_miloff, 2016-10-13 19:20:54

Why does the Time value shift on save?

Здравствуйте,
Работаю над одним Rails-приложением, в качестве СУБД использую mongoDB через mongoid. Написал свой mongoid-тип для хранения расписания, который хранит несколько Time-значений. Пару месяцев назад была проблема с тем, что при каждом сохранении экземпляра модели, время съезжало соответственно с моим часовым поясом. Гуглил, искал решение, нашел в итоге решил небольшой правкой метода demongoize:

def demongoize(hash)
...
    h[type][:start] = Time.zone.utc_to_local h[type][:start]
    h[type][:end]   = Time.zone.utc_to_local h[type][:end]
...
end

Those. the data was shifted when saved, but I "balanced" this when reading by shifting it back to my zone.
Yesterday we found that in the project all Time-values ​​are again crooked, this time in order for everything to work, we had to comment out the lines above.
In general, apparently, this problem is very common, in a bunch of rails + mongoid. I would like to find a concrete solution that will save / return the data in the form in which they are sent to the database. Please help find such a solution. :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Chronic 86, 2016-10-14
@chronic86

Try 'mongo_mapper'
But be careful.

irb(main):001:0> require 'date'
=> true
irb(main):002:0> require 'mongo_mapper'
=> true
irb(main):003:0> Date.to_mongo(Time.now)
=> 2016-10-14 00:00:00 UTC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question