Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 1 addition & 5 deletions src/commands/CmdStart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ int CmdStart (

auto interval = cli.getFilter ({now, 0});

if (interval.start > now)
{
throw std::string ("Time tracking cannot be set in the future.");
}
else if (!interval.is_started () || interval.is_ended ())
if (!interval.is_started () || interval.is_ended ())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of modifying CmdStart we should fix this by aligning CmdContinue and adding the same check for future times:

git diff src/commands/CmdContinue.cpp
diff --git a/src/commands/CmdContinue.cpp b/src/commands/CmdContinue.cpp
index 6ad679e2..62242c4d 100644
--- a/src/commands/CmdContinue.cpp
+++ b/src/commands/CmdContinue.cpp
@@ -38,10 +38,16 @@ int CmdContinue (
   Journal& journal)
 {
   const bool verbose = rules.getBoolean ("verbose");
+  const Datetime now {};
+
+  auto filter = cli.getFilter ({ now, 0 });
+
+  if (filter.start > now)
+  {
+    throw std::string ("Time tracking cannot be set in the future.");
+  }
 
-  // Gather IDs and TAGs.
   std::set <int> ids = cli.getIds ();
-  auto filter = cli.getFilter ();
 
   if (!ids.empty () && !filter.tags().empty ())
   {

{
throw std::string ("The start command does not accept ranges but only a single datetime. "
"Perhaps you want the track command?.");
Expand Down
7 changes: 6 additions & 1 deletion src/validate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ bool validate (
autoFill (rules, database, interval);
}

return autoAdjust (findHint (cli, ":adjust"), rules, database, interval);
bool should_add = autoAdjust (findHint (cli, ":adjust"), rules, database, interval);
if (should_add && (interval.is_open () && interval.start > Datetime ()))
{
throw std::string ("Time tracking cannot be set in the future.");
}
return should_add;
}

////////////////////////////////////////////////////////////////////////////////
17 changes: 17 additions & 0 deletions test/continue.t
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,23 @@ class TestContinue(TestCase):
expectedTags=["FOO"],
description="added interval")

def test_continue_with_future_time(self):
"""Verify that continue fails with time in the future"""
now_utc = datetime.now().utcnow()

one_hour_before_utc = now_utc - timedelta(hours=1)
two_hours_before_utc = now_utc - timedelta(hours=2)

code, out, err = self.t("start FOO {:%Y-%m-%dT%H}:00:00Z".format(two_hours_before_utc))
self.assertIn("Tracking FOO\n", out)
code, out, err = self.t("start BAR {:%Y-%m-%dT%H}:00:00Z".format(one_hour_before_utc))
self.assertIn("Tracking BAR\n", out)
code, out, err = self.t("stop")
self.assertIn("Recorded BAR\n", out)

code, out, err = self.t.runError("continue @2 from {:%Y-%m-%dT%H:%M:%S}".format (now_utc + timedelta(seconds=10)))
self.assertIn("Time tracking cannot be set in the future", err)


if __name__ == "__main__":
from simpletap import TAPTestRunner
Expand Down