From ed5ea4c2ab49897c034c6b7bb964e01798e7992d Mon Sep 17 00:00:00 2001 From: M1dgard Date: Thu, 17 May 2018 01:45:08 +0200 Subject: [PATCH] Write small custom clock script for bar Polybar doesn't support time zones yet, so I wrote my own date displaying script. I think I'll keep it, it is more accurate: 0.02s instead of up to 5s (configurable but higher frequency means more CPU power used) latency. --- polybar/clock.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++ polybar/config.ini | 28 +++++++++++++-------- 2 files changed, 80 insertions(+), 11 deletions(-) create mode 100755 polybar/clock.py diff --git a/polybar/clock.py b/polybar/clock.py new file mode 100755 index 0000000..615d833 --- /dev/null +++ b/polybar/clock.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +import sys +from datetime import datetime +import time +import pytz + + +if len(sys.argv) < 3: + print("Usage: {scriptname} timezone_left timezone_right".format(scriptname=sys.argv[0]), file=sys.stderr) + exit(1) + +try: + tzleft = pytz.timezone(sys.argv[1]) + tzright = pytz.timezone(sys.argv[2]) +except pytz.exceptions.UnknownTimeZoneError as e: + print('Unknown time zone "{}"'.format(e.args[0]), file=sys.stderr) + exit(1) + + +faces = "🕛🕧🕐🕜🕑🕝🕒🕞🕓🕟🕔🕠🕕🕡🕖🕢🕗🕣🕘🕤🕙🕥🕚🕦" + +def face_for(t): + minute = (t.minute + 15) // 30 + hour = (t.hour + 0 if minute < 45 else 1) % 12 + + return faces[2*hour + minute] + + +def color_for(t): + if t.hour < 5 or t.hour > 22: + return "888" + elif t.hour < 10: + return "DB9" + elif t.hour < 14: + return "DDB" + elif t.hour < 19: + return "DDD" + else: + return "D9B" + + +while True: + utcnow = datetime.now(tz=pytz.utc) + localnow = datetime.now() + leftnow = utcnow.astimezone(tzleft ) + rightnow = utcnow.astimezone(tzright) + print( + "%{T2}%{F#" + color_for(leftnow) + "}" + + face_for(leftnow) + + "%{F}%{T-} " + + + localnow.strftime("%A %%{T4}%%{F"+color_for(localnow)+"}%H:%M%%{F}%%{T-} %d %b") + + + " %{T2}%{F#" + color_for(rightnow) + "}" + + face_for(rightnow) + + "%{F}%{T-}", + + flush = True + ) + + delay = 60 - localnow.second - (localnow.microsecond * 10e-7) + time.sleep(delay) diff --git a/polybar/config.ini b/polybar/config.ini index 0a31d04..44b1aa6 100644 --- a/polybar/config.ini +++ b/polybar/config.ini @@ -37,8 +37,8 @@ module-margin-right = 6 ; Default text font-0 = "Fira Sans:size=10;1" -; TODO unused -font-1 = fixed:pixelsize=10;1 +; Other timezones clock symbols +font-1 = "Noto Sans Symbols2:size=16;4" ; Default icons font-2 = "Material Icons:pixelsize=14;3" ; Clock text @@ -317,17 +317,23 @@ interval = 3.0 ;label-connected = "↑%{T5}%upspeed%%{T-} %{T5}↓%downspeed%%{T-}" label-connected = "%{O-32}" +;[module/date] +;type = internal/date +;interval = 5 + +;date = +;date-alt = + +;time = %A %{T4}%H:%M%{T-} %e %b +;time-alt = %a %Y-%m-%d %H:%M:%S + +;label = %time% + [module/date] -type = internal/date -interval = 5 +type = custom/script +tail = true -date = -date-alt = - -time = %A %{T4}%H:%M%{T-} %e %b -time-alt = %a %Y-%m-%d %H:%M:%S - -label = %time% +exec = ~/.config/polybar/clock.py America/Detroit Asia/Seoul [module/volume] type = internal/volume