Rewrite diff methods
This commit is contained in:
parent
d6b1d76ed4
commit
af95c67110
@ -3,10 +3,43 @@ require "./spec_helper"
|
|||||||
module Dayoff::Test
|
module Dayoff::Test
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
describe "can check same date" do
|
it "planned can calc in range" do
|
||||||
planned_date = PlannedDate.new(t(1, 12), 8)
|
planned_date = PlannedDate.new(t(1, 12), 8)
|
||||||
date = t(1, 20)
|
t1 = t(1, 0)
|
||||||
res = planned_date.same_date? date
|
t2 = t(2, 0)
|
||||||
res.should be_true
|
res = planned_date.in_range t1, t2
|
||||||
|
res.total_hours.should eq 8
|
||||||
|
end
|
||||||
|
|
||||||
|
it "planned can calc in range 2" do
|
||||||
|
planned_date = PlannedDate.new(t(3, 0), 8)
|
||||||
|
t1 = t(1, 0)
|
||||||
|
t2 = t(2, 0)
|
||||||
|
res = planned_date.in_range t1, t2
|
||||||
|
res.total_hours.should eq 0
|
||||||
|
end
|
||||||
|
|
||||||
|
it "planned can calc in range 2" do
|
||||||
|
planned_date = PlannedDate.new(d(1), 8)
|
||||||
|
t1 = t(1, 0).at_beginning_of_day
|
||||||
|
t2 = t(1, 0).at_end_of_day
|
||||||
|
res = planned_date.in_range t1, t2
|
||||||
|
res.total_hours.should eq 8
|
||||||
|
end
|
||||||
|
|
||||||
|
it "worked can calc in range" do
|
||||||
|
work_record = WorkRecord.new(t(1, 10), t(1, 20))
|
||||||
|
t1 = t(1, 15).at_beginning_of_day
|
||||||
|
t2 = t(1, 15)
|
||||||
|
res = work_record.in_range t1, t2
|
||||||
|
res.total_hours.should eq 5
|
||||||
|
end
|
||||||
|
|
||||||
|
it "worked can calc in range 2" do
|
||||||
|
work_record = WorkRecord.new(t(1, 10), t(1, 20))
|
||||||
|
t1 = t(2, 15).at_beginning_of_day
|
||||||
|
t2 = t(2, 15)
|
||||||
|
res = work_record.in_range t1, t2
|
||||||
|
res.total_hours.should eq 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -18,15 +18,6 @@ module Dayoff::Test
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe Profile do
|
describe Profile do
|
||||||
it "can calc work hours" do
|
|
||||||
storage = MemoryStorage.new
|
|
||||||
storage.set_work_records [
|
|
||||||
WorkRecord.new(t(1, 10), t(1, 20)),
|
|
||||||
]
|
|
||||||
prof = Profile.new(storage)
|
|
||||||
prof.get_work_hours(t(2, 0)).total_hours.should eq 10
|
|
||||||
end
|
|
||||||
|
|
||||||
it "can write new record" do
|
it "can write new record" do
|
||||||
storage = MemoryStorage.new
|
storage = MemoryStorage.new
|
||||||
storage.set_work_records [
|
storage.set_work_records [
|
||||||
@ -54,27 +45,6 @@ module Dayoff::Test
|
|||||||
records.last.finish.should eq finish_time
|
records.last.finish.should eq finish_time
|
||||||
end
|
end
|
||||||
|
|
||||||
it "can calc planned hours" do
|
|
||||||
prof = create_profile
|
|
||||||
span = prof.get_planned_hours t(3, 12)
|
|
||||||
expected = 8 * 3
|
|
||||||
expected.should eq span.total_hours
|
|
||||||
end
|
|
||||||
|
|
||||||
it "can calc work hours" do
|
|
||||||
prof = create_profile
|
|
||||||
span = prof.get_work_hours t(3, 12)
|
|
||||||
expected = 10 * 2
|
|
||||||
expected.should eq span.total_hours
|
|
||||||
end
|
|
||||||
|
|
||||||
it "can calc remaining time" do
|
|
||||||
prof = create_profile
|
|
||||||
span = prof.remaining_time t(3, 12)
|
|
||||||
expected = 8 * 3 - 10 * 2
|
|
||||||
expected.should eq span.total_hours
|
|
||||||
end
|
|
||||||
|
|
||||||
it "not start twice" do
|
it "not start twice" do
|
||||||
prof = create_profile
|
prof = create_profile
|
||||||
start_time = t(3, 10, 0)
|
start_time = t(3, 10, 0)
|
||||||
@ -95,10 +65,17 @@ module Dayoff::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can calc remaining time" do
|
||||||
|
prof = create_profile
|
||||||
|
span = prof.remaining_time t(3, 12)
|
||||||
|
expected = 8 * 3 - 10 * 2
|
||||||
|
expected.should eq span.total_hours
|
||||||
|
end
|
||||||
|
|
||||||
it "can calc diff on concrete date" do
|
it "can calc diff on concrete date" do
|
||||||
prof = create_profile
|
prof = create_profile
|
||||||
span = prof.date_status d(1)
|
span = prof.date_status t(1, 15)
|
||||||
span.total_hours.should eq -2
|
span.total_hours.should eq 3
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -23,12 +23,12 @@ module Dayoff
|
|||||||
def initialize(@date : Time, @hours : Int32)
|
def initialize(@date : Time, @hours : Int32)
|
||||||
end
|
end
|
||||||
|
|
||||||
def time_span : Time::Span
|
def in_range(from_time : Time, to_time : Time) : Time::Span
|
||||||
Time::Span.new(hours: hours, minutes: 0, seconds: 0)
|
if @date >= from_time && @date < to_time
|
||||||
end
|
Time::Span.new(hours: @hours, minutes: 0, seconds: 0)
|
||||||
|
else
|
||||||
def same_date?(d : Time) : Bool
|
Time::Span.zero
|
||||||
@date.date == d.date
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -61,32 +61,28 @@ module Dayoff
|
|||||||
!@finish.nil?
|
!@finish.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
def finished_to_time?(time : Time) : Bool
|
def in_range(from_time : Time, to_time : Time) : Time::Span
|
||||||
@finish && @finish <= time
|
if finished?
|
||||||
end
|
in_range_finished from_time, to_time
|
||||||
|
|
||||||
def calc_span(time : Time) : Time::Span
|
|
||||||
if @finish.nil?
|
|
||||||
if @start <= time
|
|
||||||
time - @start
|
|
||||||
else
|
|
||||||
Time::Span.zero
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
if time > finish!
|
in_range_not_finished from_time, to_time
|
||||||
finish! - @start
|
|
||||||
elsif time > @start
|
|
||||||
time - @start
|
|
||||||
else
|
|
||||||
Time::Span.zero
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_date(d : Time) : Time::Span
|
private def in_range_finished(from_time : Time, to_time : Time) : Time::Span
|
||||||
if @start.date == d.date
|
if @start <= to_time && finish! >= from_time
|
||||||
fin = @finish || d
|
normalized_start = Math.max(@start, from_time)
|
||||||
fin - start
|
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
|
else
|
||||||
Time::Span.zero
|
Time::Span.zero
|
||||||
end
|
end
|
||||||
|
@ -21,21 +21,8 @@ module Dayoff
|
|||||||
@wrecords = @storage.get_work_records
|
@wrecords = @storage.get_work_records
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_planned_hours(on_time : Time) : Time::Span
|
private def started_point
|
||||||
check_date = on_time.at_beginning_of_day
|
@wrecords.find { |x| x.started? }
|
||||||
@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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def started? : Bool
|
def started? : Bool
|
||||||
@ -65,36 +52,33 @@ module Dayoff
|
|||||||
@storage.set_work_records @wrecords
|
@storage.set_work_records @wrecords
|
||||||
end
|
end
|
||||||
|
|
||||||
def remaining_time(on_time : Time) : Time::Span
|
def get_planned(from_time : Time, to_time : Time) : Time::Span
|
||||||
planned = get_planned_hours on_time
|
@pdates.reduce(Time::Span.zero) do |acc, pd|
|
||||||
worked = get_work_hours on_time
|
acc + pd.in_range(from_time, to_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
|
|
||||||
end
|
end
|
||||||
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|
|
@wrecords.reduce(Time::Span.zero) do |acc, wr|
|
||||||
acc + wr.on_date d
|
acc + wr.in_range(from_time, to_time)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def started_point
|
private def zero_time : Time
|
||||||
@wrecords.find { |x| x.started? }
|
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
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user