Skip to content

Commit

Permalink
Replace timeago
Browse files Browse the repository at this point in the history
Replace the timeago library with a simple function
  • Loading branch information
FedericoCeratto committed Jun 21, 2024
1 parent b58094b commit b5d1b76
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion meshtastic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect

import google.protobuf.json_format
import serial # type: ignore[import-untyped]
import timeago # type: ignore[import-untyped]
from dotmap import DotMap # type: ignore[import-untyped]
from google.protobuf.json_format import MessageToJson
from pubsub import pub # type: ignore[import-untyped]
from tabulate import tabulate
Expand Down
26 changes: 21 additions & 5 deletions meshtastic/mesh_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from typing import Any, Callable, Dict, List, Optional, Union

import google.protobuf.json_format
import timeago # type: ignore[import-untyped]
from pubsub import pub # type: ignore[import-untyped]
from tabulate import tabulate

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

def getTimeAgo(ts) -> Optional[str]:
"""Format how long ago have we heard from this node (aka timeago)."""
return (
timeago.format(datetime.fromtimestamp(ts), datetime.now())
if ts
else None
if ts is None:
return None
delta = datetime.now() - datetime.fromtimestamp(ts)
delta_secs = int(delta.total_seconds())
if delta_secs < 0:
return None # not handling a timestamp from the future
intervals = (
("year", 60 * 60 * 24 * 365),
("month", 60 * 60 * 24 * 30),
("day", 60 * 60 * 24),
("hour", 60 * 60),
("min", 60),
("sec", 1),
)
for name, interval_duration in intervals:
if delta_secs < interval_duration:
continue
x = delta_secs // interval_duration
plur = "s" if x > 1 else ""
return f"{x} {name}{plur} ago"

return "now"

rows: List[Dict[str, Any]] = []
if self.nodesByNum:
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"dotmap>=1.3.14",
"pyqrcode>=1.2.1",
"tabulate>=0.8.9",
"timeago>=1.0.15",
"pyyaml",
"bleak>=0.21.1",
"packaging",
Expand Down

0 comments on commit b5d1b76

Please sign in to comment.