codeplea/tinyexpr

How about support time expression?

Opened this issue · 2 comments

nobk commented

The current support of tinyexpr in notepad3 is a cool feature I am using every day, as calculator.
But time expression calculation like:

00:32:15-00:16:42=00:15:33
32:15.339-16:42.158=15:33.171
15:33.171+16:42.158=32:15.339
second(15:33.171)=933.171s

I am not find a calculator with that support. This time expressions are useful when working with audio or video.

nobk commented

I write a python script to do this, with some help of GPT-4, it does work, but not as easy use as in notepad3, I need switch to cmd window type some cmd line and copy result and switch back to notepad3.

tc.py

import sys
import re
from datetime import datetime, timedelta

def main():
    if len(sys.argv) != 2:
        print(
'''Usage: python tc.py "01:23:15.211 01:24:10.323"
python tc.py "01:24:10.323-01:23:15.211"
python tc.py "01:24:10.323+11.211"
''')
        return

    time_str = sys.argv[1]
    pattern = re.compile(r'^((\d{1,2}:)?(\d{1,2}:)?(\d{1,2}(\.\d{1,3})?))([+ \-])((\d{1,2}:)?(\d{1,2}:)?(\d{1,2}(\.\d{1,3})?))$')
    m = pattern.match(time_str)
    if not m:
        print('Invalid time expression format')
        return
    #for i in range(11):
    #    print(m[i+1], i+1)
    op = m.group(6)
    fstr1 = '%S' #'%H:%M:%S.%f'
    if m.group(2):
        fstr1 = '%M:' + fstr1
        if m.group(3):
            fstr1 = '%H:' + fstr1
    if m.group(5):
        fstr1 = fstr1 + '.%f'
    fstr2 = '%S' #'%H:%M:%S.%f'
    if m.group(8):
        fstr2 = '%M:' + fstr2
        if m.group(9):
            fstr2 = '%H:' + fstr2
    if m.group(11):
        fstr2 = fstr2 + '.%f'
    start_time = datetime.strptime(m.group(1), fstr1)
    end_time = datetime.strptime(m.group(7), fstr2)
    if op == ' ':
        res = end_time - start_time
    elif op == '-':
        res = start_time - end_time
    else:
        res = end_time + timedelta(hours=start_time.hour,minutes=start_time.minute, seconds=start_time.second, microseconds=start_time.microsecond)
        res = res - datetime(1900, 1, 1)
    #dt = datetime(1900, 1, 1) + res
    print(res.total_seconds(),'s')
    print(res)

if __name__ == '__main__':
    main()

The current support of tinyexpr in notepad3 is a cool feature I am using every day, as calculator. But time expression calculation like:

00:32:15-00:16:42=00:15:33
32:15.339-16:42.158=15:33.171
15:33.171+16:42.158=32:15.339
second(15:33.171)=933.171s

I am not find a calculator with that support. This time expressions are useful when working with audio or video.

the mini parser does not support different data types at all. So it might possible to add support parsing of "time stamps" into a double that represents the number of seconds (incl. fractions), but the information that this value was a timestamp is not kept.