Skip to content

Commit

Permalink
Resolve parsedatetime "flag style" DeprecationWarning (#1818)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
micahellison authored Oct 29, 2023
1 parent 7e94208 commit 721167a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
24 changes: 14 additions & 10 deletions jrnl/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@


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

Expand All @@ -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
Expand All @@ -53,17 +54,20 @@ 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":
y, m, d, H, M, S = default_date.timetuple()[:6]
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)
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ addopts = [

filterwarnings = [
"ignore::DeprecationWarning",
"ignore:Flag style will be deprecated in.*",
"ignore:[WinError 32].*",
"ignore:[WinError 5].*"
]
Expand Down

0 comments on commit 721167a

Please sign in to comment.