Skip to content
This repository
Browse code

Fix two issues with month command

The month command didn't previously work in December.  It didn't handle
the logic to understand that January (month 1) follows December (month
12)

Also it's not necessary to call Time#to_date in the month command.  This
method is private in ruby 1.8.6.  Removing this call provides some
additional level of compatibility running under 1.8.6.
  • Loading branch information...
commit 9b744a4366a9f3196e5f5700db45af0b32d1531f 1 parent a3b0bf1
authored December 05, 2012
9  lib/timetrap/cli.rb
@@ -375,9 +375,14 @@ def week
375 375
     end
376 376
 
377 377
     def month
378  
-      d = Chronic.parse( "last #{args['-s'] || Date.today.strftime("%B")}" ).to_date
  378
+      d = Chronic.parse( "last #{args['-s'] || Date.today.strftime("%B")}" )
  379
+
379 380
       beginning_of_month = Date.new( d.year, d.month )
380  
-      end_of_month = Date.new( d.year, d.month+1 ) - 1
  381
+      end_of_month = if d.month == 12 # handle edgecase
  382
+        Date.new( d.year + 1, 1) - 1
  383
+      else
  384
+        Date.new( d.year, d.month+1 ) - 1
  385
+      end
381 386
       args['-s'] = beginning_of_month.to_s
382 387
       args['-e'] = end_of_month.to_s
383 388
       display
36  spec/timetrap_spec.rb
@@ -599,6 +599,42 @@ def output
599 599
         end
600 600
       end
601 601
 
  602
+      describe "month" do
  603
+        it "should display all entries for the month" do
  604
+          create_entry(
  605
+            :start => Time.new(2012, 2, 5, 1, 2, 3),
  606
+            :end => Time.new(2012, 2, 5, 2, 2, 3),
  607
+          )
  608
+          create_entry(
  609
+            :start => Time.new(2012, 2, 6, 1, 2, 3),
  610
+            :end => Time.new(2012, 2, 6, 2, 2, 3),
  611
+          )
  612
+          create_entry(
  613
+            :start => Time.new(2012, 1, 5, 1, 2, 3),
  614
+            :end => Time.new(2012, 1, 5, 2, 2, 3),
  615
+          )
  616
+
  617
+          Date.should_receive(:today).and_return(Date.new(2012, 2, 5))
  618
+          invoke "month"
  619
+
  620
+          $stdout.string.should include 'Feb 05, 2012'
  621
+          $stdout.string.should include 'Feb 06, 2012'
  622
+          $stdout.string.should_not include 'Jan'
  623
+        end
  624
+
  625
+        it "should work in December" do
  626
+          create_entry(
  627
+            :start => Time.new(2012, 12, 5, 1, 2, 3),
  628
+            :end => Time.new(2012, 12, 5, 2, 2, 3),
  629
+          )
  630
+
  631
+          Date.should_receive(:today).and_return(Date.new(2012, 12, 5))
  632
+          invoke "month"
  633
+
  634
+          $stdout.string.should include 'Wed Dec 05, 2012   01:02:03 - 02:02:03'
  635
+        end
  636
+      end
  637
+
602 638
       describe "kill" do
603 639
         it "should give me a chance not to fuck up" do
604 640
           entry = create_entry

0 notes on commit 9b744a4

Please sign in to comment.
Something went wrong with that request. Please try again.