I just had a problem with a DateTime field that was submitted with the wrong timezone offset that I usually observe for people submitting from the same country (how surprising .... coming from an Internet Explorer). I am wondering if this may be linked with DST that will end soon at the end of october, and that some browser might have problems with that (although the affected DateTime was around the 15th of October, before the change).
The frontend form was using bootstrap datetimepicker (whose underlying implementation rely on momentJS). Here's a small extract of the JS that activates the picker and the field, although I'm not sure it's relevant here
<div class="col-xs-4" id="appointment-time-start">
<%= f.hidden_field :from %>
</div>
<script>
$('#appointment-time-start').datetimepicker({
format: 'LT',
defaultDate: "<%= model.from || (Time.now + 2.days) %>",
});
</script>
In order to avoid this issue, I am asking the user to also submit a timezone whose default is set to Paris timezone.
f.time_zone_select(:time_zone)
How should I use this timezone to modify the other dates that are sent as params ? I have a simple appointment form that sends two datetimes, and I need to modify them to ensure their offset matches the time_zone
param (AND the Daylight Saving Time extra +1 if it is in effect).
I am also wondering if I should do this in the controller, or directly in the model. My controller is doing the usual thing and my current model looks like (but right now I'm not doing anything with time_zone
)
class Appointment
include Mongoid::Document
field :from, type: DateTime
field :to, type: DateTime
field :time_zone, default: 'Paris'
Well I found this answer on a similar question... I'm surprised that there isn't a simpler helper that does that oO
def set_in_timezone(time, zone)
Time.use_zone(zone) { time.to_datetime.change(offset: Time.zone.now.strftime("%z")) }
end
Note that it still doesn't answer my question about whether I should do it in the model/controller.