class AlarmWidget:
    def __init__(self, view, alarm, alert):
        self.view = view
        self.alarm = alarm
        self.alert = alert
        timestr = alarm.get_time_as_string()
        repeat = alarm.get_alarm_repeat_string()
        self.drawing = DigitalClockDrawing()
        is_light = self.get_is_light(int(timestr[:2]))
        if is_light:
            img = os.path.join(Dirs.get_image_dir(), "cities", "day.png")
        else:
            img = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
        self.drawing.render(timestr, img, is_light, repeat)
        self.standalone = None

    def get_is_light(self, hours):
        return hours > 7 and hours < 19

    def get_pixbuf(self):
        return self.drawing.pixbuf

    def get_alert(self):
        return self.alert

    def get_standalone_widget(self):
        if not self.standalone:
            self.standalone = StandaloneAlarm(self.view, self.alarm, self.alert)
        return self.standalone
    def __init__(self, location):
        self.location = location
        self._last_sunrise = datetime.strptime("197007:00", "%Y%H:%M")
        self.sunrise = self._last_sunrise
        self._last_sunset = datetime.strptime("197019:00", "%Y%H:%M")
        self.sunset = self._last_sunset
        self.get_sunrise_sunset()

        self.path = None
        self.list_store = None

        weather_timezone = self.location.get_timezone()
        timezone = GLib.TimeZone.new(weather_timezone.get_tzid())
        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, time.time())
        location_offset = timezone.get_offset(i)

        timezone = GLib.TimeZone.new_local()
        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, time.time())
        here_offset = timezone.get_offset(i)

        self.offset = location_offset - here_offset

        self._last_time = None
        self.drawing = DigitalClockDrawing()
        self.standalone = None
        self.update()
 def __init__(self, view, alarm, alert):
     self.view = view
     self.alarm = alarm
     self.alert = alert
     timestr = alarm.get_time_as_string()
     repeat = alarm.get_alarm_repeat_string()
     self.drawing = DigitalClockDrawing()
     is_light = self.get_is_light(int(timestr[:2]))
     if is_light:
         img = os.path.join(Dirs.get_image_dir(), "cities", "day.png")
     else:
         img = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
     self.drawing.render(timestr, img, is_light, repeat)
     self.standalone = None
class DigitalClock():
    def __init__(self, location):
        self.location = location
        self._last_sunrise = datetime.strptime("197007:00", "%Y%H:%M")
        self.sunrise = self._last_sunrise
        self._last_sunset = datetime.strptime("197019:00", "%Y%H:%M")
        self.sunset = self._last_sunset
        self.get_sunrise_sunset()

        self.path = None
        self.list_store = None

        weather_timezone = self.location.get_timezone()
        timezone = GLib.TimeZone.new(weather_timezone.get_tzid())
        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, time.time())
        location_offset = timezone.get_offset(i)

        timezone = GLib.TimeZone.new_local()
        i = timezone.find_interval(GLib.TimeType.UNIVERSAL, time.time())
        here_offset = timezone.get_offset(i)

        self.offset = location_offset - here_offset

        self._last_time = None
        self.drawing = DigitalClockDrawing()
        self.standalone = None
        self.update()

    def get_location_time(self, secs=None):
        if not secs:
            secs = time.time()
        t = datetime.fromtimestamp(secs + self.offset)
        return t

    def update(self, secs=None):
        clock_format = SystemSettings.get_clock_format()
        location_time = self.get_location_time(secs)
        if clock_format == '12h':
            t = location_time.strftime("%I:%M %p")
        else:
            t = location_time.strftime("%H:%M")
        if t.startswith("0"):
            t = t[1:]
        if not t == self._last_time \
                or not self.sunrise == self._last_sunrise \
                or not self.sunset == self._last_sunset:
            is_light = self.get_is_light(location_time)
            if is_light:
                img = os.path.join(Dirs.get_image_dir(), "cities", "day.png")
            else:
                img = os.path.join(Dirs.get_image_dir(), "cities", "night.png")
            day = self.get_day(secs)
            if day == "Today":
                self.drawing.render(t, img, is_light)
            else:
                self.drawing.render(t, img, is_light, day)
            if self.path and self.list_store:
                self.list_store[self.path][1] = self.drawing.pixbuf
            if self.standalone:
                self.standalone.update(img, t, self.sunrise, self.sunset)

        self._last_time = t

    def get_sunrise_sunset(self):
        self.weather = GWeather.Info(location=self.location, world=gweather_world)
        self.weather.connect('updated', self._on_weather_updated)
        self.weather.update()

    def _on_weather_updated(self, weather):
        # returned as the time here
        ok, sunrise = weather.get_value_sunrise()
        ok, sunset = weather.get_value_sunset()
        self._last_sunrise = self.sunrise
        self._last_sunset = self.sunset
        self.sunrise = self.get_location_time(sunrise)
        self.sunset = self.get_location_time(sunset)
        self.update()

    def get_pixbuf(self):
        return self.drawing.pixbuf

    def get_standalone_widget(self):
        if not self.standalone:
            self.standalone = StandaloneClock(self.location, self.sunrise, self.sunset)
        self.update()
        return self.standalone

    def get_day(self, secs=None):
        clock_time = self.get_location_time(secs)

        clock_time_day = clock_time.date()
        local_today = date.today()

        date_offset = (clock_time_day - local_today).days
        if date_offset == 0:
            return "Today"
        elif date_offset == -1:
            return "Yesterday"
        elif date_offset == 1:
            return "Tomorrow"
        else:
            return clock_time_day.strftime("%A")

    def get_is_light(self, current):
        return self.sunrise.time() <= current.time() <= self.sunset.time()

    def set_path(self, list_store, path):
        self.path = path
        self.list_store = list_store