Skip to content

Commit

Permalink
Rewrite time_to_seconds, add rspec
Browse files Browse the repository at this point in the history
Signed-off-by: Vitalii Chulak <vitalii@daynix.com>
  • Loading branch information
Jedoku committed Feb 13, 2024
1 parent 7f3dfe9 commit f956c24
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/auxiliary/time_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@
module AutoHCK
# Helper module
module Helper
def time_to_seconds(time_str)
days, time_part = time_str.split('.')
hours, minutes, seconds = time_part.split(':').map(&:to_i)
total_hours = (days.to_i * 24) + hours
(total_hours * 3600) + (minutes * 60) + seconds
def time_to_seconds(time_string)

Check notice

Code scanning / Rubocop

A calculated magnitude based on number of assignments, branches, and conditions. Note

Metrics/AbcSize: Assignment Branch Condition size for time_to_seconds is too high. [<6, 18, 1> 19/17]
match = time_string.match(/^(?:(\d+)\.)?(?:(\d+):)?(\d+)(?::(\d+))?(?:\.(\d+))?$/)

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
return 0 unless match

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
days = match[1].to_i

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
hours = match[2].to_i

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
minutes = match[3].to_i

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
seconds = match[4].to_i
fractional_seconds = "0.#{match[5]}".to_f

((days * 24 + hours) * 60 + minutes) * 60 + seconds + fractional_seconds

Check warning

Code scanning / Rubocop

Checks for expressions containing multiple binary operations with ambiguous precedence. Warning

Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.

Check warning

Code scanning / Rubocop

Checks for expressions containing multiple binary operations with ambiguous precedence. Warning

Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.

Check warning

Code scanning / Rubocop

Checks for expressions containing multiple binary operations with ambiguous precedence. Warning

Lint/AmbiguousOperatorPrecedence: Wrap expressions with varying precedence with parentheses to avoid ambiguity.
end

def seconds_to_time(sec)
Expand Down
23 changes: 23 additions & 0 deletions spec/lib/auxiliary/test_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative '../../../lib/auxiliary/time_helper.rb'

Check notice

Code scanning / Rubocop

Add the frozen_string_literal comment to the top of files to help transition to frozen string literals by default. Note

Style/FrozenStringLiteralComment: Missing frozen string literal comment.

Check notice

Code scanning / Rubocop

Checks for the presence of superfluous `.rb` extension in the filename provided to `require` and `require_relative`. Note

Style/RedundantFileExtensionInRequire: Redundant .rb file extension detected.

RSpec.describe AutoHCK::Helper do
include AutoHCK::Helper

describe '#time_to_seconds' do

Check notice

Code scanning / Rubocop

Avoid trailing whitespace. Note

Layout/TrailingWhitespace: Trailing whitespace detected.
time_strings = {
"1:2" => 3720, #hh:mm

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Check notice

Code scanning / Rubocop

Comments should start with a space. Note

Layout/LeadingCommentSpace: Missing space after #.
"1:2:3" => 3723, #hh:mm:ss

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Check notice

Code scanning / Rubocop

Comments should start with a space. Note

Layout/LeadingCommentSpace: Missing space after #.
"1.2:3" => 86400 + 7380, #dd.hh:mm

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Check notice

Code scanning / Rubocop

Add underscores to large numeric literals to improve their readability. Note

Style/NumericLiterals: Use underscores(_) as thousands separator and separate every 3 digits with them.

Check notice

Code scanning / Rubocop

Comments should start with a space. Note

Layout/LeadingCommentSpace: Missing space after #.
"1.2:3:4" => 86400 + 7380 + 4, #dd.hh:mm:ss

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Check notice

Code scanning / Rubocop

Add underscores to large numeric literals to improve their readability. Note

Style/NumericLiterals: Use underscores(_) as thousands separator and separate every 3 digits with them.

Check notice

Code scanning / Rubocop

Comments should start with a space. Note

Layout/LeadingCommentSpace: Missing space after #.
"1:2:3.22" => 3723.22, #hh:mm:ss.ff

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Check notice

Code scanning / Rubocop

Comments should start with a space. Note

Layout/LeadingCommentSpace: Missing space after #.
"1.2:3:4.22" => 86400 + 7380 + 4.22, #dd.hh:mm:ss.ff

Check notice

Code scanning / Rubocop

Checks if uses of quotes match the configured preference. Note

Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.

Check notice

Code scanning / Rubocop

Add underscores to large numeric literals to improve their readability. Note

Style/NumericLiterals: Use underscores(_) as thousands separator and separate every 3 digits with them.

Check notice

Code scanning / Rubocop

Checks for trailing comma in hash literals. Note

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.

Check notice

Code scanning / Rubocop

Comments should start with a space. Note

Layout/LeadingCommentSpace: Missing space after #.
}

time_strings.each do |time, expected|
it "correctly converts '#{time}' to #{expected} seconds" do
expect(time_to_seconds(time)).to eq(expected)
end
end
end
end

Check notice

Code scanning / Rubocop

Checks trailing blank lines and final newline. Note

Layout/TrailingEmptyLines: Final newline missing.

0 comments on commit f956c24

Please sign in to comment.