Sha256: b7479aaebba718a6f2aff58c059a821641c4fa6a13066c6374902280944a89ff

Contents?: true

Size: 1 KB

Versions: 16

Compression:

Stored size: 1 KB

Contents

require "logstash/namespace"
require "date" # for DateTime

# Provide our own Time wrapper for ISO8601 support
# Example:
#   >> LogStash::Time.now.to_iso8601
#   => "2010-10-17 00:25:24.619014-0700"
#
#   >> LogStash::Time.now.utc.to_iso8601
#   => "2010-10-17 07:25:26.788704Z"
class LogStash::Time < ::Time
  ISO8601 = "%Y-%m-%dT%H:%M:%S"

  # Return a string that is this time in ISO8601 format.
  def to_iso8601
    tz = self.utc? ? "Z" : self.strftime("%z")
    # zero-pad tv_usec so the time string is sortable.
    return "%s.%06d%s" % [self.strftime(ISO8601), self.tv_usec, tz]
  end

  def self.to_iso8601(obj)
    if obj.is_a?(DateTime)
      tz = obj.offset == 0 ? "Z" : obj.strftime("%z")
      # DateTime#sec_fraction is fractional seconds "of a day"
      sec_fraction = (obj.sec_fraction.to_f * 86400 * 1000000)
      return "%s.%06d%s" % [obj.strftime(ISO8601), sec_fraction, tz]
    else
      raise "Can't convert object of type #{obj.class} (#{obj}) to iso8601."
    end
  end
end # class LogStash::Time

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
logstash-lite-0.2.20110505142231 lib/logstash/time.rb
logstash-lite-0.2.20110422152244 lib/logstash/time.rb
logstash-lite-0.2.20110405105201 lib/logstash/time.rb
logstash-lite-0.2.20110331121236 lib/logstash/time.rb
logstash-lite-0.2.20110329105411 lib/logstash/time.rb
logstash-lite-0.2.20110206003603 lib/logstash/time.rb
logstash-lite-0.2.20110203130400 lib/logstash/time.rb
logstash-lite-0.2.20110122143801 lib/logstash/time.rb
logstash-lite-0.2.20110112115019 lib/logstash/time.rb
logstash-lite-0.2.20101222161646 lib/logstash/time.rb
logstash-lite-0.2.20101208111718 lib/logstash/time.rb
logstash-lite-0.2.20101207114354 lib/logstash/time.rb
logstash-lite-0.2.20101201111523 lib/logstash/time.rb
logstash-lite-0.2.20101129210156 lib/logstash/time.rb
logstash-lite-0.2.20101129205551 lib/logstash/time.rb
logstash-lite-0.2.20101129155412 lib/logstash/time.rb