Notepad3 icon indicating copy to clipboard operation
Notepad3 copied to clipboard

How about support time expression?

Open nobk opened this issue 2 years ago • 3 comments

The current support of Tinyexpr 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
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 avatar Apr 30 '23 16:04 nobk

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()

nobk avatar May 05 '23 03:05 nobk

He already reposted as an issue on Tinyexpr upstream after 4 weeks. Nice!: https://github.com/codeplea/tinyexpr/issues/104

maboroshin avatar Jul 24 '23 10:07 maboroshin

He already reposted as an issue on Tinyexpr upstream after 4 weeks. Nice!: codeplea/tinyexpr#104

I think because Tinyexpr return double value, no time expression will be supported, so I write one using C++, not too long about 200 lines, compile with notepad3. I'm currently using it myself to calculate the length of clipped videos. If anyone here is still interested in this feature, I can submit a PR. extexpr n3

nobk avatar Dec 01 '23 13:12 nobk