mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-12 01:18:31 +02:00
commit 75113187432939a51486422c3f70b3a9e2bcf0aa Merge: 74d185447e10fb
Author: Jonathan Wren <9453067+wren@users.noreply.github.com> Date: Thu Oct 24 17:02:10 2019 -0700 Merge pull request #665 from notbalanced/issue_662 Fixes Issue #662 - Day names not treated consistently for new entry commit 74d1854a4bba468221b4eee254bdee2bb40f5d5a Merge: 97e4d6a6a5726a
Author: Jonathan Wren <9453067+wren@users.noreply.github.com> Date: Sat Oct 5 15:30:57 2019 -0700 Merge pull request #418 from philipsd6/2.0-fancy_exporter Add exporter to output entries inside unicode box character boxes commit47e10fbee7
Author: Craig Moyer <craig.moyer@gmail.com> Date: Sun Sep 29 19:06:53 2019 -0400 Fix issue #662 to properly handle day names as new entry dates and command line (-on, -from, -to). commit9588913100
Author: Craig Moyer <craig.moyer@gmail.com> Date: Sun Sep 29 08:27:27 2019 -0400 Syncing with jrnl-org/master commit4c68eb193d
Merge:81dfebb
97e4d6a Author: Craig Moyer <craig.moyer@gmail.com> Date: Sun Sep 29 07:52:02 2019 -0400 Merge remote-tracking branch 'upstream/master' into 2.0-rc1-maebert commit81dfebb2c0
Author: Manuel Ebert <manuel@1450.me> Date: Mon Apr 29 20:34:18 2019 +0200 export changes commit6a5726acd2
Author: Philip Douglass <philip@philipdouglass.com> Date: Fri Dec 22 20:56:36 2017 -0500 Enable FancyExporter plugin commit3d1b226871
Author: Philip Douglass <philip@philipdouglass.com> Date: Fri Jan 29 11:17:41 2016 -0500 Add exporter to output entries inside unicode box character boxes
69 lines
2.6 KiB
Python
69 lines
2.6 KiB
Python
from datetime import datetime
|
|
from dateutil.parser import parse as dateparse
|
|
try: import parsedatetime.parsedatetime_consts as pdt
|
|
except ImportError: import parsedatetime as pdt
|
|
|
|
FAKE_YEAR = 9999
|
|
DEFAULT_FUTURE = datetime(FAKE_YEAR, 12, 31, 23, 59, 59)
|
|
DEFAULT_PAST = datetime(FAKE_YEAR, 1, 1, 0, 0)
|
|
|
|
consts = pdt.Constants(usePyICU=False)
|
|
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, 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 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
|
|
date = None
|
|
year_present = False
|
|
while not date:
|
|
try:
|
|
date = dateparse(date_str, default=default_date)
|
|
if date.year == FAKE_YEAR:
|
|
date = datetime(datetime.now().year, date.timetuple()[1:6])
|
|
else:
|
|
year_present = True
|
|
flag = 1 if date.hour == date.minute == 0 else 2
|
|
date = date.timetuple()
|
|
except Exception as e:
|
|
if e.args[0] == 'day is out of range for month':
|
|
y, m, d, H, M, S = default_date.timetuple()[:6]
|
|
default_date = datetime(y, m, d - 1, H, M, S)
|
|
else:
|
|
date, flag = CALENDAR.parse(date_str)
|
|
|
|
if not flag: # Oops, unparsable.
|
|
try: # Try and parse this as a single year
|
|
year = int(date_str)
|
|
return datetime(year, 1, 1)
|
|
except ValueError:
|
|
return None
|
|
except TypeError:
|
|
return None
|
|
|
|
if flag is 1: # Date found, but no time. Use the default time.
|
|
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])
|
|
|
|
# Ugly heuristic: if the date is more than 4 weeks in the future, we got the year wrong.
|
|
# Rather then this, we would like to see parsedatetime patched so we can tell it to prefer
|
|
# past dates
|
|
dt = datetime.now() - date
|
|
if dt.days < -28 and not year_present:
|
|
date = date.replace(date.year - 1)
|
|
return date
|