mirror of
https://github.com/kernelkit/infix.git
synced 2026-07-22 01:13:00 +02:00
yanger: Generalize converting time deltas to YANG dates
Make sure that we have one way of getting from Python's native datetime to a YANG compatible format.
This commit is contained in:
@@ -1,5 +1,28 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from . import host
|
||||
|
||||
LOG = None
|
||||
|
||||
class YangDate:
|
||||
def __init__(self, dt=None):
|
||||
self.dt = dt if dt else host.HOST.now()
|
||||
|
||||
def __str__(self):
|
||||
datestr = self.dt.strftime("%Y-%m-%dT%H:%M:%S%z")
|
||||
|
||||
# Translate strftime's timezone format (HHMM) to the one
|
||||
# expected by YANG (HH:MM)
|
||||
return datestr[:-2] + ':' + datestr[-2:]
|
||||
|
||||
@classmethod
|
||||
def from_delta(cls, delta):
|
||||
return cls(host.HOST.now() - delta)
|
||||
|
||||
@classmethod
|
||||
def from_seconds(cls, seconds):
|
||||
return cls.from_delta(timedelta(seconds=seconds))
|
||||
|
||||
|
||||
def insert(obj, *path_and_value):
|
||||
""""This function inserts a value into a nested json object"""
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
from datetime import timedelta
|
||||
from re import match
|
||||
|
||||
from .common import insert
|
||||
from .common import insert, YangDate
|
||||
from .host import HOST
|
||||
|
||||
|
||||
def uptime2datetime(uptime):
|
||||
"""
|
||||
Convert uptime to YANG format (YYYY-MM-DDTHH:MM:SS+00:00)
|
||||
@@ -36,11 +35,7 @@ def uptime2datetime(uptime):
|
||||
h += days * 24
|
||||
|
||||
uptime_delta = timedelta(hours=h, minutes=m, seconds=s)
|
||||
current_time = HOST.now()
|
||||
last_updated = current_time - uptime_delta
|
||||
date_timestd = last_updated.strftime('%Y-%m-%dT%H:%M:%S%z')
|
||||
|
||||
return date_timestd[:-2] + ':' + date_timestd[-2:]
|
||||
return str(YangDate.from_delta(uptime_delta))
|
||||
|
||||
|
||||
def add_protocol(routes, proto):
|
||||
|
||||
Reference in New Issue
Block a user