How do I set the createTime of a newly created file?

MichaelMC

Well-known member
I'm trying to set the create date and time for GPS log files, but there doesn't seem to be a method to enable this that I can find.

Using a Teensy 4.1 with internal SD card slot with 16MB extram. Card is opened with: "SD.sdfs.begin(SdioConfig(FIFO_SDIO))". And using FsFile for file access for performance. Class FsFile doesn't appear to have any timestamp creation functionality that I can see.

How do I set the timestamp?
 
If you are using SDFat library there is an example sketch:

If you are using Teensy stuff like SD..sdFat...

The files should be derived from the class File. Which has:
Code:
class FileImpl {
protected:
    virtual ~FileImpl() { }
    virtual size_t read(void *buf, size_t nbyte) = 0;
    virtual size_t write(const void *buf, size_t size) = 0;
    virtual int available() = 0;
    virtual int peek() = 0;
    virtual void flush() = 0;
    virtual bool truncate(uint64_t size=0) = 0;
    virtual bool seek(uint64_t pos, int mode) = 0;
    virtual uint64_t position() = 0;
    virtual uint64_t size() = 0;
    virtual void close() = 0;
    virtual bool isOpen() = 0;
    virtual const char* name() = 0;
    virtual bool isDirectory() = 0;
    virtual File openNextFile(uint8_t mode=0) = 0;
    virtual void rewindDirectory(void) = 0;
    virtual bool getCreateTime(DateTimeFields &tm) { return false; }
    virtual bool getModifyTime(DateTimeFields &tm) { return false; }
    virtual bool setCreateTime(const DateTimeFields &tm) { return false; }
    virtual bool setModifyTime(const DateTimeFields &tm) { return false; }
private:
    friend class File;
    unsigned int refcount = 0; // number of File instances referencing this FileImpl
};
 
FsFile doesn't appear to have implemented setCreateTime.
The compiler complains with: "error: 'struct FsFile' has no member named 'setCreateTime'; did you mean 'getCreateDateTime'?"

Using FsDateTime::setCallback() as a temporary solution, as this requires a timedate to be known before creation, which may not always be the case.

Thank you for the help.

edit: I found a solution as Kurt hinted. Reopening the file as a File stream then using setCreateTime();
 
Last edited:
Note if you are using sdFat on it's own, without SD library.
I believe you can use the timestamp method:
Code:
  /** Set a file's timestamps in its directory entry.
   *
   * \param[in] flags Values for \a flags are constructed by a bitwise-inclusive
   * OR of flags from the following list
   *
   * T_ACCESS - Set the file's last access date and time.
   *
   * T_CREATE - Set the file's creation date and time.
   *
   * T_WRITE - Set the file's last write/modification date and time.
   *
   * \param[in] year Valid range 1980 - 2107 inclusive.
   *
   * \param[in] month Valid range 1 - 12 inclusive.
   *
   * \param[in] day Valid range 1 - 31 inclusive.
   *
   * \param[in] hour Valid range 0 - 23 inclusive.
   *
   * \param[in] minute Valid range 0 - 59 inclusive.
   *
   * \param[in] second Valid range 0 - 59 inclusive
   *
   * \note It is possible to set an invalid date since there is no check for
   * the number of days in a month.
   *
   * \note
   * Modify and access timestamps may be overwritten if a date time callback
   * function has been set by dateTimeCallback().
   *
   * \return true for success or false for failure.
   */
  bool timestamp(uint8_t flags, uint16_t year, uint8_t month, uint8_t day,
                 uint8_t hour, uint8_t minute, uint8_t second) {
    return m_fFile   ? m_fFile->timestamp(flags, year, month, day, hour, minute,
                                          second)
           : m_xFile ? m_xFile->timestamp(flags, year, month, day, hour, minute,
                                          second)
                     : false;
  }
 
Back
Top