How to change/convert the format of a timestamp?
-
@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 returningNone
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 :-DI 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? -
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.
-
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. -
Okay…so I didn’t follow any of that, but I look forward to the come back. :)
-
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[:]) -
My two comments would be:
if min > 60:
= if the data is60:00.000
, it wouldn’t change. Make itif min >=60:
- 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+
-
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 … -
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 on5: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 strange1:65:00.000
, yours won’t be a problem. I guess yours is generic enough. -
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 😉
-
:-D - always reminds me of this
-
singing: ahaaaa, ahahahaaa … all the things i could do … ;-)
-
I don’t understand all of this but what I got makes me laughing … :-D
-
@Eko-palypse
i also didn’t understand many of weird al yankovic’s insider jokes, but he made a lot of 80’s songs parodies, a funny one was “fat”, a parody of michael jacksons “bad” … or at least it used to be funny to me when i was a kid ;-) -
btw: my apologies to you @Dana-Wright if you had to read everything after your “Worked like a charm! Thank you very much!” and eko’s explanation.
sometimes (but very few) we tend to have a little “after work chat” between regulars in public, which can be a bit off topic from time to time. i hope you didn’t mind.
-
one more song and then it’s enough for today:
>>> here’s a song <<< for @Scott-Sumner 😪😉😂
-
As valuable as Scott’s (and Claudia’s) posts were, we have some really good new posters about scripting (example Eko, and Peter is developing as a Python person), so let’s not be too sad if they decide not to return.
-
Hello, @dana-wright, @eko-palypse, @alan-kilborn, @meta-chuh, @peterjones and All,
Just a bit late, but here are two regexes S/R which could achieve the goal !
Note that, regarding the initial timestamps, I will use the convention
[M]MM:SS.mmm
, where :-
[M]MM
represents the number of minutes, from00
to119
/179
, with two or three digits -
SS
represents the number of seconds, from00
to59
, with two digits -
mmm
represents the number of milliseconds, from000
to999
, with three digits
Case A) If your file contains timestamps syntaxes, from
00:00.000
to119:59.999
, only ( so0 <[M]MM < 2 hours
) , one solution could be :-
SEARCH A
(?<!:)(?:([0-5])|(6)|(7)|(8)|(9)|(10)|(11))(\d:\d{2}\.\d{3})(?=\s)
-
REPLACE A
(?{1}00:01):(?1\1)(?{2}0)(?{3}1)(?{4}2)(?{5}3)(?{6}4)(?{7}5)\8
Case B) If your file contains timestamps syntaxes, from
00:00.000
to179:59.999
, only ( so0 < [M]MM < 3 hours
), a longer S/R is :-
SEARCH B
(?<!:)(?:([0-5])|((6)|(7)|(8)|(9)|(10)|(11))|((12)|(13)|(14)|(15)|(16)|(17)))(\d:\d{2}\.\d{3})(?=\s)
-
REPLACE B
(?{1}00)(?{2}01)(?{9}02):(?1\1)(?{3}0)(?{4}1)(?{5}2)(?{6}3)(?{7}4)(?{8}5)(?{10}0)(?{11}1)(?{12}2)(?{13}3)(?{14}4)(?{15}5)$16
As usual :
-
Check the
Wrap around
option -
Select the
Regular expression
search mode -
Click on the
Replace All
button
Best Regards
guy038
P. S.
For instance :
- With the regexes A, the initial text, below :
00:00.000 23:52.984 39:43.529 59:59.999 60:00.000 78:08.168 91:38.524 103:05.216 111:41.465 119:59.999
becomes :
00:00:00.000 00:23:52.984 00:39:43.529 00:59:59.999 01:00:00.000 01:18:08.168 01:31:38.524 01:43:05.216 01:51:41.465 01:59:59.999
- With the regexes B, the following text :
00:00.000 23:52.984 39:43.529 59:59.999 60:00.000 78:08.168 91:38.524 103:05.216 111:41.465 119:59.999 120:00.000 147:33.150 160:00.058 179:59.999
becomes :
00:00:00.000 00:23:52.984 00:39:43.529 00:59:59.999 01:00:00.000 01:18:08.168 01:31:38.524 01:43:05.216 01:51:41.465 01:59:59.999 02:00:00.000 02:27:33.150 02:40:00.058 02:59:59.999
-
-
@guy038
it’s never too late, if people care … and thankfully many do 😃
thumbs up 👍 -
Thank you, @guy038. I had been trying the notation similar to
(?(1)00:01)
in the replace, rather than(?{1}00:01)
, which is why I wasn’t able to get the conditional to work right.