• Login
Community
  • Login

How to change/convert the format of a timestamp?

Scheduled Pinned Locked Moved General Discussion
38 Posts 6 Posters 11.4k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P
    PeterJones
    last edited by Jan 25, 2019, 8:22 PM

    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

    1 Reply Last reply Reply Quote 1
    • E
      Eko palypse @Dana Wright
      last edited by Jan 25, 2019, 8:24 PM

      @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?

      1 Reply Last reply Reply Quote 0
      • D
        Dana Wright
        last edited by Jan 25, 2019, 8:30 PM

        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

        1 Reply Last reply Reply Quote 1
        • E
          Eko palypse @Dana Wright
          last edited by Jan 25, 2019, 8:30 PM

          @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)
          
          1 Reply Last reply Reply Quote 4
          • D
            Dana Wright
            last edited by Jan 25, 2019, 8:34 PM

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

            1 Reply Last reply Reply Quote 1
            • D
              Dana Wright
              last edited by Jan 25, 2019, 8:48 PM

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

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

              E M 2 Replies Last reply Jan 25, 2019, 8:57 PM Reply Quote 3
              • E
                Eko palypse @Dana Wright
                last edited by Eko palypse Jan 25, 2019, 8:58 PM Jan 25, 2019, 8:57 PM

                @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.

                A 1 Reply Last reply Jan 25, 2019, 9:08 PM Reply Quote 2
                • A
                  Alan Kilborn @Eko palypse
                  last edited by Jan 25, 2019, 9:08 PM

                  @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.

                  1 Reply Last reply Reply Quote 1
                  • E
                    Eko palypse
                    last edited by Jan 25, 2019, 9:14 PM

                    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?

                    A 1 Reply Last reply Jan 25, 2019, 9:31 PM Reply Quote 0
                    • A
                      Alan Kilborn @Eko palypse
                      last edited by Alan Kilborn Jan 25, 2019, 9:32 PM Jan 25, 2019, 9:31 PM

                      @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.

                      E 1 Reply Last reply Jan 25, 2019, 9:38 PM Reply Quote 0
                      • E
                        Eko palypse @Alan Kilborn
                        last edited by Jan 25, 2019, 9:38 PM

                        @Alan-Kilborn

                        actually I’m trying to avoid function lookups as those are expensive, especially when it involves
                        Python->C->Python conversion. But I must admit, in this case I don’t think that I gain any performance improvement, it might be even slower. Let’s test it. Will come back.

                        A 1 Reply Last reply Jan 25, 2019, 9:41 PM Reply Quote 1
                        • A
                          Alan Kilborn @Eko palypse
                          last edited by Jan 25, 2019, 9:41 PM

                          @Eko-palypse

                          Okay…so I didn’t follow any of that, but I look forward to the come back. :)

                          E 1 Reply Last reply Jan 25, 2019, 9:45 PM Reply Quote 0
                          • E
                            Eko palypse @Alan Kilborn
                            last edited by Jan 25, 2019, 9:45 PM

                            @Alan-Kilborn

                            so it is still a little bit faster - to be honest, haven’t expected it.

                            looped 1000 times over the same text
                            16.7720000744 <-- return m.group(0)
                            16.6819999218 <- return ‘{}:{}’.format(*parts[:])

                            1 Reply Last reply Reply Quote 1
                            • P
                              PeterJones
                              last edited by Jan 25, 2019, 9:45 PM

                              My two comments would be:

                              1. if min > 60: = if the data is 60:00.000, it wouldn’t change. Make it if min >=60:
                              2. If the OP (or someone else) has mixed data, or had partially changed them, and came back later and tried the same script, weird stuff will happen. I’d recommend: editor.rereplace('(?<![:\d])\d+:\d+\.\d+',change_format), which adds a negative lookbehind to not match if there’s a colon or another digit before the \d+
                              E 1 Reply Last reply Jan 25, 2019, 9:48 PM Reply Quote 1
                              • E
                                Eko palypse @PeterJones
                                last edited by Jan 25, 2019, 9:48 PM

                                @PeterJones

                                YES - this is a bug it should > 59 - omg.
                                About mixed data you are right but this is always the question what if it looked like
                                hh.mm.ss.msec …

                                M 1 Reply Last reply Jan 25, 2019, 9:56 PM Reply Quote 1
                                • P
                                  PeterJones
                                  last edited by Jan 25, 2019, 9:54 PM

                                  @Eko-palypse ,

                                  Indeed, there are always more formats that might exist. I’ve only seen colon-separated in .srt files, so I think that keeping it generic enough that it won’t mess up an existing .srt, even if it does have some with hours and some without.

                                  BTW: I had forgotten why I included the [:\d] rather than just : in my negative lookbehind: without the \d in the character class, 1:15:00.000 (which shouldn’t match) would partially match on 5:00.000, which would be even worse.

                                  And running a test with 1:15:00.000, even with your simpler expression, works correctly (ie, doesn’t try to change it) – ahh, that’s because the minutes are less than 60. I guess unless there’s a strange 1:65:00.000, yours won’t be a problem. I guess yours is generic enough.

                                  1 Reply Last reply Reply Quote 2
                                  • M
                                    Meta Chuh moderator @Eko palypse
                                    last edited by Jan 25, 2019, 9:56 PM

                                    @Eko-palypse

                                    I’m trying to avoid function lookups as those are expensive …

                                    yes, i’m a bit short on money too at the moment … and don’t even dare to give me an (s.h) for this comment 😉

                                    E 1 Reply Last reply Jan 25, 2019, 10:04 PM Reply Quote 1
                                    • E
                                      Eko palypse @Meta Chuh
                                      last edited by Jan 25, 2019, 10:04 PM

                                      @Meta-Chuh

                                      :-D - always reminds me of this

                                      M 1 Reply Last reply Jan 25, 2019, 10:07 PM Reply Quote 1
                                      • M
                                        Meta Chuh moderator @Eko palypse
                                        last edited by Jan 25, 2019, 10:07 PM

                                        @Eko-palypse

                                        singing: ahaaaa, ahahahaaa … all the things i could do … ;-)

                                        E 1 Reply Last reply Jan 25, 2019, 10:13 PM Reply Quote 1
                                        • E
                                          Eko palypse @Meta Chuh
                                          last edited by Jan 25, 2019, 10:13 PM

                                          @Meta-Chuh

                                          I don’t understand all of this but what I got makes me laughing … :-D

                                          M 1 Reply Last reply Jan 25, 2019, 10:27 PM Reply Quote 1
                                          21 out of 38
                                          • First post
                                            21/38
                                            Last post
                                          The Community of users of the Notepad++ text editor.
                                          Powered by NodeBB | Contributors