Topic: DMD0221

Help File Version: 2.10.0.636

Date and Time Overview


All of the Do-more CPUs are equipped with a Real Time Clock that is accurate to within +/- 1.53 minutes per month.

 

The current date and time in the Do-more PLC is internally stored as a UTC (Coordinated Universal Time)Closed UTC (Coordinated Universal Time) is the primary time standard by which the world regulates clocks and time. Time zones around the world are expressed as positive or negative offsets from UTC, as in the list of time zones by UTC offset. UTC is used in many Internet and World Wide Web standards. The Network Time Protocol, designed to synchronise the clocks of computers over the Internet, encodes times using the UTC system. value. The UTC value is a 32-bit integer value that represents the number of seconds that have elapsed since a particular point in time. Do-more PLCs use the 1970 Epoch - which began at 00:00:00, 1 January 1970 - as that reference point in time. The current UTC value is stored in the PLC in the system-defined location $UTC (DST21).

 

The current time of a Do-more PLC can be automatically adjusted for the Time Zone by entering the number of minutes of adjustment for the current time zone in the system-defined location $TimeZone (DST384).

 

The current time of a Do-more PLC can be further adjusted for Daylight Saving Time by setting the system-defined location $SummerTime (ST768). If this bit location is ON then 1 hour of time is added to the current time calculation; if OFF then no additional time is added.

 

The current date and time is automatically rendered from the UTC time value by applying adjustments for the Time Zone, and for Daylight Saving Time stored in the system-defined location $LocalTime (DST22).

 

Viewing a UTC value is not useful for humans. The Data View has a display format called '1970 Epoch' which will display a UTC value in human-readable form (DD-MMM-YYYY Day Hour:Minute:Second). In the Data View below, the first entry is $UTC in native format, the second is $UTC displayed in the 1970 Epoch format. At the end are similar entries for $LocalTime.

 

 

In review, there are four system-defined locations in the CPU that contain UTC-centric time values, they are:

$UTC (DST21) contains the current time in UTC form.
 

$TimeZone (DST384) is the number of minutes of adjustment for the current time zone.

 

When $SummerTime (ST768) is ON one hour of time is added to the $LocalTime calculation.

 

$LocalTime (DST22) contains the current date and time in UTC form, rendered from the UTC value after applying adjustments for the time zone and daylight savings time.

The rendering of the local date and time in the CPU follows this pattern:

$UTC (DST21) + ($TimeZone (DST384) * 60) + ($SummerTime (ST768) * 3600) = $LocalTime (DST22)

 


Date/Time Structures

As stated earlier, storing native UTC values is very economical (they only consume a single DWord location), but using the Date and Time values in a ladder logic program, or referencing the Date and Time from an HMI while the values are in UTC form is not very convenient. To alleviate this, the Do-more CPU uses Date/Time structures to make the Date and Time values more user friendly.

 

The individual pieces of the Date and the Time are stored in separate members of a Date/Time structure. The individual structure members can then be used as parameters in instructions or read / written from an HMI just like any other memory location in the CPU's memory. All Date/Time structures have the following member fields:

 

.Year is a 4-digit number representing the year.

 

.Month is a 2-digit 1 to 12 representing the month.
(1 - January, 2 - February, 3 - March, 4 - April, 5 - May, 6 - June, 7 - July, 8 - August, 9 - September, 10- October, 11 - November, 12 - December)
 

.Day is a 2-digit value from 1 to 31 representing the day of the month.
 

.DayOfWeek is a 2-digit value from 0 to 6 representing the day of the week .
(0 - Sunday, 1 - Monday, 2 - Tuesday, 3 - Wednesday, 4 - Thursday, 5 - Friday, 6 - Saturday)
 

.Hour is a 2-digit value from 0 to 23 representing the hours.
 

.Minute is a 2-digit value from 0 to 59 representing the hours.
 

.Second is a 2-digit value from 0 to 59 representing the hours.

 

.Date is a 32-bit hexadecimal value that contains the .Year, .Month, and .Day field values packed into a DWord as follows:

YYYYMMDD (use the Date Field display format in Data View to view the value in a human readable form)

 

.Time - a 32-bit hexadecimal value that contains the .DayOfWeek, .Hour, .Minute, and .Second field values packed into a DWord as follows:

DDHHMMSS (use the Time Field display format in Data View to view the value in a human readable form)

 

The default configuration of a Do-more CPU has two predefined memory blocks of Date/Time structures: SDT0 - SDT7 (System Date/Time) which contain date/time values generated the PLC, and UDT0 - UDT31 (User Date/Time) which the programmer can use in the ladder logic project. The first three system-defined Date/Time structures have the following values:


$Now (SDT0) contains the system-generated current date and time value, including adjustments for Time Zone and Daylight Saving Time.
 

$SysShutdown (SDT1) contains the date and time when the CPU was last powered OFF.
 

$SysStartup (SDT2) - contains the date and time when the CPU was last powered ON.

 

 



Using the Date and Time Programmatically

There will be instances when you want to use the PLCs current date and / or time in the project's ladder logic, for example, when storing a time-stamp that an event occurred, or triggering an event at a certain time. And while UTC values are not very human-friendly, they are very efficient when used by computers for doing these types of operations. The Do-more instruction set contains the following instructions that use the date and/or time information. Refer to the specific Help on each instruction for detailed information on using them in your ladder logic:

Use Date/Time Structures

DTCMP - Compare Date/Time compares some or all of the fields of two Date/Time structures. The calculation is always performed as First Date/Time - Second Date/Time.
 

DTDIFF - Difference between two Date/Times calculates the number of seconds of time between the contents of two Date/Time structures. The calculation is always performed as Date/Time A - Date/Time B.
 

DTOFFSET - Add Offset to Date/Time adds a specified amount of time to the contents of a Date/Time structure.

 

Convert UTC Values to Date/Time Structures and Vice Versa

DT2EPOCH - Convert Date/Time to 1970 Epoch calculates the number of seconds between the contents of the designated Date/Time structure in the CPU and midnight on January 1, 1970.
 

EPOCH2DT - Convert 1970 Epoch Time to Date/Time converts a value that represents the number of seconds since midnight on January 1, 1970 to a Date/Time structure.


Use $LocalTime in a Math Calculation

NOW - Get Date/Time as a 32-bit Integer  is a MATH function that returns a 32-bit integer value that represents the number of seconds since midnight on January 1, 1970.

 

Setting the PLC Clock

NETTIME - SNTP Client uses SNTP protocol to retrieve the clock and calendar information from a Network Time Server.

 

SETTIME - Set PLC Date/Time sets the clock to values entered from an external source (like an HMI).

.

Instruction to Copy Entire Data/Time Structures

Copy Memory Range (MEMCOPY) instruction must be used to copy entire Date/Time structures because the Date/Time information in structures contains multiple values of varying sizes. For example: MEMCOPY ($Now, UDT0) would copy the values of all of the structure members of $Now to the corresponding members of User Date/Time structure UDT0.

Instruction to Copy Individual Date/Time Structure Fields

Move Value (MOVE) instruction can be used to copy an individual Date/Time structure member to another memory location. For example, MOVE ($Now.Year, V0) will copy only the 4-digit value in $Now.Year to memory location V0.

 


See Also:

Date and Time Overview


Setting the Clock


Synchronizing the Clocks on Multiple Do-more PLCs