EXAMPLE: Set Time Date / Time Synchronization in runtime

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
onedumbtrucker
Posts: 14
Joined: Thu Nov 14, 2013 12:14 pm

EXAMPLE: Set Time Date / Time Synchronization in runtime

Post by onedumbtrucker »

Lately we have been coming up with a lot of solutions to problems that I thought I should document in case others need to do the same or similar.

This example shows how to set the time and date within the runtime using the InteropServices. We have a number of customers that connect complex plant SCADA systems to our controls system and they often want to make sure all devices on a SCADA network are time synchronized to make alarms easier to troubleshoot.

This code for the most part is copied directly from the MSDN example but I added the using statement to show what namespace was needed. The function SetTime() is just adding 1 hour to the current time but it is easy to adapt to what you want.

Code: Select all


using System.Runtime.InteropServices;

//these dll imports go inside the beginning of the class definition
[DllImport("coredll.dll")]
private extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime);

[DllImport("coredll.dll")]
private extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);


private struct SYSTEMTIME 
{
    public ushort wYear;
    public ushort wMonth; 
    public ushort wDayOfWeek; 
    public ushort wDay; 
    public ushort wHour; 
    public ushort wMinute; 
    public ushort wSecond; 
    public ushort wMilliseconds; 
}

private void GetTime()
{
    // Call the native GetSystemTime method 
    // with the defined structure.
    SYSTEMTIME stime = new SYSTEMTIME();
    GetSystemTime(ref stime);

    // Show the current time.           
    MessageBox.Show("Current Time: "  + stime.wHour.ToString() + ":" + stime.wMinute.ToString());
}
private void SetTime()
{
    // Call the native GetSystemTime method 
    // with the defined structure.
    SYSTEMTIME systime = new SYSTEMTIME();
    GetSystemTime(ref systime);

    // Set the system clock ahead one hour.
    systime.wHour = (ushort)(systime.wHour + 1 % 24);
    SetSystemTime(ref systime);
    MessageBox.Show("New time: " + systime.wHour.ToString() + ":" + systime.wMinute.ToString());
}
Cheers,
Dan

User avatar
Jae V
Posts: 118
Joined: Wed Jan 15, 2020 11:00 am

Re: EXAMPLE: Set Time Date / Time Synchronization in runtime

Post by Jae V »

Thanks Dan for the information. If there is anyone who has related issues to setting time in the panels and require assistance, please visit us at https://www.beijerelectronics.us/en-US/ ... ___support or give us call at 801.708.6690. Thank you.
Best regards,
Jason
(801) 708-6690
Technical Support
Contact Us



Beijer Electronics AB
http://www.beijerelectronics.us

Post Reply