Skip to content

Commit

Permalink
Merge pull request #272 from ifad/chore/remove-string-to-utc-conversion
Browse files Browse the repository at this point in the history
Remove `string_to_utc_time` conversion function
  • Loading branch information
tagliala authored Feb 27, 2024
2 parents ee5c45f + de6eb98 commit a02bf93
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 57 deletions.
8 changes: 4 additions & 4 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions lib/chrono_model/conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,6 @@ module Conversions

ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(?:\.(\d+))?\z/

# rubocop:disable Style/PerlBackrefs
def string_to_utc_time(string)
return string if string.is_a?(Time)

return unless string =~ ISO_DATETIME

# .1 is .100000, not .000001
usec = $7.ljust(6, '0') unless $7.nil?

Time.utc $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i, usec.to_i
end
# rubocop:enable Style/PerlBackrefs

def time_to_utc_string(time)
time.to_fs(:db) << '.' << format('%06d', time.usec)
end
Expand Down
4 changes: 1 addition & 3 deletions lib/chrono_model/time_machine/timeline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ def timeline(record = nil, options = {})
sql << " LIMIT #{options[:limit].to_i}" if options.key?(:limit)

connection.on_schema(Adapter::HISTORY_SCHEMA) do
connection.select_values(sql, "#{name} periods").map! do |ts|
Conversions.string_to_utc_time ts
end
connection.select_values(sql, "#{name} periods")
end
end

Expand Down
37 changes: 0 additions & 37 deletions spec/chrono_model/conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,6 @@
require 'spec_helper'

RSpec.describe ChronoModel::Conversions do
describe '.string_to_utc_time' do
subject(:string_to_utc_time) { described_class.string_to_utc_time(string) }

context 'with a valid UTC time string' do
let(:string) { '2017-02-06 09:46:31.129626' }

it { is_expected.to be_a(Time) }
it { expect(string_to_utc_time.year).to eq 2017 }
it { expect(string_to_utc_time.month).to eq 2 }
it { expect(string_to_utc_time.day).to eq 6 }
it { expect(string_to_utc_time.hour).to eq 9 }
it { expect(string_to_utc_time.min).to eq 46 }
it { expect(string_to_utc_time.sec).to eq 31 }
it { expect(string_to_utc_time.usec).to eq 129_626 } # Ref Issue #32
end

context 'with a valid UTC string without least significant zeros' do
let(:string) { '2017-02-06 09:46:31.129' }

it { is_expected.to be_a(Time) }
it { expect(string_to_utc_time.usec).to eq 129_000 } # Ref Issue #32
end

context 'with a valid UTC string without usec' do
let(:string) { '2017-02-06 09:46:31' }

it { is_expected.to be_a(Time) }
it { expect(string_to_utc_time.usec).to eq 0 }
end

context 'with an invalid UTC time string' do
let(:string) { 'foobar' }

it { is_expected.to be_nil }
end
end

describe '.time_to_utc_string' do
subject { described_class.time_to_utc_string(time) }

Expand Down

0 comments on commit a02bf93

Please sign in to comment.