Skip to content

refactor Time.parse to use String#to_time #465

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

Merged
merged 1 commit into from
Mar 6, 2025
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
3 changes: 2 additions & 1 deletion lib/result_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'time'

require "#{LKP_SRC}/lib/string"
require "#{LKP_SRC}/lib/common"
require "#{LKP_SRC}/lib/property"
require "#{LKP_SRC}/lib/yaml"
Expand Down Expand Up @@ -128,7 +129,7 @@ def each
class Completion
def initialize(line)
fields = line.split
@time = Time.parse(fields[0..2].join(' '))
@time = fields[0..2].join(' ').to_time
@_rt = MResultRoot.new fields[4]
@status = fields.drop(4).join ' '
end
Expand Down
1 change: 1 addition & 0 deletions lib/string.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
require 'active_support/core_ext/string/conversions'

REGEX_ANSI_COLOR = /\e\[([0-9;]+m|[mK])/.freeze

Expand Down
2 changes: 1 addition & 1 deletion programs/igt/parse
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ subtest_name = nil
has_dynamic_subtest = false

def valid_datetime?(datetime_str)
Time.parse(datetime_str)
datetime_str.to_time
rescue StandardError
false
end
Expand Down
4 changes: 3 additions & 1 deletion programs/iostat/parse
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
# Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
# sda 0.15 12.76 3.02 9.81 63.05 126.27 29.51 0.16 12.72 3.96 15.42 1.55 1.99
# sdd 0.09 34.01 8.43 20.57 189.15 750.68 64.80 0.79 27.07 1.31 37.63 0.45 1.31
LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.dirname(File.realpath($PROGRAM_NAME))))

require 'time'
require "#{LKP_SRC}/lib/string"

def normalize_key(key)
key.sub('%', '')
Expand Down Expand Up @@ -45,7 +47,7 @@ while (line = $stdin.gets)
case line
when /^[0-9T:+-]{24}$/ # "2013-04-02T13:58:40+0800", by 'S_TIME_FORMAT=ISO isotat -t'
time = line.chomp('\n')
puts "time: #{Time.parse(time).to_i}"
puts "time: #{time.to_time.to_i}"
when /^avg-cpu:/
extract_avg_cpu line
when /^Device:/
Expand Down
3 changes: 2 additions & 1 deletion programs/mpstat/parse
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ require 'time'
require 'json'
require 'fileutils'
require "#{LKP_SRC}/lib/log"
require "#{LKP_SRC}/lib/string"

# The below is an example of mpstat output, read it then parse it into hash data.
# {"sysstat": {
Expand Down Expand Up @@ -170,7 +171,7 @@ mpstat_hash['sysstat']['hosts'][0]['statistics'].each do |item|
elsif k == 'timestamp'
# Append UTC to timestamp to fix the timezone as no timezone is provided by
# mpstat, otherwise the parsed result is related to the timezone of local system.
time = Time.parse([data, v, 'UTC'].join(' ')).to_i
time = [data, v, 'UTC'].join(' ').to_time.to_i
results['time'] = time
end
end
Expand Down
5 changes: 3 additions & 2 deletions programs/sar/parse
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env ruby
LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.dirname(File.realpath($PROGRAM_NAME))))

require 'time'
require 'json'

require "#{LKP_SRC}/lib/string"
RESULT_ROOT = ENV['RESULT_ROOT']

# The below is part of the sar json file, read it then parse it into hash data.
Expand Down Expand Up @@ -96,7 +97,7 @@ sar_hash['sysstat']['hosts'][0]['statistics'].each do |item|
elsif v_.instance_of?(Hash)
get_hash_result k_, v_, results
elsif k == 'timestamp'
timestamp = Time.parse([v['data'], v['time']].join(' ')).to_i
timestamp = [v['data'], v['time']].join(' ').to_time.to_i
results['timestamp'] = timestamp
else
results["#{k}.#{k_}"] = v_
Expand Down
4 changes: 2 additions & 2 deletions programs/unixbench/parse
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ while (line = $stdin.gets)
when /^Benchmark Run: .+ (\d+:\d+:\d+) - (\d+:\d+:\d+)$/
# Benchmark Run: Tue Jan 01 2019 15:48:24 - 15:56:36
# We only care about the difference value between end time and start time
start_time = Time.parse($1).to_f
end_time = Time.parse($2).to_f
start_time = $1.to_time.to_f
end_time = $2.to_time.to_f
duration = if end_time > start_time
end_time - start_time
else
Expand Down
6 changes: 3 additions & 3 deletions programs/vmstat/parse
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.dirname(File.realpath($PROGRAM_NAME))))
require 'time'

require "#{LKP_SRC}/lib/string"
# procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu----- -----timestamp-----
# r b swpd free buff cache si so bi bo in cs us sy id wa st PST
# 2 0 0 224636 33636 6760228 0 0 54 141 129 23 1 1 98 0 0 2015-01-11 20:18:35
Expand Down Expand Up @@ -29,7 +29,7 @@ while (line = $stdin.gets)

data = line.split
if data.size == keys.size + 1 # has "date time" in end of line
time = Time.parse(data.pop(2).join(' ')).to_i
time = data.pop(2).join(' ').to_time.to_i
data.push time
show_record keys, data
elsif data.size == keys.size - 1 # no timestamp in old versions of vmstat
Expand Down
Loading