procmail
[Top] [All Lists]

a shell or a recursive INCLUDERC?

1998-04-04 14:46:47
I would like to express a number representing seconds of time elapsed,
derived from incoming mail, to days, hours, minutes, and seconds: the raw
number of seconds often has five digits and is rather hard to interpret.

$SHELL is pdksh, so by forcing a shell whenever the elapsed time exceeds 59
seconds, I can use pdksh's built-in integer arithmetic (this code is already
in place and functioning):

  :0 # one day or longer
  * $$ELAPSED^0
  * -86399^0
  { LAPSE=`echo $((ELAPSED/86400))d \
   $(((ELAPSED%86400)/3600))h $(((ELAPSED%3600)/60))m $((ELAPSED%60))s;` }
  :0E # at least one hour but not a full day
  * $$ELAPSED^0
  * -3599^0
  { LAPSE=`echo $((ELAPSED/3600))h $(((ELAPSED%3600)/60))m $((ELAPSED%60))s;` }
  :0E # at least one minute but not a full hour
  * $$ELAPSED^0
  * -59^0
  { LAPSE=`echo $((ELAPSED/60))m $((ELAPSED%60))s;` }
  :0E # less than one minute
  { LAPSE=${ELAPSED}s }

My guess is that despite invoking a shell, that method is more efficient in
most cases than a recursive includerc to subtract 86400, 3600, 600, and 60
repeatedly to get the numbers of whole days, whole hours, tens of minutes,
and remaining minutes (leaving the number of leftover seconds).  Is it?
An untested sample of what the recursive includerc would look like is below.

Or is there some way other than repeated subtraction to code integer division
with procmail scoring?  (Multiplication is possible, but it's ugly.)  Unfor-
tunately, the reciprocals of these divisors are not terminating decimals.
And I don't see a way to utilize the division algorithms in procmailsc's
size tests.
________

   # draft includerc to resolve seconds to d h m s

   SECONDS=$ELAPSED

   :0
   * $ $SECONDS^0
   * -86399^0
   {
    :0
    * $ $=^0
    * -1^0
    { SECONDS = $= }

    :0
    * $ ${DAYS-0}^0
    * 1^0
    { DAYS = $= }

    INCLUDERC = $_
   }
  
   :0E # E is probably overkill, given the recursion call before
   * $ $SECONDS^0
   * -3599^0
   {
    :0
    * $ $=^0
    * -1^0
    { SECONDS = $= }

    :0
    * $ ${HOURS-0}^0
    * 1^0
    { HOURS = $= }

    INCLUDERC = $_
   }
  
   :0E
   * $ $SECONDS^0
   * -599^0
   {
    :0
    * $ $=^0
    * -1^0
    { SECONDS = $= }

    :0
    * $ ${TENMINUTES-0}^0
    * 1^0
    { TENMINUTES = $= }

    INCLUDERC = $_
   }
  
   :0E
   * $ $SECONDS^0
   * -59^0
   {
    :0
    * $ $=^0
    * -1^0
    { SECONDS = $= }

    :0
    * $ ${MINUTES-0}^0
    * 1^0
    { MINUTES = $= }

    INCLUDERC = $_
   }

   :0
   * TENMINUTES ?? ^^0+^^
   { TENMINUTES }

   :0
   * DAYS ?? ^^0+^^
   {
    DAYS

    :0
    * HOURS ?? ^^0+^^
    {
     HOURS

     :0
     * TENMINUTES ?? ^^0*^^
     * MINUTES ?? ^^0+^^
     { MINUTES }
    }
   }

   LAPSE = "${DAYS+${DAYS}d }${HOURS+${HOURS}h }\
${MINUTES+$TENMINUTES${MINUTES}m }${SECONDS}s"

<Prev in Thread] Current Thread [Next in Thread>
  • a shell or a recursive INCLUDERC?, David W. Tamkin <=