5 Replies Latest reply on Jun 21, 2013 7:55 PM by Rick Davin

    GetValueArrays and Utcseconds

    legionofone22

      Hello all,

       

      Currently I have pulled some RecordedValues in a AFValues "myvalues"

       

      I can then take "myvalues.GetValueArrays(vals,stamps,flags)" and get my 3 arrays "vals" as an object, "stamps" as DateTime, and "flags" as AFValueStaus

      Now for my specific needs I require to get stamps as .Utcseconds and have it in an object array. Is there a way from here to do that without looping? Is there a different avenue that I can take instead of GetValueArrays? Remember when creating the Utcseconds array can it become an object array instead?

       

       

       

      Thanks,

       

      Anthony

        • Re: GetValueArrays and Utcseconds
          Rick Davin

          Based on your specific requirements, I'm afraid you will have to have a loop.  I'm sure you are aware the the GetValueArrays method returns the timestamps as a DateTime array with Kind=Utc, and that UtcSeconds is a property of AFTime and not DateTime.  One possible way to perform the assignment inside your custom loop would be:

           

          seconds[ i ] = (object) new AFTime(stamps[ i ]).UtcSeconds;

           

          Alternatives:

           

          Rather than call GetValueArrays, you could get the AFValues yourself and then build the arrays as you see fit.  I'm not suggesting that its better, smarter, or faster: merely different.  For instance, if you didn't need the flags array or maybe all you care about is only the stamps array, then skipping GetValueArrays and rolling your own may be the better choice.

            • Re: GetValueArrays and Utcseconds
              Rick Davin

              Forgive my answer.  I see it is PISDK and my brain is way too deep into AFSDK.  So its not the best answer but still along the right track.

                • Re: GetValueArrays and Utcseconds
                  Rick Davin

                  Avoiding AFSDK and without a handy-dandy PITime constructor, you could have a function or extension method along the lines of:

                   

                         public static double UtcSeconds(DateTime time)

                   

                         {

                   

                             var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

                   

                             return (time.ToUniversalTime() - epoch).TotalSeconds;

                   

                         }

                   

                  And if need be, you can make the function return an object type instead of double.  But you would still need to put it in a loop.

                    • Re: GetValueArrays and Utcseconds
                      legionofone22

                      Valid response. I would however like to possible have this as a method contained under the "AFValues" class. Is it possible to do this through the use of a second library file?

                        • Re: GetValueArrays and Utcseconds
                          Rick Davin

                          You may try extension methods.  To avoid some naming conflicts, I have renamed my custom UtcSeconds to be ToUtcSeconds:

                           

                           

                           

                           

                           
                              public static class MyExtensions
                              {
                                  // doesn't have to be an extension method for this example
                                  public static double ToUtcSeconds(this DateTime time)
                                  {
                                      var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                                      return (time.ToUniversalTime() - epoch).TotalSeconds;
                                  }
                          
                                  public static void GetCustomValueArrays(this AFValues afvalues, out object[] values, out object[] utcseconds, out AFValueStatus[] flags)
                                  {
                                      DateTime[] stamps;
                                      afvalues.GetValueArrays(out values, out stamps, out flags);
                                      utcseconds = new object[stamps.Length];
                                      for (int i = 0; i < stamps.Length; i++)
                                      {
                                          utcseconds[ i ] = (object)ToUtcSeconds(stamps[ i ]);
                                      }
                                  }
                          

                           

                           

                          You may find out more about extension methods on MSDN or with a Google search.

                           

                          http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx

                           

                          http://social.msdn.microsoft.com/Search/en-US?query=c%23%20extension%20methods&ac=2