File: round_timestamps.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (49 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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

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