Skip to content

Convert FatFS date/time to Unix timestamp in SFTP#977

Open
ejohnstown wants to merge 1 commit into
wolfSSL:masterfrom
ejohnstown:fatfs-times
Open

Convert FatFS date/time to Unix timestamp in SFTP#977
ejohnstown wants to merge 1 commit into
wolfSSL:masterfrom
ejohnstown:fatfs-times

Conversation

@ejohnstown
Copy link
Copy Markdown
Contributor

@ejohnstown ejohnstown commented May 12, 2026

  • Add SetAttrTime() helper to decode fdate/ftime into a Unix timestamp via mktime() and assigns it to atr->atime/mtime.
  • Use it from both SFTP_GetAttributes() (was using raw fdate) and SFTP_GetAttributes_Handle() (was using raw ftime, losing the date).

Copilot AI review requested due to automatic review settings May 12, 2026 22:58
@ejohnstown ejohnstown marked this pull request as draft May 12, 2026 22:59
@ejohnstown
Copy link
Copy Markdown
Contributor Author

This should hopefully fix issue #975.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the SFTP FatFS backend to report file timestamps as Unix epoch seconds instead of returning raw FatFS fdate values, aligning the returned attributes with SFTP’s expected time representation.

Changes:

  • Add a FatFS fdate/ftime decoder (TimeTo32_FatFS()) that builds a struct tm and converts it via mktime().
  • Populate atr->atime / atr->mtime using the converted Unix timestamp (rather than raw info.fdate).
Comments suppressed due to low confidence (2)

src/wolfsftp.c:4893

  • struct tm.tm_year is defined as “years since 1900”, but WS_GETYEAR(d) returns a FatFS year offset from 1980 (0..127). As written this will convert 1980->1900, 2024->1944, etc. Adjust the year by +80 (same as the existing Nucleus TimeTo32() implementation above) before calling mktime().
    tmp.tm_mday = WS_GETDAY(d);
    tmp.tm_mon  = WS_GETMON(d);
    tmp.tm_year = WS_GETYEAR(d);
    tmp.tm_hour = WS_GETHOUR(t);

src/wolfsftp.c:4897

  • This adds a new platform-specific date/time conversion that’s easy to get subtly wrong (year base, month base, seconds/2). There is an internal regression test for the analogous Nucleus month conversion (TestNucleusMonthConversion); consider adding a small WOLFSSH_TEST_INTERNAL helper + regress test for FatFS conversion (at least year base + month conversion) so future refactors don’t break timestamps again.
static word32 TimeTo32_FatFS(word16 d, word16 t)
{
    struct tm tmp = {0};

    tmp.tm_mday = WS_GETDAY(d);
    tmp.tm_mon  = WS_GETMON(d);
    tmp.tm_year = WS_GETYEAR(d);
    tmp.tm_hour = WS_GETHOUR(t);
    tmp.tm_min  = WS_GETMIN(t);
    tmp.tm_sec  = WS_GETSEC(t);

    return mktime(&tmp);

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/wolfsftp.c Outdated
Comment thread src/wolfsftp.c Outdated
Comment thread src/wolfsftp.c Outdated
- Add SetAttrTime() helper to decode fdate/ftime into a Unix
  timestamp via mktime() and assigns it to atr->atime/mtime.
- Use it from both SFTP_GetAttributes() (was using raw fdate) and
  SFTP_GetAttributes_Handle() (was using raw ftime, losing the date).
@ejohnstown ejohnstown marked this pull request as ready for review May 13, 2026 16:13
@ejohnstown ejohnstown self-assigned this May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants