Is it currently/technically possible to display the current date & time (ie: digital clock numbers) to the currently active window? Like appending some text from a script?
Ubuntu – Display the current date & time in the window’s title
scriptswindowwindow-managerxdotool
Related Solutions
Okay so no one has answered yet. I would really like someone to, but in the meantime, I have a "workaround". If you hold Alt while clicking on a window, it won't register the click. This is normally used for "dragging" the window, so while you're holding Alt, all windows will be draggable from anywhere, not just the titlebar. If you just click, it will bring the window into focus, but it won't actually click in the window. Hope this helps someone.
Epoch time
In general, calculations on time are easiest if we first convert time into (Unix) epoch time (seconds from 1-1-1970). In python, we have tools to convert time into epoch time, and back into any date format we prefer.
We can simply set a format, like:
pattern = "%Y-%m-%d"
...and define today:
today = "2016-12-07"
and subsequently write a function to do the job:
def convert_toepoch(pattern, stamp):
return int(time.mktime(time.strptime(stamp, pattern)))
Then the output of:
nowepoch = convert_toepoch(pattern, today)
print(nowepoch)
> 1481065200
...which is, as mentioned, the number of seconds since 1-1-1970
Calculating the days between two dates
If we do this on both today and our future date, subsequently calculate the difference:
#!/usr/bin/env python3
import time
# set our date pattern
pattern = "%Y-%m-%d"
def convert_toepoch(pattern, stamp):
return int(time.mktime(time.strptime(stamp, pattern)))
# automatically get today's date
today = time.strftime(pattern); future = "2016-12-28"
nowepoch = convert_toepoch(pattern, today)
future_epoch = convert_toepoch(pattern, future)
print(int((future_epoch - nowepoch)/86400))
The output will be calculated by date, since we use the format %Y-%m-%d
. Rounding on seconds would possibly give an incorrect date difference, if we are near 24 hrs for example.
Terminal version
#!/usr/bin/env python3
import time
# set our date pattern
pattern = "%Y-%m-%d"
def convert_toepoch(pattern, stamp):
return int(time.mktime(time.strptime(stamp, pattern)))
# automatically get today's date
today = time.strftime(pattern)
# set future date
future = input("Please enter the future date (yyyy-mm-dd): ")
nowepoch = convert_toepoch(pattern, today)
future_epoch = convert_toepoch(pattern, future)
print(int((future_epoch - nowepoch)/86400))
...And the Zenity option
#!/usr/bin/env python3
import time
import subprocess
# set our date pattern
pattern = "%Y-%m-%d"
def convert_toepoch(pattern, stamp):
return int(time.mktime(time.strptime(stamp, pattern)))
# automatically get today's date
today = time.strftime(pattern)
# set future date
try:
future = subprocess.check_output(
["zenity", "--entry", "--text=Enter a date (yyyy-mm-dd)"]
).decode("utf-8").strip()
except subprocess.CalledProcessError:
pass
else:
nowepoch = convert_toepoch(pattern, today)
future_epoch = convert_toepoch(pattern, future)
subprocess.call(
["zenity", "--info",
"--text="+str(int((future_epoch - nowepoch)/86400))
])
And just for fun...
A tiny application. Add it to a shortcut if you use it often.
The script:
#!/usr/bin/env python3
import time
import subprocess
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Pango, Gdk
class OrangDays(Gtk.Window):
def __init__(self):
self.pattern = "%Y-%m-%d"
self.currdate = time.strftime(self.pattern)
big_font = "Ubuntu bold 45"
self.firstchar = True
Gtk.Window.__init__(self, title="OrangeDays")
maingrid = Gtk.Grid()
maingrid.set_border_width(10)
self.add(maingrid)
datelabel = Gtk.Label("Enter date")
maingrid.attach(datelabel, 0, 0, 1, 1)
self.datentry = Gtk.Entry()
self.datentry.set_max_width_chars(12)
self.datentry.set_width_chars(12)
self.datentry.set_placeholder_text("yyyy-mm-dd")
maingrid.attach(self.datentry, 2, 0, 1, 1)
sep1 = Gtk.Grid()
sep1.set_border_width(10)
maingrid.attach(sep1, 0, 1, 3, 1)
buttongrid = Gtk.Grid()
buttongrid.set_column_homogeneous(True)
maingrid.attach(buttongrid, 0, 2, 3, 1)
fakebutton = Gtk.Grid()
buttongrid.attach(fakebutton, 0, 0, 1, 1)
calcbutton = Gtk.Button("Calculate")
calcbutton.connect("clicked", self.showtime)
calcbutton.set_size_request(80,10)
buttongrid.attach(calcbutton, 1, 0, 1, 1)
fakebutton2 = Gtk.Grid()
buttongrid.attach(fakebutton2, 2, 0, 1, 1)
sep2 = Gtk.Grid()
sep2.set_border_width(5)
buttongrid.attach(sep2, 0, 1, 1, 1)
self.span = Gtk.Label("0")
self.span.modify_font(Pango.FontDescription(big_font))
self.span.set_alignment(xalign=0.5, yalign=0.5)
self.span.modify_fg(Gtk.StateFlags.NORMAL, Gdk.color_parse("#FF7F2A"))
maingrid.attach(self.span, 0, 4, 100, 1)
sep3 = Gtk.Grid()
sep3.set_border_width(5)
maingrid.attach(sep3, 0, 5, 1, 1)
buttonbox = Gtk.Box()
maingrid.attach(buttonbox, 0, 6, 3, 1)
quitbutton = Gtk.Button("Quit")
quitbutton.connect("clicked", Gtk.main_quit)
quitbutton.set_size_request(80,10)
buttonbox.pack_end(quitbutton, False, False, 0)
def convert_toepoch(self, pattern, stamp):
return int(time.mktime(time.strptime(stamp, self.pattern)))
def showtime(self, button):
otherday = self.datentry.get_text()
try:
nextepoch = self.convert_toepoch(self.pattern, otherday)
except ValueError:
self.span.set_text("?")
else:
todayepoch = self.convert_toepoch(self.pattern, self.currdate)
days = str(int(round((nextepoch-todayepoch)/86400)))
self.span.set_text(days)
def run_gui():
window = OrangDays()
window.connect("delete-event", Gtk.main_quit)
window.set_resizable(True)
window.show_all()
Gtk.main()
run_gui()
- Copy it into an empty file, save it as
orangedays.py
Run it:
python3 /path/to/orangedays.py
To wrap it up
Use for the tiny application script above the following .desktop
file:
[Desktop Entry]
Exec=/path/to/orangedays.py
Type=Application
Name=Orange Days
Icon=org.gnome.Calendar
- Copy the code into an empty file, save it as
orangedays.desktop
in~/.local/share/applications
In the line
Exec=/path/to/orangedays.py
set the actual path to the script...
Best Answer
Showing date & time in the active window's title
Running the script below (an edited version of this one) in the background, the front most window will show the current date & time:
If the window loses focus, the time will not be updated; only on the front most window, the time will be updated.
The script also shows the date & time per tab on any application using tabs, like
firefox
orgnome-terminal
The script
How to use
The script uses
xdotool
Copy the script below into an empty file, save it as
show_datetime.py
Test-run it by the command:
Open a new window or give an existing one focus. The date & time should appear in the window's title within 1-2 seconds. Wait a minute to see if the time is updated.
If all works fine, add it to your startup applications: Dash > Startup Applications > Add, add the command:
If you are having difficulties running it from start up, use the command (in startup applications):
Explanation
The script keeps an eye on three things:
If there is a change in either one, the date/time is appended or updated to the currently frontmost window.
Why use the name -and- the id of the window?
Firefox
. The window name will change when another tab is chosen or opened, while the window- id won't change.if any([])
is to edit the window's title only if there is a reason to.