Method to convert the Duration to seconds
Method to convert the Duration to seconds

REST API returned duration like this {'ElapsedTime': 'P0DT00H02M16S', 'WaitTime': 'P0DT00H00M00S'} can be converted to seconds (136).

from TM1py.Services import TM1Service
import pandas as pd
import re

def get_seconds_from_duration(time_str: str) -> int:
    """
    This function will convert the TM1 time to seconds
    :param time_str: P0DT00H01M43S
    :return: int
    """
    pattern = re.compile('\w(\d+)\w\w(\d+)\w(\d+)\w(\d+)\w')
    matches = pattern.search(time_str)
    d, h, m, s = matches.groups()
    seconds = (int(d) * 86400) + (int(h) * 3600) + (int(m) * 60) + int(s)
    return seconds

with TM1Service(address=ADDRESS, port=PORT, ssl=SSL, user=USER, password=PASSWORD) as tm1:
    threads = pd.DataFrame(tm1.monitoring.get_threads())
    threads[['ElapsedTime', 'WaitTime']] = threads[['ElapsedTime', 'WaitTime']].applymap(get_seconds_from_duration)
    print(threads[['ElapsedTime', 'WaitTime']])
OLAPLINE/TM1py