Benjamin-eecs's picture
release v1: 65 held-out tasks, 78 bootstrap records, r2e_local adapter, manifest ae94c40b (part 4)
622387b verified
Raw History Blame Contribute Delete
1.69 kB
diff --git a/timefhuman/main.py b/timefhuman/main.py
index 448a4e2..58b7faa 100644
--- a/timefhuman/main.py
+++ b/timefhuman/main.py
@@ -1,8 +1,8 @@
-from datetime import timedelta
-import datetime
+from datetime import datetime, timedelta
from pathlib import Path
-from lark import Lark, Transformer, v_args, Token, Discard
+from dataclasses import replace
+from lark import Lark, Transformer, v_args
import pytz
from timefhuman.utils import generate_timezone_mapping, nodes_to_dict, nodes_to_multidict, get_month_mapping, tfhConfig, Direction, direction_to_offset
from timefhuman.renderers import tfhDatetime, tfhDate, tfhTime, tfhRange, tfhList, tfhTimedelta, tfhAmbiguous, tfhUnknown, tfhDatelike, tfhMatchable
@@ -28,12 +28,16 @@ def get_parser():
return parser
-def timefhuman(string, config: tfhConfig = tfhConfig(), raw=None):
+def timefhuman(string, config: tfhConfig = tfhConfig(), raw: bool=False, now: bool=False):
parser = get_parser()
tree = parser.parse(string)
if raw:
return tree
+
+ config = replace(config, now=config.now or datetime.now())
+ if now:
+ return config.now
transformer = tfhTransformer(config=config)
renderers = transformer.transform(tree)
diff --git a/timefhuman/utils.py b/timefhuman/utils.py
index 7f39218..c715513 100644
--- a/timefhuman/utils.py
+++ b/timefhuman/utils.py
@@ -24,7 +24,7 @@ class tfhConfig:
infer_datetimes: bool = True
# The 'current' datetime, used if infer_datetimes is True
- now: datetime = datetime.now()
+ now: datetime | None = None
# Return the matched text from the input string
return_matched_text: bool = False