# Copyright (c) 2010 Aldo Cortesi
# Copyright (c) 2012 Andrew Grigorev
# Copyright (c) 2014 Sean Vig
# Copyright (c) 2014 Tycho Andersen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import sys
import time
from datetime import datetime, timedelta, timezone

from libqtile.log_utils import logger
from libqtile.widget import base

try:
    import pytz
except ImportError:
    pass


class Clock(base.InLoopPollText):
    """A simple but flexible text-based clock"""
    orientations = base.ORIENTATION_HORIZONTAL
    defaults = [
        ('format', '%H:%M', 'A Python datetime format string'),
        ('update_interval', 1., 'Update interval for the clock'),
        ('timezone', None, 'The timezone to use for this clock, either as'
         ' string if pytz is installed (e.g. "US/Central" or anything in'
         ' /usr/share/zoneinfo), or as tzinfo (e.g. datetime.timezone.utc).'
         ' None means the system local timezone and is the default.')
    ]
    DELTA = timedelta(seconds=0.5)

    def __init__(self, **config):
        base.InLoopPollText.__init__(self, **config)
        self.add_defaults(Clock.defaults)
        if isinstance(self.timezone, str):
            if "pytz" in sys.modules:
                self.timezone = pytz.timezone(self.timezone)
            else:
                logger.warning('Clock widget can not infer its timezone from a'
                               ' string without the pytz library. Install pytz'
                               ' or give it a datetime.tzinfo instance.')
        if self.timezone is None:
            logger.info('Defaulting to the system local timezone.')

    def tick(self):
        self.update(self.poll())
        return self.update_interval - time.time() % self.update_interval

    # adding .5 to get a proper seconds value because glib could
    # theoreticaly call our method too early and we could get something
    # like (x-1).999 instead of x.000
    def poll(self):
        if self.timezone:
            now = datetime.now(timezone.utc).astimezone(self.timezone)
        else:
            now = datetime.now(timezone.utc).astimezone()
        return (now + self.DELTA).strftime(self.format)

@@ -1386,16 +1386,74 @@
   pass%0A%0A
+try:%0A    import dateutil.tz%0Aexcept ImportError:%0A    pass%0A%0A
 %0Aclass C
@@ -1823,16 +1823,28 @@
 if pytz 
+or dateutil 
 is insta
@@ -1873,20 +1873,8 @@
 %22 or
- anything in
 '%0A  
@@ -1881,16 +1881,28 @@
        '
+ anything in
  /usr/sh
@@ -1934,16 +1934,28 @@
 fo (e.g.
+'%0A         '
  datetim
@@ -1970,28 +1970,16 @@
 ne.utc).
-'%0A         '
  None me
@@ -2014,16 +2014,28 @@
 e and is
+'%0A         '
  the def
@@ -2347,32 +2347,141 @@
 (self.timezone)%0A
+            elif %22dateutil%22 in sys.modules:%0A                self.timezone = dateutil.tz.gettz(self.timezone)%0A
             else
@@ -2614,24 +2614,24 @@
 out 
-the 
 pytz 
-library
+or dateutil
 . In
@@ -2636,20 +2636,19 @@
 Install 
-pytz
+one
 '%0A      
@@ -2679,19 +2679,73 @@
  ' o
-r give it a
+f these libraries, or give it a'%0A                               '
  dat

Add dateutil support for the Clock widget timezones (#1696)