ClockToSeconds ?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • RizlaUK
    Indigo Rose Customer
    • May 2006
    • 5552

    ClockToSeconds ?

    ok, been playing with a project and i need to take a clock value "HH:MM:SS" and convert it back to the number of seconds passed since "00:00:00"

    before i go writeing a over complicated function, is there a one line lua trick (im thinking string.format but i just dont know how)

    any ideas ?
    Embrace change in your life, you never know, it could all work out for the best
  • Imagine Programming
    Indigo Rose Customer
    • Apr 2007
    • 4252

    #2
    It's not one line, but i guess it will work though

    Code:
    function string.mid(str, nStart, nNumOfChars)
    	if(type(str)~="string")then error("Argument 1, string expected!",2);return -1;end
    	if(type(nStart)~="number")then error("Argument 2, number expected!",2);return -1;end
    	if(type(nNumOfChars)~="number")then error("Argument 3, number expected!",2);return -1;end
    	
    	local nEnd=(nStart+nNumOfChars)-1
    	if(nNumOfChars==-1)then
    		nEnd=-1;
    	end
    	return string.sub(str, nStart, nEnd);
    end
    function string.timetoseconds(strTime)
        local Hour = string.mid(strTime, 1, 2);
        local Minute = string.mid(strTime, 4, 2);
        local Second = string.mid(strTime, 7, 2);
        return ((tonumber(Hour)*(60*60))+(tonumber(Minute)*60)+tonumber(Second))
    end
    print(tostring(string.timetoseconds("12:06:26")));
    Bas Groothedde
    Imagine Programming :: Blog

    AMS8 Plugins
    IMXLH Compiler

    Comment

    • RizlaUK
      Indigo Rose Customer
      • May 2006
      • 5552

      #3
      cool, a pure lua reply too

      thanks CB :yes
      Embrace change in your life, you never know, it could all work out for the best

      Comment

      • Imagine Programming
        Indigo Rose Customer
        • Apr 2007
        • 4252

        #4
        Originally posted by RizlaUK View Post
        cool, a pure lua reply too

        thanks CB :yes
        Np, always happy to help btw, check inbox
        Bas Groothedde
        Imagine Programming :: Blog

        AMS8 Plugins
        IMXLH Compiler

        Comment

        • Dermot
          Indigo Rose Customer
          • Apr 2004
          • 1791

          #5
          Since AMS as a Mid function you only need
          Code:
          function string.timetoseconds(strTime)
              local Hour = String.ToNumber(String.Mid(strTime, 1, 2));
              local Minute = String.ToNumber(String.Mid(strTime, 4, 2));
              local Second = String.ToNumber(String.Mid(strTime, 7, 2));
              return ((Hour*(60*60))+(Minute*60)+Second)
          end
          Dermot

          I am so out of here :yes

          Comment

          • Imagine Programming
            Indigo Rose Customer
            • Apr 2007
            • 4252

            #6
            I figured Rizla likes plain lua, so i placed the string.mid function in it
            Bas Groothedde
            Imagine Programming :: Blog

            AMS8 Plugins
            IMXLH Compiler

            Comment

            • Mikhail
              Forum Member
              • Aug 2004
              • 62

              #7
              Originally posted by RizlaUK View Post
              ok, been playing with a project and i need to take a clock value "HH:MM:SS" and convert it back to the number of seconds passed since "00:00:00"

              before i go writing a over complicated function, is there a one line lua trick (im thinking string.format but i just don't know how)

              any ideas ?
              A simple solution:

              Code:
              
              n_unixtime_start = os.time();
              n_unixtime_base = os.time{year=1970, month=1, day=1, hour=0};
              
              Application.Sleep(3000); -- put something to do here :-)
              
              n_unixtime_end = os.time();
              n_unixtime_diff = os.difftime(n_unixtime_end,n_unixtime_start) + n_unixtime_base;
              
              Dialog.Message("Notice", os.date("%X", n_unixtime_diff), MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);

              Comment

              • RizlaUK
                Indigo Rose Customer
                • May 2006
                • 5552

                #8
                A simple solution:
                its not for time/date, its for audio timing, but thanks anyway :yes


                I figured Rizla likes plain lua, so i placed the string.mid function in it
                that i do, i have been working on a lua 5.1 project with CB and i asked for "lua tips"

                btw CB, is that function already in the includes ? , if so then i had it on my hdd all along ... doh!
                Embrace change in your life, you never know, it could all work out for the best

                Comment

                • Imagine Programming
                  Indigo Rose Customer
                  • Apr 2007
                  • 4252

                  #9
                  The string.mid function was, but the string.timetoseconds wasn't lol. I guess you can add it now
                  Bas Groothedde
                  Imagine Programming :: Blog

                  AMS8 Plugins
                  IMXLH Compiler

                  Comment

                  Working...
                  X