How to change/convert the format of a timestamp?



  • Hi,

    I could use some help please.

    I need to convert the format of timestamps in a text file as follows:

    From old format:
    78:08.706 --> 78:18.550

    To new format:
    01:18:08.706 --> 01:18:10.550

    Is this possible in Notepad++? I’m using version v7.5.1 (32-bit)

    Thanks in advance for any help.



  • @Dana-Wright

    do you have some reference about what kind of timestamp (the old one) this is?
    Or do you know the min and max values?



  • I smell math, which means it isn’t really practical for N++ doing it. :)



  • @Alan-Kilborn
    it is using python script … or isn’t it ;-)

    @Dana-Wright

    it should be possible with the python script plugin, eg.
    use regex to find the time stamps, divide 78 by 60 to get the $hours, and old minutes minus (60*$hours) to get the new minutes

    but i lack py knowledge, so if you need more help on that, we’d need help from our python script gurus ;-)



  • Here’s a link to the file in question.



  • @Meta-Chuh

    Oh sure, easy to script (if full spec is known, see Eko’s comment), but I think the typical reaction to installing a plugin, and then writing (or just copying prewritten) code is lukewarm from most users that just wanna get something done quickly. :)

    Even regex probably generates such feelings (for the uninitiated) but at least that is built-in.



  • Sorry, forgot to mention that it’s only after the 1 hour mark that the format changes.



  • So, as @Alan-Kilborn already pointed out this is nothing npp can do with its builtin methods but
    if you are willing to install the python script plugin, then it should be easily possible to write a little
    script.
    One further question if you don’t mind.
    Did you already change the format in the document?
    It begins with hour:minute:second.milliseconds but then it changes to minute:seconds.milliseconds.
    The question basically is, is it safe to assume that the time format initially is always minute:seconds.milliseconds.



  • @Eko-palypse the correct time format should always be hour:minute:second.milliseconds.



  • @Dana-Wright
    and the source format is always minutes:second.millisecond, correct ?
    or can it be mixed with some lines that are having h:m:s.ms ?



  • @Eko-palypse the correct time format should always be hour:hour:minute:second.milliseconds

    Originally the file was auto created. My job was to correct the text and punctuation and adjust the timestamp as needed.

    I used a program called Transcribe! for this. It has the ability to copy/paste a timestamp but only in the following format: minute:second.milliseconds

    When it was under an hour it was easy to simply replace the correct time by pasting the updated timestamp after the hour.

    However, after an hour the format completely changed. Since Transcribe! does not use a separate number for the hour. I hope that’s clear.

    So, instead of giving me this: 01:18:13.201
    It gave me this: 78:08.168

    So I want to convert all the timestamps over the hour point from this 78:08.168 to this 01:18:13.201



  • You might want to search the forum for more about timestamps and/or captioning files (I am assuming this is an .srt or similar captioning file), because I know similar things have been asked.

    I know there’s a conditional replace that @guy038 could bring to the party. I couldn’t get the syntax working correctly.
    Without the conditionals, I can do it in n search/replace operations, where n depends on the possible minutes: if minutes is 60-99, then I can do it in 4 s/r (6x, 7x, 8x, 9x). If minutes can ever start at 100 or above, my method would need to increase n for every 10-minute group above the 9x.

    assuming

    • that we only ever edit the minutes and append an hours segment when necessary (ie, don’t ever have to fix invalid seconds)
    • that 0:00.000 up to 59:59.999 will remain unchanged

    The search/replace pairs (in regular expression mode) would be

    (?<![:\d\.])(6)(\d{1}:\d{2}\.\d{3})
    01:0$2
    (?<![:\d\.])(7)(\d{1}:\d{2}\.\d{3})
    01:1$2
    (?<![:\d\.])(8)(\d{1}:\d{2}\.\d{3})
    01:2$2
    (?<![:\d\.])(9)(\d{1}:\d{2}\.\d{3})
    01:3$2
    

    If it goes higher, you can give it a try yourself, and paste what you tried if it didn’t work.

    Honestly, if this were mine, I’d write up a Perl script to do it, completely outside of Notepad++. Others would choose Python or Lua. I could probably cobble it together in PythonScript, to have python make the changes, running inside Notepad++ editor window. But I don’t have time right now to do it, and I think I’ll wait until you’ve responded to the questions and clarifications, and maybe shown that you’ve tried to look for previous answers



  • @Dana-Wright

    sorry, not 100% clear for me - what is the format I should expect to convert.
    I understand the result should be 01:18:14.269 --> 01:18:21.079
    but which format is provided initially?
    This 00:78:14.269 --> 00:78:21.079 or
    that 78:14.269 --> 78:21.079?



  • Sorry for the confusion. To hopefully clarify things, here’s an updated file with only timestamps that need to be converted. Please ignore the earlier file



  • @Dana-Wright

    I guess this python script should do the job

    def change_format(m):
        parts = m.group(0).split(':')
        min = int(parts[0])
        if min > 60:
            real_minute = min % 60
            hours = min / 60
            return '{:02}:{:02}:{}'.format(hours, real_minute, parts[1])
        else:
            return '{}:{}'.format(*parts[:])
        
        
    editor.rereplace('\d+:\d+\.\d+',change_format)
    


  • @Eko-palypse thanks. I’ll give it a try.



  • @Eko-palypse Worked like a charm! Thank you very much!

    Now I need to analyze this and figure it out how it was done.



  • @Dana-Wright

    this is quite easy.
    editor is a object exposed by pythonscript plugin which can manipulate the scintilla component used by notepad++.
    rereplace is the method which allows to have the first parameter being a regular expression and
    the second parameter being a function which is called for each match of the regex.

    The function change_format gets the match object in variable m
    As no regular expression matching group has been defined everything should be accessible in group 0.
    parts = m.group(0).split(':')
    split what has been reported in match object by colon and return a list in variable parts
    min = int(parts[0])
    take the first element in that list and convert it to an integer, save it in variable min
    if min > 60:
    if min greater than 60 we need to do something, if not else branch gets executed
    real_minute = min % 60
    real_minute will get the remainder of division by 60
    hours = min / 60
    hours the result of division by 60
    return '{:02}:{:02}:{}'.format(hours, real_minute, parts[1])
    return the new string.
    :02 means that we want to have it in two digit format 1->01 but 10->10 …
    That’s it.



  • @Eko-palypse said:

    That’s it.

    Well…you didn’t explain the else part, and that’s where I have a question. I would do it this way and I don’t know why you did it differently:

    return '{}:{}'.format(*parts)

    I just had the thought that it would be nice if editor.rereplace() would take a replace function returning None as a signal to not do any replacement.



  • you are absolutely correct - this survived the test when having hh::mm::ss::msec :-)
    But python slicing is rescuing me :-D

    I don’t understand the none return - I mean, why would you want to replace something with None
    where None means don’t replace anything? Which case do you have in mind?



  • @Eko-palypse

    Maybe I misunderstood the intent of the return line I called out. I was thinking that it is simply putting back together the original text but I didn’t look at the OP’s data or problem description all that closely. I guess you would have returned m.group(0) if that were the case.

    Anyway, a replace function could have some logic that in certain cases it would not change the original text. Returning None (or even, gasp, falling off the end of the function without returning anything) could be that signal. I certainly did not try editor.rereplace() in that manner, maybe it already works that way.


Log in to reply