Skip to content

Commit

Permalink
Merge pull request #85 from mreinsch/fix-time-subtraction
Browse files Browse the repository at this point in the history
easier way to subtract a duration from a time
  • Loading branch information
jmettraux authored Jan 19, 2023
2 parents 8072982 + 0607952 commit 0027200
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/fugit/duration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def subtract(a)
when Numeric then add_numeric(-a)
when Fugit::Duration then add_duration(-a)
when String then add_duration(-self.class.parse(a))
when ::Time, ::EtOrbi::EoTime then add_to_time(a)
when ::Time, ::EtOrbi::EoTime then opposite.add_to_time(a)
else fail ArgumentError.new(
"cannot subtract #{a.class} instance to a Fugit::Duration")
end
Expand Down
9 changes: 9 additions & 0 deletions spec/duration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,15 @@
expect((d - s).to_plain_s).to eq('3h1s')
end

it 'subtracts Time instances' do

d = Fugit.parse('1Y2h')
t = Time.parse('2016-02-02T11:11:11Z')

expect(Fugit.time_to_plain_s(d.subtract(t), false)).to eq('2015-02-02 09:11:11')
expect(Fugit.time_to_plain_s(d - t, false)).to eq('2015-02-02 09:11:11')
end

it 'fails else' do

d = Fugit.parse('1Y2h')
Expand Down

0 comments on commit 0027200

Please sign in to comment.