aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-07-21 20:21:42 +0200
committerMattias Andrée <maandree@operamail.com>2014-07-21 20:21:42 +0200
commitc3ddb7ccd143cd18d33c166fb0cbb0d4ed3e5eca (patch)
treedbfdf60123a0c4c85c3c496b349d38acb26ab099 /src/plugins
parentupdate todo (diff)
downloadxpybar-c3ddb7ccd143cd18d33c166fb0cbb0d4ed3e5eca.tar.gz
xpybar-c3ddb7ccd143cd18d33c166fb0cbb0d4ed3e5eca.tar.bz2
xpybar-c3ddb7ccd143cd18d33c166fb0cbb0d4ed3e5eca.tar.xz
add timezone support
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to '')
-rw-r--r--src/plugins/tzclock.py102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/plugins/tzclock.py b/src/plugins/tzclock.py
new file mode 100644
index 0000000..07599f7
--- /dev/null
+++ b/src/plugins/tzclock.py
@@ -0,0 +1,102 @@
+# -*- python -*-
+'''
+xpybar – xmobar replacement written in python
+Copyright © 2014 Mattias Andrée (maandree@member.fsf.org)
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+'''
+
+from datetime import datetime
+from pytz import timezone as tz
+import time
+
+
+class TZClock:
+ '''
+ Date and time information retrieval with timezone support
+ '''
+
+
+ SECONDS = 1
+ '''
+ :float Synchronisation value for syncing to seconds
+ '''
+
+ MINUTES = 60
+ '''
+ :float Synchronisation value for syncing to minutes
+ '''
+
+
+ def __init__(self, timezone, format = '%Y-(%m)%b-%d %T, %a w%V, %Z', sync_to = 1):
+ '''
+ Constructor
+
+ @param timezone:str The timezone
+ @param format:str The format as in the `date` command
+ @param sync_to:float The time parameter to sync to, the number of seconds between the reads
+ '''
+ self.utc = tz('UTC')
+ self.timezone = tz(timezone)
+ self.format = format
+ self.sync_to = sync_to
+
+
+ def read(self):
+ '''
+ Reads the clock
+
+ @return :str The time and date in the format specified at construction
+ '''
+ return self.from_posix_time(TZClock.posix_time())
+
+
+ def from_posix_time(self, time):
+ '''
+ Converts POSIX time into human readable time with the selected timezone
+
+ @param time:float The POSIX time
+ @return :str The time and date in the format specified at construction
+ '''
+ return datetime.fromtimestamp(time, tz = self.utc).astimezone(self.timezone).strftime(self.format)
+
+
+ def sync(self):
+ '''
+ Wait for the clock to reach the next synchronisation time point
+ '''
+ time.sleep(self.sync_to - (time.time() % self.sync_to))
+
+
+ def continuous_sync(self, function):
+ '''
+ Wait for the clock to reach the next synchronisation time point
+ and the call a function and start over, i.e. sync in a loop forever
+
+ @param function:()→void The function to call
+ '''
+ while True:
+ time.sleep(self.sync_to - (time.time() % self.sync_to))
+ function()
+
+
+ @staticmethod
+ def posix_time():
+ '''
+ Returns the current POSIX time
+
+ @return :float The current POSIX time
+ '''
+ return time.time()
+