scripting@wanadoo.nl




Flip_SerTime()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
16 May 2002 Flip_SerTime()
AuthorW.M. Hinsch (New Mexico Mark)
Action Given a time, returns seconds since midnight. Given seconds since midnight, returns a time.
SyntaxFlip_SerTime (TimeOrSeconds) 
Parameters
  • TimeOrSeconds (Required / String) Either Time as a string in HH:MM[:SS] format OR Seconds as an integer or string that can be converted to an integer. 
  • RemarksVery handy for time math. If seconds greater than 86400 are supplied (24 hours), 86400 will be subtracted until the value is less than one day.
    This means if you add 15 minutes at 23:50, Flip_SerTime will return 00:05:00. It is up to you to handle the dates.

    Note: This is essentially the same function as the SerialTime() UDF I submitted some time back. However, this one eliminates all dependencies on other UDFs, and is a little more flexible in that it can handle serial times larger than exceed one day. 
    ReturnsA string with either time or seconds since midnight, depending upon what was supplied. 
    DependenciesNone. 
    Examples
    BREAK ON
    COLOR w+/b
    CLS
    'This function makes time math easier.'
    CLS
    Box(7,16,11,64,"double")
    At(5,19) "Testing boundary times and unusual formats."
    ; Some of these examples should NOT work.
    ; Test odd times and formats
    $s = 3
    testtime("0:0:0")
    SLEEP $s
    Box(7,16,11,64,"double")
    testtime("23:59:59")
    SLEEP $s
    Box(7,16,11,64,"double")
    testtime("1:5:")
    SLEEP $s
    Box(7,16,11,64,"double")
    testtime("5:15:010")
    SLEEP $s
    Box(7,16,11,64,"double")
    testtime("24:0:0")
    SLEEP $s
    Box(7,16,11,64,"double")
    testtime("1:1:1")
    SLEEP $s
    Box(7,16,11,64,"double")
    testtime(87000)
    SLEEP $s
    Box(7,16,11,64,"double")
    At(4,16) Ucase("Testing all times from 00:00:00 through 23:59:59")
    At(5,15) "     86,400 iterations. This may take a while.     "
    FOR $h = 0 TO 23
      FOR $m = 0 TO 59
        FOR $s = 0 TO 59
          IF $h < 10 $hh = '0' + $h
          ELSE
            $hh = '' + $h
          ENDIF
          IF $m < 10 $mm = '0' + $m
          ELSE
            $mm = '' + $m
          ENDIF
          IF $s < 10 $ss = '0' + $s
          ELSE
            $ss = '' + $s
          ENDIF
          testtime($hh + ":" + $mm + ":" + $ss)
        NEXT
      NEXT
    NEXT
    At(23,0)
    EXIT
    FUNCTION testtime($time)
      DIM $at, $att, $i, $bsame
      At(10,18) "No errors.                   "
      $st = flip_sertime($time)
      $tt = flip_sertime($st)
      At(8,18) "Flip_SerTime(" + $time + ") = " + $st
      At(9,18) "Flip_SerTime(" + $st + ") = " + $tt
      $at  = Split($time,":")
      $att = Split($tt,":")
      $bsame = 1
      IF UBound($at) = UBound($att)
        FOR $i = 0 TO UBound($at)
          IF Val($at[$i]) <> Val($att[$i]) $bsame = 0
          ENDIF
        NEXT
        IF NOT $bsame
          At(10,18) "Possible error -- " + $time + " <> " + $tt
        ENDIF
      ENDIF
    ENDFUNCTION
    
     
    Source
    FUNCTION Flip_SerTime ($var)
      ;
      ; SYNTAX: Flip_SerTime(Time | SerialTime)
      ;
      ; Where Time is a time string in HH:MM[:SS] format    -or-
      ; SerialTime is the seconds since midnight) 0 - 86399
      ;
      ; - Values greater than 86399 will be reduced one day (86400 seconds) at
      ;   a time until the value is smaller than one day.
      ; - Sets Error to -1 for error
      ; - DEPENDENCIES: None
      ;
      DIM $h, $m, $s, $atime
      IF InStr($var,':') ; Time supplied, return seconds since midnight
        IF (Len($var) > 3) AND (Len($var) < 9)
          $atime=Split($var,':')
          REDIM PRESERVE $atime[2]
          $h=Val($atime[0])
          $m=Val($atime[1])
          $s=Val($atime[2])
          IF ($h > 23) OR ($m > 59) OR ($s > 59)
            EXIT -1
          ENDIF
          $flip_sertime=($h*3600)+($m*60)+$s
        ELSE
          EXIT -1
        ENDIF
      ELSE
        ; Seconds since midnight supplied, return time
        $var=Val($var)
        WHILE $var > 86399
          $var=$var-86400
        LOOP
        IF ($var < 0)
          EXIT -1
        ENDIF
    
        $h=$var/3600  $var=$var-($h*3600)  $h=Right('0'+$h,2)
        $m=($var/60)  $var=$var-($m*60)    $m=Right('0'+$m,2)
        $s=$var                            $s=Right('0'+$s,2)
        $flip_sertime=''+$h+':'+$m+':'+$s
      ENDIF
    ENDFUNCTION ; - Flip_SerTime -
     
      original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
              printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




    Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


    Site Meter