Skip to content

Commit

Permalink
Merge pull request #894 from jmondo/time_zone_fix
Browse files Browse the repository at this point in the history
UTC parsing for Time fields
  • Loading branch information
bbenezech committed Dec 25, 2011
2 parents 0ed7ef9 + 85caaff commit 3317182
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rails_admin/config/fields/types/datetime.rb
Expand Up @@ -41,7 +41,12 @@ def normalize(date_string, format)
end
end
end
::Time.zone.parse(date_string, format)
parse_date_string(date_string)
end

# Parse normalized date strings using time zone
def parse_date_string(date_string)
::Time.zone.parse(date_string)
end

end
Expand Down
5 changes: 5 additions & 0 deletions lib/rails_admin/config/fields/types/time.rb
Expand Up @@ -21,6 +21,11 @@ def parse_input(params)
params[name] = self.class.normalize(params[name], localized_time_format) if params[name]
end

# Parse normalized date (time) strings using UTC
def self.parse_date_string(date_string)
::DateTime.parse(date_string)
end

register_instance_option(:strftime_format) do
(localized_format.include? "%p") ? "%I:%M %p" : "%H:%M"
end
Expand Down
10 changes: 10 additions & 0 deletions spec/requests/config/edit/rails_admin_config_edit_spec.rb
Expand Up @@ -617,6 +617,16 @@ class Team
@record.time_field.strftime("%H:%M").should eql(@time.strftime("%H:%M"))
end

it "should interpret time value as UTC when timezone is specified" do
Time.zone = 'Eastern Time (US & Canada)' # -05:00

visit new_path(:model_name => "field_test")
fill_in "field_test[time_field]", :with => @time.strftime("%H:%M")
click_button "Save"
@record = RailsAdmin::AbstractModel.new("FieldTest").first
@record.time_field.strftime("%H:%M").should eql(@time.strftime("%H:%M"))
end

it "should have a customization option" do
RailsAdmin.config FieldTest do
edit do
Expand Down

0 comments on commit 3317182

Please sign in to comment.