Loading ...
Sorry, an error occurred while loading the content.

[Clip] Re: convert time

Expand Messages
  • RS
    Hi David, I wrote: (I know it s ugly to quote myself, but I want to add something -:) ... greater than 24 hours, before displaying the result. Say you got a
    Message 1 of 3 , Jan 30, 2000
    • 0 Attachment
      Hi David,

      I wrote:
      (I know it's ugly to quote myself, but I want to add something -:)

      >All you need to do is add 24 hours to the smaller time. Here's the
      pseudo-code:
      >
      >h2 = DMS(01:23:00) + 24
      >h1 = DMS(23:45:00)
      >diff = h2 - h1
      >ResultString = InvDMS(diff)
      >
      >
      >If you do several operations, you might want to make sure that diff is not
      greater than 24 hours, before displaying the result. Say you got a value
      like 36.5678, you can make it readable again by using a mod operator:
      >
      >^!Set diff=^$calc(36.5678 mod 24)$


      Well, here's a working demo, together with an updated version of the
      routines to convert between Degrees and Decimals.

      They are now presented as user-defined functions, which makes the code
      easier to read and program. As you can see, the example below doesn't look
      very different from the pseudo-code I posted earlier. User functions are
      definitely a bonus of the new NoteTab version.

      You'll need the latest version of NoteTab currently in beta (version 4.8
      beta #23).

      ;======== Start ========
      H="Demo Diff Hours"
      ; RS - 01/30/00 11:22:04 AM
      ; Edited with ClipWriter
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ^!Set precision=4
      ^!Set h2=^$DegToDec(01:23:00)$
      ;Add 24 hrs to make sure we won't get a negative value
      ;when subtracting h2 - h1:
      ^!Set h2 =^$Calc(^%h2% + 24;^%precision%)$
      ^!Set h1=^$DegToDec(23:45:00)$

      ^!Set diff=^$Calc(^%h2% - ^%h1%;^%precision%)$
      ^!Set diff=^$Module(^%diff%;24;^%precision%)$
      ;Format result as HH:MM:SS:
      ^!Set ResultString=^$DecToDeg(^%diff%)$

      ^!Info ^%ResultString%


      H="MODULE"
      ; RS - 01/30/00 11:22:21 AM
      ; Edited with ClipWriter
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ; MOD function - REQUIRES NTB 4.8 or above
      ;Usage: ^$Module(DMS;MODvalue;decimals)$
      ;Example for Hours value: ^$Module(10:23:44;24;4)$
      ;Example for Degrees value: ^$Module(320:23:44;360;4)$

      ^!SetListDelimiter ";"
      ^!SetArray %a%=^&
      ^!Set decs=^%a3%
      ^!Set i=^%a1%

      ^!Set frac=^$Calc(^%i% - int(^%i%);^%decs%)$
      ^!Set i=^$Calc(^%i% mod 24)$
      ^!Set result=^$Calc(^%i% + ^%frac%;^%decs%)$

      H="DegToDec"
      ; RS - 08/25/99 1:36:55 PM
      ; Edited with ClipWriter 1.0
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ; DMS function - rev 01.30.00 REQUIRES NTB 4.8 or above
      ;Converts value in DMS (Degrees-Minutes-Seconds)
      ;to Decimal value.

      ^!SetListDelimiter :
      ^!SetArray %t%=^&
      ^!Set h=^%t1%
      ^!Set m=^%t2%
      ^!Set s=^%t3%

      ^!If ^%m% <> "" Skip
      ^!Set m=0
      ^!If ^%s% <> "" Skip
      ^!Set s=0

      :convert
      ^!Set result=^$Calc((^%h% * 3600 + ^%m% * 60 + ^%s%)/3600;4)$

      ;clean:
      ^!ClearVariable h
      ^!ClearVariable m
      ^!ClearVariable s
      ^!ClearVariable t




      H="DecToDeg"
      ; RS - 08/25/99 1:48:13 PM
      ; Edited with ClipWriter 1.0
      ; http://home.att.net/~ptools/ClipWriter/cw.htm
      ; http://home.att.net/~ptools/main/index.htm

      ; InvDMS function - rev 01.30.00 REQUIRES NTB 4.8 or above
      ;Converts decimal value to DMS
      ;(Degrees-Minutes-Seconds)


      ^!SetListDelimiter .
      ^!SetArray %t%=^&

      ;integer:
      ^!Set h=^%t1%

      ;decimals:
      ^!Set x=^$Calc(.^%t2% * 60;2)$
      ^!Set m=^$Calc(int(^%x%);0)$
      ^!Set s=^$Calc((^%x% - int(^%x%))* 60;0)$


      :Next1
      ;format:
      ^!If ^%m% > 9 skip
      ^!Set m=0^%m%
      ^!If ^%s% > 9 Next2
      ^!Set s=0^%s%

      :Next2
      ^!Set result=^%h%:^%m%:^%s%

      ;clean:
      ^!ClearVariable h
      ^!ClearVariable m
      ^!ClearVariable s
      ^!ClearVariable t
      ^!ClearVariable x

      ;========= End =========


      Regards,

      RS
    Your message has been successfully submitted and would be delivered to recipients shortly.