Sha256: 4cebc77ef4d32375a03d98d3259552cdbca9d0528a4c253e9074f47aa08f8add

Contents?: true

Size: 1.75 KB

Versions: 1

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require 'time'

module Toggl
  module Worktime
    # Time-entries merger
    class Merger
      attr_reader :total_time

      ONE_DAY_MINUTES = 24 * 60

      def initialize(time_entries, config)
        @time_entries = time_entries
        @config = config
        @current_start = nil
        @current_stop = nil
        @continuing = true
        @last_stop = nil
        @total_time = 0
      end

      def merge
        work_time = []
        time_entries_each do |start, stop|
          if continuing(start)
            @current_stop = stop
            next
          end
          work_time << [@current_start, @current_stop]
          @current_start = start
          @current_stop = stop
        end
        work_time << [@current_start, @last_stop]
        work_time
      end

      def time_entries_each
        zone_offset = Toggl::Worktime::Time.zone_offset(@config.timezone)
        @time_entries.each do |te|
          start = parse_date(te['start'], zone_offset)
          stop = parse_date(te['stop'], zone_offset)
          @last_stop = stop
          @current_start = start if @current_start.nil?
          @current_stop = stop if @current_stop.nil?
          if start.nil? || stop.nil?
            warn 'start or stop time is nil: total time may be incomplete'
          else
            @total_time += stop - start
          end
          yield [start, stop]
        end
      end

      def parse_date(date, zone_offset)
        return nil if date.nil?
        ::Time.parse(date).getlocal(zone_offset)
      end

      def continuing(start)
        return true if @current_stop.nil?
        interval = (start - @current_stop) * ONE_DAY_MINUTES
        @continuing = interval < @config.working_interval_min
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
toggl-worktime-0.3.0 lib/toggl/worktime/merger.rb