mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 08:38:32 +02:00
Resolve parsedatetime "flag style" DeprecationWarning (#1818)
* Stop ignoring parsedatetime warning flag style warning * 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
7e94208574
commit
721167a079
2 changed files with 14 additions and 11 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():
|
||||
try:
|
||||
import parsedatetime.parsedatetime_consts as pdt
|
||||
except ImportError:
|
||||
import parsedatetime as pdt
|
||||
import parsedatetime as pdt
|
||||
|
||||
consts = pdt.Constants(usePyICU=False)
|
||||
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
|
||||
|
||||
|
@ -42,6 +39,10 @@ def parse(
|
|||
default_date = DEFAULT_FUTURE if inclusive else DEFAULT_PAST
|
||||
date = None
|
||||
year_present = False
|
||||
|
||||
hasTime = False
|
||||
hasDate = False
|
||||
|
||||
while not date:
|
||||
try:
|
||||
from dateutil.parser import parse as dateparse
|
||||
|
@ -53,7 +54,8 @@ def parse(
|
|||
)
|
||||
else:
|
||||
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()
|
||||
except Exception as e:
|
||||
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)
|
||||
else:
|
||||
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
|
||||
year = int(date_str)
|
||||
return datetime.datetime(year, 1, 1)
|
||||
|
@ -72,8 +76,8 @@ def parse(
|
|||
except TypeError:
|
||||
return None
|
||||
|
||||
if flag == 1: # Date found, but no time. Use the default time.
|
||||
date = datetime.datetime(
|
||||
if hasDate and not hasTime:
|
||||
date = datetime.datetime( # Use the default time
|
||||
*date[:3],
|
||||
hour=23 if inclusive else default_hour or 0,
|
||||
minute=59 if inclusive else default_minute or 0,
|
||||
|
|
|
@ -121,7 +121,6 @@ addopts = [
|
|||
|
||||
filterwarnings = [
|
||||
"ignore::DeprecationWarning",
|
||||
"ignore:Flag style will be deprecated in.*",
|
||||
"ignore:[WinError 32].*",
|
||||
"ignore:[WinError 5].*"
|
||||
]
|
||||
|
|
Loading…
Add table
Reference in a new issue