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

How about support time expression? #4760

Open
nobk opened this issue Apr 30, 2023 · 3 comments
Open

How about support time expression? #4760

nobk opened this issue Apr 30, 2023 · 3 comments

Comments

@nobk
Copy link

nobk commented Apr 30, 2023

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
Copy link
Author

nobk commented May 5, 2023

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

@maboroshin
Copy link
Contributor

maboroshin commented Jul 24, 2023

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

@nobk
Copy link
Author

nobk commented Dec 1, 2023

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

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

No branches or pull requests

3 participants