﻿<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://www.plcopen.org/xml/tc6_0200">
  <fileHeader companyName="" productName="CODESYS" productVersion="CODESYS V3.5 SP10 Patch 1" creationDateTime="2020-01-03T09:07:28.2215204" />
  <contentHeader name="PLC090419_1.project" modificationDateTime="2019-04-09T18:09:39">
    <coordinateInfo>
      <fbd>
        <scaling x="1" y="1" />
      </fbd>
      <ld>
        <scaling x="1" y="1" />
      </ld>
      <sfc>
        <scaling x="1" y="1" />
      </sfc>
    </coordinateInfo>
    <addData>
      <data name="http://www.3s-software.com/plcopenxml/projectinformation" handleUnknown="implementation">
        <ProjectInformation>
          <property name="Project" type="string">PLC090419_1</property>
        </ProjectInformation>
      </data>
    </addData>
  </contentHeader>
  <types>
    <dataTypes />
    <pous>
      <pou name="fbdMakeDateTimeFromINTs" pouType="functionBlock">
        <interface>
          <inputVars>
            <variable name="Year">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="Month">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="Day">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="Hour">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="Minute">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="Second">
              <type>
                <INT />
              </type>
            </variable>
          </inputVars>
          <outputVars>
            <variable name="DateTimeNow">
              <type>
                <DT />
              </type>
            </variable>
          </outputVars>
          <localVars>
            <variable name="LeapYearCheck">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="NoofLeapYears">
              <type>
                <LINT />
              </type>
            </variable>
            <variable name="NoofNonLeapYears">
              <type>
                <LINT />
              </type>
            </variable>
            <variable name="CurrentYearIsLeapYear">
              <type>
                <BOOL />
              </type>
            </variable>
            <variable name="NoofSecondsToLastYear">
              <type>
                <LINT />
              </type>
            </variable>
            <variable name="DaysThisYearAsSeconds">
              <type>
                <LINT />
              </type>
            </variable>
            <variable name="HoursThisDayAsSeconds">
              <type>
                <LINT />
              </type>
            </variable>
            <variable name="MinutesThisHourAsSeconds">
              <type>
                <LINT />
              </type>
            </variable>
            <variable name="DaysPerMonthLookup">
              <type>
                <array>
                  <dimension lower="1" upper="12" />
                  <baseType>
                    <LINT />
                  </baseType>
                </array>
              </type>
              <initialValue>
                <arrayValue>
                  <value>
                    <simpleValue value="31" />
                  </value>
                  <value>
                    <simpleValue value="28" />
                  </value>
                  <value>
                    <simpleValue value="31" />
                  </value>
                  <value>
                    <simpleValue value="30" />
                  </value>
                  <value>
                    <simpleValue value="31" />
                  </value>
                  <value>
                    <simpleValue value="30" />
                  </value>
                  <value repetitionValue="2">
                    <simpleValue value="31" />
                  </value>
                  <value>
                    <simpleValue value="30" />
                  </value>
                  <value>
                    <simpleValue value="31" />
                  </value>
                  <value>
                    <simpleValue value="30" />
                  </value>
                  <value>
                    <simpleValue value="31" />
                  </value>
                </arrayValue>
              </initialValue>
            </variable>
            <variable name="NoofCompleteDaysThisYear">
              <type>
                <LINT />
              </type>
            </variable>
            <variable name="Loop">
              <type>
                <INT />
              </type>
            </variable>
            <variable name="NoofSecondsPerYear">
              <type>
                <LINT />
              </type>
              <initialValue>
                <simpleValue value="31536000" />
              </initialValue>
            </variable>
            <variable name="NoofSecondsPerLeapYear">
              <type>
                <LINT />
              </type>
              <initialValue>
                <simpleValue value="31622400" />
              </initialValue>
            </variable>
            <variable name="NoofSecondsPerDay">
              <type>
                <LINT />
              </type>
              <initialValue>
                <simpleValue value="86400" />
              </initialValue>
            </variable>
            <variable name="NoofSecondsPerHour">
              <type>
                <LINT />
              </type>
              <initialValue>
                <simpleValue value="3600" />
              </initialValue>
            </variable>
            <variable name="NoofSecondsPerMinute">
              <type>
                <LINT />
              </type>
              <initialValue>
                <simpleValue value="60" />
              </initialValue>
            </variable>
            <variable name="SecondsThisMinuteAsSeconds">
              <type>
                <LINT />
              </type>
            </variable>
          </localVars>
        </interface>
        <body>
          <ST>
            <xhtml xmlns="http://www.w3.org/1999/xhtml">//Find number of leap years upto last year
NoofLeapYears := 0;
FOR LeapYearCheck := INT#1972 TO (Year - 1) DO
	IF LeapYearCheck MOD 4 = 0 THEN
		NoofLeapYears := NoofLeapYears + 1;
	END_IF;
END_FOR;

//Find number of non-leap years
NoofNonLeapYears := Year - INT#1970 - NoofLeapYears;

//Determine this year is a leap year
CurrentYearIsLeapYear := Year MOD (4) = 0;

//Seconds to last complete year
NoofSecondsToLastYear := (NoofSecondsPerLeapYear * NoofLeapYears) + (NoofSecondsPerYear * NoofNonLeapYears);

//Start calculating number of seconds this year - start with days
NoofCompleteDaysThisYear := 0;
FOR Loop := 1 TO Month - 1 DO
	NoofCompleteDaysThisYear := NoofCompleteDaysThisYear + DaysPerMonthLookup[Loop];
END_FOR;

//Add on 29th Feb
IF CurrentYearIsLeapYear THEN
	IF Month &gt; 2 THEN
		NoofCompleteDaysThisYear := NoofCompleteDaysThisYear + 1;
	END_IF;
END_IF;

DaysThisYearAsSeconds := (NoofCompleteDaysThisYear + INT_TO_LINT(Day) - LINT#1) * NoofSecondsPerDay;

//Seconds in the hours of today
HoursThisDayAsSeconds := INT_TO_LINT(Hour) * NoofSecondsPerHour;

//Seconds in the minutes of this hour
MinutesThisHourAsSeconds := INT_TO_LINT(Minute) * NoofSecondsPerMinute;

SecondsThisMinuteAsSeconds := INT_TO_LINT(Second);

//Combine
DateTimeNow := LINT_TO_DT(NoofSecondsToLastYear + DaysThisYearAsSeconds + HoursThisDayAsSeconds + MinutesThisHourAsSeconds + SecondsThisMinuteAsSeconds);</xhtml>
          </ST>
        </body>
        <addData>
          <data name="http://www.3s-software.com/plcopenxml/objectid" handleUnknown="discard">
            <ObjectId>47137886-f538-4d4f-998c-4aa015179f3f</ObjectId>
          </data>
        </addData>
      </pou>
    </pous>
  </types>
  <instances>
    <configurations />
  </instances>
  <addData>
    <data name="http://www.3s-software.com/plcopenxml/projectstructure" handleUnknown="discard">
      <ProjectStructure>
        <Folder Name="FBs">
          <Object Name="fbdMakeDateTimeFromINTs" ObjectId="47137886-f538-4d4f-998c-4aa015179f3f" />
        </Folder>
      </ProjectStructure>
    </data>
  </addData>
</project>