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
# File 'lib/logstash/timestamp.rb', line 26

def self.at(*args)
  Timestamp.new(::Time.at(*args))
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:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/logstash/timestamp.rb', line 42

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



34
35
36
# File 'lib/logstash/timestamp.rb', line 34

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

.parse(*args) ⇒ Object



30
31
32
# File 'lib/logstash/timestamp.rb', line 30

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

.parse_iso8601(t) ⇒ Object



59
60
61
62
63
64
# File 'lib/logstash/timestamp.rb', line 59

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



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

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

#to_iso8601Object Also known as: to_s



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

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

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



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

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

#utcObject Also known as: gmtime



76
77
78
79
# File 'lib/logstash/timestamp.rb', line 76

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