4 Replies Latest reply on Jun 16, 2016 2:34 PM by HEPC

    Set AF SDK to a fix timezone

    HEPC

      Hi there,

       

      I'm building a connector between our system and a PI System using the PI AF SDK (2015 R2).

      The Connector reads an writes data to the PI System using Methods of the class PI Point (RecordedValue(s), Summaries, UpdateValues).

      Our system uses CET as timezone, so both the From- and To-timestamps that I get from our system for reading and writing are CET-timestamps and I have to deliver all read values with CET-timestamps.

      As far as I see, PI AF SDK can interpret timestamps either as UTC or in the local timezone, which can change depending on the customer installation.

       

      So here is my question:

      What is the best way to convert timestamps between our system (CET) and the PI AF SDK and vice versa? Is there a way, to set the local timezone for the AF SDK installation to CET?

      Or do I have to convert all timestamps using the TimeZoneInfo.ConvertTime-Method?

      Thanx in advance :-)

      Cheers

       

      Peter

        • Re: Set AF SDK to a fix timezone
          John Messinger

          I would opt for programmatic conversion of the timestamps to your required time zone over setting the timezone of the AF client system to something other than the local timezone for it's actual location. The TimeZoneInfo.ConvertTime method looks probably like the best option for this.

          • Re: Set AF SDK to a fix timezone
            Roger Palmen

            In general i'd say: keep any system in UTC, and only use local timezones for exposing data to clients.

            UTC keeps you away from DST issues and the like which we do have in central europe.

            1 of 1 people found this helpful
            • Re: Set AF SDK to a fix timezone
              Rick Davin

              Building upon John and Roger's answers, which are both correct ... let's first take AFSDK out of the picture.  How would you make your app handle time zones given that on a given workstation some user may have changed the local time zone?  Short of adding in something like NodaTime, the limitation of a DateTime.Kind of Unspecified is that either it means (a) the date time value is in some unknown time zone, or (b) the date time value is in some known time zone that is neither UTC nor local.  The problem with (b) is that two different Unspecified's are not necessarily from the same time zone.

               

              The safe way to proceed is that regardless of the workstation's local time zone is for you to convert all date times to UTC.  So, yes, you would use TimeZoneInfo's Convert methods, but more specifically the ConvertTimeToUtc and ConvertTimeFromUtc methods.  Once you have a DateTime with a Kind of Utc, you can create an AFTime with that UTC-based value. 

               

              Your efforts don't have to worry about the stuff in the middle.  Your concern is on the very first and last stages.  The first stage is grabbing the time.  Whichever workstation, grab the time in UTC.  If you must display that immediately, you will need to convert it to CET, if CET is not currently the local time zone.  If CET is the local time zone, then using the ToLocalTime() method works.  But you use the UTC time when working with AFSDK in the middle. 

               

              The last stage is when you read values via AFSDK and you wish to display those values to the user in CET.  Use the AFTime's UtcTime method to convert to a DateTime with Kind of Utc.  Then you would convert that once again to the desired time zone.

               

              It's not rocket science, but the burden is on you.  Also this works for your custom app, where you have total control over the presentation system, but may not work as desired using PSE because PSE only displays date times in local time.

              3 of 3 people found this helpful
              • Re: Set AF SDK to a fix timezone
                HEPC

                Hi there,

                 

                thank you all for your hints.

                 

                I found a solution which is quite straight forward, but it seems to work well.

                 

                I have to convert timestamps from CET (GMT+1) to UTC and vice versa. Both Timezones have no Daylight saving support. The only difference is, that CET is one hour ahead of UTC.

                 

                So I do my conversion simply by adding or subtracting 3600 seconds. The only thing, I have to keep an eye on is, that I always work with the UTC-Represantaion of AFTime and DayTime.

                 

                CET --> UTC

                (AFTime OutTs)

                DateTime tmpDateTime = new DateTime(InTs.getJahr(), InTs.getMonat(), InTs.getTag(), InTs.getStunde(), InTs.getMinute(), InTs.getSekunde(), DateTimeKind.Utc);

                tmpDateTime = tmpDateTime.AddSeconds(-3600);

                OutTs = new AFTime(tmpDateTime);

                 

                 

                UTC --> CET

                (AFTime InTs)

                DateTime tmpDateTime = InTs.UtcTime;

                tmpDateTime = tmpDateTime.AddSeconds(3600);

                 

                OutTs = new TBAS_DateTimeCS();

                OutTs.setJahr((ushort)tmpDateTime.Year);

                OutTs.setMonat((byte)tmpDateTime.Month);

                OutTs.setTag((byte)tmpDateTime.Day);

                OutTs.setStunde((byte)tmpDateTime.Hour);

                OutTs.setMinute((byte)tmpDateTime.Minute);

                OutTs.setSekunde((byte)tmpDateTime.Second);

                OutTs.setMilli(0);

                 

                Cheers & thanx

                 

                Peter