Fix time shift when creating visits manually

This commit is contained in:
Eugene Burmakin 2025-10-07 22:00:11 +02:00
parent 194f8c3c45
commit a7b92c10f5
4 changed files with 32 additions and 6 deletions

View file

@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Fixed a bug with stats sharing settings being not filled. #1826
- Fixed a bug where user could not be deleted due to counter cache on points. #1818
- Introduce apt-get upgrade before installing new packages in the docker image to prevent vulnerabilities. #1793
- Fixed time shift when creating visits manually. #1679
# [0.33.0] - 2025-09-29

File diff suppressed because one or more lines are too long

View file

@ -71,16 +71,16 @@ module Visits
end
def create_visit(place)
started_at = DateTime.parse(params[:started_at])
ended_at = DateTime.parse(params[:ended_at])
duration_minutes = (ended_at - started_at) * 24 * 60
started_at = Time.zone.parse(params[:started_at])
ended_at = Time.zone.parse(params[:ended_at])
duration_minutes = ((ended_at - started_at) / 60).to_i
@visit = user.visits.create!(
name: params[:name],
place: place,
started_at: started_at,
ended_at: ended_at,
duration: duration_minutes.to_i,
duration: duration_minutes,
status: :confirmed
)

View file

@ -166,6 +166,31 @@ RSpec.describe Visits::Create do
expect(service.visit.duration).to eq(36 * 60) # 36 hours in minutes
end
end
context 'when datetime-local input is provided without timezone' do
let(:params) do
valid_params.merge(
started_at: '2023-12-01T19:54',
ended_at: '2023-12-01T20:54'
)
end
subject(:service) { described_class.new(user, params) }
it 'parses the datetime in the application timezone' do
service.call
visit = service.visit
expect(visit.started_at.hour).to eq(19)
expect(visit.started_at.min).to eq(54)
expect(visit.ended_at.hour).to eq(20)
expect(visit.ended_at.min).to eq(54)
end
it 'calculates correct duration' do
service.call
expect(service.visit.duration).to eq(60) # 1 hour in minutes
end
end
end
end
end