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
6.7 kB
diff --git a/timefhuman/grammar.lark b/timefhuman/grammar.lark
index 566cb2c..3f8b00f 100644
--- a/timefhuman/grammar.lark
+++ b/timefhuman/grammar.lark
@@ -64,14 +64,14 @@ single: datetime
// Only add houronly to specific formats that immediately
// indicate this is an hour-only time.
-datetime: date ("at" (time | houronly))?
- | date (time | houronly)
- | date "T" time // ISO 8601
- | (time | houronly) date
- | (time | houronly) "on" date
- | date
- | time
- | datetimename
+datetime: date ("at" (time | houronly))? timezone?
+ | date (time | houronly) timezone?
+ | date "T" time timezone? // ISO 8601
+ | (time | houronly) date timezone?
+ | (time | houronly) "on" date timezone?
+ | date timezone?
+ | time timezone?
+ | datetimename timezone?
// NOTE: process month modifiers here since the modifier affects the year, which
// won't be processed until the date rule. If additional modifiers are added,
@@ -96,9 +96,9 @@ date: weekday? month "/" day "/" year
// classified as an ambiguous token (in case it's a month, day, year, etc.)
// However, that means to support single-integer (e.g., hour) times, we need
// to manually add them to the `datetime` rule above.
-time: hour COLON minute (COLON second ("." millisecond)?)? meridiem? timezone?
- | hour ("o'clock")? meridiem timezone?
- | timename timezone?
+time: hour COLON minute (COLON second ("." millisecond)?)? meridiem?
+ | hour ("o'clock")? meridiem
+ | timename
duration: ("in"|"for")? duration_part (("and"|",")? duration_part)* ("ago")?
duration_part: duration_number (duration_unit | duration_unit_letter)
diff --git a/timefhuman/main.py b/timefhuman/main.py
index 58b7faa..f366541 100644
--- a/timefhuman/main.py
+++ b/timefhuman/main.py
@@ -203,7 +203,7 @@ class tfhTransformer(Transformer):
data = nodes_to_dict(children)
if 'datetime' in data:
return data['datetime']
- return tfhDatetime(date=data.get('date'), time=data.get('time'))
+ return tfhDatetime(date=data.get('date'), time=data.get('time'), tz=data.get('timezone'))
def date(self, children):
data = nodes_to_dict(children)
@@ -320,8 +320,7 @@ class tfhTransformer(Transformer):
minute=int(data.get("minute", 0)),
second=int(data.get("second", 0)),
millisecond=int(data.get("millisecond", 0)),
- meridiem=data.get("meridiem", None),
- tz=data.get("timezone", None),
+ meridiem=data.get("meridiem", None)
)}
def meridiem(self, children):
diff --git a/timefhuman/renderers.py b/timefhuman/renderers.py
index 42bbdbc..81af967 100644
--- a/timefhuman/renderers.py
+++ b/timefhuman/renderers.py
@@ -148,14 +148,12 @@ class tfhTime:
second: Optional[int] = None,
millisecond: Optional[int] = None,
meridiem: Optional[Meridiem] = None,
- tz: Optional[pytz.timezone] = None,
):
self.hour = hour
self.minute = minute
self.second = second
self.millisecond = millisecond
self.meridiem = meridiem
- self.tz = tz
def to_object(self, config: tfhConfig = tfhConfig()) -> time:
"""Convert to a real time object. Assumes all fields are filled in."""
@@ -163,7 +161,7 @@ class tfhTime:
self.hour += 12
elif self.meridiem == tfhTime.Meridiem.AM and self.hour == 12:
self.hour = 0
- object = time(self.hour, self.minute or 0, self.second or 0, self.millisecond or 0, tzinfo=self.tz)
+ object = time(self.hour, self.minute or 0, self.second or 0, self.millisecond or 0)
return object
@classmethod
@@ -176,7 +174,13 @@ class tfhTime:
class tfhDatetime(tfhDatelike):
- """A combination of tfhDate + tfhTime."""
+ """
+ A combination of tfhDate + tfhTime.
+
+ Note that only this datetime has a notion of timezone, so we cannot directly return a
+ date or time object. Even those singleton objects *must* pass through this object to
+ be timezone aware.
+ """
def getter(attr, key):
def get(self):
@@ -195,28 +199,33 @@ class tfhDatetime(tfhDatelike):
month = property(getter('date', 'month'), setter('date', 'month'))
day = property(getter('date', 'day'), setter('date', 'day'))
meridiem = property(getter('time', 'meridiem'), setter('time', 'meridiem'))
- tz = property(getter('time', 'tz'), setter('time', 'tz'))
def __init__(
self,
date: Optional[tfhDate] = None,
- time: Optional[tfhTime] = None
+ time: Optional[tfhTime] = None,
+ tz: Optional[pytz.timezone] = None,
):
self.date = date
self.time = time
+ self.tz = tz
def to_object(self, config: tfhConfig = tfhConfig()) -> Union[datetime, date, time]:
"""Convert to real datetime, assumes partial fields are filled."""
+ _time = self.time.to_object(config) if self.time else None
+ _date = self.date.to_object(config) if self.date else None
+ tzinfo = self.tz or config.now.tzinfo
+
if self.date and self.time:
- return datetime.combine(self.date.to_object(config), self.time.to_object(config), tzinfo=self.time.tz)
+ return datetime.combine(_date, _time, tzinfo=tzinfo)
elif self.date:
if config.infer_datetimes:
- return datetime.combine(self.date.to_object(config), time(0, 0))
- return self.date.to_object(config)
+ return datetime.combine(_date, time(0, 0), tzinfo=tzinfo)
+ return _date # NOTE: a date object cannot hold a timezone
elif self.time:
if config.infer_datetimes:
- _now = config.now.replace(tzinfo=self.time.tz)
- candidate = datetime.combine(_now.date(), self.time.to_object(config))
+ _now = config.now.replace(tzinfo=tzinfo)
+ candidate = datetime.combine(_now.date(), _time, tzinfo=tzinfo)
if candidate < _now and config.direction == Direction.next:
candidate += timedelta(days=1)
elif candidate > _now and config.direction == Direction.previous:
@@ -224,7 +233,7 @@ class tfhDatetime(tfhDatelike):
elif config.direction == Direction.this:
pass
return candidate
- return self.time.to_object(config)
+ return _time.replace(tzinfo=tzinfo)
raise ValueError("Datetime is missing both date and time")
@classmethod