class PunchTime::TimeRecord

Constants

DAY_HOURS
HOUR_MINUTES
MINUTE_SECONDS
Scale

Attributes

configuration[R]
end_time[RW]
start_time[RW]

Public Class Methods

new(&config) click to toggle source

@param [Hash] config

# File lib/punch_time/time_record.rb, line 28
def initialize(&config)
  @configuration = Configuration.new(&config)
end

Public Instance Methods

night_overtime_work?() click to toggle source

@return [Boolean]

# File lib/punch_time/time_record.rb, line 105
def night_overtime_work?
  night_start_time = @configuration.night[:start_time].strftime('%H:%M:%S')
  end_time >
    DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{night_start_time}")
            .change(offset: @configuration.offset)
end
overtime_work?() click to toggle source

@return [Boolean]

# File lib/punch_time/time_record.rb, line 98
def overtime_work?
  shift_out_time = @configuration.shift_out_time.strftime('%H:%M:%S')
  end_time > DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_out_time}")
                     .change(offset: @configuration.offset)
end
punch(start_time, end_time) click to toggle source

@param [DateTime] start_time @param [DateTime] end_time

# File lib/punch_time/time_record.rb, line 34
def punch(start_time, end_time)
  self.start_time = start_time
  self.end_time = end_time
  raise ArgumentError, errors.messages unless valid?
end
sum_night_work() click to toggle source

@return [Scale]

# File lib/punch_time/time_record.rb, line 41
def sum_night_work
  night_works = []
  night_start_time = @configuration.night[:start_time].strftime('%H:%M:%S')
  night_end_time = @configuration.night[:end_time].strftime('%H:%M:%S')
  start_time.to_date.upto(end_time.to_date) do |x|
    night_start = DateTime.parse("#{x.strftime('%Y-%m-%d')} #{night_start_time}")
                          .change(offset: @configuration.offset)
    night_end = DateTime.parse("#{x.next_day.strftime('%Y-%m-%d')} #{night_end_time}")
                        .change(offset: @configuration.offset)
    if end_time > night_end
      night_works.append(night_end - night_start)
    elsif (end_time - night_start).positive?
      night_works.append(end_time - night_start)
    end
  end
  night_work = night_works.inject(:+)
  convert_humanize(night_work)
end
sum_over_work() click to toggle source

@return [Scale]

# File lib/punch_time/time_record.rb, line 61
def sum_over_work
  shift_out_time = @configuration.shift_out_time.strftime('%H:%M:%S')
  over_work = end_time -
              DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_out_time}")
                      .change(offset: @configuration.offset) -
              sum_night_work.rational

  convert_humanize(over_work)
end
sum_tardy() click to toggle source

@return [Scale]

# File lib/punch_time/time_record.rb, line 81
def sum_tardy
  shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
  tardy = start_time -
          DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
                  .change(offset: @configuration.offset)
  convert_humanize(tardy)
end
sum_work() click to toggle source

@return [Scale]

# File lib/punch_time/time_record.rb, line 72
def sum_work
  shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
  shift_in = DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
                     .change(offset: @configuration.offset)
  work = end_time - shift_in - sum_break
  convert_humanize(work)
end
tardy?() click to toggle source

@return [Boolean]

# File lib/punch_time/time_record.rb, line 90
def tardy?
  shift_in_time = @configuration.shift_in_time.strftime('%H:%M:%S')
  start_time >
    DateTime.parse("#{start_time.strftime('%Y-%m-%d')} #{shift_in_time}")
            .change(offset: @configuration.offset)
end

Private Instance Methods

convert_humanize(value) click to toggle source

@return [Struct]

# File lib/punch_time/time_record.rb, line 144
def convert_humanize(value)
  value ||= Rational(0)
  value = Rational(0) if value.negative?
  Scale.new(value,
            value.to_i,
            (value * DAY_HOURS).to_i,
            (value * DAY_HOURS * HOUR_MINUTES).to_i,
            (value * DAY_HOURS * HOUR_MINUTES * MINUTE_SECONDS).to_i)
end
no_format() click to toggle source
# File lib/punch_time/time_record.rb, line 138
def no_format
  errors.add(:start_time, 'Allow only DateTime format') unless start_time.is_a?(DateTime)
  errors.add(:end_time, 'Allow only DateTime format') unless end_time.is_a?(DateTime)
end
sum_break() click to toggle source

@return [Rational]

# File lib/punch_time/time_record.rb, line 119
def sum_break
  start_day = start_time.strftime('%Y-%m-%d')
  breaks = @configuration
           .breaks
           .select do |x|
             end_time >
               DateTime.parse("#{start_day} #{x[:end_time].strftime('%H:%M:%S')}")
                       .change(offset: @configuration.offset)
           end.select do |x|
    DateTime.parse("#{start_day} #{x[:start_time].strftime('%H:%M:%S')}")
            .change(offset: @configuration.offset) >
      start_time
  end
  return Rational(0) if breaks.blank?

  breaks.map { |x| to_datetime(x[:end_time]) - to_datetime(x[:start_time]) }
        .inject { :+ }
end
to_datetime(value) click to toggle source

@see stackoverflow.com/questions/279769/convert-to-from-datetime-and-time-in-ruby

# File lib/punch_time/time_record.rb, line 155
def to_datetime(value)
  # Convert seconds + microseconds into a fractional number of seconds
  seconds = value.sec + Rational(value.usec, 10**6)

  # Convert a UTC offset measured in minutes to one measured in a
  # fraction of a day.
  offset = Rational(value.utc_offset, 60 * 60 * 24)
  DateTime.new(value.year, value.month, value.day, value.hour, value.min, seconds, offset)
end