Skip to content

Commit

Permalink
Merge 120a3ad into e154f50
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsnyder committed Mar 3, 2021
2 parents e154f50 + 120a3ad commit 75c0f5c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Changelog

- Drop Python 2.7 and 3.5 support.

- Add support for positive time offset syntax in entries.


0.11.3 (2019-04-23)
~~~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 4 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ Back-dating Entries

If you forget to enter an activity, you can enter it after the fact by
prefixing it with a full time ("09:30 morning meeting") or a two digit minute-offset
("-10 morning meeting"). Note that the new activity must still be after
the last entered event, or things will become confusing!
("-10 morning meeting" or "+10 morning meeting"). Where "-" offsets from the
the current time and "+" offsets from the last entry.
Note that the new activity must still be after the last entered event and before the
current time, or things will become confusing!


Tasks pane
Expand Down
1 change: 1 addition & 0 deletions src/gtimelog/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ In alphabetic order:
- Jean Jordaan
- Jeroen Langeveld
- Jonatan Cloutier
- Jonathan Snyder
- Kees Cook
- Lars Wirzenius
- Laurynas Speičys
Expand Down
33 changes: 30 additions & 3 deletions src/gtimelog/tests/test_timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,24 +1222,51 @@ def test_parse_correction_ignores_absolute_times_before_last_entry(self):
("15:20 did stuff", None))

@freezegun.freeze_time("2015-05-12 16:27")
def test_parse_correction_recognizes_relative_times(self):
def test_parse_correction_recognizes_negative_relative_times(self):
timelog = TimeLog(StringIO(), datetime.time(2, 0))
self.assertEqual(timelog.parse_correction("-20 did stuff"),
("did stuff", datetime.datetime(2015, 5, 12, 16, 7)))

@freezegun.freeze_time("2015-05-12 16:27")
def test_parse_correction_ignores_relative_times_before_last_entry(self):
def test_parse_correction_recognizes_positive_relative_times(self):
timelog = TimeLog(StringIO("2015-05-12 15:50: stuff"),
datetime.time(2, 0))
self.assertEqual(timelog.parse_correction("+20 did stuff"),
("did stuff", datetime.datetime(2015, 5, 12, 16, 10)))

@freezegun.freeze_time("2015-05-12 16:27")
def test_parse_correction_ignores_positive_relative_times_without_initial_entry(self):
timelog = TimeLog(StringIO(), datetime.time(2, 0))
self.assertEqual(timelog.parse_correction("+20 did stuff"),
("+20 did stuff", None))

@freezegun.freeze_time("2015-05-12 16:27")
def test_parse_correction_ignores_negative_relative_times_before_last_entry(self):
timelog = TimeLog(StringIO("2015-05-12 16:00: stuff"),
datetime.time(2, 0))
self.assertEqual(timelog.parse_correction("-30 did stuff"),
("-30 did stuff", None))

@freezegun.freeze_time("2015-05-12 16:27")
def test_parse_correction_ignores_bad_relative_times(self):
def test_parse_correction_ignores_positive_relative_times_in_the_future(self):
timelog = TimeLog(StringIO("2015-05-12 15:50: stuff"),
datetime.time(2, 0))
self.assertEqual(timelog.parse_correction("+40 did stuff"),
("+40 did stuff", None))

@freezegun.freeze_time("2015-05-12 16:27")
def test_parse_correction_ignores_bad_negative_relative_times(self):
timelog = TimeLog(StringIO(), datetime.time(2, 0))
self.assertEqual(timelog.parse_correction("-200 did stuff"),
("-200 did stuff", None))

@freezegun.freeze_time("2015-05-12 16:27")
def test_parse_correction_ignores_bad_positive_relative_times(self):
timelog = TimeLog(StringIO("2015-05-12 15:50: stuff"),
datetime.time(2, 0))
self.assertEqual(timelog.parse_correction("+200 did stuff"),
("+200 did stuff", None))


class TestTotals(unittest.TestCase):

Expand Down
19 changes: 13 additions & 6 deletions src/gtimelog/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ def parse_correction(self, entry):
"""
now = None
date_match = re.match(r'(\d\d):(\d\d)\s+', entry)
delta_match = re.match(r'-([1-9]\d?|1\d\d)\s+', entry)
delta_match = re.match(r'[\-+]([1-9]\d?|1\d\d)\s+', entry)
if date_match:
h = int(date_match.group(1))
m = int(date_match.group(2))
Expand All @@ -1048,12 +1048,19 @@ def parse_correction(self, entry):
now = None
if delta_match:
seconds = int(delta_match.group()) * 60
now = datetime.datetime.now().replace(second=0, microsecond=0)
now += datetime.timedelta(seconds=seconds)
if self.valid_time(now):
entry = entry[delta_match.end():]
# If positive, offset from the end of the last entry.
if seconds >= 0:
now = self.window.last_time()
# Otherwise, offset from the current time.
else:
now = None
now = datetime.datetime.now().replace(second=0, microsecond=0)

if now is not None:
now += datetime.timedelta(seconds=seconds)
if self.valid_time(now):
entry = entry[delta_match.end():]
else:
now = None
return entry, now


Expand Down

0 comments on commit 75c0f5c

Please sign in to comment.