user2931445
user2931445

Reputation: 19

How to save time zone for each record

I have one model with name Job I have a column name with due_date for that column I want to save time_zone

This is my code:

def create
   @job = Job.new(params[:job])

#saving time_zone
   if params[:job][:assigned_to] != "NULL"
      @assigned_to = params[:job][:assigned_to]
      @user = User.find_by_name(@assigned_to)
      Time.zone = @user.time_zone
      @job.due_date = Time.use_zone(@user.time_zone) {Time.zone.parse("#{params[:job][:due_date]}").in_time_zone(@user.time_zone)}
   end

   if @job.save
end

please help me how to save time_zone for each record.

Upvotes: 0

Views: 84

Answers (1)

DashControl
DashControl

Reputation: 284

Since no one has posted an answer to this yet, here is my trivial answer. Generate a regex for each time-zone and apply it to due_date

Something like this:

re = Regexp.new('ADT|AKDT|AKST|AST|CDT|CST|EDT|EGST|EGT|EST|HADT|HAST|MDT|MST|NDT|NST|PDT|PMDT|PMST|PST|WGST|WGT')
ans = @job.due_date.to_s re

Should parse out the timezone for you to save.

Upvotes: 1

Related Questions