lib/ensurance/time_ensure.rb in ensurance-0.1.18 vs lib/ensurance/time_ensure.rb in ensurance-0.1.19
- old
+ new
@@ -1,6 +1,7 @@
require 'active_support/core_ext/time'
+require 'active_support/time_with_zone'
# NOTE: https://blog.daveallie.com/clean-monkey-patching/
module Ensurance
module TimeEnsure
@@ -8,25 +9,27 @@
base.singleton_class.prepend(ClassMethods)
end
module ClassMethods
def ensure(thing)
+ ::Time.zone ||= "UTC"
+
case thing.class.name
when 'NilClass'
thing
when 'Time'
thing
when 'Date'
thing.beginning_of_day
when 'Integer', 'Float'
- ::Time.at(thing)
+ ::Time.zone.at(thing)
when 'String'
if thing.to_i.to_s == thing
- ::Time.at(thing.to_i)
+ ::Time.zone.at(thing.to_i)
elsif thing.to_f.to_s == thing
- ::Time.at(thing.to_f)
+ ::Time.zone.at(thing.to_f)
else
- ::Time.parse(thing)
+ ::Time.zone.parse(thing)
end
else
if thing.respond_to?(:to_time)
thing.to_time
else