且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

windows下用python修改文件创建/访问/写入时间戳

更新时间:2023-10-04 21:51:04

您可能需要在两个地方更正冬/夏一小时的差异.在这两种情况下,我们都使用 tm_isdst 字段,time.localtime 可以方便地计算出 夏令时 (DST)对特定时间戳有效.

There are two places where you might want to correct for winter/summer difference of one hour. 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.

如果您在夏季设置冬季时间戳,反之亦然,除非您在调用 SetFileTime 之前进行补偿,否则它将在其匹配季节到来时关闭一个小时:

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()
createTime = Time(time.mktime(cTime_t) + 3600 * (now.tm_isdst - cTime_t.tm_isdst))
accessTime = Time(time.mktime(aTime_t) + 3600 * (now.tm_isdst - aTime_t.tm_isdst))
modifyTime = Time(time.mktime(mTime_t) + 3600 * (now.tm_isdst - mTime_t.tm_isdst))
SetFileTime(fh, createTime, accessTime, modifyTime) 

输出校正

为了使 Python 报告与 Windows 资源管理器匹配,我们在调用 strftime 之前应用更正:

# check if all was ok
now = time.localtime()
ctime = os.path.getctime(fName)
mtime = os.path.getmtime(fName)
atime = os.path.getatime(fName)
ctime += 3600 * (now.tm_isdst - time.localtime(ctime).tm_isdst)
mtime += 3600 * (now.tm_isdst - time.localtime(mtime).tm_isdst)
atime += 3600 * (now.tm_isdst - time.localtime(atime).tm_isdst)
ctime = time.strftime(format,time.localtime(ctime))
mtime = time.strftime(format,time.localtime(mtime))
atime = time.strftime(format,time.localtime(atime))

两个更正

请注意,如果您同时应用两者,您的 Python 输出将再次与您的输入不匹配.这可能是可取的(见下文),但如果它困扰您:

Both Corrections

Beware, if you apply both, your Python output will again seem to mismatch your input. This may be desirable (see below), but if it bothers you:

  • 如果您更喜欢与一年中的原始时间相同的时间戳,请仅选择 输入更正.
  • 仅选择输出校正,如果您习惯于在 DST 生效后一年两次看到它们跳一小时然后消失.
  • Choose only Input Correction if you prefer timestamps that look right at their native time of year.
  • 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.

Python 和 Windows 选择了不同的方法在 UTC 和本地时区之间转换时间戳:

Python and Windows have chosen different methods to convert timestamps between UTC and the local time zone:

  • Python 使用在时间戳有效的 DST 代码.这样,时间戳全年都有一致的表示.

  • Python uses the DST code that was in effect at the timestamp. This way, the time stamp has a consistent representation year-round.

Windows 使用现在生效的 DST 代码.这样,显示的所有时间戳都具有相同的隐式代码.

Windows uses the DST code in effect right now. This way, all time stamps shown have the same implicit code.

如果您使用 '%Z' 在转换后的字符串中包含时区(例如 PST 与 PDT),这一点很明显,但由于大多数应用程序(包括 Windows 资源管理器)不这样做,明显的一小时不一致可能清单.

This is evident if you use '%Z' to include the time zone in the converted string (PST vs. PDT, for example) but since most apps (including Windows Explorer) do not, an apparent one-hour inconsistency can manifest.

当使用明确的时间码打印时,很明显每列中的标记确实都代表同一时间:

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)
2000-01-30 20:00:00 UTC  2000-06-22 20:00:00 UTC

observed in January in California:
2000-01-30 12:00:00 PST  2000-06-30 13:00:00 PDT  [Python]
2000-01-30 12:00:00 PST  2000-06-30 12:00:00 PST  [Windows]

observed in June in California:
2000-01-30 12:00:00 PST  2000-06-30 13:00:00 PDT  [Python]
2000-01-30 13:00:00 PDT  2000-06-30 13:00:00 PDT  [Windows]

observed in June in New York:
2000-01-30 15:00:00 EST  2000-06-30 16:00:00 EDT  [Python]
2000-01-30 16:00:00 EDT  2000-06-30 16:00:00 EDT  [Windows]

如果我们可以要求 strftime 遵守 tm_isdst 字段,以匹配 Windows 资源管理器和大多数其他显示文件时间戳的应用程序,那就太好了,但至少有一个简单的解决方法可以自己进行计算.

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.

def adjustForDST (seconds):
    now = time.localtime()
    correction = 60*60 * (now.tm_isdst - time.localtime(seconds).tm_isdst)
    return seconds + correction

time.strftime(format, time.localtime(adjustforDST(mtime)))

来源:

http://bytes.com/topic/python/answers/655606-python-2-5-1-broken-os-stat-modulehttp://search.cpan.org/~shay/Win32-UTCFileTime-1.58/lib/Win32/UTCFileTime.pm

如果 cpan 链接再次因新修订而中断,请按以下方式查找:

If the cpan link breaks again with a new revision, find it this way:

https://www.google.com/search?q=UTCFileTime.pm