POSIX atime , mtime , ctime
如果您包含完整的脚本及其实际和预期的输出而不是 REPL 片段,这可能会有所帮助。
import sys, os, stat, time
def get_times(p):
s = os.stat(p)
return (
os.path.getatime(p),
os.path.getmtime(p),
os.path.getctime(p),
s[stat.ST_ATIME],
s[stat.ST_MTIME],
s[stat.ST_CTIME],
)
def main(p, delay=1):
delay = float(delay)
(a,b) = (1000000000, 1500000000)
open(p,'w').close()
print 'initial:'
print get_times(p)
for t in [ (a,a), (a,b), (b,a), (b,b) ]:
print
print 'test:', t
os.utime(p,t)
print get_times(p)
time.sleep(delay)
print get_times(p)
main(*sys.argv[1:])
我在我的 10.4 系统上使用cd "$HOME" && python test.py tempfile 5
(系统默认 Python 2.3.6 和 MacPorts Python 2.6.4 都给出了相同的结果(当然,省略了初始时间和ctime)):
% python /tmp/test.py tempfile 5
initial:
(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)
test: (1000000000, 1000000000)
(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)
(1000000000.0, 1000000000.0, 1269629881.0, 1000000000, 1000000000, 1269629881)
test: (1000000000, 1500000000)
(1000000000.0, 1500000000.0, 1269629886.0, 1000000000, 1500000000, 1269629886)
(1000000000.0, 1500000000.0, 1269629886.0, 1000000000, 1500000000, 1269629886)
test: (1500000000, 1000000000)
(1500000000.0, 1000000000.0, 1269629891.0, 1500000000, 1000000000, 1269629891)
(1500000000.0, 1000000000.0, 1269629891.0, 1500000000, 1000000000, 1269629891)
test: (1500000000, 1500000000)
(1500000000.0, 1500000000.0, 1269629896.0, 1500000000, 1500000000, 1269629896)
(1500000000.0, 1500000000.0, 1269629896.0, 1500000000, 1500000000, 1269629896)
这似乎是合理的。我想知道你得到了什么。
我听说 Spotlight 有时会因为重新索引更改的文件而主动重置时间。我不希望它重新索引仅经历过 utime()/utimes() 的文件,但我认为这是可能的。要消除 Spotlight 作为可能的并发症,请使用 Spotlight 未索引的位置中的文件(例如 /tmp/testfile)。
在Finder中创建的日期
(在 Finder 的获取信息窗口中显示为“已创建:”)
如果您安装了开发者工具,您可以使用它/Developer/Tools/GetFileInfo
来查看 HFS 创建日期。我在每一行之后添加了以下几print get_times(p)
行:
sys.stdout.flush()
os.system('/Developer/Tools/GetFileInfo ' + p)
我还更改了迭代以匹配您的初始描述 ( [ (a,b), (a,a), (b,a), (b,b) ]
)。
结果现在看起来像这样:
% rm /tmp/tempfile; python /tmp/test.py /tmp/tempfile 1
initial:
(1269636574.0, 1269636574.0, 1269636574.0, 1269636574, 1269636574, 1269636574)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 03/26/2010 15:49:34
modified: 03/26/2010 15:49:34
test: (1000000000, 1500000000)
(1000000000.0, 1500000000.0, 1269636574.0, 1000000000, 1500000000, 1269636574)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 03/26/2010 15:49:34
modified: 07/13/2017 21:40:00
(1000000000.0, 1500000000.0, 1269636574.0, 1000000000, 1500000000, 1269636574)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 03/26/2010 15:49:34
modified: 07/13/2017 21:40:00
test: (1000000000, 1000000000)
(1000000000.0, 1000000000.0, 1269636576.0, 1000000000, 1000000000, 1269636576)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
(1000000000.0, 1000000000.0, 1269636576.0, 1000000000, 1000000000, 1269636576)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
test: (1500000000, 1000000000)
(1500000000.0, 1000000000.0, 1269636577.0, 1500000000, 1000000000, 1269636577)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
(1500000000.0, 1000000000.0, 1269636577.0, 1500000000, 1000000000, 1269636577)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 09/08/2001 20:46:40
test: (1500000000, 1500000000)
(1500000000.0, 1500000000.0, 1269636578.0, 1500000000, 1500000000, 1269636578)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 07/13/2017 21:40:00
(1500000000.0, 1500000000.0, 1269636578.0, 1500000000, 1500000000, 1269636578)
file: "/private/tmp/tempfile"
type: ""
creator: ""
attributes: avbstclinmedz
created: 09/08/2001 20:46:40
modified: 07/13/2017 21:40:00
这似乎与您在Finder的 Get Info 窗口中的观察结果一致。我的解释(由其他实验证实)是 HFS creationDate 由 utime 更新,但它只会向后(从不向前)。如果您想将 HFS creationDate 更新为更新的值,那么您可能必须使用特定于 Mac 的 API 来执行此操作。
另一个注意事项:您可能需要稍微切换窗口才能更新“获取信息”窗口。在我的系统上,它的显示不会自动更新,除非我将窗口切换到“获取信息”窗口或从“获取信息”窗口切换。