Rewrite diff methods

This commit is contained in:
2019-11-17 20:20:05 +03:00
parent d6b1d76ed4
commit af95c67110
4 changed files with 92 additions and 102 deletions

View File

@ -23,12 +23,12 @@ module Dayoff
def initialize(@date : Time, @hours : Int32)
end
def time_span : Time::Span
Time::Span.new(hours: hours, minutes: 0, seconds: 0)
end
def same_date?(d : Time) : Bool
@date.date == d.date
def in_range(from_time : Time, to_time : Time) : Time::Span
if @date >= from_time && @date < to_time
Time::Span.new(hours: @hours, minutes: 0, seconds: 0)
else
Time::Span.zero
end
end
end
@ -61,32 +61,28 @@ module Dayoff
!@finish.nil?
end
def finished_to_time?(time : Time) : Bool
@finish && @finish <= time
end
def calc_span(time : Time) : Time::Span
if @finish.nil?
if @start <= time
time - @start
else
Time::Span.zero
end
def in_range(from_time : Time, to_time : Time) : Time::Span
if finished?
in_range_finished from_time, to_time
else
if time > finish!
finish! - @start
elsif time > @start
time - @start
else
Time::Span.zero
end
in_range_not_finished from_time, to_time
end
end
def on_date(d : Time) : Time::Span
if @start.date == d.date
fin = @finish || d
fin - start
private def in_range_finished(from_time : Time, to_time : Time) : Time::Span
if @start <= to_time && finish! >= from_time
normalized_start = Math.max(@start, from_time)
normalized_finish = Math.min(finish!, to_time)
normalized_finish - normalized_start
else
Time::Span.zero
end
end
private def in_range_not_finished(from_time : Time, to_time : Time) : Time::Span
if @start <= to_time
normalized_start = Math.max(@start, from_time)
to_time - normalized_start
else
Time::Span.zero
end

View File

@ -21,21 +21,8 @@ module Dayoff
@wrecords = @storage.get_work_records
end
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 <= check_date
acc + wd.time_span
else
acc
end
end
end
def get_work_hours(on_time : Time) : Time::Span
@wrecords.reduce(Time::Span.zero) do |acc, wr|
acc + wr.calc_span on_time
end
private def started_point
@wrecords.find { |x| x.started? }
end
def started? : Bool
@ -65,36 +52,33 @@ module Dayoff
@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
def date_status(d : Time) : Time::Span
planned = get_planned_hours_on_date d
worked = get_work_hours_on_date d
planned - worked
end
private def get_planned_hours_on_date(d : Time) : Time::Span
@pdates.reduce(Time::Span.zero) do |acc, wd|
if wd.same_date? d
acc + wd.time_span
else
acc
end
def get_planned(from_time : Time, to_time : Time) : Time::Span
@pdates.reduce(Time::Span.zero) do |acc, pd|
acc + pd.in_range(from_time, to_time)
end
end
private def get_work_hours_on_date(d : Time) : Time::Span
def get_worked(from_time : Time, to_time : Time) : Time::Span
@wrecords.reduce(Time::Span.zero) do |acc, wr|
acc + wr.on_date d
acc + wr.in_range(from_time, to_time)
end
end
private def started_point
@wrecords.find { |x| x.started? }
private def zero_time : Time
location = Time::Location.load("Europe/Moscow")
Time.local(1, 1, 1, 0, 0, location: location)
end
def remaining_time(on_time : Time) : Time::Span
planned = get_planned zero_time, on_time
worked = get_worked zero_time, on_time
planned - worked
end
def date_status(date : Time) : Time::Span
planned = get_planned date.at_beginning_of_day, date.at_end_of_day
worked = get_worked date.at_beginning_of_day, date
planned - worked
end
end
end