-->
Page 1 of 1

LittleFS Timestamp --wrong in new function

PostPosted: Thu May 21, 2020 8:54 am
by Sirquil
I am using the "LittleFS"library example and replacing thelistDir function with new "listDel function; issue I have is wrong timestamp date and time. Only change from example sketch was replacing the function.

Timestamps are correct with this function.
Original library example function:

Code: Select allvoid listDir(const char * dirname) {
  Serial.printf("Listing directory: %s\n", dirname);

  Dir root = LittleFS.openDir(dirname);

  while (root.next()) {
    File file = root.openFile("r");
    Serial.print("  FILE: ");
    Serial.print(root.fileName());
    Serial.print("  SIZE: ");
    Serial.print(file.size());
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();
    file.close();
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("    CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
    tmstruct = localtime(&lw);
    Serial.printf("  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
  }
}


Timestamps are wrong with this function.
New "listDel" function:

Code: Select allvoid listDel(const char * dirname) {
  Serial.printf("Listing directory: %s\n", dirname);

  Dir root = LittleFS.openDir("/");

  while(root.next())
  {

    File file = root.openFile("r");
 
    String str = root.fileName();
         
    i++;
    filelist[i] = strdup(str.c_str());
    Serial.print(filelist[i]);
    Serial.print("  ");
    Serial.print(i);
    Serial.println("");
    file = file.openNextFile();
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();
    file.close();
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("    CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
    tmstruct = localtime(&lw);
    Serial.printf("  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);

   
  }
   
  for(i = 1;i < 5; i++)  //Delete only first four files; keep from getting too many log files.
  {
     
      LittleFS.remove("/" + String(filelist[i]));
      Serial.print("Removed:  ");
      Serial.print(filelist[i]);
      Serial.print("  ");
      Serial.print(i);
      Serial.println("");

  }
  i = 0; 
   

   
}



Why do I have wrong Timestamp using "listDel" instead of "listDir?"

Attached modified library example uses a modified "ESP8266FtpServer.cpp for use with "LittleFS."
"LittleFS" is the replacement for "SPIFFS."

Purpose of "listDel" function is put filenames into an array; then to delete the first four files.

William

Re: LittleFS Timestamp --wrong in new function

PostPosted: Thu May 21, 2020 2:48 pm
by Sirquil
Serial Monitor output for "listDir" function and "listDel" function are attached.

William

Re: Solved --LittleFS Timestamp --wrong in new function

PostPosted: Fri May 22, 2020 12:38 am
by Sirquil
Found line of code that was interferring with getting a valid timestamp.


Code: Select allvoid listDel(const char * dirname) {
  Serial.printf("Function listDel --Listing directory: %s\n", dirname);

  Dir root = LittleFS.openDir("/");

  while(root.next())
  {

    File file = root.openFile("r");
 
    String str = root.fileName();
         
    i++;
    filelist[i] = strdup(str.c_str());
    Serial.print(filelist[i]);
    Serial.print("  ");
    Serial.print(i);
    Serial.println("");
    //file = file.openNextFile();    --Not required; was interferring with timestamp
    time_t cr = file.getCreationTime();
    time_t lw = file.getLastWrite();
    file.close();
    struct tm * tmstruct = localtime(&cr);
    Serial.printf("    CREATION: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
    tmstruct = localtime(&lw);
    Serial.printf("  LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);

   
  }
   
  for(i = 1;i < 5; i++)  //Delete only first four files; keep from getting too many log files.
  {
     
      LittleFS.remove("/" + String(filelist[i]));
      Serial.print("Removed:  ");
      Serial.print(filelist[i]);
      Serial.print("  ");
      Serial.print(i);
      Serial.println("");

  }
  i = 0; 
   

   
}


William