Add remaining time calculation

This commit is contained in:
2019-11-04 20:53:31 +03:00
parent b5e7e61d64
commit 18b2bd5b01
4 changed files with 108 additions and 18 deletions

View File

@ -6,6 +6,22 @@ module Dayoff
date: String,
hours: Int32,
)
FORMAT = "%Y-%m-%d"
def initialize(date : Time, hours : Int32)
@date = date.to_s FORMAT
@hours = hours
end
def date_time : Time
location = Time::Location.load("Europe/Moscow")
Time.parse(@date, FORMAT, location)
end
def time_span : Time::Span
Time::Span.new(hours: hours, minutes: 0, seconds: 0)
end
end
class WorkRecord
@ -26,12 +42,12 @@ module Dayoff
def start_time : Time
location = Time::Location.load("Europe/Moscow")
Time.parse(start, FORMAT, location)
Time.parse(@start, FORMAT, location)
end
def finish_time : Time
location = Time::Location.load("Europe/Moscow")
Time.parse(finish.as(String), FORMAT, location)
Time.parse(@finish.as(String), FORMAT, location)
end
def finish_time=(v : Time)
@ -41,5 +57,31 @@ module Dayoff
def started? : Bool
@finish.nil?
end
def finished? : Bool
!@finish.nil?
end
def finished_to_time?(time : Time) : Bool
@finish && finish_time <= time
end
def calc_span(time : Time) : Time::Span
if @finish.nil?
if start_time <= time
time - start_time
else
Time::Span.zero
end
else
if time > finish_time
finish_time - start_time
elsif time > start_time
time - start_time
else
Time::Span.zero
end
end
end
end
end

View File

@ -19,22 +19,24 @@ module Dayoff
@wrecords = @storage.get_work_records
end
def get_planned_hours
sum = 0
@pdates.each do |wd|
sum += wd.hours
end
sum
end
def get_work_hours
@wrecords.reduce 0 do |acc, wr|
diff = wr.finish_time - wr.start_time
acc + diff.total_hours.to_i32
def get_planned_hours(on_time : Time) : Time::Span
check_date = on_time.at_beginning_of_day
@pdates.reduce(Time::Span.zero) do |acc, wd|
if wd.date_time <= check_date
acc + wd.time_span
else
acc
end
end
end
def start(time : Time)
def get_work_hours(on_time : Time) : Time::Span
@wrecords.reduce(Time::Span.zero) do |acc, wr|
acc + wr.calc_span on_time
end
end
def start(time : Time) : Nil
@wrecords.each do |wr|
if time <= wr.start_time || time <= wr.finish_time
raise CrossedTimeSpan.new
@ -45,7 +47,7 @@ module Dayoff
@storage.set_work_records @wrecords
end
def finish(time : Time)
def finish(time : Time) : Nil
started = @wrecords.find { |x| x.started? }
if started.nil?
raise StartedRecordNotFound.new
@ -53,5 +55,11 @@ module Dayoff
started.finish_time = time
@storage.set_work_records @wrecords
end
def remaining_time(on_time : Time) : Time::Span
planned = get_planned_hours on_time
worked = get_work_hours on_time
planned - worked
end
end
end