Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time: Add EventTime#to_time for faster Time creation #2469

Merged
merged 1 commit into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/fluent/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ def to_s
@sec.to_s
end

begin
# ruby 2.5 or later
Time.at(0, 0, :nanosecond)

def to_time
Time.at(@sec, @nsec, :nanosecond)
end
rescue
def to_time
Time.at(@sec, @nsec / 1000.0)
end
end

def to_json(*args)
@sec.to_s
end
Expand Down
13 changes: 13 additions & 0 deletions test/test_event_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ class EventTimeTest < Test::Unit::TestCase
assert_equal('100', "#{time}")
end

test '#to_time' do
time = Fluent::EventTime.new(@now.to_i, @now.nsec).to_time
assert_instance_of(Time, time)
assert_equal(@now.to_i, time.to_i)
begin
::Time.at(0, 0, :nanosecond)
assert_equal(@now.nsec, time.nsec)
rescue
# Time.at(@sec, @nsec / 1000.0) sometimes cause 1 diff error in nsec by 1000.0
assert_in_delta(@now.nsec, time.nsec, 1)
end
end

test '#to_json' do
time = Fluent::EventTime.new(100)
assert_equal('100', time.to_json)
Expand Down