File::getLastModificationTime() resolution

On Windows, file modification times are accurate to the millisecond. On macOS / Linux they are only accurate to the second. Can we get increased resolution? This should do it, but it’s not thoroughly tested. (Or really tested at all)

void File::getFileTimesInternal (int64& modificationTime, int64& accessTime, int64& creationTime) const
{
    modificationTime = 0;
    accessTime = 0;
    creationTime = 0;

    juce_statStruct info;

    if (juce_stat (fullPath, info))
    {
        modificationTime  = (int64) info.st_mtimespec.tv_sec * 1000 + info.st_mtimespec.tv_nsec / 1000000;
        accessTime        = (int64) info.st_atimespec.tv_sec * 1000 + info.st_atimespec.tv_nsec / 1000000;
       #if JUCE_MAC || JUCE_IOS
        creationTime      = (int64) info.st_birthtimespec.tv_sec * 1000 + info.st_birthtimespec.tv_nsec / 1000000;
       #else
        creationTime      = (int64) st_ctimespec.tv_sec * 1000 + st_ctimespec.tv_nsec / 1000000;
       #endif
    }
}

bool File::setFileTimesInternal (int64 modificationTime, int64 accessTime, int64 /*creationTime*/) const
{
    juce_statStruct info;

    if ((modificationTime != 0 || accessTime != 0) && juce_stat (fullPath, info))
    {
        struct timeval times[2];
        if (accessTime != 0)
        {
            times[0].tv_sec  = accessTime / 1000;
            times[0].tv_usec = (accessTime % 1000) * 1000;
        }
        else
        {
            times[0].tv_sec  = info.st_atimespec.tv_sec;
            times[0].tv_usec = int (info.st_atimespec.tv_nsec / 1000);
        }

        if (modificationTime != 0)
        {
            times[1].tv_sec  = modificationTime / 1000;
            times[1].tv_usec = (modificationTime % 1000) * 1000;
        }
        else
        {
            times[1].tv_sec  = info.st_mtimespec.tv_sec;
            times[1].tv_usec = int (info.st_mtimespec.tv_nsec / 1000);
        }

        return utimes (fullPath.toUTF8(), times) == 0;
    }

    return false;
}

Thanks for the code snippet, I’ve added this in 052e932. This will only affect macOS and iOS though as the higher resolution stat members aren’t supported on Linux AFAICT.

1 Like

Thanks, MacOS is all I really need. I didn’t try it on Linux.