Siemens Industry Online Support
Siemens AG
Entry type: FAQ Entry ID: 21222026, Entry date: 01/12/2016

How can you read out the date and time with SFC1 (READ_CLK) and set them with SFC0 (SET_CLK)?

  • Entry
  • Associated product(s)
The system functions SFC1 and SFC0 are respectively for reading out and setting the system clock of a CPU. The data of the system clock is stored in a variable of the data type DATE_AND_TIME. Access to the single components of the system clock is via a pointer.

You can use the system functions SFC1 (READ_CLK) and SFC0 (SET_CLK) respectively to read out and set the system clock of a CPU. The system clock is stored in a variable of the data type "DATE_AND_TIME". The data type DATE_AND_TIME occupies 8 bytes which contain the information in BCD format about the year, month, day, hour, etc. The range covered is:

  • DT#1990-1-1-0:0:0.0 to DT#2089-12-31-23:59:59.999

The table below shows the occupation of the bytes in the data type DATE_AND_TIME taking an example for Thursday, January 2, 2014 3:05:11.854 AM.

Absolute addressContent


Byte 0YearB#16#14
Byte 1MonthB#16#01
Byte 2DayB#16#02
Byte 3HourB#16#03
Byte 4MinuteB#16#05
Byte 5SecondsB#16#11
Byte 6 + 4 MSB* of Byte 7
(Word 6)
Byte 7 (4 LSB*)

1: Sunday
2: Monday
3: Tuesday
4: Wednesday
5: Thursday
6: Friday
7: Saturday


* 12 bits are required to display the maximum millisecond value range of 999 in BCD format. For this reason the millisecond content occupies Byte 6 and the 4 most significant bits (MSB) of Byte 7. The remaining 4 least significant bits (LSB) of Byte 7 are used to code the day of the week.

Access to the content of a single variable of the data type DATE_AND_TIME is made via pointer arithmetic. The procedure for this when reading out or setting the system clock is shown in the following instructions taking a sample program with 4 networks in the OB1.

Instructions for reading out the system clock with SFC1 (READ_CLK)

  1. Call the system function SFC1 (READ_CLK) to read out the system clock (Fig. 1).
    At the output parameter "CDT" you receive the current system clock time of the CPU. Interconnect the parameter "CDT" with a temporary variable (here "readDateAndTime") of the data type DATE_AND_TIME.

    Fig. 1
  2. Using the instruction "LAR1 P##readDateAndTime" (pointer) you download the address of the temporary variable "readDateAndTime" into the address register AR1 (Fig. 2). Then, via indirect load commands, you access the content of the separate bytes/words of the variable "readDateAndTime" and have them displayed via the operands provided for this.
  3. Example for the access to Byte 3 (hour) and output via the operand "Read".Hour (here DB1.DBB3):
    LB[AR1, P#3.0]

    T "Read".Hour

  4. Because of the overlapping in Bytes 6 and 7 for milliseconds and day of the week you have to access Word 6 for access to the milliseconds and shift the content 4 bits to the right before output (SRW 4). For output of the day of the week you have to mask the Byte 7 with B#16#F in order to obtain only the 4 LSB of Byte 7.

    Fig. 2

Instructions for setting the system clock with SFC0 (SET_CLK)

  1. You define a temporary variable of the data type DATE_AND_TIME, "writeDateAndTime", for example, and download the address of this variable into the address register AR1 using the instruction "LAR1 P##writeDateAndTime" (Fig. 3). Downloading and transferring the data to the separate bytes for year, month, day, hour, etc. is done via indirect addressing.
  2. Example for transferring the hour to be set "Write".Hour (here DB2.DBB3) to Byte 3 (Hour) in the temporary variable "writeDateAndTime":

    L "Write".Hour
    T B[AR1, P#3.0]

  3. Because of the overlapping in Bytes 6 and 7 for milliseconds and day of the week, before transfer of the value to be set for the milliseconds you have shift the content of the operand (here "Write".Millisecond, DB2.DBW6) 4 bits to the left (SLW 4).

  4. You do not need to set the day of the week because this is derived from the date set.

    Fig. 3
  5. Call the system function SFC0 (SET_CLK) to set the system clock and provide the input parameter "PDT" with the temporary variable "writeDateAndTime" (Fig. 4). It is important that the SFC0 is called only for one single cycle, otherwise the system clock would be set again at each cycle and would therefore remain on the time set.

  6. In the example (Fig. 4) the operand "Write".setCLK (here DB2.DBX8.0) is checked for a positive edge. In the case of a positive edge the SFC0 is called and the system clock is set to the values given in the temporary variable "writeDateAndTime".

    Fig. 4

Further Information
More information about the data type "DATE_AND_TIME" and the system functions SFC0 and SFC1 is available in the Online Help of STEP 7.


Security information
In order to protect technical infrastructures, systems, machines and networks against cyber threats, it is necessary to implement – and continuously maintain – a holistic, state-of-the-art IT security concept. Siemens’ products and solutions constitute one element of such a concept. For more information about cyber security, please visit
Rate entry
no rating
Requests and feedback
What do you want to do?
Note: The feedback always relates to the current entry / product. Your message will be forwarded to our technical editors working in the Online Support. In a few days, you will receive a response if your feedback requires one. If we have no further questions, you will not hear from us.