[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Plugins
. . -> [Subject]  Simple Time Code
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Simple Time Code

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


Posted by Neves   USA  (78 posts)  [Biography] 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?)
[Go to top] top

Posted by Bast   (77 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,417 posts)  [Biography] 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
[Go to top] 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.


3,263 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]