Skip to content

Commit a781479

Browse files
committed
Do not allow adding open intervals with a future time
If an interval is started at a future time, as was possible with the continue command when given a future time, subsequent commands could fail if the end data was set to the current time. Which would result in the following message: Range.cpp:359: time_t Range::total() const: Assertion `is_open () || end >= start' failed. This change makes it invalid to start an open interval in the future, regardless of what command is validating the in the interval. However it might still be possible to get in this case if the time on the system jumps backward after starting an interval. Reported by @kbcb Related to #350 Closes #364 Signed-off-by: Shaun Ruffell <sruffell@sruffell.net>
1 parent 02491f8 commit a781479

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/commands/CmdStart.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@ int CmdStart (
4040

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

43-
if (interval.start > now)
44-
{
45-
throw std::string ("Time tracking cannot be set in the future.");
46-
}
47-
else if (!interval.is_started () || interval.is_ended ())
43+
if (!interval.is_started () || interval.is_ended ())
4844
{
4945
throw std::string ("The start command does not accept ranges but only a single datetime. "
5046
"Perhaps you want the track command?.");

src/validate.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,12 @@ bool validate (
211211
autoFill (rules, database, interval);
212212
}
213213

214-
return autoAdjust (findHint (cli, ":adjust"), rules, database, interval);
214+
bool should_add = autoAdjust (findHint (cli, ":adjust"), rules, database, interval);
215+
if (should_add && (interval.is_open () && interval.start > Datetime ()))
216+
{
217+
throw std::string ("Time tracking cannot be set in the future.");
218+
}
219+
return should_add;
215220
}
216221

217222
////////////////////////////////////////////////////////////////////////////////

test/continue.t

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,23 @@ class TestContinue(TestCase):
502502
expectedTags=["FOO"],
503503
description="added interval")
504504

505+
def test_continue_with_future_time(self):
506+
"""Verify that continue fails with time in the future"""
507+
now_utc = datetime.now().utcnow()
508+
509+
one_hour_before_utc = now_utc - timedelta(hours=1)
510+
two_hours_before_utc = now_utc - timedelta(hours=2)
511+
512+
code, out, err = self.t("start FOO {:%Y-%m-%dT%H}:00:00Z".format(two_hours_before_utc))
513+
self.assertIn("Tracking FOO\n", out)
514+
code, out, err = self.t("start BAR {:%Y-%m-%dT%H}:00:00Z".format(one_hour_before_utc))
515+
self.assertIn("Tracking BAR\n", out)
516+
code, out, err = self.t("stop")
517+
self.assertIn("Recorded BAR\n", out)
518+
519+
code, out, err = self.t.runError("continue @2 from {:%Y-%m-%dT%H:%M:%S}".format (now_utc + timedelta(seconds=10)))
520+
self.assertIn("Time tracking cannot be set in the future", err)
521+
505522

506523
if __name__ == "__main__":
507524
from simpletap import TAPTestRunner

0 commit comments

Comments
 (0)