The easiest thing is that you simply read the ARM Cortex cycle counter:
https://mcuoneclipse.com/2017/01/30/cycle-counting-on-arm-cortex-m-with-dwt/
https://mcuoneclipse.com/2018/06/28/measuring-arm-cortex-m-cpu-cycles-spent-with-the-mcuxpresso-ecli...
With the SystemCoreClock variable you know the clock frequency, with this you have a time information. Just keep in mind that this would not reflect any clock speed changes.
With the above you can measure the time elapsed for a function too.
Otherwise you can use the RTC registers too for a system time.
I hope this helps,
Erich