File size: 6,698 Bytes
559318d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | 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
|