Sha256: ea7391f2d01fc860f7b5bb2b8891e3e64de90bef3fd6b72223d5f3a997c0e656

Contents?: true

Size: 1.6 KB

Versions: 25

Compression:

Stored size: 1.6 KB

Contents

# frozen-string-literal: true
#
# The round_timestamps extension will automatically round timestamp
# values to the database's supported level of precision before literalizing
# them.
#
# For example, if the database supports millisecond precision, and you give
# it a Time value with microsecond precision, it will round it appropriately:
#
#   Time.at(1405341161.917999982833862)
#   # default: 2014-07-14 14:32:41.917999
#   # with extension: 2014-07-14 14:32:41.918000
#
# The round_timestamps extension correctly deals with databases that support
# millisecond or second precision.  In addition to handling Time values, it
# also handles DateTime values and Sequel::SQLTime values (for the TIME type).
#
# To round timestamps for a single dataset:
#
#   ds = ds.extension(:round_timestamps)
#
# To round timestamps for all datasets on a single database:
#
#   DB.extension(:round_timestamps)
#
# Related module: Sequel::Dataset::RoundTimestamps

unless RUBY_VERSION >= '1.9'
  # :nocov:
  raise LoadError, 'the round_timestamps extension only works on ruby 1.9+'
  # :nocov:
end

module Sequel
  class Dataset
    module RoundTimestamps
      # Round DateTime values before literalizing
      def literal_datetime(v)
        super(v + Rational(5, 10**timestamp_precision)/864000)
      end

      # Round Sequel::SQLTime values before literalizing
      def literal_sqltime(v)
        super(v.round(timestamp_precision))
      end

      # Round Time values before literalizing
      def literal_time(v)
        super(v.round(timestamp_precision))
      end
    end

    register_extension(:round_timestamps, RoundTimestamps)
  end
end

Version data entries

25 entries across 23 versions & 2 rubygems

Version Path
sequel-4.49.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.48.0 lib/sequel/extensions/round_timestamps.rb
tdiary-5.0.5 vendor/bundle/gems/sequel-4.44.0/lib/sequel/extensions/round_timestamps.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/sequel-4.44.0/lib/sequel/extensions/round_timestamps.rb
tdiary-5.0.5 vendor/bundle/gems/sequel-4.47.0/lib/sequel/extensions/round_timestamps.rb
sequel-4.47.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.46.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.45.0 lib/sequel/extensions/round_timestamps.rb
tdiary-5.0.4 vendor/bundle/gems/sequel-4.44.0/lib/sequel/extensions/round_timestamps.rb
sequel-4.44.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.43.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.42.1 lib/sequel/extensions/round_timestamps.rb
sequel-4.42.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.41.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.40.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.39.0 lib/sequel/extensions/round_timestamps.rb
tdiary-5.0.2 vendor/bundle/gems/sequel-4.37.0/lib/sequel/extensions/round_timestamps.rb
sequel-4.38.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.37.0 lib/sequel/extensions/round_timestamps.rb
sequel-4.36.0 lib/sequel/extensions/round_timestamps.rb