mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Aug 2004
Posts: 7,252
R
RusselB Offline OP
Hoopy frood
OP Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
I'm looking for a way (preferably easy) to turn a time, as reported in seconds, to one of the following:
1) If the time is less than 1 day, use HH:nn:ss format
1a) I realize that this is easily done with $duration
2) If the time is greater than one day, return A weeks B days HH:nn:ss

$duration(<time>) returns Aweeks Bdays Chrs Dmins Esecs
unless the numeric is 0, in which case the associated wording also isn't displayed.

I hope this makes sense to more than just me, but if it doesn't, let me know and I'll try to clarify.

P.S.: This is intended to be used as an alias that will return the value to a dialog.

Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
weeks, days, hours, minutes, seconds are all known constants. You just need to create an alias that sequentially counts how many of each time-period are in your provided information.

Code:

alias wdur {
  var %in = $1
  if (%in !isnum 0-) return 0 0 0 0 0

  var %w = $floor($calc(%in / (60 * 60 * 24 * 7)))
  %in = $calc(%in - (%w * 60 * 60 * 24 * 7))

  var %d = $floor($calc(%in / (60 * 60 * 24)))
  %in = $calc(%in - (%d * 60 * 60 * 24))

  var %h = $floor($calc(%in / (60 * 60)))
  %in = $calc(%in - (%h * 60 * 60))

  var %m = $floor($calc(%in / 60))
  %in = $calc(%in - (%m * 60))

  var %s = %in

  return %w %d %h %m %s
}



-genius_at_work

Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
Just another approach... it uses the formatting of $duration for the weeks/days part (which omits 0 wk(s) or 0 day(s) and puts singular/plural abbreviations respectively)
Code:
alias wtime {

  if ($$1 < $duration(1day)) return $duration($1,3)

  tokenize 58 $duration($1,3)
  ; $2=nn and $3=ss are fine; calculate HH out of $1
  var %hh = $base($calc($1 % 24),10,10,2)
  ; get full-written weeks/days/hours string, remove the hours-part from it (if any)
  var %wdh = $duration($duration($1 h)), %wd = $iif(%hh,$gettok(%wdh,1--2,32),%wdh)
  return %wd $+(%hh,:,$2,:,$3)
}



Link Copied to Clipboard