mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Use new VERSION_CONTEXT_STYLE to bypass parsedatetime flag style warning
Replace "flag" and its magic numbers with contextual booleans
This commit is contained in:
parent
2b013ddde5
commit
3e3422f68c
1 changed files with 14 additions and 10 deletions
24
jrnl/time.py
24
jrnl/time.py
|
@ -9,14 +9,11 @@ DEFAULT_PAST = datetime.datetime(FAKE_YEAR, 1, 1, 0, 0)
|
||||||
|
|
||||||
|
|
||||||
def __get_pdt_calendar():
|
def __get_pdt_calendar():
|
||||||
try:
|
import parsedatetime as pdt
|
||||||
import parsedatetime.parsedatetime_consts as pdt
|
|
||||||
except ImportError:
|
|
||||||
import parsedatetime as pdt
|
|
||||||
|
|
||||||
consts = pdt.Constants(usePyICU=False)
|
consts = pdt.Constants(usePyICU=False)
|
||||||
consts.DOWParseStyle = -1 # "Monday" will be either today or the last Monday
|
consts.DOWParseStyle = -1 # "Monday" will be either today or the last Monday
|
||||||
calendar = pdt.Calendar(consts)
|
calendar = pdt.Calendar(consts, version=pdt.VERSION_CONTEXT_STYLE)
|
||||||
|
|
||||||
return calendar
|
return calendar
|
||||||
|
|
||||||
|
@ -42,6 +39,10 @@ def parse(
|
||||||
default_date = DEFAULT_FUTURE if inclusive else DEFAULT_PAST
|
default_date = DEFAULT_FUTURE if inclusive else DEFAULT_PAST
|
||||||
date = None
|
date = None
|
||||||
year_present = False
|
year_present = False
|
||||||
|
|
||||||
|
hasTime = False
|
||||||
|
hasDate = False
|
||||||
|
|
||||||
while not date:
|
while not date:
|
||||||
try:
|
try:
|
||||||
from dateutil.parser import parse as dateparse
|
from dateutil.parser import parse as dateparse
|
||||||
|
@ -53,7 +54,8 @@ def parse(
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
year_present = True
|
year_present = True
|
||||||
flag = 1 if date.hour == date.minute == 0 else 2
|
hasTime = not (date.hour == date.minute == 0)
|
||||||
|
hasDate = True
|
||||||
date = date.timetuple()
|
date = date.timetuple()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if e.args[0] == "day is out of range for month":
|
if e.args[0] == "day is out of range for month":
|
||||||
|
@ -61,9 +63,11 @@ def parse(
|
||||||
default_date = datetime.datetime(y, m, d - 1, H, M, S)
|
default_date = datetime.datetime(y, m, d - 1, H, M, S)
|
||||||
else:
|
else:
|
||||||
calendar = __get_pdt_calendar()
|
calendar = __get_pdt_calendar()
|
||||||
date, flag = calendar.parse(date_str)
|
date, parse_context = calendar.parse(date_str)
|
||||||
|
hasTime = parse_context.hasTime
|
||||||
|
hasDate = parse_context.hasDate
|
||||||
|
|
||||||
if not flag: # Oops, unparsable.
|
if not hasDate and not hasTime:
|
||||||
try: # Try and parse this as a single year
|
try: # Try and parse this as a single year
|
||||||
year = int(date_str)
|
year = int(date_str)
|
||||||
return datetime.datetime(year, 1, 1)
|
return datetime.datetime(year, 1, 1)
|
||||||
|
@ -72,8 +76,8 @@ def parse(
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if flag == 1: # Date found, but no time. Use the default time.
|
if hasDate and not hasTime:
|
||||||
date = datetime.datetime(
|
date = datetime.datetime( # Use the default time
|
||||||
*date[:3],
|
*date[:3],
|
||||||
hour=23 if inclusive else default_hour or 0,
|
hour=23 if inclusive else default_hour or 0,
|
||||||
minute=59 if inclusive else default_minute or 0,
|
minute=59 if inclusive else default_minute or 0,
|
||||||
|
|
Loading…
Add table
Reference in a new issue