Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Plugins ➜ Simple Time Code

Simple Time Code

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Neves   USA  (78 posts)  Bio
Date Wed 23 Feb 2011 04:49 PM (UTC)
Message
I'm sure this is simple, yet it isn't working, so I am wondering if someone can help me. I want to convert a number which represents the amount of time connected in seconds to be in days, hours, minutes, seconds for a plugin in Lua script.
Assuming the variable is called time_secs and I want the final string to be called time_string, how would it be done?
One extra requirement if possible is that it will only display the time in hrs or days if it is over 1 hr or 1 day.

I was using:
time_hrs = time_secs%3600
time_mins = time_secs%60
but it didn't seem to work write.

Thanks,
Neves
(also I don't get e-mail replies to these posts, yet my e-mail is working, can that be reset?)
Top

Posted by Bast   (78 posts)  Bio
Date Reply #1 on Wed 23 Feb 2011 05:16 PM (UTC)
Message
this returns a string in the format h:m:s

function SecondsToClock(sSeconds)
  local nSeconds = tonumber(sSeconds)
  if nSeconds == 0 or nSeconds == nil then
    --return nil;
    return "00:00:00";
  elseif nSeconds < 0 then
    return tostring(sSeconds)
  else
    nHours = string.format("%02.f", math.floor(nSeconds/3600));
    nMins = string.format("%02.f", math.floor(nSeconds/60 - (nHours*60)));
    nSecs = string.format("%02.f", math.floor(nSeconds - nHours*3600 - nMins *60));
    if nHours ~= "00" then
      return nHours..":"..nMins..":"..nSecs
    else
      return nMins..":"..nSecs
    end
  end
end

or this returns the numbers and then you can put them in a string however you want to (based on a function from Nick)

function SecondsToDHMS(sSeconds)
  local nSeconds = tonumber(sSeconds)
  if nSeconds == 0 then
    return 0, 0, 0, 0
  else
    nDays = math.floor(nSeconds/(3600 * 24))
    nHours = math.floor(nSeconds/3600 - (nDays * 24))
    nMins = math.floor(nSeconds/60 - (nHours * 60) - (nDays * 24 * 60))
    nSecs = sSeconds % 60
    return nDays, nHours, nMins, nSecs
  end
end


Bast

Bast

Scripts: http://github.com/endavis
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #2 on Thu 24 Feb 2011 01:10 AM (UTC)
Message
In commas.lua module is a function to convert seconds to roughly what you want.

I reset your email flag.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


13,114 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.