View Full Version : LUA - minutes and seconds to seconds only?
Mastiff
January 31st, 2005, 12:44 PM
For a small command I need to convert minutes and seconds to just seconds. The format is 12:55 (minutes:seconds). I tried to search around here, but didn't find anything.
Promixis
January 31st, 2005, 01:21 PM
s = strsub (time,1,2) * 60 + strsub (time,4,1)
Mastiff
January 31st, 2005, 02:05 PM
So what am I doing wrong? ElapsedMinZ0 = 1:20
s = strsub (ElapsedMinZ0,1,2) * 60 + strsub (ElapsedMinZ0,4,1)
print(s)
Error message:
error: attempt to perform arithmetic on a string value
stack traceback:
1: main of string "?" at line 2
danward79
January 31st, 2005, 03:15 PM
Couple of reasons
first
ElapsedMinZ0 = "01:20"
second
s = tonumber(strsub (ElapsedMinZ0,1,2)) * 60 + tonumber(strsub (ElapsedMinZ0,4,5))
ElapsedMinZ0 = "01:20"
s = tonumber(strsub (ElapsedMinZ0,1,2)) * 60 + tonumber(strsub (ElapsedMinZ0,4,5))
print(s)
Rob H
January 31st, 2005, 03:16 PM
ElapsedMinZ0 = "01:20"
s = strsub (ElapsedMinZ0,1,2) * 60 + strsub (ElapsedMinZ0,4,1)
print(s)
if that doesn't work then try
ElapsedMinZ0 = "01:20"!
s = tonumber(strsub (ElapsedMinZ0,1,2)) * 60 + tonumber(strsub (ElapsedMinZ0,4,1))
print(s)
Rob H
January 31st, 2005, 03:19 PM
Dan beat me to it!
If the format of 1:20 is permitted then you're better off using a pattern
local hours, mins = strfind(ElapsedMinZ0, "(%d+):(%d+)")
Mastiff
January 31st, 2005, 03:19 PM
Hmmm... The first one gave me the same result, the second one gave me: error: attempt to perform arithmetic on a nil value
stack traceback:
1: main of string "?" at line 3
danward79
January 31st, 2005, 03:20 PM
That way is not very robust
you have to have your numbers in a set pattern
i.e. 01:20 for above, however if you had 1:20, you would get an error.
ideally you should use captures to do this.
Mastiff
January 31st, 2005, 03:20 PM
I don't think the format is permitted, but that's the way I get it. And I can't change it to 1.20, that would of course be wrong.
Mastiff
January 31st, 2005, 03:22 PM
I'll just go to bed now... I'm not feeling well (slight touch of the flu), and you guys are confusing me with your LUA war here! :lol: I'll check in tomorrow morning to see if you have agreed on something... :wink:
In case I have been unclear, I need to convert 1:20 to 80 seconds, 10:30 to 630 seconds and so on.
danward79
January 31st, 2005, 03:23 PM
Dan beat me to it!
If the format of 1:20 is permitted then you're better off using a pattern
local hours, mins = strfind(ElapsedMinZ0, "(%d+):(%d+)")
I would have finished the job, but I had a crying baby to sort out! Thanks Rob!
danward79
January 31st, 2005, 03:27 PM
Here you go
ElapsedMinZ0 = "1:20"
local _,_,mins, seconds = strfind(ElapsedMinZ0, "(%d+):(%d+)")
s = tonumber(mins) * 60 + tonumber(seconds)
print(s)
Rob H
January 31st, 2005, 03:50 PM
That should work.
danward79
January 31st, 2005, 03:58 PM
LOL
Mastiff
February 1st, 2005, 02:01 AM
I'm glad you finally agreed! :roll: Thanks a lot, guys! :D
So with Mike's addition in my other LUA thread from yesterday, the full script is:
r,ElapsedMinRegZ0 = RegGetStringValue("HKEY_LOCAL_MACHINE", "Software\\PlayList","Zone 0 elapsed")
WindowHandle = FindWindow ("MJFrame",nil)
print(WindowHandle)
result = PostMessage ( WindowHandle, 33768, 10017, 16777216)
result = PostMessage ( WindowHandle, 33768, 10000, 16777216)
TIME_Sleep(500)
local _,_,mins, seconds = strfind(ElapsedMinRegZ0, "(%d+):(%d+)")
s = tonumber(mins) * 60 + tonumber(seconds)
print(s)
for x = 1,s/5 do
--print("hi")
result = PostMessage ( WindowHandle, 33768, 10008, 16777216)
TIME_Sleep(100)
end
result = PostMessage ( WindowHandle, 33768, 10017, 16777216)
s = nil
WindowHandle = nil
The ElapsedMinZ0Reg variable comes from ElapsedMinZ0 that DVD sets, via a windows registry setting. If I used only ElapsedMinZ0 that would be messed up when DVDSpy started setting the seconds on playback.
So what does it all do? Very simple, it's for my carputer system. It has annoyed me (and weakened the WAF) that when you hibernate with JRMC you can't just let it hibernate and then let it keep playing. It will far to often stop on "Buffering" when it resumes, and playback doesn't start again. So you'll have to listen to the song from the beginning, and especially with audio books that can be a looong time. Which is why the last command sets the ElapsedMinZ0 every second (I may adjust it to every five or ten seconds, it depends on the CPU load this causes). On hibernation I have a command that save ElapsedMinZ0 to a Windows registry and on resume I call the main script. It will first start the playback (the first windows mesage), and for every one of those repeated windows messages JRMC fast forwards 5 seconds, so calling it 60 seconds /5 makes it jump one minute. It happens almost instantly. Now the only thing missing is that MMcM makes DVDSpy multi-zone aware, so I can do the same for all zones (which is why I use Z0 in this one, that makes it easy to add more zones). I added the windows message 10017, toggle mute, before starting playback and after finishing fast forward since it sounded a bit funky jumping around...
Oh, and on hibernation this is what gets called:
r = RegSetStringValue ("HKEY_LOCAL_MACHINE", "Software\\PlayList","Zone 0 elapsed", ElapsedMinZ0 );
Thanks again! Will this have to be refactored? :lol:
danward79
February 1st, 2005, 03:28 AM
Glad you are happy now!
Is there no way of sending JRMC a time to goto directly? i.e. straight to 1 minute.
Mastiff
February 1st, 2005, 03:58 AM
Not as far as I know. I wish there was. I have asked for a resume function, but I think it's a bit to personal (meaning that nobody else needs or wants it...) to put in.
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.