I am using AwesomeWM v4.0-170-g6c24848-dirty, compiled against Lua 5.3.3; and I am starting to customise my widgets.
One of them is the clock, technically wibox.widget.textclock()
. I have been able to alter the format
in order to change the order, add custom messages like 'Today is Sunday, the 23 of July of 2017' but... no clue about the ordinals.
What I mean here is how to add that 'rd' to 23rd, and make it change depending on the current date to, for example, 21st, 22nd or 24th.
I have tried to add an ordinal
variable before the format, followed to an if-else statement to determine its value depending on the day. This, however, does not work: I can't neither 'use' the date format outside the function, nor implement the variable inside format
.
As far as I have read, variables in strings can be implemented like in the following example:
print("Hello " .. name .. ", the value of key " .. k .. " is " .. v .. "!")
However, this won't work here. I have run out of clues, can you shed me a light, please?
So far, I have coded a generic 'th' date format:
mytextclock = wibox.widget.textclock(" %a %dth %B, %H:%M ", 60)
...whose output would be: dayoftheweek dayth month, HH, MM.
Background:
At first I laid out the problem with two options in mind:
1. Select the day from the whole output string: After being processed by the program, use some kind of Bash echo $2
(considering an output like dayoftheweek day month hh:mm
) equivalence in Lua to...
2. Treat the variable day
individually from the start: This would mean finding a way to get the variable without using the whole string, once we have it...
...process it later with an if-else
structure which would alter the output depending on its value.
For speed reasons, I used the second way. I found easier and cleaner to get the variable from the start instead of dedicating some lines of code to the extraction from the output.
So I started using %d
as my main variable to work, which is used in Lua to represent the day in a date. (source)
The main deal here was to convert the content of %d
into a string:
day = "%d" -- This is supposed to be an integer now.
daynumber = tostring(day) -- Converts it to a string.
lastdigit = tostring(day, -1)
print(lastdigit) -- Output: d.
BOOM! Failure. This is not working, I hope somebody can say why in comments. The output if I print the latest char (-1) is always d, and if I try with -2 I will get the whole day value.
My main theory bases on the fact typing:
a = "%d"
print(a)
in the Lua interpreter ($ lua
in your shell) just returns %d
, no integers at all; but it's just a supposition. What's more, as far as I've read %d
is used in a date context, not independently as the value of a variable.
A possible solution:
day = os.date("%d") -- First of all we grab the day from the system time.
-- As Lua displays the day with two digits, we are storing both of them in variables in order to process them separately later.
firstdigit = string.sub(day, 0, 1)
lastdigit = string.sub(day, -1)
-- We don't want Awesome to display '01st August' or '08th September'. We are going to suppress the '0'.
if firstdigit == "0" then
day = lastdigit
end
-- Now we want to display the day with its respective ordinal: 1st, 2nd, 3rd, 4th... we are going to process the last digit for this.
if lastdigit == "1" then
ordinal = "st"
elseif lastdigit == "2" then
ordinal = "nd"
elseif lastdigit == "3" then
ordinal = "rd"
else
ordinal = "th"
end
-- Finally, we display the final date.
mytextclock = wibox.widget.textclock(" %a " ..day..ordinal.. " %B %H:%M ", 60)
...so we get the following outputs: