检查 lua 中的月份日期

Cheking day of the month in lua

所以我一直在 Linux 配置我的 AwesomeWM 主题,我遇到了这个问题。现在我当然不是专家程序员所以我想我会来这里寻求帮助。我一直在尝试检查一个月中的第几天是否是从 1 到 9 的数字,这将更改日历上聚焦数字的填充,但它似乎不起作用..

if (os.time(%d)>= 1) and (os.time(%d) <= 9) then
    theme.calendar_focus_padding = dpi(5)
else
    theme.calendar_focus_padding = dpi(10)
end

我在一个完全不同的文件(我的 rc.lua)上遇到错误,我真的不明白为什么。如果有人看懂了,这里是截图。

据我所知,它与以下代码行有关(来自我的 rc.lua 文件):

client.connect_signal("focus", function(c) c.border_color = beautiful.border_focus end)
client.connect_signal("unfocus", function(c) c.border_color = beautiful.border_normal end)

我知道这是关于堆栈溢出的非常具体的问题,但如果有人能提供帮助,我将不胜感激。

如@lhf 所述,您的代码无效 lua 代码。 Lua 参数可以是字符串、表格、数字或 null——对模式没有特殊处理(因此通常包含在字符串中)

此代码应该有效:

if (os.date("%d")>= 1) and (os.date("%d") <= 9) then
    theme.calendar_focus_padding = dpi(5)
else
    theme.calendar_focus_padding = dpi(10)
end

将模式包含在字符串中允许 lua 词法分析器解析它,这意味着代码至少会执行。 os.time 然后应在函数调用上解析 %d 以提供相关值,在本例中为该月的第几天。

是的,所以这个版本似乎可以工作:

if (os.date("%d") >= "1") and (os.date("%d") <= "9") then
    theme.calendar_focus_padding = dpi(5)
else
    theme.calendar_focus_padding = dpi(10)
end

但是代码没有按应有的方式运行。我的意思是,尽管这是 7 月的第二天(这应该将 if 语句切换为 true 并执行 if 下和 else 语句之前的所有内容,对吧?)。相反,它执行 else 语句 (theme.calendar_focus_padding = dpi(10)) 下的所有内容。现在怎么样了?

编辑: 所以我发现我必须将 os.date(etc) 转换为 int。我这样做是使用 tonumber() 函数,现在我有了这个,它似乎可以工作:

day = tonumber(os.date("%d"))

if (day >= 1) and (day <= 9) then
    theme.calendar_focus_padding = dpi(20)
else
    theme.calendar_focus_padding = dpi(10)
end