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

Remove string_to_utc_time conversion function #272

Merged
merged 1 commit into from
Feb 27, 2024
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
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
Loading