Sha256: 7f0faeabd92bc1174c98aa80440b734dd398153cadd321d9859e969c270ec4c4

Contents?: true

Size: 848 Bytes

Versions: 23

Compression:

Stored size: 848 Bytes

Contents

module Mongoid #:nodoc:
  module Extensions #:nodoc:
    module TimeConversions #:nodoc:
      def set(value)
        return nil if value.blank?
        time = convert_to_time(value)
        strip_milliseconds(time).utc
      end

      def get(value)
        return nil if value.blank?
        if Mongoid::Config.instance.use_utc?
          value
        else
          value.getlocal
        end
      end

      protected

      def strip_milliseconds(time)
        ::Time.at(time.to_i)
      end

      def convert_to_time(value)
        case value
          when ::String then ::Time.parse(value)
          when ::DateTime then ::Time.utc(value.year, value.month, value.day, value.hour, value.min, value.sec)
          when ::Date then ::Time.utc(value.year, value.month, value.day)
          else value
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 6 rubygems

Version Path
mongoid-1.9.5 lib/mongoid/extensions/time_conversions.rb
mongoid-with-auth-1.9.4 lib/mongoid/extensions/time_conversions.rb
mongoid-rails2-1.9.4 lib/mongoid/extensions/time_conversions.rb
mongoid-rails2-1.9.3 lib/mongoid/extensions/time_conversions.rb
mongoid-1.9.2 lib/mongoid/extensions/time_conversions.rb
sskirby-mongoid-1.9.1 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.18 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.17 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.16 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.15 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.14 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.13 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.11 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.10 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.7 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.8 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.9 lib/mongoid/extensions/time_conversions.rb
mongoid-2.0.0.beta.12 lib/mongoid/extensions/time_conversions.rb
mongoid-locomotive-2.0.0.beta9 lib/mongoid/extensions/time_conversions.rb
mongoid-1.9.1 lib/mongoid/extensions/time_conversions.rb