I try to set the file timestamp with with SD.h If i use sdfat.h then it works fine but i must use SD.h.

Here is my code:

    #include <SPI.h>
#include <SD.h>

File myFile;

unsigned int year = 2011;
byte month = 7;     
byte day = 21;
byte hour = 9;
byte minute = 11;
byte second = 33;

void dateTime(uint16_t* date, uint16_t* time)
  *date = FAT_DATE(2011, 7, 21);
  *time = FAT_TIME(hour, minute, second);

void setup() {
  // Open serial communications and wait for port to open:

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");

  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");

  // open a new file and immediately close it:
  Serial.println("Creating example.txt...");
  SD.dateTimeCallback(dateTime);  // !!! Set the Timestamp !!!
  myFile = SD.open("example.txt", FILE_WRITE);

  // Check to see if the file exists:
  if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
  } else {
    Serial.println("example.txt doesn't exist.");


void loop() {
  // nothing happens after setup finishes.

This i have found in SD.H:

    // Wrapper to allow obsolete datetimecallback use, silently convert to time_t in wrappertimecb
void dateTimeCallback(void (*cb)(uint16_t*, uint16_t*)) {
    extern void (*__SD__userDateTimeCB)(uint16_t*, uint16_t*);
    __SD__userDateTimeCB = cb;

This is only a test. But the file date time is always 21.12.2097 23:00

Any suggestions what is wrong and how can i set the timestamp? Thank you.

  • 1
    You can print something inside the callback function, to see if it is called at least. – Michel Keijzers Jun 5 at 11:47
  • Thank you for this idea. and yes the callback function is called. – Pascal Jun 5 at 12:05
  • What SD library are you using? – Edgar Bonet Jun 5 at 13:25
  • SD.h from the esp8266 core 2.7.1 – Pascal Jun 5 at 14:29
  • In that case I don't know ... I read somewhere that the callback is not used in all libraries, but in your case it seems called. If I knew for sure or most certain, I would have made it an answer. Maybe you can find the original source code to see what the flow is. Also you might check to remove it or write different values to see if it changes (although it would be strange if it's called and not being used). Than you possibly can reverse engineer what values should be set in the callback function. – Michel Keijzers Jun 5 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.