Beispiel #1
0
    def dump_alarms(self, tag=""):
        # Useful when debugging
        dump = "\n" + "="*30 + " ALARMS " + tag + " " + "="*30 + "\n"
        dump += "raw = " + str(self.settings["alarm"]) + "\n\n"

        now_ts = to_utc(now_utc()).timestamp()
        dt = datetime.fromtimestamp(now_ts)
        dump += "now = {} ({})\n".format(
            nice_time(self.get_alarm_local(timestamp=now_ts),
                      speech=False, use_ampm=not self.use_24hour, use_24hour = self.use_24hour),
            now_ts)
        dump += "      U{} L{}\n".format(to_utc(dt), to_local(dt))
        dump += "\n\n"

        idx = 0
        for alarm in self.settings["alarm"]:
            dt = self.get_alarm_local(alarm)
            dump += "alarm[{}] - {} \n".format(idx, alarm)
            dump += "           Next: {} {}\n".format(
                nice_time(dt, speech=False, use_ampm= not self.use_24hour, use_24hour = self.use_24hour),
                nice_date(dt, now=now_local()))
            dump += "                 U{} L{}\n".format(dt, to_local(dt))
            if 'snooze' in alarm:
                dtOrig = self.get_alarm_local(timestamp=alarm['snooze'])
                dump += "           Orig: {} {}\n".format(
                    nice_time(dtOrig, speech=False, use_ampm= not self.use_24hour, use_24hour = self.use_24hour),
                    nice_date(dtOrig, now=now_local()))
            idx += 1

        dump += "="*75

        self.log.info(dump)
Beispiel #2
0
 def test_to_local(self, mock_conf):
     mock_conf.get.return_value = test_config
     dt = datetime(year=2000, month=1, day=1,
                   hour=0, minute=0, second=0,
                   tzinfo=gettz('Europe/Stockholm'))
     self.assertEqual(to_local(dt), dt)
     self.assertEqual(to_local(dt).tzinfo, gettz('America/Chicago'))
Beispiel #3
0
 def __to_Local(self, when):
     try:
         # First try with modern mycroft.util.time functions
         return to_local(when)
     except Exception:
         # Fallback to the old pytz code
         if not when.tzinfo:
             when = when.replace(tzinfo=pytz.utc)
         timezone = pytz.timezone(self.location["timezone"]["code"])
         return when.astimezone(timezone)
Beispiel #4
0
def nice_relative_time_cs(when, relative_to=None, lang=None):
    """ Create a relative phrase to roughly describe a datetime

    Examples are "25 seconds", "tomorrow", "7 days".

    Args:
        when (datetime): Local timezone
        relative_to (datetime): Baseline for relative time, default is now()
        lang (str, optional): Defaults to "en-us".
    Returns:
        str: Relative description of the given time
    """
    if relative_to:
        now = relative_to
    else:
        now = now_local()
    delta = (to_local(when) - now)

    if delta.total_seconds() < 1:
        return "teď"

    if delta.total_seconds() < 90:
        if delta.total_seconds() == 1:
            return "jednu vteřinu"
        elif 1 < delta.total_seconds() < 5:
            return "{} vteřiny".format(int(delta.total_seconds()))
        else:
            return "{} vteřin".format(int(delta.total_seconds()))

    minutes = int((delta.total_seconds()+30) // 60)  # +30 to round minutes
    if minutes < 90:
        if minutes == 1:
            return "jedna minuta"
        elif 1 < minutes < 5:
            return "{} minuty".format(minutes)    
        else:
            return "{} minut".format(minutes)

    hours = int((minutes+30) // 60)  # +30 to round hours
    if hours < 36:
        if hours == 1:
            return "jedna hodina"
        elif 1 < hours < 5:
            return "{} hodiny".format(hours)
        else:
            return "{} hodin".format(hours)

    # TODO: "2 weeks", "3 months", "4 years", etc
    days = int((hours+12) // 24)  # +12 to round days
    if days == 1:
        return "1 den"
    elif 1 < days < 5:
        return "{} dny".format(days)
    else:
        return "{} dní".format(days)
    def handle_query_date(self, message, response_type="simple"):
        utt = message.data.get('utterance', "").lower()
        try:
            extract = extract_datetime(utt)
        except Exception:
            self.speak_dialog('date.not.found')
            return
        day = extract[0] if extract else now_local()

        # check if a Holiday was requested, e.g. "What day is Christmas?"
        year = extract_number(utt)
        if not year or year < 1500 or year > 3000:  # filter out non-years
            year = day.year
        all_holidays = {}
        # TODO: How to pick a location for holidays?
        for st in holidays.US.STATES:
            holiday_dict = holidays.US(years=[year], state=st)
            for d, name in holiday_dict.items():
                if name not in all_holidays:
                    all_holidays[name] = d
        for name in all_holidays:
            d = all_holidays[name]
            # Uncomment to display all holidays in the database
            # self.log.info("Day, name: " +str(d) + " " + str(name))
            if name.replace(" Day", "").lower() in utt:
                day = d
                break

        location = self._extract_location(utt)
        today = to_local(now_utc())
        if location:
            # TODO: Timezone math!
            if (day.year == today.year and day.month == today.month
                    and day.day == today.day):
                day = now_utc()  # for questions ~ "what is the day in sydney"
            day = self.get_local_datetime(location, dtUTC=day)
        if not day:
            return  # failed in timezone lookup

        speak_date = nice_date(day, lang=self.lang)
        # speak it
        if response_type == "simple":
            self.speak_dialog("date", {"date": speak_date})
        elif response_type == "relative":
            # remove time data to get clean dates
            day_date = day.replace(hour=0, minute=0, second=0, microsecond=0)
            today_date = today.replace(hour=0,
                                       minute=0,
                                       second=0,
                                       microsecond=0)
            num_days = (day_date - today_date).days
            if num_days >= 0:
                speak_num_days = nice_duration(num_days * 86400)
                self.speak_dialog("date.relative.future", {
                    "date": speak_date,
                    "num_days": speak_num_days
                })
            else:
                # if in the past, make positive before getting duration
                speak_num_days = nice_duration(num_days * -86400)
                self.speak_dialog("date.relative.past", {
                    "date": speak_date,
                    "num_days": speak_num_days
                })

        # and briefly show the date
        self.answering_query = True
        self.show_date(location, day=day)
        time.sleep(10)
        mycroft.audio.wait_while_speaking()
        if self.platform == "mycroft_mark_1":
            self.enclosure.mouth_reset()
            self.enclosure.activate_mouth_events()
        self.answering_query = False
        self.displayed_time = None