Quick - what time is it in Singapore?
By Bob Mesibov, published 01/08/2017 in Tutorials
If you want to know the local time around the world, Linux has options ranging from plain to gorgeous.
The plainest one is the tzselect command, which offers choices in a terminal that lead you to the local time in a single region. Middling-pretty is gworldclock, a little GUI window that can be configured to show the time in several places. And at the simply gorgeous end of the spectrum is slashtime (on GitHub here).
What I wanted, though, was a time-notifier that showed me just the local time and day in a few selected places, that could be called up with a keyboard shortcut, that displayed the information in an eye-catching way in the corner of my screen and that disappeared automatically after, say, 5 seconds.
Getting the info
The date command creates the information I want. I format the output with strftime and set the places of interest with the TZ specification from tzselect:
echo -e "Home $(date "+ %R %A")\nSofia $(TZ="Europe/Sofia" date "+ %R %A")\nSingapore $(TZ="Asia/Singapore" date "+ %R %A")"
Displaying the info
Choices, choices... Linux has many different ways to send a message box to the desktop, but I decided to look at the three applications I knew best. The results are shown here in order from "just OK" to "that'll do nicely".
notify-send
On my Xfce4 desktop, there's a GUI configuration tool (in Settings/Notifications) that sets the theme, desktop position, persistence and transparency for notifications, and also controls the programs, like notify-send, that are allowed to use the notification system. notify-send on the command line doesn't have useful options for display other than persistence time (in milliseconds) and choice of icon to accompany the notification text.
Using this command
notify-send Times -t 5000 "$(echo -e "\n Home $(date "+ <b>%R</b> %A")\n Sofia $(TZ="Europe/Sofia" date "+ <b>%R</b> %A") \n Singapore $(TZ="Asia/Singapore" date "+ <b>%R</b> %A")")"
I got this in the lower-right-hand corner of my screen:
The word "Times" is what notify-send calls a "Summary", and is required by the command. The notification uses my standard system font, and as you can see from the command I had to do some fiddling with spacing to get the places, times and days to line up vertically. Note also that notify-send in Xfce4 understands bold HTML markup, but I can't use markup to set the font or font color. The timeout is set for 5 seconds with the -t option.
YAD --text-info
YAD is one of my favourite and most-used dialog programs, but the obvious dialog choice for my time notifier, namely text-info, isn't very flexible:
echo -e "\n Home $(date "+ %R %A")\n Sofia $(TZ="Europe/Sofia" date "+ %R %A") \n Singapore $(TZ="Asia/Singapore" date "+ %R %A")" | yad --text-info --width="250" --posx="1600" --posy="900" --no-buttons --undecorated --vscroll-policy="never" --fore="yellow" --back="black" --timeout=5
The posx and posy options set the screen position and width sets the notification box width, while box height is set automatically by YAD. For a minimal box I used the no-buttons and undecorated options; setting vscroll-policy to "never" removes a right-hand scrollbar. The text and background color are set for the whole notification area with fore and back, and using yellow on black I get an eye-catching message. For this dialog YAD uses a monospace font by default and it was easy to use equal-width spaces in the text message to line up the data.
YAD --list
echo -e "Home\n$(date "+ <b>%R</b>\n%A")\nSofia\n$(TZ="Europe/Sofia" date "+ <b>%R</b>\n%A")\nSingapore\n$(TZ="Asia/Singapore" date "+ <b>%R</b>\n%A")" | yad --list --column="Place" --column="Time" --column="Day" --width="250" --height="90" --posx="1600" --posy="900" --no-buttons --undecorated --timeout=5 --no-headers
With a bit of fiddling, YAD's list dialog could also be used for my time notifier. The no-headers option removes the column titles. Note that "\n" is used as an item separator in the text, that HTML bold markup will work, and that because items are aligned in columns, no special spacing-out of words is needed. YAD offers a way to colour the text in lists but not to colour the background, so I kept the default colours.
dzen2
I've used dzen2 before to create a picker for special characters, as described in my first Linux Rain contribution. It's a highly configurable dialog program and I recommend its GitHub wiki page for learning about its many options.
(echo " Home $(date "+ ^fg(yellow)%R ^fg(white)%A")"; echo -e " Sofia $(TZ="Europe/Sofia" date "+ ^fg(yellow)%R ^fg(white)%A")\n Singapore $(TZ="Asia/Singapore" date "+ ^fg(yellow)%R ^fg(white)%A")") | dzen2 -x 1600 -y 900 -w 250 -h 25 -bg black -fg white -fn monospace -p 5 -l 2 -sa l -ta l -e 'onstart=uncollapse'
A dzen2 dialog has a one-line title window followed (if needed) by a multi-line slave window. In the command above, the title window is fed with the "echo Home..." command and the slave window by the echo -e command. Most of the options will be self-evident, but -p 5 sets the persistence at 5 seconds, -l 2 says that two lines are to be displayed in the slave window, and -sa l and -ta l align the text to the left in both windows. There's some spacing-out of words in the monospace font to get the alignment just right, and an interesting tweak to the font color. That's set as white with -fg, but it's over-ridden in the message with the dzen2 special command ^fg(yellow) and restored with ^fg(white). The most important option, though is the cryptic -e 'onstart=uncollapse'. This tells dzen2 to show both the title and slave windows when it puts the dialog on the screen. Without that option, only the title window would appear, and to see the slave window contents I'd have to hover over or click on the dialog box.
Firing it up
dzen2 was the winner for me. The final step was to put the one-line command into an executable BASH script and launch it with a keyboard shortcut. My script is called times and the keyboard shortcut I use to launch the script is Ctrl + Shift + t.
#!/bin/bash
(echo " Home $(date "+ ^fg(yellow)%R ^fg(white)%A")"; echo -e " Sofia $(TZ="Europe/Sofia" date "+ ^fg(yellow)%R ^fg(white)%A")\n Singapore $(TZ="Asia/Singapore" date "+ ^fg(yellow)%R ^fg(white)%A")") | dzen2 -x 1600 -y 900 -w 300 -h 25 -bg black -fg white -fn monospace -p 5 -l 2 -sa l -ta l -e 'onstart=uncollapse'
exit