Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uptime string fails parsing if missing seconds #615

Open
tyler-8 opened this issue Jan 20, 2025 · 2 comments
Open

Uptime string fails parsing if missing seconds #615

tyler-8 opened this issue Jan 20, 2025 · 2 comments

Comments

@tyler-8
Copy link

tyler-8 commented Jan 20, 2025

Environment

  • Python version: 3.12
  • netutils version: 1.12.0

Expected Behavior

uptime_string_to_seconds("2w1d") would result in an integer of the number of seconds.

Observed Behavior

ValueError: Unable to parse uptime string.

Steps to Reproduce

  1. In a Python shell, from netutils.time import uptime_string_to_seconds
  2. Call function with a string argument that does not contain a seconds value (such as "2w1d")
  3. Observe exception

The closest matching regex parser assumes every uptime string will contain a 'seconds' value, which isn't always true. For example; in the up-time attribute of a NETCONF query to a Cisco IOS-XE device to show BGP neighbors.

@itdependsnetworks
Copy link
Contributor

Can you provide the perceived rules you see in NETCONF query to a Cisco IOS-XE device to show BGP neighbors ? Meaning, is it always just day level details? Would year show up as well? Month? etc...

Should be super simple to update regex to include it here once understood:

UPTIME_REGEX_PATTERNS = [
(
r"((?P<years>\d+) year(s)?,\s+)?((?P<weeks>\d+) week(s)?,\s+)?"
r"((?P<days>\d+) day(s)?,\s+)?((?P<hours>\d+) "
r"hour(s)?,\s+)?((?P<minutes>\d+) minute(s)?)"
),
(
r"((?P<days>\d+) day(s)?,\s+)?"
r"((?P<hours>\d+)):((?P<minutes>\d+)):((?P<seconds>\d+))" # pylint: disable=implicit-str-concat
),
(
r"(((?P<years>\d+)y)?(?P<weeks>\d+)w)?((?P<days>\d+)d)?((?P<hours>\d+)h)?"
r"((?P<minutes>\d+)m)?((?P<seconds>\d+)s)"
), # pylint: disable=implicit-str-concat
]

@tyler-8
Copy link
Author

tyler-8 commented Jan 23, 2025

That's a good question. I'll comb through some more outputs to see if there's a "rollover" that happens where seconds are no longer included.

Also can we talk about how a structured data API like NETCONF is still providing a human readable timestamp instead of a raw number value or other machine-friendly timestamp? 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants