11 Replies Latest reply on Mar 12, 2014 3:12 AM by disabled_winfried

    Converting minutes to D:H:M

    PaulWebb

      I came up with the below and it works. Just wanted to see if there is a better/shorter way to write it and if there are any gotchas I might be missing. Thanks for looking. The goal is to convert minutes to D:H:M.

       

      examples

      72 minutes = 1:12

      1827 minutes = 1:06:27

      // DAYS SECTION //////////////////////////////////

       

      Case (
      Int ( ( SR::INITIAL_RESPONSE_MIN / 60 ) / 24 ) < 1 ; "" ;
      Int ( ( SR::INITIAL_RESPONSE_MIN / 60 ) / 24 ) & GetAsText ( ":" ) )

       

      //////////////////////////////////////////////////////

       

      &

       

      // HOURS SECTION //////////////////////////////////

       

      Case (
      Int ( ( ( ( SR::INITIAL_RESPONSE_MIN / 60 ) / 24 ) - Int ( ( SR::INITIAL_RESPONSE_MIN / 60 ) / 24 ) ) * 24 ) < 10 ; 0 ; "" ) &
      Int ( ( ( ( SR::INITIAL_RESPONSE_MIN / 60 ) / 24 ) - Int ( ( SR::INITIAL_RESPONSE_MIN / 60 ) / 24 ) ) * 24 )

       

      & ":" &

       

      //////////////////////////////////////////////////////

       

      // MINUTES SECTION //////////////////////////////////

       

      Case (

       

      Round ( ( ( ( SR::INITIAL_RESPONSE_MIN / 60 ) - Int ( ( SR::INITIAL_RESPONSE_MIN / 60 ) ) ) * 60 ) ; 1 ) < 10 ; "0" ; "" ) &
      Round ( ( ( ( SR::INITIAL_RESPONSE_MIN / 60 ) - Int ( ( SR::INITIAL_RESPONSE_MIN / 60 ) ) ) * 60 ) ; 1 )

       

      //////////////////////////////////////////////////////

        • 1. Re: Converting minutes to D:H:M
          erolst

          Paul Webb wrote:

          Just wanted to see if there is a better/shorter way to write it and if there are any gotchas I might be missing

          If it looks ugly, it can almost always be improved … e.g.

           

          Let ( [

            ~i = 1827 ; // your initial response field

           

            ~d = SerialIncrement ( "0" ; Div ( ~i ; 1440 ) ) ;

            ~dR = Mod ( ~i ; 1440 ) ;

           

            ~h = SerialIncrement ( "00" ; Div ( ~dR ; 60 ) ) ;

           

            ~m = SerialIncrement ( "00" ; Mod ( ~dR ; 60 ) )

            ] ;

            Case ( ~d ; ~d & ":" ) & Case ( ~h or ~d ; ~h & ":" ) & ~m

          )

           

          = 1:06:27

           

          Even if you don't use Div() and Mod(), you should definitely start getting familiar with Let().

          1 of 1 people found this helpful
          • 2. Re: Converting minutes to D:H:M
            PaulWebb

            Thanks erolst. I had cleaned it up some with the Let function. I try to use the Let function on just about every calculation. I've never used the Mod function before. I'll give your setup a try. Thanks!

            • 3. Re: Converting minutes to D:H:M
              keywords

              As Erolst says, at the very least make use of the Let ( ) function. In your calculation FM makes at least five and possibly as many as ten calls on the SR::INITIAL_RESPONSE_MIN  field. By declaring this field as a variable within a Let ( ) statement reduces this to one call. If that field happens to be itself a calculation (eg. a subtraction of start and finish timestamps) this change alone will make the calculation faster.

               

              If you study your current calc further you can see that there are ten instances of the same sum—SR::INITIAL_RESPONSE_MIN / 60—so you could add further elegance and clarity, not to mention ease of maintenance, by declaring this as a second variable.

              1 of 1 people found this helpful
              • 4. Re: Converting minutes to D:H:M
                user19752

                Convert the number to h:mm:ss and remove preceding 0:

                 

                Let ( s = GetAsTime ( Div ( SR::INITIAL_RESPONSE_MIN ; 24*60 ) * 60*60 + Mod ( SR::INITIAL_RESPONSE_MIN ; 24*60 ) ) ;

                Case ( Left ( s ; 2 ) = "0:" ; Replace ( s ; 1 ; 2 ; "" ) ; s )

                )

                • 5. Re: Converting minutes to D:H:M
                  PaulWebb

                  Thanks guys. I think I originally missunderstood the variables of the LET function. I originally thought that when the variable was applied later in the function it input exactly what you typed in and not the result of the variable. My understanding now is that it applies the result. So if you set a variable to @math = 1+1 then later in the function when it calls for @math it applies 2 and not 1+1. Is that correct or make sense?

                   

                  I cleaned up the function as best I could with the below. Seems to work just fine but I'm only pushing 120K records through it currently.

                   

                  Let ( [

                   

                  @Field = dash_SR__SRsByDateRangeCreate::zsAvgInitResponseMin ;

                  @TimeField = Round ( @Field ; 0 ) ;

                  @Convert = ( @TimeField / 60 ) ;

                  @Days = Int ( @Convert / 24 ) ;

                  @Hours = Int ( ( ( @Convert / 24 ) - @Days ) * 24 ) ;

                  @Minutes = Round ( ( ( @Convert - Int ( @Convert ) ) * 60 ) ; 1 ) ] ;

                   

                  // DAYS SECTION //

                   

                  Case (

                  @Days < 1 ; "" ;

                  @Days & GetAsText ( ":" )

                  ) &

                   

                  // HOURS SECTION //

                   

                  Case (

                  @Hours < 10 ; 0 ; "" ) &

                  @Hours

                   

                  & ":" &

                   

                  // MINUTES SECTION //

                   

                  Case (

                   

                  @Minutes  < 10 ; "0" ; "" )  &

                  @Minutes

                   

                  )

                  • 6. Re: Converting minutes to D:H:M
                    PaulWebb

                    Thanks but this looks like you are calculating H:M:S and I need D:H:M

                    • 7. Re: Converting minutes to D:H:M
                      raybaudi

                      GetAsText ( ":" )

                       

                      Which is the difference with a simple ":" ?

                      • 8. Re: Converting minutes to D:H:M
                        DrewTenenholz

                        Paul --

                         

                        If you have time in minutes, I'd leverage the FileMaker date/time engine as below.  People tend to forget that both Date ( month ; day ; year ) and Time ( hours ; minutes ; seconds ) automatically convert to internal date engine values and can then be easily manipulated after that.  (i.e.  Date ( 2 ; 1 + 28 ; someYear ) will give either Feb 29 or Mar 1 as expected.  The same goes for times like Time ( 12 + 16 ; 30 + 45 ; 0 ) = 29:15:00.  TimeStamps work like this too.

                         

                        Let ([

                        initialValue= 1827 //

                         

                         

                        ; theTime= Time ( 0 ; initialValue ; 0 ) // that's Time ( hours ; minutes ; seconds )  and in this case will yield 30:27:00

                         

                        ; theDays= Int ( Hour ( theTime ) / 24 )

                        ; remainingTime= theTime - ( theDays * 60 * 60 * 24 )  // or you could use MOD ( theTime ; 60 * 60 * 24 )

                         

                        ; remainingHours= Right ( "00" & Hour ( remainingTime ) ; 2 )  // the zero-padded hours

                        ; remainingMinutes= Right ( "00" & Minute ( remainingTime ) ; 2 ) // the zero-padded minutes

                         

                        ; finalResult= theDays & ":" & remainingHours & ":" & remainingMinutes

                         

                        ];

                        finalResult

                        )

                         

                        for the above, the result is : "1:06:27"

                         

                         

                        One could condense this calculation into a single line, but it would be harder for me to read and debug.  That's why I love Let()

                         

                        -- Drew Tenenholz

                        • 9. Re: Converting minutes to D:H:M
                          user19752

                          The calculation 'converts' the D part of the value to H part in H:M:S for simplize text conversion.

                          (GetAsText(GetAsTime(H;M;S) ) automatically add preceding '0' on M and S part.)

                          I tested it on FM13 that the result is same as your expression in first post.

                          • 10. Re: Converting minutes to D:H:M
                            PaulWebb

                            I like it! Simple enough I almost understand it. I need to study it a bit more.

                             

                            Thanks for the education from everyone. Hope to meet you guys at DevCon this year.

                            • 11. Re: Converting minutes to D:H:M

                              I know I'm late here, but

                                 Int ( m / 24 ) & " days " & Left ( Time ( 0 ; Mod ( m ; 24 ) ; 0 ) ; 5 )

                              where m = 5000 yields "208 days 00:08".

                              I don't like 208:00:08 because it looks confusing for me - at least.

                              -Winfried

                               

                              --

                              Huslik Verlag GmbH • Bgm.-Widmeier-Str. 42 • 86179 Augsburg, DE

                              CEO Winfried Huslik - HRB Augsburg 12386 -  VAT-Id. DE127485099

                              Phone +49 821 565606, Fax +49 821 565001, Email info@fmdiff.com

                              Verify your FileMaker Pro files with FMDiff - http://fmdiff.com

                              FileMaker Developer Conference July 28-31 2014 • San Antonio TX

                              Linkedin: http://de.linkedin.com/pub/winfried-huslik/2/505/1a1/