Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pkg
*.swp
Gemfile.lock
.bundle
vendor/bundle
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'http://rubygems.org'
source 'https://rubygems.org'

gem 'timetrap'
gem 'timetrap-harvest'
Expand Down
24 changes: 20 additions & 4 deletions formatters/day.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) && [email protected]?(entry.sheet)
if is_target_date(entry[:start], entry.end_or_now, target_date) && [email protected]?(entry.sheet)
todays_duration += entry.duration
todays_entries << entry
end
Expand All @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions spec/timetrap_formatters_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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