当文件名可能包含 UTF8 字符时,如何以整数形式获取 DateLastModified?

发布于 2025-01-18 05:48:47 字数 480 浏览 1 评论 0原文

我正在Windows环境中编写Lua(5.3);结果将在许多国家/地区使用,因此文件处理必须满足多个区域设置(排除 lfs 和 Penlight 库)以及许多日期格式(由用户选择)。

我使用 luacom 和 Microsoft FileObject 访问文件,这消除了文件名问题,但我在使用以文本形式返回的 DateLastModified 属性时遇到了困难。

我需要比较数百个文件的 DateLastModified 以确定哪个是最新的。

我考虑过的一种技术涉及暂时将 PC 的短日期格式设置为可预测的、可以简单解析的格式,例如“yyyy-MM-dd”(然后重新设置),但我本能地反对弄乱用户的注册表设置,尤其是那些在我的程序之外产生影响的设置。

我可以将每个文件复制到一个临时文件,其名称允许 lfs 获取整数秒的修改数据,但这会严重影响应该是相当基本的实用程序的运行时间。

是否有我忽略的解决方案,或者我是否必须选择最不坏的选项?注意:我对目标环境中可用的库非常有限。

I'm writing Lua (5.3) in a Windows environment; the result will be used in many countries so file handling has to cater for multiple locales (which rules out the lfs and Penlight libraries) and also many date formats (as selected by the user).

I'm accessing files using luacom and Microsoft FileObject, which eliminates problems with file names, but I'm having difficulty with the DateLastModified property, which is returned in text.

I need to compare the DateLastModified for many hundreds of files to determine which is the latest.

One technique I've considered would involve temporarily setting (via the registry) the PC's Short Date format to something predictable that can be simply parsed such as "yyyy-MM-dd" (and resetting it afterwards) but I'm instinctively averse to messing with the user's registry settings, especially those that have ramifications outside my program.

I could copy each file to a temporary file with a name that allows lfs to get the modification data as an integer number of seconds, but that would seriously impact the run-time of what's supposed to be a fairly basic utility.

Is there a solution I've overlooked, or am I going to have to choose the least worst option? Note: I'm quite restricted in the libraries available in the target environment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-25 05:48:47

尽管 Luacom 的默认行为是将日期作为文本字符串返回(基于当前语言环境),但可以修改此行为:

3.4.6 日期类型
从 COM 转换为 Lua 时,默认行为是将 DATE 值转换为根据当前语言环境格式化的字符串。反之亦然:LuaCOM 将根据当前语言环境格式化的字符串转换为 DATE 值。该脚本可以通过将 luacom 表(LuaCOM 命名空间)的 DateFormat 字段设置为字符串“table”来更改从字符串到表的转换。该表将包含“日”、“星期几”、“月”、“年”、“小时”、“分钟”、“秒”和“毫秒”字段。要返回到字符串的转换,请将 DateFormat 字段设置为“string”。请小心使用此功能,因为它可能会破坏与其他脚本的兼容性。

来自 Luacom 文档

Although the default behaviour of Luacom is to return a Date as a text string (based on the current locale), it is possible to modify this behaviour:

3.4.6 DATE type
When converting from COM to Lua, the default behavior is to transform DATE values to strings formatted according to the current locale. The converse is true: LuaCOM converts strings formatted according to the current locale to DATE values. The script can change the conversion from strings to tables by setting the DateFormat field of the luacom table (the LuaCOM namespace) to the string "table". The table will have Day, DayOfWeek, Month, Year, Hour, Minute, Second, and Milliseconds fields. To return the conversion to strings, set the DateFormat field to "string". Be careful with this feature, as it may break compatibility with other scripts.

From Luacom documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文