mIRC Home    About    Download    Register    News    Help

Print Thread
#178694 13/06/07 07:42 PM
P
pouncer
pouncer
P
can someone give me a snippet to convert seconds to minutes

if the seconds is over 60 seconds then it shud show the minutes like 1 minute, 5 seconds (65 seconds)

if its 43 seconds, then it should just seconds as its below 60

any help guys?

#178702 13/06/07 08:00 PM
S
schaefer31
schaefer31
S
There's already an identifier for this: $duration(t,[n])

t = time in seconds
n is an optional parameter of either '2' or '3'. 2 will not show the seconds if there are any. 3 will give an output in the hh:nn:ss form.

You can use a simple $gettok on the output if you need to do any parsing.

#178737 14/06/07 01:35 AM
Joined: Feb 2005
Posts: 342
R
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
$duration() works for that.

//echo Seconds 90, translates to $duration(90)

Would echo "Seconds 90, translates to 1min 30secs"

Somewhat on topic, and more of a "Just for your information." You could easily script your own, by doing something like this.

Code:
alias sec2dur {
  var %secs = $1
  var %weeks = $int($calc(%secs / $calc(60*60*24*7)))
  var %secs = $calc(%secs % $calc(60*60*24*7))
  var %days = $int($calc(%secs / $calc(60*60*24)))
  var %secs = $calc(%secs % $calc(60*60*24))
  var %hours = $int($calc(%secs / $calc(60*60)))
  var %secs = $calc(%secs % $calc(60*60))
  var %min = $int($calc(%secs / 60))
  var %secs = $calc(%secs % 60)
  echo -a $iif(%weeks,$v1 weeks) $iif(%days,$v1 days) $iif(%hours,$v1 hours) $iif(%min,$v1 minutes) $iif(%secs,$v1 seconds)
}

Rand #178738 14/06/07 01:38 AM
Joined: Dec 2002
Posts: 3,534
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,534
Originally Posted By: Rand
$duration() works for that.


Which has already been suggested, a number of hours ago.

Originally Posted By: Rand

Code:
alias sec2dur {
  var %secs = $1
  var %weeks = $int($calc(%secs / $calc(60*60*24*7)))
  var %secs = $calc(%secs % $calc(60*60*24*7))
  var %days = $int($calc(%secs / $calc(60*60*24)))
  var %secs = $calc(%secs % $calc(60*60*24))
  var %hours = $int($calc(%secs / $calc(60*60)))
  var %secs = $calc(%secs % $calc(60*60))
  var %min = $int($calc(%secs / 60))
  var %secs = $calc(%secs % 60)
  echo -a $iif(%weeks,$v1 weeks) $iif(%days,$v1 days) $iif(%hours,$v1 hours) $iif(%min,$v1 minutes) $iif(%secs,$v1 seconds)
}



Which is unnecessary.

Joined: Feb 2005
Posts: 342
R
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
I was pointing out how he could make one of his own, and yes I know $duration() was already mentioned. I just showed an extra example of it.

Did you overlook the part where I said "Just for your information"? It looks that way. It never hurts for people to see how things are done. So it's not uncalled for, the script isn't even large.

I know, I know, after reading that paragraph you're probably dying to hit reply without reading further to argue the fact that he doesn't need that script and has no use for it. However, that is why I said "Just for your information" previously. I pointed out how it can be scripted. I did not say "Oh look oh look, use this!"

Rand #178740 14/06/07 01:51 AM
Joined: Dec 2002
Posts: 3,534
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,534
I'm just wondering why it was there for an FYI in the first place, if there's already an identifier that does the trick. I know the script isn't large, and I'm not trying to cause any arguments here. I was just confused why you'd have wasted your time posting an alternative (I know as an FYI). Sorry if I offended you in any way.

Joined: Feb 2005
Posts: 342
R
Fjord artisan
Offline
Fjord artisan
R
Joined: Feb 2005
Posts: 342
The answer to that, is in the original post.

"can someone give me a snippet to convert seconds to minutes"

I'm not offended. It just seemed rather pointless for you to post about me posting something that you thought was pointless. When I see people do that, and they have a really large post count, well.... generally I just assume they post just to post. It was rather unnecessary.

Had my post just consisted of the "$duration()" acknowledgement, then I would see a reason. I intentionally included that in the first half of my original post so that people would not jump on the fact that I was supplying the snippet that the person requested. ( If I did that, someone would have just said "Didn't you read the reply above you? $duration()!" )

Not meaning to belittle you, but that's the way it came off to me.

Edit = I should probably be a little more precise. If I supply something when a person asks a question, it's generally so they can see what's going on and how it's done. The script itself is simple math with the useage of modulus. Many people are unaware of the modulus.

Look at the bright side, if this person ever needs to break numbers down based on certain criteria, the snippet I gave addresses the question of "how can I do it?"

Edit2 = Umn, wow. I must have been tired when I wrote that code. I put $calc() inside of $calc() in my code. heh.

Joined: Oct 2005
Posts: 1,671
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,671
A possible use for the custom script could be to customize the words/format that are returned. $duration returns the words in a specific order and with specific words inserted. The custom script could be changed so the output was, for example: "4w3d 5hours 8minutes 6seconds" if the weeks and days were less important than the time or "6seconds 8minutes 5hours" if the user wanted the time in a different order.

-genius_at_work

Joined: Oct 2004
Posts: 8,061
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Oct 2004
Posts: 8,061
It is also useful in that $duration takes a value in seconds. If you have a value in hours, then you'd have to manually calculate it to seconds before using $duration. With the script shown, you could easily convert that to accept all time amounts and convert them automatically to a duration.

Example:
//echo -a $dur(59h125m)

That may not be something you'd see often, but it's a valid code to show how to do it if it were needed.

For larger durations, the identifers $datediff and $DateXpander (not part of mIRC, but separate scripts) handle months and years in the durations. $datediff does somethings more easily, such as countdown timers, though $DateXpander can do that as well, but with more work. And $DateXpander does somethings more accurately, such as working with leap years. So if you are interested in having months/years in your durations and having them be accurate, you may want to consider looking into both of those identifiers to see which does what you need.


Link Copied to Clipboard