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)
- Step-by-Step CFC Logic: SFC1, FC3, and FC34 Implementation
- Decomposing Milliseconds into Days, Hours, and Minutes
- Formatting Duration Strings in WinCC Graphics Designer
- Verification
- Limitations and Operational Precautions
- Engineer FAQ
- Technical References
- Related Articles
- Related Tags
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:
DATE_AND_TIME (DT)TIME (IEC Time)DINT (Epoch Seconds)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):
- Read Current System Time:
- In the CFC block catalog, locate Standard Library -> System Function Blocks -> SFC1 (READ_CLK).
- CallSFC1cyclically (e.g., in OB32 at 1000 ms); the outputCDTdelivers the currentDATE_AND_TIME. - Generate Target Timestamp:
- If the operator inputs year, month, day, and hour as integers from WinCC, assemble them into a validDATE_AND_TIMEstructure using standard functionFC3(CONCAT_DATE_TOD). - Subtract Dates via FC34:
- In the block catalog, locate Standard Library -> IEC Function Blocks -> FC34 (SB_DT_DT).
- InterconnectSFC1.CDTtoDT1(current time).
- Interconnect the target date structure toDT2(start time).
- The outputRET_VALofFC34delivers the elapsed duration as aTIMEdata type in milliseconds.
Decomposing Milliseconds into Days, Hours, and Minutes
To convert raw milliseconds into discrete integer units suitable for operator display:
- Convert
TIMEtoDINT(orREAL) using blockDI_R. - Divide total milliseconds by $86,400,000$ (milliseconds per day) to calculate Days:
$$\text{Days} = \lfloor \text{Total_ms} / 86400000 \rfloor$$ - Calculate the remaining milliseconds using modulo arithmetic (
MOD):
$$\text{Remaining_ms} = \text{Total_ms} \pmod{86400000}$$ - Divide the remainder by $3,600,000$ (milliseconds per hour) to calculate Hours:
$$\text{Hours} = \lfloor \text{Remaining_ms} / 3600000 \rfloor$$ - Expose
DaysandHoursas 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
- SIMATIC S7-400 System and Standard Functions Manual (A5E00261404)
- SIMATIC PCS 7 Advanced Process Library Manual (A5E33814036)
- SIMATIC WinCC V7.4 Scripting: VBS, ANSI-C, VBA Manual (A5E37536430)
Related Articles
- Resolving Negative Value Display in APL CntOhSc Operating Hours Counter
- Implementing Daily and Monthly Energy Totalizers with APL TimeTrig in PCS 7
- Resolving SICLOCK Time Jumps and Permanent Slave Mode on Redundant PCS 7 OS Servers
Related Tags
- Date and Time
- FC34
- Elapsed Duration
- WinCC Scripting
- SFC1