Sunday, June 24, 2012

12 - Hour clock in Awesome WM

Awesome Window Manager comes with a widget called "Text Clock" that displays the time in a text box in the upper right-hand corner of the screen. It is in 24-hour format instead of 12, which can cause some confusion for users used to the standard 12-hour time.  Your options are to live with it, or change it. If you want a Day,date, hours:minute:second: am/pm format, look at the end of this post.

Option one- Living with it
I found a neat trick to convert 24-hour time into 12-hour time. Take 13:00, for instance. Subtract 2 from the second number and get the second number of 12-hour time. (3-2=1, so it is 1:00). At 23:00 you get "1" again, meaning that it is 11 O' clock. This method uses too many brain cycles, so I decided to get lazy and just change the source of the script.

Option two - Taking Action
Use this command: sudo gedit /usr/share/awesome/lib/awful/widget/textclock.lua
where gedit is any text editor. This is the source code for the clock widget. Make a backup of the real one. I was a ninny and didn't do this. The two important lines are lines 22 and 23: format and timeout. Format uses the strftime function to display the time. Some important things to note: %H is 24-hour hours and %l is 12-hour time with a whitespace instead of a 0 in the first digit in 1-digit times. (For instance 9:42 would by 9:42, not 09:42). All you need to change is %H to %l.


Now, you are faced with a decision. By default this resets itself every 60 seconds. If you use %S (seconds), you need to change this setting to "1". (5 is also commonly used, but it's a pain if you log in on a non-multiple of 5). This way the clock will reset every second.


The two lines for me look like this:

%p should be the only unfamiliar one. This means am or pm (great for any basement dweller with odd sleeping habits!).

You will not notice changes right away. Restart awesome. If you did something wrong the text clock may not be there. Just use your old source in place of the one you edited. This is the advantage of always keeping a backup.

Here is the FULL source of my script. This is what it outputs:


---------------------------------------------------------------------------
-- @author Julien Danjou <julien@danjou.info>
-- @copyright 2009 Julien Danjou
-- @release v3.4.11
---------------------------------------------------------------------------


local setmetatable = setmetatable
local os = os
local capi = { widget = widget,
               timer = timer }


--- Text clock widget.
module("awful.widget.textclock")


--- Create a textclock widget. It draws the time it is in a textbox.
-- @param args Standard arguments for textbox widget.
-- @param format The time format. Default is " %a %b %d, %H:%M ".
-- @param timeout How often update the time. Default is 60.
-- @return A textbox widget.
function new(args, format, timeout)
    local args = args or {}
    local format =  " %a %b %d, %l:%M:%S %p "
    local timeout = 1
    args.type = "textbox"
    local w = capi.widget(args)
    local timer = capi.timer { timeout = timeout }
    w.text = os.date(format)
    timer:add_signal("timeout", function() w.text = os.date(format) end)
    timer:start()
    return w
end


setmetatable(_M, { __call = function(_, ...) return new(...) end })


-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80


You can replace your script with this and it will give you the output I showed you above.

1 comment:

  1. making modifications in the /usr/share/ directory is kinda messy. Why not just use the textclock format in rc.lua. Mine looks like...

    mytextclock = awful.widget.textclock({align = "right" }, " %m/%d %H:%M%P")

    ReplyDelete