Commit 604c559ed3577b2226048c31bf5c4cff5de478a8
1 parent
50ba7750
Exists in
master
and in
17 other branches
Time keys should use time zone instead of UTC - closes #29
Showing
3 changed files
with
10 additions
and
3 deletions
Show diff stats
lib/groupdate/scopes.rb
... | ... | @@ -13,6 +13,7 @@ module Groupdate |
13 | 13 | column = connection.quote_table_name(args[0]) |
14 | 14 | time_zone = args[1] || options[:time_zone] || Groupdate.time_zone || Time.zone || "Etc/UTC" |
15 | 15 | if time_zone.is_a?(ActiveSupport::TimeZone) or time_zone = ActiveSupport::TimeZone[time_zone] |
16 | + time_zone_object = time_zone | |
16 | 17 | time_zone = time_zone.tzinfo.name |
17 | 18 | else |
18 | 19 | raise "Unrecognized time zone" |
... | ... | @@ -75,7 +76,7 @@ module Groupdate |
75 | 76 | group = group(Groupdate::OrderHack.new(sanitize_sql_array(query), field, time_zone)) |
76 | 77 | range = args[2] || options[:range] || true |
77 | 78 | unless options[:series] == false |
78 | - Series.new(group, field, column, time_zone, range, week_start, day_start) | |
79 | + Series.new(group, field, column, time_zone_object, range, week_start, day_start) | |
79 | 80 | else |
80 | 81 | group |
81 | 82 | end | ... | ... |
lib/groupdate/series.rb
... | ... | @@ -23,7 +23,7 @@ module Groupdate |
23 | 23 | when "day_of_week", "hour_of_day" |
24 | 24 | lambda{|k| k.to_i } |
25 | 25 | else |
26 | - lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).utc } | |
26 | + lambda{|k| (k.is_a?(String) ? utc.parse(k) : k.to_time).in_time_zone(@time_zone) } | |
27 | 27 | end |
28 | 28 | |
29 | 29 | count = Hash[ count.map{|k, v| [cast_method.call(k), v] } ] |
... | ... | @@ -76,7 +76,7 @@ module Groupdate |
76 | 76 | series << series.last + step |
77 | 77 | end |
78 | 78 | |
79 | - series.map{|s| s.to_time.utc } | |
79 | + series | |
80 | 80 | else |
81 | 81 | [] |
82 | 82 | end | ... | ... |
test/test_helper.rb
... | ... | @@ -474,6 +474,12 @@ module TestGroupdate |
474 | 474 | assert_empty User.where("id = 0").group_by_day(:created_at).count |
475 | 475 | end |
476 | 476 | |
477 | + def test_time_zone | |
478 | + create_user "2013-05-01 00:00:00 UTC" | |
479 | + time_zone = "Pacific Time (US & Canada)" | |
480 | + assert_equal time_zone, User.group_by_day(:created_at, time_zone: time_zone).count.keys.first.time_zone.name | |
481 | + end | |
482 | + | |
477 | 483 | # helpers |
478 | 484 | |
479 | 485 | def assert_result_time(method, expected, time_str, time_zone = false, options = {}) | ... | ... |