开发者

Input time + timezone with Rails

开发者 https://www.devze.com 2023-04-12 15:07 出处:网络
I want ask for a specific time that includes a timezone field. Is this possible? At the moment I\'m using this to retrieve the time:

I want ask for a specific time that includes a timezone field. Is this possible?

At the moment I'm using this to retrieve the time:

<%= f.time_select :meeting_time %>
<%= f.time_zone_select :timezone %>

and this to try to change timezone:

meeting_time.in_time_zone(params[:meeting][:timezone])

but when the user inputs the time it will be automatically set to the timezone on the rails server(in this case GMT). Then if the user selects for example GMT-1 it will decrease the time by one hour but what I want is to increase it so all the times are shown in GMT.

Example:

The user inputs 18:00 GMT-1 and the f.time_select will put it as 18:00 UTC. Next, I try to convert it to GMT-1 but as it is already in UTC it will give me 17:00 GMT-1 instead of 19:00 GMT that I want.

Note: I do not want necessarily to开发者_JAVA技巧 show all the times in GMT but I don't think I can save in other format without changing the server timezone


I'm not sure if this is the proper rails convention, but it's a workaround that seems to be working fine for me.

If you change your local time to utc before saving it to the database, it should save as the correct utc time regardless of server time zone.

Set the Time.zone for the current request

    Time.zone = params[:meeting][:timezone]

Adjust to utc

    meeting_time = Time.zone.local_to_utc meeting_time
0

精彩评论

暂无评论...
验证码 换一张
取 消