mIRC Home    About    Download    Register    News    Help

Print Thread
#95845 27/08/04 06:25 PM
Joined: Jun 2004
Posts: 16
T
turboke Offline OP
Pikka bird
OP Offline
Pikka bird
T
Joined: Jun 2004
Posts: 16
Is there a way to display the result of a $duration (calculated by substracting two $ctime() functions of different dates) in a custom format? Like, choosing if you want weeks/days/hours or days/hours (2wks 2days 3hrs versus 16days 3hrs).

I tried searching the forum and the web, but I always end up being pointed to $asctime and my $duration hasn't elapsed since Jan. 1 1970.

Joined: Jun 2004
Posts: 87
B
Babel fish
Offline
Babel fish
B
Joined: Jun 2004
Posts: 87
type /help $calc for the calculator and type /help $duration

Joined: Jun 2004
Posts: 16
T
turboke Offline OP
Pikka bird
OP Offline
Pikka bird
T
Joined: Jun 2004
Posts: 16
$duration(seconds,N)
Returns the specified number of seconds in a week/day/hour/minute/second format.

The N parameter is optional. If N = 2, the result does not include the seconds value. If N = 3, the result is in h:m:s format.

Note: This identifier can also take its own output and change it back into seconds.

So it has 3 predefined formats, but no custom ones. The output in seconds is of no use, unless I write an algorithm to interpret it. But I want to use it with a /timer, so I prefer something that can be done in a single line without involving aliases and such...

Joined: Nov 2003
Posts: 257
A
Fjord artisan
Offline
Fjord artisan
A
Joined: Nov 2003
Posts: 257
you can try this

alias dur {
if ($1 < 60) { .return $round($1-,0) $+ s }
else { return $replace($duration($1-),wk,w,secs,s,mins,m,hrs,h,days,d,wks,w,sec,s,min,m,hr,h,day,d) }
}

Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Code:
alias fmtdur {
  ; Usage: $fmtdur(secs [, format])
  ; Formatting: [y year] [m month] [w week] [d day] [h hour] [n minute] [s second] [u units]
  ; Using the same character consecutively will zeropad that unit to the given length (ie. n = 7, nnn = 007)
  ; Doubling the u character will cause it to use the full format (ie. nu = 6mins, nuu = 6minutes)
  ; Uppercase U characters will capitalize units.
  var %fmt = $iif($2,$2,wu du hu nu su), %secs = $int($1), %count = $regex(fmtdur, %fmt, /(([^ymwdhns]*)(y+|m+|w+|d+|h+|n+|s+)([^ymwdhnsUu]*)([Uu]*))/g)
  var %yrs, %mnths, %wks, %days, %hrs, %mins, %i = 1, %prespace, %fmtchars, %postspace, %unitchars, %durstr, %amount, %units
  if (y isincs %fmt) { %yrs = $int($calc(%secs / 31557600)) | %secs = $calc(%secs % 31557600) }
  if (m isincs %fmt) { %mnths = $int($calc(%secs / 2629800)) | %secs = $calc(%secs % 2629800) }
  if (w isincs %fmt) { %wks = $int($calc(%secs / 604800)) | %secs = $calc(%secs % 604800) }
  if (d isincs %fmt) { %days = $int($calc(%secs / 86400)) | %secs = $calc(%secs % 86400) }
  if (h isincs %fmt) { %hrs = $int($calc(%secs / 3000)) | %secs = $calc(%secs % 3600) }
  if (n isincs %fmt) { %mins = $int($calc(%secs / 60)) | %secs = $calc(%secs % 60) }
  while %i &lt;= $calc(%count * 5) {
    %fmtchars = $regml(fmtdur, $calc(%i + 2))
    %unitchars = $regml(fmtdur, $calc(%i + 4))
    %amount = $charpad($replace($left(%fmtchars, 1),y,%yrs,m,%mnths,w,%wks,d,%days,h,%hrs,n,%mins,s,%secs), $len(%fmtchars), 0)
    if (u isin %unitchars) {
      if ($len(%unitchars) == 1) %units = $replacex($left(%fmtchars,1),y,yr,m,mth,w,wk,d,dy,h,hr,n,min,s,sec)
      else %units = $replacex($left(%fmtchars,1),y,year,m,month,w,week,d,day,h,hour,n,minute,s,second)
      if (%amount != 1) %units = $+(%units, s)
      if (U isincs %unitchars) %units = $+($upper($left(%units,1)), $right(%units,-1))
    }
    else %units = $null
    %durstr = $+(%durstr, $regml(fmtdur, $calc(%i + 1)), %amount, $regml(fmtdur, $calc(%i + 3)), %units))
    inc %i 5
  }
  return %durstr
}
alias charpad {
  ; Usage: $charpad(string, padwidth, character)
  return $iif($len($1) &lt; $int($2), $+($str($left($$3,1),$calc($int($2) - $ifmatch)),$1), $1)
}


A few examples:
$fmtdur(253, hu nu su) returns 0hrs 4mins 13secs
$fmtdur(253, hh uu nu suu) returns 00 hours 4mins 13seconds
$fmtdur(2352527253, yuu muu wu hu nu su) returns 74years 6months 2wks 92hrs 47mins 33secs
$fmtdur(2352527253, yuu wu du hu/nu/su) returns 74years 28wks 3dys 23hrs/47mins/33secs

I know of at least one bug already: It will remove trailing spaces if a u isn't used for any given setting (ie. $fmtdur(121, n su) will return 21sec instead of 2 1sec), I'll see about fixing that tomorrow, it's 2am here.


Spelling mistakes, grammatical errors, and stupid comments are intentional.
Joined: Jun 2004
Posts: 16
T
turboke Offline OP
Pikka bird
OP Offline
Pikka bird
T
Joined: Jun 2004
Posts: 16
Thanks a lot. At the moment the bug isn't really a problem for me. The alias works just fine for what I need right now.

I would've figured something out myself if I had more time, but if I had more time I wouldn't be bound by a timer anyway ;-).

Joined: Dec 2002
Posts: 2,962
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,962
Here's the version with the bugfix.

Code:
alias fmtdur {
  ; Usage: $fmtdur(secs [, format])
  ; Formatting: [y year] [m month] [w week] [d day] [h hour] [n minute] [s second] [u units]
  ; Using the same character consecutively will zeropad that unit to the given length (ie. n = 7, nnn = 007)
  ; Doubling the u character will cause it to use the full format (ie. nu = 6mins, nuu = 6minutes)
  ; Uppercase U characters will capitalize units.
  var %fmt = $iif($2,$2,wu du hu nu su), %secs = $int($1), %count = $regex(fmtdur, %fmt, /(([^ymwdhns]*)(y+|m+|w+|d+|h+|n+|s+)([^ymwdhnsUu]*)([Uu]*))/g)
  var %yrs, %mnths, %wks, %days, %hrs, %mins, %i = 1, %fmtchars, %unitchars, %durstr, %amount, %units
  if (y isincs %fmt) { %yrs = $int($calc(%secs / 31557600)) | %secs = $calc(%secs % 31557600) }
  if (m isincs %fmt) { %mnths = $int($calc(%secs / 2629800)) | %secs = $calc(%secs % 2629800) }
  if (w isincs %fmt) { %wks = $int($calc(%secs / 604800)) | %secs = $calc(%secs % 604800) }
  if (d isincs %fmt) { %days = $int($calc(%secs / 86400)) | %secs = $calc(%secs % 86400) }
  if (h isincs %fmt) { %hrs = $int($calc(%secs / 3600)) | %secs = $calc(%secs % 3600) }
  if (n isincs %fmt) { %mins = $int($calc(%secs / 60)) | %secs = $calc(%secs % 60) }
  while %i &lt;= $calc(%count * 5) {
    %fmtchars = $regml(fmtdur, $calc(%i + 2))
    %unitchars = $regml(fmtdur, $calc(%i + 4))
    %amount = $charpad($replace($left(%fmtchars, 1),y,%yrs,m,%mnths,w,%wks,d,%days,h,%hrs,n,%mins,s,%secs), $len(%fmtchars), 0)
    if (u isin %unitchars) {
      if ($len(%unitchars) == 1) %units = $replacex($left(%fmtchars,1),y,yr,m,mth,w,wk,d,dy,h,hr,n,min,s,sec)
      else %units = $replacex($left(%fmtchars,1),y,year,m,month,w,week,d,day,h,hour,n,minute,s,second)
      if (%amount != 1) %units = $+(%units, s)
      if (U isincs %unitchars) %units = $+($upper($left(%units,1)), $right(%units,-1))
    }
    else %units = $null
    %durstr = $+($left(%durstr, -1), $regml(fmtdur, $calc(%i + 1)), %amount, $regml(fmtdur, $calc(%i + 3)), %units, x))
    inc %i 5
  }
  return $left(%durstr, -1)
}
alias charpad {
  ; Usage: $charpad(string, padwidth, character)
  return $iif($len($1) &lt; $int($2), $+($str($left($$3,1),$calc($int($2) - $ifmatch)),$1), $1)
}


Edit (5 minutes later): Just fixed one more bug and cleaned up a tiny bit of code.


Spelling mistakes, grammatical errors, and stupid comments are intentional.

Link Copied to Clipboard