Better $duration

By LIQUID_NiTrO on Dec 30, 2006

This snippet does what $duration should do and converts a string like 2w4d10h3m2s into seconds. Usage is $lduration(string) where string should be formatted as above. Accepted time measures are the same as for $duration and include w, d, h, m, and s. Poorly formatted strings, such as 5m2h4m, are okay (the previous example would be evaluated the same as 2h9m).

alias lduration {
  if ( $regex($1,/[^wdhms\d]/) || $regex($1,/[wdhms]{2}/) || $1 isnum || $1 isalpha || $right($1,1) isnum ) {
    return $null
  }
  var %str = $1,%tot = 0,%x = 1
  while ( %str ) {
    .echo -q $regsub(%x,%str,/(\d+[a-z])/i,$null,%str)
    inc %tot $duration($regml(%x,1))
    inc %x
  }
  return %tot
}

Comments

Sign in to comment.
Yoinx   -  Dec 31, 2006

It can, however, that would only work in 6.2 ^ I believe.

As it is, this should work from at aleast 6.16 and up.

 Respond  
Cherish   -  Dec 31, 2006

.echo -q can be replaced with .noop

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.