Calculating Elapsed Duration Between Two Date-and-Time Values in PCS 7 CFC and WinCC

Calculating Elapsed Duration Between Two Date-and-Time Values in PCS 7 CFC and WinCC

In SIMATIC PCS 7, calculating the elapsed duration between a manually specified timestamp and the current system time to display formatted durations (such as "24 days 12 hours") on an operator station is achieved by utilizing standard IEC date/time subtraction functions or custom S7-SCL logic. In the S7-400 controller, the current time is retrieved via SFC1 (READ_CLK) and subtracted from the target timestamp using IEC standard function FC34 (SB_DT_DT), which outputs elapsed time as a 32-bit TIME variable (milliseconds). By decomposing this millisecond value into days, hours, and minutes in CFC or formatting it within a WinCC VBScript display script, the resulting duration renders clearly on process graphics.

Table of Contents

Date and Time Architecture in SIMATIC S7 (DATE_AND_TIME vs TIME)

Tracking equipment run cycles, maintenance intervals, or process aging requires calculating the mathematical difference between two timestamps. In SIMATIC STEP 7 and PCS 7, two distinct temporal data types are utilized:

Data TypeMemory FootprintInternal StructureFunctional RoleDATE_AND_TIME (DT)8 Bytes (BCD)Year, Month, Day, Hour, Minute, Second, Milliseconds, Day of weekRepresents an absolute calendar point in timeTIME (IEC Time)4 Bytes (Signed DINT)Integer milliseconds (-2,147,483,648 ms to +2,147,483,647 ms)Represents a relative duration (up to ~24.8 days)DINT (Epoch Seconds)4 Bytes (Signed DINT)Continuous seconds elapsed since reference epoch (e.g., 1970/1990)Enables calculations spanning years without overflow

Because standard IEC function FC34 outputs a 32-bit TIME variable, differences exceeding 24 days, 20 hours, 31 minutes, and 23 seconds overflow the standard signed TIME range. For long-term intervals spanning months or years, custom epoch arithmetic must be implemented.

Step-by-Step CFC Logic: SFC1, FC3, and FC34 Implementation

To construct a duration calculator in Continuous Function Charts (for intervals under 24 days):

  1. Read Current System Time:
    - In the CFC block catalog, locate Standard Library -> System Function Blocks -> SFC1 (READ_CLK).
    - Call SFC1 cyclically (e.g., in OB32 at 1000 ms); the output CDT delivers the current DATE_AND_TIME.
  2. Generate Target Timestamp:
    - If the operator inputs year, month, day, and hour as integers from WinCC, assemble them into a valid DATE_AND_TIME structure using standard function FC3 (CONCAT_DATE_TOD).
  3. Subtract Dates via FC34:
    - In the block catalog, locate Standard Library -> IEC Function Blocks -> FC34 (SB_DT_DT).
    - Interconnect SFC1.CDT to DT1 (current time).
    - Interconnect the target date structure to DT2 (start time).
    - The output RET_VAL of FC34 delivers the elapsed duration as a TIME data type in milliseconds.

Decomposing Milliseconds into Days, Hours, and Minutes

To convert raw milliseconds into discrete integer units suitable for operator display:

  1. Convert TIME to DINT (or REAL) using block DI_R.
  2. Divide total milliseconds by $86,400,000$ (milliseconds per day) to calculate Days:
    $$\text{Days} = \lfloor \text{Total_ms} / 86400000 \rfloor$$
  3. Calculate the remaining milliseconds using modulo arithmetic (MOD):
    $$\text{Remaining_ms} = \text{Total_ms} \pmod{86400000}$$
  4. Divide the remainder by $3,600,000$ (milliseconds per hour) to calculate Hours:
    $$\text{Hours} = \lfloor \text{Remaining_ms} / 3600000 \rfloor$$
  5. Expose Days and Hours as OCM parameters (S7_m_c := 'true') for automated WinCC tag generation.

Formatting Duration Strings in WinCC Graphics Designer

Alternatively, duration formatting can be offloaded entirely to the WinCC client display layer using a simple VBScript on an I/O text field:

Function OutputValue_Trigger(ByVal Item)
    Dim dtTarget, dtNow, diffSeconds, days, hours
    dtTarget = HMIRuntime.Tags("Target_Timestamp").Read
    dtNow = Now()

    diffSeconds = DateDiff("s", dtTarget, dtNow)
    If diffSeconds < 0 Then diffSeconds = 0

    days = Fix(diffSeconds / 86400)
    hours = Fix((diffSeconds Mod 86400) / 3600)

    OutputValue_Trigger = days & " days " & hours & " hours"
End Function

This VBScript eliminates S7 controller memory overhead and bypasses the 24.8-day limit of the controller's TIME data type, handling durations spanning decades cleanly.

Verification

To verify elapsed duration calculation:
1. In WinCC, enter a test start timestamp of exactly 5 days and 10 hours prior to current system time.
2. Verify that the CFC calculation logic outputs Days = 5 and Hours = 10.
3. Verify that the WinCC graphic display immediately renders: 5 days 10 hours.
4. Allow the system to run for one hour; verify that the display automatically advances to 5 days 11 hours.

Limitations and Operational Precautions

Never use FC34 directly in CFC for dates spanning more than 24 days without range checking. When elapsed time exceeds 24 days, 20 hours, standard 32-bit TIME overflows into negative values, corrupting displays and downstream interlocking logic. For long-term equipment run times, utilize the APL operating hours counter block CntOhSc (FB1803) or perform calculations in WinCC.

Engineer FAQ

Why does FC34 fail when the difference exceeds 24 days?

In STEP 7, the TIME data type is a signed 32-bit integer in milliseconds. The maximum positive value is $2,147,483,647$ ms, which equals approximately 24.85 days. Exceeding this value causes integer overflow.

Can S7-SCL handle date subtraction easier than CFC?

Yes. In S7-SCL, you can convert timestamps to DINT seconds or utilize high-level time libraries, avoiding the graphical complexity of multiple math blocks in CFC.

What block does APL provide for tracking equipment operating hours?

APL provides CntOhSc (FB1803), which tracks operating hours and maintenance countdown intervals spanning up to decades without overflow.

Technical References

Related Articles

Related Tags

  • Date and Time
  • FC34
  • Elapsed Duration
  • WinCC Scripting
  • SFC1
6 respuestas