Class: LogStash::Timestamp

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/logstash/timestamp.rb

Constant Summary collapse

ISO8601_STRFTIME =
"%04d-%02d-%02dT%02d:%02d:%02d.%06d%+03d:00".freeze
ISO8601_PRECISION =
3
JODA_ISO8601_PARSER =
org.joda.time.format.ISODateTimeFormat.dateTimeParser
UTC =
org.joda.time.DateTimeZone.forID("UTC")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time = Time.new) ⇒ Timestamp

Returns a new instance of Timestamp.



22
23
24
# File 'lib/logstash/timestamp.rb', line 22

def initialize(time = Time.new)
  @time = time.utc
end

Instance Attribute Details

#timeObject (readonly)

Returns the value of attribute time.



17
18
19
# File 'lib/logstash/timestamp.rb', line 17

def time
  @time
end

Class Method Details

.at(*args) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/logstash/timestamp.rb', line 26

def self.at(*args)
  epoch = args.first
  if epoch.is_a?(BigDecimal)
    # bug in JRuby prevents correcly parsing a BigDecimal fractional part, see https://github.com/elastic/logstash/issues/4565
    Timestamp.new(::Time.at(epoch.to_i, epoch.frac.to_f * 1000000))
  else
    Timestamp.new(::Time.at(*args))
  end
end

.coerce(time) ⇒ Timestamp?

coerce tries different strategies based on the time object class to convert into a Timestamp.

Parameters:

  • time (String, Time, Timestamp)

    the time object to try coerce

Returns:

  • (Timestamp, nil)

    Timestamp will be returned if successful otherwise nil

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/logstash/timestamp.rb', line 48

def self.coerce(time)
  case time
  when String
    LogStash::Timestamp.parse_iso8601(time)
  when LogStash::Timestamp
    time
  when Time
    LogStash::Timestamp.new(time)
  else
    nil
  end
end

.nowObject



40
41
42
# File 'lib/logstash/timestamp.rb', line 40

def self.now
  Timestamp.new(::Time.now)
end

.parse(*args) ⇒ Object



36
37
38
# File 'lib/logstash/timestamp.rb', line 36

def self.parse(*args)
  Timestamp.new(::Time.parse(*args))
end

.parse_iso8601(t) ⇒ Object



65
66
67
68
69
70
# File 'lib/logstash/timestamp.rb', line 65

def self.parse_iso8601(t)
  millis = JODA_ISO8601_PARSER.parseMillis(t)
  LogStash::Timestamp.at(millis / 1000, (millis % 1000) * 1000)
rescue => e
  raise(TimestampParserError, "invalid timestamp string #{t.inspect}, error=#{e.inspect}")
end

Instance Method Details

#-(value) ⇒ Object



99
100
101
# File 'lib/logstash/timestamp.rb', line 99

def -(value)
  @time - (value.is_a?(Timestamp) ? value.time : value)
end

#to_iso8601Object Also known as: to_s



94
95
96
# File 'lib/logstash/timestamp.rb', line 94

def to_iso8601
  @iso8601 ||= @time.iso8601(ISO8601_PRECISION)
end

#to_json(*args) ⇒ Object Also known as: inspect



88
89
90
91
# File 'lib/logstash/timestamp.rb', line 88

def to_json(*args)
  # ignore arguments to respect accepted to_json method signature
  "\"" + to_iso8601 + "\""
end

#utcObject Also known as: gmtime



82
83
84
85
# File 'lib/logstash/timestamp.rb', line 82

def utc
  @time.utc # modifies the receiver
  self
end