mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 06:37:19 -04:00
Allow unary operators and comparable on timestamp object closes #2052
Fixes #2061
This commit is contained in:
parent
14023618b4
commit
9af51505b6
2 changed files with 50 additions and 1 deletions
|
@ -10,8 +10,9 @@ module LogStash
|
|||
|
||||
class Timestamp
|
||||
extend Forwardable
|
||||
include Comparable
|
||||
|
||||
def_delegators :@time, :tv_usec, :usec, :year, :iso8601, :to_i, :tv_sec, :to_f, :to_edn
|
||||
def_delegators :@time, :tv_usec, :usec, :year, :iso8601, :to_i, :tv_sec, :to_f, :to_edn, :<=>, :+
|
||||
|
||||
attr_reader :time
|
||||
|
||||
|
@ -89,5 +90,12 @@ module LogStash
|
|||
end
|
||||
alias_method :to_s, :to_iso8601
|
||||
|
||||
def -(value)
|
||||
if value.is_a?(Timestamp)
|
||||
@time - value.time
|
||||
else
|
||||
@time - value
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
require 'spec_helper'
|
||||
require "logstash/timestamp"
|
||||
|
||||
describe LogStash::Timestamp do
|
||||
|
@ -39,4 +40,44 @@ describe LogStash::Timestamp do
|
|||
it "should support to_json and ignore arguments" do
|
||||
expect(LogStash::Timestamp.parse_iso8601("2014-09-23T00:00:00-0800").to_json(:some => 1, :argumnents => "test")).to eq("\"2014-09-23T08:00:00.000Z\"")
|
||||
end
|
||||
|
||||
it "should support timestamp comparaison" do
|
||||
current = LogStash::Timestamp.new(Time.now)
|
||||
future = LogStash::Timestamp.new(Time.now + 100)
|
||||
|
||||
expect(future > current).to eq(true)
|
||||
expect(future < current).to eq(false)
|
||||
expect(current == current).to eq(true)
|
||||
|
||||
expect(current <=> current).to eq(0)
|
||||
expect(current <=> future).to eq(-1)
|
||||
expect(future <=> current).to eq(1)
|
||||
end
|
||||
|
||||
it "should allow unary operation +" do
|
||||
current = Time.now
|
||||
t = LogStash::Timestamp.new(current) + 10
|
||||
expect(t).to eq(current + 10)
|
||||
end
|
||||
|
||||
describe "subtraction" do
|
||||
it "should work on a timestamp object" do
|
||||
t = Time.now
|
||||
current = LogStash::Timestamp.new(t)
|
||||
future = LogStash::Timestamp.new(t + 10)
|
||||
expect(future - current).to eq(10)
|
||||
end
|
||||
|
||||
it "should work on with time object" do
|
||||
current = Time.now
|
||||
t = LogStash::Timestamp.new(current + 10)
|
||||
expect(t - current).to eq(10)
|
||||
end
|
||||
|
||||
it "should work with numeric value" do
|
||||
current = Time.now
|
||||
t = LogStash::Timestamp.new(current + 10)
|
||||
expect(t - 10).to eq(current)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue