2 Replies Latest reply on Dec 29, 2017 12:52 PM by Rick Davin

    AF Attribute - format hours from double to hh:mm:ss

    Mirmanero

      Hi all,

       

      into AF I have a "double" attribute representing the number of hours during a day where some condition is fullfilled (using Analysis with TimeGE function). Now I'd like to show this number into PIVision Display with "hh:mm:ss" format.

       

      I can do that using some support attributes:

      1-  with formula DR calculate three integer attributes using "floor" to get the number of hours, minutes and seconds (es for minutes: "A=|Double;[floor((A-floor(A))*60)]")

      2 - with string builder DR other three attributes concatenate "0" and the integers found at step 1 (es for seconds: 0'|Double|Seconds';)

      3- with string builder concatenate all attributes found at step 2 using right formula: right('|Double|Hours|Concatenate',2);:;right('|Double|Minutes|Concatenate',2);:;right('|Double|Seconds|Concatenate',2)

       

      is there a more "elegant" solution to avoid all these support attributes? (I have multiple time attributes to format)

       

      PS: I tryed also to sum Double attribute to a "Today" attribute and use Format function inside String Builder DR but such solution fails when double is 24 since returns 00:00:00, moreover I'm not confident of behavior during DST switch days.

       

      thank you,

        • Re: AF Attribute - format hours from double to hh:mm:ss
          John Messinger

          Hi Carlos,

           

          This is pretty close to the approach I took a little while ago to doing this same thing. My solution used slightly less attributes than yours, but there are some similar steps:

           

          I'm still using the Floor function to extract the values for the individual time components, but used a slightly different set of formatting rules when I concatenated the whole lot into a time string:

           

           

          Is this a more 'elegant' solution? Not really, but it does use a slightly less complicated child attribute hierarchy to accomplish the end result.

           

          Hopefully others in the community might have found a more elegant way to do this.

           

          Hope this helps.

           

          John

          1 of 1 people found this helpful
          • Re: AF Attribute - format hours from double to hh:mm:ss
            Rick Davin

            Hi Carlo,

             

            You could use an Analysis and a little bit of trickery.  An AFAttribute does not support a TimeSpan value type directly, but it does support <Anything> which can be any object type.  The problem is the burden is on you to correctly assign the object value.  On the surface this sounds a bit complicated but it's actually a few simple steps.

             

            First, here's my structure.  The Seconds attribute is static for this simple example but it could just as easily be assigned to a PIPoint.  You do not have to map the output to a PIPoint.

             

            2017-12-29 06_36_10-__DESKTOP-L6P2PBN_Analytics Tests - PI System Explorer (Administrator).png

            TimeSpan is configured as:

             

            2017-12-29 06_38_07-__DESKTOP-L6P2PBN_Analytics Tests - PI System Explorer (Administrator).png

             

            The Analysis is defined as:

             

            2017-12-29 06_41_50-__DESKTOP-L6P2PBN_Analytics Tests - PI System Explorer (Administrator).png

             

            This example would produce decimal seconds if the input has them.  So you could end up with:

             

            2017-12-29 06_43_06-__DESKTOP-L6P2PBN_Analytics Tests - PI System Explorer (Administrator).png

             

            If you absolutely, positively only want integer seconds, you would add an extra expression:

             

            2017-12-29 06_49_05-__DESKTOP-L6P2PBN_Analytics Tests - PI System Explorer (Administrator).png

             

            You might be tempted to perform the first UOM conversion and truncation to integer in one expression.  However, if your input is every not seconds, perhaps its hours, you really want to take the double hours and convert to double seconds, and only after that is done should you truncate to integer seconds.

            3 of 3 people found this helpful