From 47e10fbee746dba14f2550610b06301e7925a24f Mon Sep 17 00:00:00 2001 From: Craig Moyer Date: Sun, 29 Sep 2019 19:06:53 -0400 Subject: [PATCH] Fix issue #662 to properly handle day names as new entry dates and command line (-on, -from, -to). --- features/regression.feature | 47 +++++++++++++++++++++++++++++++++++++ features/steps/core.py | 13 +++++++--- jrnl/Journal.py | 3 ++- jrnl/time.py | 12 ++++++---- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/features/regression.feature b/features/regression.feature index 3e644b19..fb4f0060 100644 --- a/features/regression.feature +++ b/features/regression.feature @@ -62,3 +62,50 @@ Feature: Zapped bugs should stay dead. Then the output should contain "I'm going to activate the machine." Then the output should contain "I've crossed so many timelines. Is there any going back?" + Scenario: Create entry using day of the week as entry date. + Given we use the config "basic.yaml" + When we run "jrnl monday: This is an entry on a Monday." + Then we should see the message "Entry added" + When we run "jrnl -1" + Then the output should contain "monday at 9am" in the local time + Then the output should contain "This is an entry on a Monday." + + Scenario: Create entry using day of the week abbreviations as entry date. + Given we use the config "basic.yaml" + When we run "jrnl fri: This is an entry on a Friday." + Then we should see the message "Entry added" + When we run "jrnl -1" + Then the output should contain "friday at 9am" in the local time + + Scenario: Displaying entries using -on today should display entries created today. + Given we use the config "basic.yaml" + When we run "jrnl today: Adding an entry right now." + Then we should see the message "Entry added" + When we run "jrnl -on today" + Then the output should contain "Adding an entry right now." + + Scenario: Displaying entries using -from day should display correct entries + Given we use the config "basic.yaml" + When we run "jrnl yesterday: This thing happened yesterday" + Then we should see the message "Entry added" + When we run "jrnl today at 11:59pm: Adding an entry right now." + Then we should see the message "Entry added" + When we run "jrnl tomorrow: A future entry." + Then we should see the message "Entry added" + When we run "jrnl -from today" + Then the output should contain "Adding an entry right now." + Then the output should contain "A future entry." + Then the output should not contain "This thing happened yesterday" + + Scenario: Displaying entries using -from and -to day should display correct entries + Given we use the config "basic.yaml" + When we run "jrnl yesterday: This thing happened yesterday" + Then we should see the message "Entry added" + When we run "jrnl today at 11:59pm: Adding an entry right now." + Then we should see the message "Entry added" + When we run "jrnl tomorrow: A future entry." + Then we should see the message "Entry added" + When we run "jrnl -from yesterday -to today" + Then the output should contain "This thing happened yesterday" + Then the output should contain "Adding an entry right now." + Then the output should not contain "A future entry." diff --git a/features/steps/core.py b/features/steps/core.py index 83981d13..02f1a5e2 100644 --- a/features/steps/core.py +++ b/features/steps/core.py @@ -6,11 +6,18 @@ from jrnl import cli, install, Journal, util, plugins from jrnl import __version__ from dateutil import parser as date_parser from collections import defaultdict +try: import parsedatetime.parsedatetime_consts as pdt +except ImportError: import parsedatetime as pdt +import time import os import json import yaml import keyring +consts = pdt.Constants(usePyICU=False) +consts.DOWParseStyle = -1 # Prefers past weekdays +CALENDAR = pdt.Calendar(consts) + class TestKeyring(keyring.backend.KeyringBackend): """A test keyring that just stores its valies in a hash""" @@ -180,9 +187,9 @@ def check_output(context, text=None): def check_output_time_inline(context, text): out = context.stdout_capture.getvalue() local_tz = tzlocal.get_localzone() - utc_time = date_parser.parse(text) - local_date = utc_time.astimezone(local_tz).strftime("%Y-%m-%d %H:%M") - assert local_date in out, local_date + date, flag = CALENDAR.parse(text) + output_date = time.strftime("%Y-%m-%d %H:%M",date) + assert output_date in out, output_date @then('the output should contain') diff --git a/jrnl/Journal.py b/jrnl/Journal.py index 72fe94b1..33b3f8f2 100644 --- a/jrnl/Journal.py +++ b/jrnl/Journal.py @@ -122,7 +122,8 @@ class Journal(object): last_entry_pos = 0 for match in date_blob_re.finditer(journal_txt): date_blob = match.groups()[0] - new_date = time.parse(date_blob) + # Passing in a date that had brackets around it + new_date = time.parse(date_blob, bracketed=True) if new_date: if entries: entries[-1].text = journal_txt[last_entry_pos:match.start()] diff --git a/jrnl/time.py b/jrnl/time.py index 9ff125aa..66d3f4f8 100644 --- a/jrnl/time.py +++ b/jrnl/time.py @@ -12,15 +12,16 @@ consts.DOWParseStyle = -1 # "Monday" will be either today or the last Monday CALENDAR = pdt.Calendar(consts) -def parse(date_str, inclusive=False, default_hour=None, default_minute=None): +def parse(date_str, inclusive=False, default_hour=None, default_minute=None, bracketed=False): """Parses a string containing a fuzzy date and returns a datetime.datetime object""" if not date_str: return None elif isinstance(date_str, datetime): return date_str - # Don't try to parse anything with 6 or less characters. It's probably a markdown footnote - if len(date_str) <= 6: + # Don't try to parse anything with 6 or less characters and was parsed from the existing journal. + # It's probably a markdown footnote + if len(date_str) <= 6 and bracketed: return None default_date = DEFAULT_FUTURE if inclusive else DEFAULT_PAST @@ -52,7 +53,10 @@ def parse(date_str, inclusive=False, default_hour=None, default_minute=None): return None if flag is 1: # Date found, but no time. Use the default time. - date = datetime(*date[:3], hour=default_hour or 0, minute=default_minute or 0) + date = datetime(*date[:3], + hour=23 if inclusive else default_hour or 0, + minute=59 if inclusive else default_minute or 0, + second=59 if inclusive else 0) else: date = datetime(*date[:6])