DateTimeFormat, format the Date/Time

List

Questa pagina fa parte del Manuale Programmazione IEC 61131-3. Vai all indice.

Questa funzione da eseguire in task Back, restituisce una stringa formattata in base alla stringa di formato indicata in pFormat utilizzando il valore di Data/Ora indicato in LDTValue.

La stringa formattata è accodata al contenuto del buffer indicato da pRString la cui dimensione deve essere passata in RSLength.

Per inizializzare la stringa di ritorno si può inserire il carattere “^” come primo carattere della definizione di formattazione. In caso di errore esecuzione la funzione ritorna 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.

Information Circle

Funzione

CODESYS: Non disponibile

LogicLab: eLLabCommonLib

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.

Immagine F_DateTimeFormat
Definizione caratteri formattazione
FormatDescription
SpaceReturned on output
dDay of the month, 2 digits with leading zeros01~31
mNumeric representation of a month, with leading zeros01~12
YA full numeric representation of a year, 4 digitsExamples: 1999 or 2003
yA two digit representation of a yearExamples: 99 or 03
H24-hour format of an hour with leading zeros00~23
iMinutes with leading zeros00~59
sSeconds with leading zeros00~59
uMicrosecond000~999
vMillisecond000~999
USeconds since the Unix EpochExample 1663863622
\Escape the following characterExample \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
    i : UDINT; (* Auxiliary variable *)
    LDTValue : LDATE_AND_TIME; (* Long Date/Time value *)
    Result1 : STRING[ 32 ]; (* Result string *)
    Result2 : STRING[ 32 ]; (* Result string *)
    Result3 : STRING[ 32 ]; (* Result string *)
    Result4 : STRING[ 32 ]; (* Result string *)
    Result5 : STRING[ 32 ]; (* Result string *)
    Result6 : 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
    i:=DateTimeFormat(LDTValue, ADR('^\E\p\o\c\h U'), ADR(Result1), SIZEOF(Result1)); //Epoch 1663863622
    i:=DateTimeFormat(LDTValue, ADR('^"Time" H\:i\:s'), ADR(Result2), SIZEOF(Result2)); //Time 16:20:33
    i:=DateTimeFormat(LDTValue, ADR('^"Long time" H\:i\:s\.vu'), ADR(Result3), SIZEOF(Result3)); //Long time 16:20:41.122918
    i:=DateTimeFormat(LDTValue, ADR('^"Date" d\/m\/Y'), ADR(Result4), SIZEOF(Result4)); //Date 22/09/2022
    i:=DateTimeFormat(LDTValue, ADR('^"Date/Time" d/m/Y\ H\:i\:s'), ADR(Result5), SIZEOF(Result5)); //Date/Time 22/09/2022 16:21:08

    // Here an example how to concatenate formatted results.

    i:=DateTimeFormat(LDTValue, ADR('^"Date" d/m/Y'), ADR(Result6), SIZEOF(Result6));
    i:=DateTimeFormat(LDTValue, ADR('", Time" H\:i\:s'), ADR(Result6), SIZEOF(Result6)); //Date 22/09/2022, Time 16:21:08

// [End of file]
Was this article helpful?