The key to this is using FB_TzSpecificLocalTimeToFileTime to covert the current T_FILETIME to UTC using current timezone information (ST_TimeZoneInformation). This will you a UTC windows filetime (ticks) which needs to be coverted to UTC unix time.
Here is a function block implementation of this process:
Declaration
FUNCTION_BLOCK UnixTimestamp
VAR_OUTPUT
seconds: ULINT;
milliseconds: ULINT;
END_VAR
VAR
localSystemTime : FB_LocalSystemTime := ( bEnable := TRUE, dwCycle := 1 );
getTimeZoneInformation : FB_GetTimeZoneInformation;
timeZoneInformation : ST_TimeZoneInformation;
specificLocalTimeToFileTime : FB_TzSpecificLocalTimeToFileTime;
fileTime: T_FILETIME;
onZerothSecondLastCycle : BOOL;
END_VAR
Implementation
// Get local system time
localSystemTime();
// On the zeroth second of each minutesync timezone information
IF (timeZoneInformation.standardName = '' OR (localSystemTime.systemTime.wSecond = 0 AND NOT onZerothSecondLastCycle)) THEN
getTimeZoneInformation(sNetID := '', bExecute := TRUE, tzInfo => timeZoneInformation);
END_IF;
// Convert local system time to unix timestamps
specificLocalTimeToFileTime(in := Tc2_Utilities.SYSTEMTIME_TO_FILETIME(localSystemTime.systemTime), tzInfo := timeZoneInformation, out => fileTime);
seconds := (SHL(DWORD_TO_ULINT(fileTime.dwHighDateTime), 32) + DWORD_TO_ULINT(fileTime.dwLowDateTime)) / 10000000 - 11644473600;
milliseconds := (SHL(DWORD_TO_ULINT(fileTime.dwHighDateTime), 32) + DWORD_TO_ULINT(fileTime.dwLowDateTime)) / 10000 - 11644473600000;
onZerothSecondLastCycle := localSystemTime.systemTime.wSecond = 0;
Usage
VAR
unixTime: UnixTimestamp;
timestampSeconds: ULINT;
timestampMilliseconds: ULINT;
END_VAR
-----
unixTime();
timestampMilliseconds := unixTime.milliseconds;
timestampSeconds := unixTime.seconds;