Skip to content

Commit b5d1b76

Browse files
Replace timeago
Replace the timeago library with a simple function
1 parent b58094b commit b5d1b76

3 files changed

Lines changed: 22 additions & 7 deletions

File tree

meshtastic/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect
7676

7777
import google.protobuf.json_format
7878
import serial # type: ignore[import-untyped]
79-
import timeago # type: ignore[import-untyped]
79+
from dotmap import DotMap # type: ignore[import-untyped]
8080
from google.protobuf.json_format import MessageToJson
8181
from pubsub import pub # type: ignore[import-untyped]
8282
from tabulate import tabulate

meshtastic/mesh_interface.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from typing import Any, Callable, Dict, List, Optional, Union
1515

1616
import google.protobuf.json_format
17-
import timeago # type: ignore[import-untyped]
1817
from pubsub import pub # type: ignore[import-untyped]
1918
from tabulate import tabulate
2019

@@ -158,11 +157,28 @@ def getLH(ts) -> Optional[str]:
158157

159158
def getTimeAgo(ts) -> Optional[str]:
160159
"""Format how long ago have we heard from this node (aka timeago)."""
161-
return (
162-
timeago.format(datetime.fromtimestamp(ts), datetime.now())
163-
if ts
164-
else None
160+
if ts is None:
161+
return None
162+
delta = datetime.now() - datetime.fromtimestamp(ts)
163+
delta_secs = int(delta.total_seconds())
164+
if delta_secs < 0:
165+
return None # not handling a timestamp from the future
166+
intervals = (
167+
("year", 60 * 60 * 24 * 365),
168+
("month", 60 * 60 * 24 * 30),
169+
("day", 60 * 60 * 24),
170+
("hour", 60 * 60),
171+
("min", 60),
172+
("sec", 1),
165173
)
174+
for name, interval_duration in intervals:
175+
if delta_secs < interval_duration:
176+
continue
177+
x = delta_secs // interval_duration
178+
plur = "s" if x > 1 else ""
179+
return f"{x} {name}{plur} ago"
180+
181+
return "now"
166182

167183
rows: List[Dict[str, Any]] = []
168184
if self.nodesByNum:

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"dotmap>=1.3.14",
4141
"pyqrcode>=1.2.1",
4242
"tabulate>=0.8.9",
43-
"timeago>=1.0.15",
4443
"pyyaml",
4544
"bleak>=0.21.1",
4645
"packaging",

0 commit comments

Comments
 (0)