Sha256: b6b0eaba069baf6bd1f80b70ee1471128e53bd20567fae5002b049143f015870

Contents?: true

Size: 1.72 KB

Versions: 3

Compression:

Stored size: 1.72 KB

Contents

# Copyright (C) 2009-2014 MongoDB Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Modified 2015 Elastic

module BSON

  # Injects behaviour for encoding and decoding time values to
  # and from raw bytes as specified by the BSON spec.
  #
  # @see http://bsonspec.org/#/specification
  module LogStashTimestamp

    # A time is type 0x09 in the BSON spec.
    BSON_TYPE = 9.chr.force_encoding(BINARY).freeze

    def to_bson(buffer = ByteBuffer.new, validating_keys = Config.validating_keys?)
      time.to_bson(buffer)
    end

    module ClassMethods
      # Deserialize UTC time from BSON.
      # @param [ BSON ] bson encoded time.
      # @return [ ::LogStash::Timestamp ] The decoded UTC time as a ::LogStash::Timestamp.
      # @see http://bsonspec.org/#/specification
      def from_bson(bson)
        seconds, fragment = BSON::Int64.from_bson(bson).divmod(1000)
        new(::Time.at(seconds, fragment * 1000).utc)
      end
    end

    # Register this type when the module is loaded.
    Registry.register(BSON_TYPE, ::LogStash::Timestamp)
  end

  # Enrich the core LogStash::Timestamp class with this module.
  ::LogStash::Timestamp.send(:include, LogStashTimestamp)
  ::LogStash::Timestamp.send(:extend, LogStashTimestamp::ClassMethods)
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
logstash-output-mongo-1.0.1 lib/logstash/outputs/bson/logstash_timestamp.rb
logstash-output-mongo-1.0.0 lib/logstash/outputs/bson/logstash_timestamp.rb
logstash-output-mongodb-3.1.7 lib/logstash/outputs/bson/logstash_timestamp.rb