benji7

AFSDK Beta - UTC Timestamps

Discussion created by benji7 on Jun 14, 2012
Latest reply on Jun 14, 2012 by benji7

I'm trying to understand the behavior I'm seeing with UTC timestamps on AF Values. It seems as though the times are shifted for "UtcTime" rather than "LocalTime", which is the opposite of what I would expect.

 

Below is an example console application I’ve created to demonstrate what I’ve seen.  At the bottom of the code is the output I’ve gotten for 3 settings of my local client windows clock:
1. Client Windows Clock: Pacific time with DST
2. Client Windows Clock: Pacific time without DST
3. Client Windows Clock: Coordinated Universal Time (UTC)

 
using System;
using OSIsoft.AF;
using OSIsoft.AF.Asset;
using OSIsoft.AF.Data;
using OSIsoft.AF.Time;

namespace timestampsAFSDK
{
    class Program
    {
        static void Main(string[] args)
        {
            PISystems piSystems = new PISystems();
            AFDatabase afDatabase = piSystems["piafdev01"].Databases["SPWR"];
            DateTime now = DateTime.UtcNow;
            AFTimeRange timeRange = new AFTimeRange();

            // March 9, 2012
            timeRange.StartTime = new DateTime(now.Year, 3, 9, 0, 0, 0, DateTimeKind.Utc);
            // March 16, 2012
            timeRange.EndTime = new DateTime(now.Year, 3, 16, 0, 0, 0, DateTimeKind.Utc);
            // one day
            AFTimeSpan timeSpanDay = new AFTimeSpan(0, 0, 1, 0, 0, 0, 0);

            AFValues afValues = afDatabase.Elements["test"].Attributes["sinusoid"].Data.Summaries(timeRange, timeSpanDay, AFSummaryTypes.Average, AFCalculationBasis.TimeWeighted, AFTimestampCalculation.Auto)[AFSummaryTypes.Average];

            Console.WriteLine("timeRange.StartTime         : " + timeRange.StartTime);
            Console.WriteLine("timeRange.EndTime           : " + timeRange.EndTime);
            Console.WriteLine("timeRange.StartTime.UtcTime : " + timeRange.StartTime.UtcTime);
            Console.WriteLine("timeRange.EndTime.UtcTime   : " + timeRange.EndTime.UtcTime);

            Console.WriteLine("\n afValue... Timestamp.UtcTime | Timestamp.LocalTime | Timestamp.UtcSeconds");

            foreach (var afValue in afValues)
            {
                Console.WriteLine(afValue.Timestamp.UtcTime + " | " + afValue.Timestamp.LocalTime + " | " + afValue.Timestamp.UtcSeconds);
            }

            // --- output with Windows Clock: Pacific time (with DST):
            // 
            //timeRange.StartTime         : 3/8/2012 4:00:00 PM
            //timeRange.EndTime           : 3/15/2012 5:00:00 PM
            //timeRange.StartTime.UtcTime : 3/9/2012 12:00:00 AM
            //timeRange.EndTime.UtcTime   : 3/16/2012 12:00:00 AM

            // afValue... Timestamp.UtcTime | Timestamp.LocalTime | Timestamp.UtcSeconds
            //3/9/2012 12:00:00 AM | 3/8/2012 4:00:00 PM | 1331251200
            //3/10/2012 12:00:00 AM | 3/9/2012 4:00:00 PM | 1331337600
            //3/11/2012 12:00:00 AM | 3/10/2012 4:00:00 PM | 1331424000
            //3/11/2012 11:00:00 PM | 3/11/2012 4:00:00 PM | 1331506800
            //3/12/2012 11:00:00 PM | 3/12/2012 4:00:00 PM | 1331593200
            //3/13/2012 11:00:00 PM | 3/13/2012 4:00:00 PM | 1331679600
            //3/14/2012 11:00:00 PM | 3/14/2012 4:00:00 PM | 1331766000

            // --- output with Windows Clock: Pacific time (without DST):
            // 
            //timeRange.StartTime         : 3/8/2012 4:00:00 PM
            //timeRange.EndTime           : 3/15/2012 4:00:00 PM
            //timeRange.StartTime.UtcTime : 3/9/2012 12:00:00 AM
            //timeRange.EndTime.UtcTime   : 3/16/2012 12:00:00 AM

            // afValue... Timestamp.UtcTime | Timestamp.LocalTime | Timestamp.UtcSeconds
            //3/9/2012 12:00:00 AM | 3/8/2012 4:00:00 PM | 1331251200
            //3/10/2012 12:00:00 AM | 3/9/2012 4:00:00 PM | 1331337600
            //3/11/2012 12:00:00 AM | 3/10/2012 4:00:00 PM | 1331424000
            //3/12/2012 12:00:00 AM | 3/11/2012 4:00:00 PM | 1331510400
            //3/13/2012 12:00:00 AM | 3/12/2012 4:00:00 PM | 1331596800
            //3/14/2012 12:00:00 AM | 3/13/2012 4:00:00 PM | 1331683200
            //3/15/2012 12:00:00 AM | 3/14/2012 4:00:00 PM | 1331769600

            // --- output with Windows Clock: Coordiated Universal Time (UTC):
            // 
            //timeRange.StartTime         : 3/9/2012 12:00:00 AM
            //timeRange.EndTime           : 3/16/2012 12:00:00 AM
            //timeRange.StartTime.UtcTime : 3/9/2012 12:00:00 AM
            //timeRange.EndTime.UtcTime   : 3/16/2012 12:00:00 AM

            // afValue... Timestamp.UtcTime | Timestamp.LocalTime | Timestamp.UtcSeconds
            //3/9/2012 12:00:00 AM | 3/9/2012 12:00:00 AM | 1331251200
            //3/10/2012 12:00:00 AM | 3/10/2012 12:00:00 AM | 1331337600
            //3/11/2012 12:00:00 AM | 3/11/2012 12:00:00 AM | 1331424000
            //3/12/2012 12:00:00 AM | 3/12/2012 12:00:00 AM | 1331510400
            //3/13/2012 12:00:00 AM | 3/13/2012 12:00:00 AM | 1331596800
            //3/14/2012 12:00:00 AM | 3/14/2012 12:00:00 AM | 1331683200
            //3/15/2012 12:00:00 AM | 3/15/2012 12:00:00 AM | 1331769600

        }
    }
}

 

Outcomes