Commits

James Hartzell  committed 5bd5544

t i accepts --at, --out, maintains consistency

  • Participants
  • Parent commits 367ecf4

Comments (0)

Files changed (3)

File timebook/cmdutil.py

         if patt.match(dt_str):
             res = time.strptime(prepend + dt_str + postpend, fmt)
             return int(time.mktime(res))
+    raise ValueError, "%s is not in a valid time format"%dt_str
+
+def parse_date_time_or_now(dt_str):
+    if dt_str:
+        return parse_date_time(dt_str)
+    else:
+        return int(time.time())
+
 
 def timedelta_hms_display(timedelta):
     hours = timedelta.days * 24 + timedelta.seconds / 3600

File timebook/commands.py

     parser.add_option('-s', '--switch', dest='switch', type='string',
                       help='Switch to another timesheet before \
 starting the timer.')
+    parser.add_option('-o', '--out', dest='out', action='store_true',
+                      default=False, help='''Clocks out before clocking \
+in''')
+    parser.add_option('-a', '--at', dest='at', type='string',
+                      help='''Set time of clock-in''')
     opts, args = parser.parse_args(args=args)
-    now = int(time.time())
     if opts.switch:
         sheet = opts.switch
         switch(db, [sheet])
     else:
         sheet = dbutil.get_current_sheet(db)
+    if opts.out:
+        clock_out(db)
+    timestamp = cmdutil.parse_date_time_or_now(opts.at)
     running = dbutil.get_active_info(db, sheet)
     if running is not None:
         raise SystemExit, 'error: timesheet already active'
+    most_recent_clockout = dbutil.get_most_recent_clockout(db, sheet)
+    if most_recent_clockout and timestamp < most_recent_clockout:
+        print most_recent_clockout
+        print timestamp
+        raise SystemExit, 'error: time periods could end up overlapping'
     description = u' '.join(args) or None
     db.execute(u'''
     insert into entry (
         sheet, start_time, description, extra
     ) values (?,?,?,?)
-    ''', (sheet, now, description, extra))
+    ''', (sheet, timestamp, description, extra))
 
 @command('delete a timesheet', aliases=('delete',))
 def kill(db, args):
     parser.add_option('-a', '--at', dest='at',
                       help='Set time of clock-out')
     opts, args = parser.parse_args(args=args)
-    if not opts.at:
-        timestamp = int(time.time())
-    else:
-        timestamp = cmdutil.parse_date_time(opts.at)
+    clock_out(db, opts.at, opts.verbose)
+
+def clock_out(db, at=None, verbose=False):
+    timestamp = cmdutil.parse_date_time_or_now(at)
     active = dbutil.get_current_start_time(db)
     if active is None:
         raise SystemExit, 'error: timesheet not active'
     active_id, start_time = active
     active_time = timestamp - start_time
-    if opts.verbose:
+    if verbose:
         print timedelta(seconds=active_time)
     if active_time < 0:
         raise SystemExit, "Error: Negative active time"

File timebook/dbutil.py

         sheet = m.value;
     ''')
     return db.fetchone()[0]
+
+def get_most_recent_clockout(db, sheet):
+    db.execute(u'''
+    select
+        end_time
+    from
+        entry
+    where
+        sheet = ?
+    order by
+        -end_time
+    ''', (sheet,))
+    row = db.fetchone()
+    if not row:
+        # we've never clocked out on this timesheet
+        return None
+    else:
+        return row[0]