Error while saving data in text file

I created a function that saves the timestamp in a text file, but it saves unreadable. Full of invalid characters, example: Ÿ@ O ƒº!¥ Ÿ@ O ƒº!¥ Ÿ@ O ƒº!¥ Ÿ

#include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include "RTClib.h"
    
    RTC_Millis rtc;
    File txt;
    const char * getCurrentTimestamp()
    {
        char dateBuffer[40];
        DateTime now = rtc.now();
        sprintf(dateBuffer,"%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());
        return dateBuffer;
    }
    
    void saveLog()
    {
      txt = SD.open("dados.txt", FILE_WRITE);
      txt.print(getCurrentTimestamp());
      txt.close();
    }
    
    void setup() {
      pinMode(53, OUTPUT);
      SD.begin(4); 
      Serial.begin(9600);
      rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
    }
    
    void loop() {
      Serial.println(getCurrentTimestamp());
      saveLog();
      delay(1000);
    }

My code is very simple, do not understand where I went wrong. if I copy the following code snippet for saveData() function and save the dateBuffer variable, everything works normally.

  char dateBuffer[40];
    DateTime now = rtc.now();
    sprintf(dateBuffer,"%02u-%02u-%04u %02u:%02u:%02u", now.day(), now.month(), now.year(), now.hour(), now.minute(), now.second());

how to save the timestamp correctly in the file through the function getCurrentTimestamp() ?

Your variable "char dateBuffer[40];" is a LOCAL variable that is ONLY VALID while your function is executed. As soon as the function ends, this variable is no longer valid, the RAM will be released and overwritten.

You can either declare the variable 'static' to become valid all the time:

static char dateBuffer[40];

Or you can use a global variable to format the timestamp.

Thank you, my friend