Questa funzione consente di generare una stringa formattata sulla base del formato specificato nel parametro pFormat, utilizzando il valore di data/ora fornito in LDTValue.
Il risultato della formattazione viene accodato al contenuto già presente nel buffer puntato da pRString; è responsabilità del chiamante garantire che tale buffer abbia una dimensione sufficiente, indicata dal parametro RSLength, per contenere la stringa risultante.
Qualora si desideri inizializzare (ovvero svuotare) la stringa di destinazione prima della formattazione, è possibile anteporre il carattere speciale “^” all’inizio della stringa di formato (pFormat). In tal caso, il contenuto precedente di pRString verrà ignorato e sostituito dal nuovo risultato formattato.
La funzione restituisce TRUE in caso di esecuzione corretta; in caso contrario (ad esempio formato non valido, buffer insufficiente o parametri errati) restituisce FALSE.
Sostituisce il FB “DateTimeFormat”
Per eseguire la formattazione della data/ora si utilizzava il FB DateTimeFormat, ora sostituito da una funzione con lo stesso nome. Se non si vuole modificare il programma nella libreria eLLabObsoleteLib si trova il FB FBDateTimeFormat copia del precedente FB.
Approfondimenti
- Vedi eventuali note di rilascio raccolta librerie.
Descrizione
LDTValue (LDATE_AND_TIME) Valore data in Unix Epoch time (1 gennaio 1970).
pFormat (@STRING) Puntatore a definizione formattazione.
pRString (@STRING) Puntatore a buffer stringa risultato formattata.
RSLength (UDINT) Dimensione buffer stringa risultato formattata.
La funzione ritorna un (BOOL) FALSE se errore esecuzione, TRUE se esecuzione corretta.

Definizione caratteri formattazione
| Format | Description | |
|---|---|---|
| Space | Returned on output | |
| d | Day of the month, 2 digits with leading zeros | 01~31 |
| m | Numeric representation of a month, with leading zeros | 01~12 |
| Y | A full numeric representation of a year, 4 digits | Examples: 1999 or 2003 |
| y | A two digit representation of a year | Examples: 99 or 03 |
| H | 24-hour format of an hour with leading zeros | 00~23 |
| i | Minutes with leading zeros | 00~59 |
| s | Seconds with leading zeros | 00~59 |
| u | Microsecond | 000~999 |
| v | Millisecond | 000~999 |
| U | Seconds since the Unix Epoch | Example 1663863622 |
| \ | Escape the following character | Example \H |
| ^ | Initialize the return string (To be used at beginning of definition) |
Esempi
Come utilizzare gli esempi.
Nell’esempio è riportata la formattazione della Data/Ora di sistema in diversi formati.
LogicLab (Ptp201, ST_DateTimeFormat)
PROGRAM ST_DateTimeFormat
VAR
RSts : ARRAY[0..7] OF BOOL; (* Result status *)
DTimeLocal : DATE_AND_TIME; (* Local Date/Time *)
LDTValue : LDATE_AND_TIME; (* Long Date/Time value *)
Result : ARRAY[0..6] OF STRING[ 32 ]; (* Result string *)
END_VAR
// *****************************************************************************
// PROGRAM "ST_DateTimeFormat"
// *****************************************************************************
// The system Date/Time is formatted in some various strings.
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// DATE/TIME FORMATTING
// -------------------------------------------------------------------------
// Date/Time formatting examples.
LDTValue:=TO_LDATE_AND_TIME(SysDateGetNs()); //Long Date/Time value
RSts[0]:=DateTimeFormat(LDTValue, ADR('^\E\p\o\c\h U'), ADR(Result[0]), SIZEOF(Result[0])); //Epoch 1663863622
RSts[1]:=DateTimeFormat(LDTValue, ADR('^"Time" H\:i\:s'), ADR(Result[1]), SIZEOF(Result[1])); //Time 16:20:33
RSts[2]:=DateTimeFormat(LDTValue, ADR('^"Long time" H\:i\:s\.vu'), ADR(Result[2]), SIZEOF(Result[2])); //Long time 16:20:41.122918
RSts[3]:=DateTimeFormat(LDTValue, ADR('^"Date" d\/m\/Y'), ADR(Result[3]), SIZEOF(Result[3])); //Date 22/09/2022
RSts[4]:=DateTimeFormat(LDTValue, ADR('^"Date/Time" d/m/Y\ H\:i\:s'), ADR(Result[4]), SIZEOF(Result[4])); //Date/Time 22/09/2022 16:21:08
// Here is an example of how to concatenate formatted results.
RSts[5]:=DateTimeFormat(LDTValue, ADR('^"Date" d/m/Y'), ADR(Result[5]), SIZEOF(Result[5]));
RSts[6]:=DateTimeFormat(LDTValue, ADR('", Time" H\:i\:s'), ADR(Result[5]), SIZEOF(Result[5])); //Date 22/09/2022, Time 16:21:08
// Here is an example of how to use localized Date/Time.
DTimeLocal:=TO_DATE_AND_TIME(SysDateLocalize(SysDateGetS(), +1, DAYLIGHT_ZONE#DLZ_EUROPE)); //Local Date/Time (S)
LDTValue:=TO_LDATE_AND_TIME(TO_ULINT(DTimeLocal)*ULINT#1000000000); //Local time in nanosecond
RSts[7]:=DateTimeFormat(LDTValue, ADR('^"Date/Time" d/m/Y\ H\:i\:s'), ADR(Result[6]), SIZEOF(Result[6])); //Date/Time 22/09/2022 18:21:08
// [End of file]