diff --git a/.gitignore b/.gitignore index 11e1cde..b9fe64f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ pkg *.swp Gemfile.lock .bundle +vendor/bundle diff --git a/Gemfile b/Gemfile index 3356734..a456694 100644 --- a/Gemfile +++ b/Gemfile @@ -1,4 +1,4 @@ -source 'http://rubygems.org' +source 'https://rubygems.org' gem 'timetrap' gem 'timetrap-harvest' diff --git a/formatters/day.rb b/formatters/day.rb index 30908ce..c3da385 100644 --- a/formatters/day.rb +++ b/formatters/day.rb @@ -15,8 +15,12 @@ def output output = '' todays_duration = 0.0 todays_entries = [] + + # Determine the target date from entries or use today + target_date = determine_target_date + @entries.each do |entry| - if is_today(entry[:start], entry.end_or_now) && !@skip.include?(entry.sheet) + if is_target_date(entry[:start], entry.end_or_now, target_date) && !@skip.include?(entry.sheet) todays_duration += entry.duration todays_entries << entry end @@ -28,10 +32,22 @@ def output return output end - def is_today(start_time, end_time) - return (Date.parse(start_time.strftime(DATE_FORMAT))..Date.parse(end_time.strftime(DATE_FORMAT))) === Date.today + def determine_target_date + # If entries exist, use the date of the last entry, otherwise use today + # This allows the formatter to work with filtered date ranges + if @entries.empty? + Date.today + else + # Use the latest date among the entries + @entries.map { |e| Date.parse(e[:start].strftime(DATE_FORMAT)) }.max || Date.today + end + end + private :determine_target_date + + def is_target_date(start_time, end_time, target_date) + return (Date.parse(start_time.strftime(DATE_FORMAT))..Date.parse(end_time.strftime(DATE_FORMAT))) === target_date end - private :is_today + private :is_target_date def hours_to_seconds(hour_amount) return (hour_amount * 60.0 * 60.0) diff --git a/spec/timetrap_formatters_spec.rb b/spec/timetrap_formatters_spec.rb index c5b3bec..e43c346 100644 --- a/spec/timetrap_formatters_spec.rb +++ b/spec/timetrap_formatters_spec.rb @@ -1,4 +1,12 @@ TEST_MODE = true # This tells timetrap not to use the real database. + +# Monkey patch for Ruby 3.2 compatibility with old RSpec version +class File + class << self + alias_method :exists?, :exist? unless method_defined?(:exists?) + end +end + require 'rubygems' require 'bundler' Bundler.require @@ -104,4 +112,45 @@ def invoke command OUTPUT end end + + describe "day" do + before do + # Configure the day formatter settings + Timetrap::Config.stub(:[]).with('day_length_hours').and_return(8.0) + Timetrap::Config.stub(:[]).with('progress_width').and_return(40.0) + Timetrap::Config.stub(:[]).with('day_exclude_sheets').and_return([]) + Timetrap::Config.stub(:[]).with('day_countdown').and_return(false) + Timetrap::Config.stub(:[]).with('formatter_search_paths').and_return( + [File.expand_path(File.join(File.dirname(__FILE__), '..', 'formatters'))] + ) + + # Create entries for a past date (2013-07-29) + Timetrap::Entry.create( :sheet => 'work', + :note => 'past entry', :start => '2013-07-29 10:00:00', :end => '2013-07-29 14:00:00' + ) + end + + it "should correctly display entries for past dates" do + Timetrap::Timer.current_sheet = 'work' + invoke 'display --start "2013-07-29" --end "2013-07-29" --format day' + # Should show 4 hours of 8 hours = 50% + output = $stdout.string + output.should include('50%') + output.should include('4:00:00') + end + + it "should correctly display entries for a different date" do + # Create entries for a specific date (not necessarily today) + Timetrap::Entry.create( :sheet => 'work', + :note => 'entry for specific date', :start => '2013-07-30 09:00:00', :end => '2013-07-30 11:00:00' + ) + + Timetrap::Timer.current_sheet = 'work' + invoke "display --start \"2013-07-30\" --format day" + # Should show 2 hours of 8 hours = 25% + output = $stdout.string + output.should include('25%') + output.should include('2:00:00') + end + end end