Solved Set LastModified date of file
-
Hi
I dont think you have a virus :)You are right its related to the isdst / daylight savings.
http://stackoverflow.com/questions/3660983/c-time-t-problem
But it seems you tried all values for it and it still have the 1 hour diff so you should try some of the suggestion from the links.
Some seems to do
setenv("TZ", "", 1);
tzset();
before mktime to correct it. -
@samdol In fact, you may just pass a null pointer to the second argument of utime. Then it will be set to current time.
That is how I implement touch():
https://github.com/benlau/qtshell/blob/master/qtshell.cpp#L178
-
@benlau QtShell looks nice, thanks for sharing!
-
@benlau why you do this change? path.toLocal8Bit().constData()
-
@Jeronimo Convert QString to const char*.
p.s Just updated the source to use toUtf8() instead of toLocal8Bit().
-
In fact my computer adjust clock for Daylight saving time.
So I did,
Control Panel --> Time Zone Settings
--> Uncheck Automatically adjust clock for Daylight Saving Time.Now the lastmodified time on system and explorer are the same.
Thank you. -
@benlau
Hi Benla,
I tested the touch function in qtshell.cpp.bool QtShell::touch(const QString &path)
{
...
QByteArray bytes = path.toUtf8();if (utime(bytes.constData(), 0) == -1) { qWarning() << "utimes failed:" << path;
res = false;
...
}It works fine for english filename. But if the filename contains some chinese
charactors then it gives an error and I could not modified time stamp of file.
I am using Windows 7. Thank you. -
@samdol Hi, I just setup a Windows 10 to test. Both of VC and Mingw works. Could you send me the failed file name?
-
@benlau
Here is a zip file containing two files. link textThe following is the code, I tested on my Windows7 English version.
bool res = true; QFileInfo info(path); if (!info.exists()) { QFile file(path); if (!file.open(QIODevice::WriteOnly)) { qWarning() << "Failed to create file:" << path; res = false; } file.close(); } else { QByteArray bytes = path.toUtf8(); if (utime(bytes.constData(), 0) == -1) { qWarning() << "utimes failed:" << path; res = false; } }
-
Thank you for this, very simple, quick and it worked for me (tested only on windows so far)