Skip to content

Commit

Permalink
Merge pull request #78 from virtUOS/time-fmt
Browse files Browse the repository at this point in the history
Time formatting in logs
  • Loading branch information
janmn authored Apr 23, 2024
2 parents b7b9b9c + 2a07981 commit ebe8832
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions occameracontrol/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import datetime
import logging
import requests
import time
Expand Down Expand Up @@ -103,6 +104,13 @@ def move_to_preset(self, preset: int):
register_camera_move(self.url, preset)
self.last_updated = time.time()

def from_now(self, ts: float) -> str:
'''Get a string representation of the time until the provided time
stamp is reached.
'''
seconds = int(ts - time.time()) # seconds are enough accuracy
return str(datetime.timedelta(seconds=seconds))

def update_position(self):
'''Check for currently active events with the camera's capture agent
and move the camera to the appropriate (active, inactive) position if
Expand All @@ -113,11 +121,11 @@ def update_position(self):

level = logging.DEBUG if int(time.time()) % 60 else logging.INFO
if event.future():
logger.log(level, '[%s] Next event `%s` starts in %i seconds',
agent_id, event.title[:40], event.start - time.time())
logger.log(level, '[%s] Next event `%s` starts in %s',
agent_id, event.title[:40], self.from_now(event.start))
elif event.active():
logger.log(level, '[%s] Active event `%s` ends in %i seconds',
agent_id, event.title[:40], event.end - time.time())
logger.log(level, '[%s] Active event `%s` ends in %s',
agent_id, event.title[:40], self.from_now(event.end))
else:
logger.log(level, '[%s] No planned events', agent_id)

Expand Down

0 comments on commit ebe8832

Please sign in to comment.