Page 1 of 2

Enable Time stamp on FatFs SD card

Posted: Tue Sep 11, 2018 6:32 pm
by snahmad75
Hi,

Does any one knows how can enable current time stamp for files which gets created and update on SD card File system.

I can pass current time stamp from windows to Esp32 via http post.
Thanks,
Naeem

Re: Enable Time stamp on FatFs SD card

Posted: Wed Sep 12, 2018 3:45 pm
by snahmad75
Can any one reply to this thread please. waiting.

At the end my Http Web server mongoose need to return time stamp for each file. I guess using file stats function to return file time stamp in order to tell web browser to cache file and do to get reloaded/read again from SD card file system.

I know current time stamp. I set file stamp during file creation or updating file.

Re: Enable Time stamp on FatFs SD card

Posted: Wed Sep 12, 2018 7:47 pm
by loboris
Why do you think files on SD Card do not have timestamp?

Here is simple example, the result printed from this simple loop:

Code: Select all

    vTaskDelay(5000 / portTICK_RATE_MS);
    for (int n=1; n<5; n++) {
        ESP_LOGI(TAG, "Opening file");
        f = fopen("/sdcard/hello.txt", "w");
        if (f == NULL) {
            ESP_LOGE(TAG, "Failed to open file for writing");
            return;
        }
        fprintf(f, "Hello %s!\n", card->cid.name);
        fclose(f);
        ESP_LOGI(TAG, "File written");
        if (stat("/sdcard/hello.txt", &st) == 0) {
            strftime(date, 20, "%d-%m-%y %H:%M:%S", gmtime(&(st.st_mtime)));
            ESP_LOGI(TAG, "=== hello.txt created ===");
            ESP_LOGI(TAG, "File Size: %d bytes",(int)st.st_size);
            ESP_LOGI(TAG, "File Time: %d [%s]",(int)st.st_mtime, date);
        }
        ESP_LOGI(TAG, "Waiting 5 seconds");
        vTaskDelay(5000 / portTICK_RATE_MS);
    }
Log:

Code: Select all

I (396) example: Initializing SD card
I (400) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
Name: SL08G
Type: SDHC/SDXC
Speed: 20 MHz
Size: 7580MB
I (10476) example: Opening file
I (10486) example: File written
I (10487) example: === hello.txt created ===
I (10487) example: File Size: 13 bytes
I (10488) example: File Time: 315532810 [01-01-80 00:00:10]
I (10494) example: Waiting 5 seconds
I (15499) example: Opening file
I (15510) example: File written
I (15511) example: === hello.txt created ===
I (15511) example: File Size: 13 bytes
I (15512) example: File Time: 315532814 [01-01-80 00:00:14]
I (15518) example: Waiting 5 seconds
I (20522) example: Opening file
I (20533) example: File written
I (20533) example: === hello.txt created ===
I (20534) example: File Size: 13 bytes
I (20535) example: File Time: 315532820 [01-01-80 00:00:20]
I (20541) example: Waiting 5 seconds
I (25545) example: Opening file
I (25556) example: File written
I (25557) example: === hello.txt created ===
I (25557) example: File Size: 13 bytes
I (25558) example: File Time: 315532824 [01-01-80 00:00:24]
I (25564) example: Waiting 5 seconds

Re: Enable Time stamp on FatFs SD card

Posted: Wed Sep 12, 2018 8:26 pm
by snahmad75
ok, this is reading time stamp of file. If I put my SD card to read in my Windows machine. Time stamp columns was blank in windows explorer.

I will try your code. How can I set/update time stamp of file. current time it is showing 1980.

Re: Enable Time stamp on FatFs SD card

Posted: Wed Sep 12, 2018 10:08 pm
by loboris
Just set the right system time using SNTP or otherwise. The file timestamp is set when file is created or modified.
The code was executed after boot with no time set.

Re: Enable Time stamp on FatFs SD card

Posted: Sun Sep 16, 2018 11:57 am
by snahmad75
I want to provide my own time stamp.

How can I override fatfs get_fattime to return my own file time stamp.

Re: Enable Time stamp on FatFs SD card

Posted: Sun Sep 16, 2018 12:23 pm
by snahmad75
I am trying f_utime to st time stamp for file.

http://www.elm-chan.org/fsw/ff/doc/utime.html


not working for me.

I am getting FR_NO_PATH error.


Any idea?

Re: Enable Time stamp on FatFs SD card

Posted: Sun Sep 16, 2018 1:42 pm
by ESP_igrr
What path are you passing to this function, can you give an example? And what is the actual path of the same file on SD card?

Re: Enable Time stamp on FatFs SD card

Posted: Sun Sep 16, 2018 6:47 pm
by snahmad75
I am sure my file path is correct.

Can you try yourself.

Also I used this fatfs library. I manged to override get_fattime function and return my own date and time. but i cannot do on esp32. esp32 fatfs does not allow. compilers says it is already define.

http://elm-chan.org/fsw/ff/doc/fattime.html

kindly do try this two options as your end. let me know results.

Re: Enable Time stamp on FatFs SD card

Posted: Mon Sep 17, 2018 3:07 pm
by ESP_igrr
Well, you say above that you are getting FR_NO_PATH error, and then you say that the path is certainly correct... but if it was correct, FATFS would not be returning this error, right?

get_fattime function is implemented in fatfs component of ESP-IDF, if you implement another copy in your application you will get a duplicate definition error. To override a function implemented in IDF you may copy the whole fatfs component to your project components directory, and do modifications there.