AFDatabase.ModifyDate Bug?

I am having an issue where the AFDatabase.ModifyDate value is set to an incorrect time after executing AFDatabase.Refresh(). Prior to the Refresh(), the correct modified timestamp is returned.

 

Here's an output demonstrating this issue:

Attempting to connect...

Successfully connected to MyServer.
Database: MyDatabase

-------------------------------------------------
  -  Database Modify Date (UTC): 2025-07-17 20:04:35
  -  Current Execution Time (UTC): 2025-07-17 22:55:22
-------------------------------------------------
[RESULT] The ModifyDate appears correct (the bug is NOT present).

-------------------------------------------------
  -  Database Modify Date (UTC): 2025-07-17 22:55:12
  -  Current Execution Time (UTC): 2025-07-17 22:55:22
-------------------------------------------------
[RESULT] The ModifyDate appears to be the current time (the bug is present).

Press any key to exit.

Here's the code:

using System;
using OSIsoft.AF;

namespace ConsoleApp13
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Attempting to connect...");

            try
            {
                string afServerName = "MyServer";
                string afDatabaseName = "MyDb";

                PISystems systems = new PISystems();
                PISystem afServer = systems[afServerName];
                AFDatabase afDatabase = afServer.Databases[afDatabaseName];

                DateTime modifyDate = afDatabase.ModifyDate.UtcTime;
                DateTime currentTime = DateTime.UtcNow;

                Console.WriteLine($"\nSuccessfully connected to {afServerName}.");
                Console.WriteLine($"Database: {afDatabaseName}");

                Console.WriteLine("\n-------------------------------------------------");
                Console.WriteLine($"  -  Database Modify Date (UTC): {modifyDate:yyyy-MM-dd HH:mm:ss}");
                Console.WriteLine($"  -  Current Execution Time (UTC): {currentTime:yyyy-MM-dd HH:mm:ss}");
                Console.WriteLine("-------------------------------------------------");


                if (modifyDate.Date == currentTime.Date && modifyDate.Hour == currentTime.Hour && modifyDate.Minute == currentTime.Minute)
                {
                    Console.WriteLine("[RESULT] The ModifyDate appears to be the current time (the bug is present).");
                }
                else
                {
                    Console.WriteLine("[RESULT] The ModifyDate appears correct (the bug is NOT present).");
                }

                afDatabase.Refresh();
                modifyDate = afDatabase.ModifyDate.UtcTime;

                Console.WriteLine("\n-------------------------------------------------");
                Console.WriteLine($"  -  Database Modify Date (UTC): {modifyDate:yyyy-MM-dd HH:mm:ss}");
                Console.WriteLine($"  -  Current Execution Time (UTC): {currentTime:yyyy-MM-dd HH:mm:ss}");
                Console.WriteLine("-------------------------------------------------");

                if (modifyDate.Date == currentTime.Date && modifyDate.Hour == currentTime.Hour && modifyDate.Minute == currentTime.Minute)
                {
                    Console.WriteLine("[RESULT] The ModifyDate appears to be the current time (the bug is present).");
                }
                else
                {
                    Console.WriteLine("[RESULT] The ModifyDate appears correct (the bug is NOT present).");
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine($"\nAn error occurred: {ex.Message}");
            }

            Console.WriteLine("\nPress any key to exit.");
            Console.ReadKey();
        }
    }
}

Can anyone reproduce this issue? I am using OSIsoft.AFSDK v3.1.0.1156.

  • Non-public AFDatabase class members before and after AFDatabase.Refresh();


    image.png
    
    image_1.png

  • Hi Nick,

     

    I cannot reproduce your problem. I intentionally chose a database that has not been touched for many years. I modified the code to make it a bit DRY:

     

    using System;
    using OSIsoft.AF;
    
    namespace ConsoleApp13
    {
        internal class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    const string afServerName = "MyServer";
                    const string afDatabaseName = "MyDb";
    
                    PISystems afclient = new PISystems();
                    Console.WriteLine($"AF SDK Client Version: {afclient.Version}");
                    PISystem afServer = afclient[afServerName];
                    Console.WriteLine("Attempting to connect...");
                    AFDatabase afDatabase = afServer.Databases[afDatabaseName];
                    Console.WriteLine($"\nSuccessfully connected to {afServerName}.");
                    Console.WriteLine($"AF Server Version: {afServer.ServerVersion}");
                    Console.WriteLine($"Database: {afDatabaseName}");
    
                    DateTime currentTime = DateTime.UtcNow;
                    DisplayModifyDate(afDatabase, currentTime);
    
                    Console.WriteLine($"\nIssuing AFDatabase.Refresh().");
                    afDatabase.Refresh();
    
                    DisplayModifyDate(afDatabase, currentTime);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"\nAn error occurred: {ex.Message}");
                }
    
                Console.WriteLine("\nPress any key to exit.");
                Console.ReadKey();
            }
    
            private static void DisplayModifyDate(AFDatabase afDatabase, DateTime currentTime)
            {
                DateTime modifyDate = afDatabase.ModifyDate.UtcTime;
    
                Console.WriteLine("\n-------------------------------------------------");
                Console.WriteLine($"  -  Database Modify Date (UTC): {modifyDate:yyyy-MM-dd HH:mm:ss}");
                Console.WriteLine($"  -  Current Execution Time (UTC): {currentTime:yyyy-MM-dd HH:mm:ss}");
                Console.WriteLine("-------------------------------------------------");
    
                if (Math.Abs((currentTime - modifyDate).TotalMinutes) <= 1)
                {
                    Console.WriteLine("[RESULT] The ModifyDate is within 1-minute of the current time (the bug is present).");
                }
                else
                {
                    Console.WriteLine("[RESULT] The ModifyDate appears correct (the bug is NOT present).");
                }
            }
        }
    }

    I also print the AF SDK Client and AF Server versions. I changed the logic on comparing to see if the modifyDate and currentTime were within the same minute of each other.

     

    Here's what my output looks like:

     

    AF SDK Client Version: 3.1.0.1156
    Attempting to connect...
    
    Successfully connected to OBFUSCATED.
    AF Server Version: 2.10.9.593
    Database: OBFUSCATED
    
    -------------------------------------------------
     - Database Modify Date (UTC): 2022-10-05 20:13:50
     - Current Execution Time (UTC): 2025-07-18 17:23:34
    -------------------------------------------------
    [RESULT] The ModifyDate appears correct (the bug is NOT present).
    
    Issuing AFDatabase.Refresh().
    
    -------------------------------------------------
     - Database Modify Date (UTC): 2022-10-05 20:13:50
     - Current Execution Time (UTC): 2025-07-18 17:23:34
    -------------------------------------------------
    [RESULT] The ModifyDate appears correct (the bug is NOT present).
    
    Press any key to exit.

     

     I ran it in Debug mode, not Release mode. For Build settings, I got same results whether I enabled Prefer 32-bit or disabled it.

     

     

  • Interesting. Today I wasn't able to reproduce the bug until I loaded the database in PSE 2024, made a small modification and checked in, and then refreshed the database in PSE.

    I'm going to move this into a fresh VM for further testing.