gpt4 book ai didi

ruby - 冒号(:) appears as forward slash (/) when creating file name

转载 作者:数据小太阳 更新时间:2023-10-29 06:27:39 32 4
gpt4 key购买 nike

我正在使用日期和时间来标记我正在创建的新文件,但是当我查看该文件时,冒号是一个正斜杠。我正在使用 10.7+ 在 Mac 上开发

这是我使用的代码:

 File.open("#{time.hour} : 00, #{time.month}-#{time.day}-#{time.year}", "a") do |mFile|
mFile.syswrite("#{pKey} - #{tKey}: \n")
mFile.syswrite("Items closed: #{itemsClosed} | Total items: #{totalItems} | Percent closed: % #{pClosed} \n")
mFile.syswrite("\n")
mFile.close
end

这是输出(假设时间是下午 1 点):

13 / 00, 11-8-2012

为什么会发生这种情况,我该如何解决?我希望输出为:

13:00, 11-8-2012

最佳答案

曾几何时,在 Mac OS X 之前,: 是目录分隔符,而不是 /。显然 OS X 10.7 仍在尝试修复这样的程序。如果您真的需要 : ,我不知道如何解决这个问题。我会忽略它 :-)。

编辑:经过更多搜索this USENIX paper描述发生了什么。他们使用的规则显然是这样的:

Another obvious problem is the different path separators between HFS+ (colon, ':') and UFS (slash, '/'). This also means that HFS+ file names may contain the slash character and not colons, while the opposite is true for UFS file names. This was easy to address, though it involves transforming strings back and forth. The HFS+ implementation in the kernel's VFS layer converts colon to slash and vice versa when reading from and writing to the on-disk format. So on disk the separator is a colon, but at the VFS layer (and therefore anything above it and the kernel, such as libc) it's a slash. However, the traditional Mac OS toolkits expect colons, so above the BSD layer, the core Carbon toolkit does yet another translation. The result is that Carbon applications see colons, and everyone else sees slashes. This can create a user-visible schizophrenia in the rare cases of file names containing colon characters, which appear to Carbon applications as slash characters, but to BSD programs and Cocoa applications as colons.

关于ruby - 冒号(:) appears as forward slash (/) when creating file name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13298434/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com