It would be nice if we could ask strftime to honor the tm_isdst field, to match Windows Explorer and most other apps that display file timestamps, but at least there's a simple workaround to do the calculation ourselves. When printed with explicit time codes, it becomes clear that the stamps in each column really do all represent the same instant in time: File #1 (January) File #2 (June) PDT, for example) but since most apps (including Windows Explorer) do not, an apparent one-hour inconsistency can manifest. This is evident if you use '%Z' to include the time zone in the converted string (PST vs. This way, all time stamps shown have the same implicit code. Windows uses the DST code in effect right now. This way, the time stamp has a consistent representation year-round. Python uses the DST code that was in effect at the timestamp. Python and Windows have chosen different methods to convert timestamps between UTC and the local time zone: Choose only Output Correction if you're used to seeing them jump an hour twice a year as DST takes effect and then goes away.Choose only Input Correction if you prefer timestamps that look right at their native time of year.This may be desirable (see below), but if it bothers you: Mtime = time.strftime(format,time.localtime(mtime))Ītime = time.strftime(format,time.localtime(atime))īeware, if you apply both, your Python output will again seem to mismatch your input. Mtime += 3600 * (now.tm_isdst - time.localtime(mtime).tm_isdst)Ītime += 3600 * (now.tm_isdst - time.localtime(atime).tm_isdst)Ĭtime = time.strftime(format,time.localtime(ctime)) To make Python reports match Windows Explorer, we apply the correction before calling strftime: # check if all was okĬtime += 3600 * (now.tm_isdst - time.localtime(ctime).tm_isdst) ModifyTime = Time(time.mktime(mTime_t) + 3600 * (now.tm_isdst - mTime_t.tm_isdst)) If you are setting a winter timestamp during summer, or vice versa, it will become off by an hour when its matching season comes around unless you compensate before calling SetFileTime: now = time.localtime()ĬreateTime = Time(time.mktime(cTime_t) + 3600 * (now.tm_isdst - cTime_t.tm_isdst))ĪccessTime = Time(time.mktime(aTime_t) + 3600 * (now.tm_isdst - aTime_t.tm_isdst)) In both cases, we make use of the tm_isdst field, which time.localtime conveniently calculates to tell us whether Daylight Savings Time (DST) was in effect for a particular timestamp. There are two places where you might want to correct for winter/summer difference of one hour. Mtime = time.strftime(format,time.localtime(os.path.getmtime(fName)))Ītime = time.strftime(format,time.localtime(os.path.getatime(fName))) SetFileTime(fh, createTime, accessTime, modifyTime)Ĭtime = time.strftime(format,time.localtime(os.path.getctime(fName))) Print "Change Access from",accessTime,"to %s" % (time.strftime(format,aTime_t)) Print "Change Modify from",modifyTime,"to %s" % (time.strftime(format,mTime_t)) Print "Change Create from",createTime,"to %s" % (time.strftime(format,cTime_t)) Print "Access : %s -> %s OK" % (aTime,time.strftime(format,aTime_t))įh = CreateFile(fName, GENERIC_READ | GENERIC_WRITE, 0, None, OPEN_EXISTING, 0, 0)ĬreateTime, accessTime, modifyTime = GetFileTime(fh) Print "Modify : %s -> %s OK" % (mTime,time.strftime(format,mTime_t)) Print "Create : %s -> %s OK" % (cTime,time.strftime(format,cTime_t)) MTime_t = time.localtime(time.mktime(time.strptime(mTime,format))+offset)ĪTime_t = time.localtime(time.mktime(time.strptime(aTime,format))+offset) Maybe someone can add a correction? from win32file import CreateFile, SetFileTime, GetFileTime, CloseHandleįrom win32file import GENERIC_READ, GENERIC_WRITE, OPEN_EXISTING if a time in summer is given, the result differs by one hour. The only thing I didn't solve is the summer time - winter time issue, i.e. It might be done easier and more beautiful, but it works. To shorten the search for others, the code follows here. After searching a while I got a solution. I tried to find an easy way to modifiy a file timestamp under windows using python, but there was not much clear information on the web.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |