forked from MycroftAI/skill-date-time
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path__init__.py
More file actions
824 lines (713 loc) · 33.8 KB
/
Copy path__init__.py
File metadata and controls
824 lines (713 loc) · 33.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
# Copyright 2017, Mycroft AI Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import calendar
import datetime
import os
import re
from typing import Optional
import geocoder
import pytz
from ovos_bus_client.message import Message
from ovos_date_parser import nice_time, extract_datetime, nice_date, nice_duration, date_time_format, nice_weekday, \
nice_month, nice_day, nice_year
from ovos_utils import classproperty
from ovos_utils.log import LOG
from ovos_utils.parse import fuzzy_match
from ovos_utils.process_utils import RuntimeRequirements
from ovos_utils.time import now_local, get_next_leap_year
from ovos_utterance_normalizer import UtteranceNormalizerPlugin
from ovos_workshop.decorators import intent_handler
from ovos_workshop.resource_files import ResourceFile
from ovos_workshop.skills import OVOSSkill
from timezonefinder import TimezoneFinder
def speakable_timezone(tz):
"""Convert a timezone string to a more speakable form.
This function reformats a timezone string by:
- Inserting spaces between camel case words (e.g., 'EasterIsland' → 'Easter Island')
- Replacing underscores with spaces
- Reversing the components of a timezone path (e.g., 'America/North_Dakota/Center' → 'Center North Dakota America')
Args:
tz (str): A timezone string in pytz format.
Returns:
str: A more natural, speakable form of the timezone.
"""
say = re.sub(r"([a-z])([A-Z])", r"\g<1> \g<2>", tz)
say = say.replace("_", " ")
say = say.split("/")
say.reverse()
return " ".join(say)
class TimeSkill(OVOSSkill):
"""A skill for interacting with date and time information."""
@classproperty
def runtime_requirements(self):
"""this skill does not need internet"""
return RuntimeRequirements(internet_before_load=False,
network_before_load=False,
gui_before_load=False,
requires_internet=False,
requires_network=False,
requires_gui=False,
no_internet_fallback=True,
no_network_fallback=True,
no_gui_fallback=True)
def initialize(self):
"""Initialize the skill by pre-loading language settings and scheduling
the hourly chime.
This method is called automatically when the skill starts, preloading
language-related formatting for date and time and setting up the initial
scheduling for the hourly chime event.
"""
date_time_format.cache(self.lang)
self._schedule_hour_chime()
def _handle_play_hour_chime(self, message: Message):
"""Play the hourly chime audio and re-schedule the next chime event.
Args:
message (Message): message object
This method checks if the hourly chime setting is enabled. If it is, it
plays the specified chime audio. Then, it re-schedules the next hourly
chime event.
"""
if self.play_hour_chime:
self.play_audio(self.hour_chime, instant=True)
self._schedule_hour_chime()
def _schedule_hour_chime(self):
"""Schedule the next hourly chime event for the start of the next hour.
This method calculates the time for the upcoming hour, setting it as
the scheduled time for the next chime event.
"""
n = now_local() + datetime.timedelta(hours=1)
self.schedule_event(self._handle_play_hour_chime,
when=datetime.datetime(year=n.year, month=n.month, day=n.day,
hour=n.hour, minute=0, second=0))
@property
def play_hour_chime(self) -> bool:
"""Check if the hourly chime setting is enabled.
Returns:
bool: True if the chime should be played on the hour, False otherwise.
"""
return self.settings.get("play_hour_chime", False)
@property
def hour_chime(self) -> str:
"""Get the file path for the hourly chime sound.
Returns:
str: The file path to the chime audio file. If not set in settings,
defaults to 'casio-watch.wav' in the 'res' folder.
"""
snd = self.settings.get("hour_sound", "casio-watch.wav")
if not os.path.isfile(snd):
snd2 = f"{os.path.dirname(__file__)}/res/{snd}"
snd = snd2 if os.path.isfile(snd2) else snd
return snd
@property
def use_24hour(self):
"""Determine if 24-hour time format is used.
Returns:
bool: True if using 24-hour format, False otherwise.
"""
return self.time_format == 'full'
@staticmethod
def _normalize_phrase(text: str) -> str:
"""Normalize phrases for case-insensitive locale resource matching."""
return text.strip(" \t,;:.!?").casefold()
def _load_locale_phrase_set(self, name: str):
"""Load a locale phrase list once and return a normalized set."""
cache_key = f"{name}.list.normalized"
if cache_key not in self.resources.static:
self.resources.static[cache_key] = {
self._normalize_phrase(phrase)
for phrase in self.resources.load_list_file(name)
if phrase.strip()
}
return self.resources.static[cache_key]
######################################################################
# parsing
def _extract_location(self, utt: str) -> str:
"""Extract a location name from a spoken utterance using regex patterns.
Args:
utt (str): The user utterance.
Returns:
str: Extracted location if matched, otherwise None.
"""
rx_file = self.find_resource('location.rx', 'regex')
if rx_file:
with open(rx_file) as f:
for pat in f.read().splitlines():
pat = pat.strip()
if pat and pat[0] == "#":
continue
res = re.search(pat, utt, flags=re.IGNORECASE)
if res:
try:
return res.group("Location").strip(" \t,;:.!?")
except IndexError:
pass
return None
def _is_ambiguous_location(self, location_string: str) -> bool:
"""Return True when a locale marks a location name as timezone-ambiguous."""
return (
self._normalize_phrase(location_string)
in self._load_locale_phrase_set("ambiguous_locations")
)
def _sanitize_location(self, location_string: Optional[str]) -> Optional[str]:
"""Discard locale-specific phrases accidentally captured as locations."""
if not location_string:
return None
cleaned = location_string.strip(" \t,;:.!?")
if (
self._normalize_phrase(cleaned)
in self._load_locale_phrase_set("non_location_phrases")
):
return None
return cleaned
def _resolve_location(self,
location_string: Optional[str] = None,
utterance: str = "") -> Optional[str]:
"""Resolve a sanitized location from an explicit slot or from the utterance."""
if location_string:
return self._sanitize_location(location_string)
if utterance:
return self._sanitize_location(self._extract_location(utterance))
return None
def _mentions_current_weekend(self, utterance: str) -> bool:
"""Check whether the active locale explicitly asked for the current weekend."""
current_weekend_phrases = self._load_locale_phrase_set("current_weekend_phrases")
if not current_weekend_phrases:
return False
normalized_utterance = utterance.casefold()
return any(phrase in normalized_utterance for phrase in current_weekend_phrases)
def _load_cached_entity(self, name: str):
"""Load and cache a locale entity resource."""
cache_key = f"{self.lang}:{name}.entity"
if cache_key not in self.resources.static:
entity_resource = ResourceFile(self.resources.types.entity, name)
values = []
if entity_resource.file_path:
with open(entity_resource.file_path, encoding="utf-8") as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
values.append(line)
self.resources.static[cache_key] = values
return self.resources.static[cache_key]
def _get_requested_weekday(self, message):
"""Extract the requested weekday from intent-matched entities."""
weekday_name = (message.data.get("weekday") or "").strip().lower()
if not weekday_name:
return None
weekdays = [day.lower() for day in self._load_cached_entity("weekday")]
try:
weekday_index = weekdays.index(weekday_name)
except ValueError:
return None
return weekday_index, self._render_weekday(weekday_index)
def _render_weekday(self, weekday_index: int) -> str:
"""Render a localized weekday from a weekday index."""
reference_monday = datetime.datetime(2024, 1, 1)
return nice_weekday(reference_monday + datetime.timedelta(days=weekday_index),
lang=self.lang)
def _get_leap_year_query_scope(self, utterance: str) -> str:
"""Return whether the user asked about the current, next, or either year."""
if self.voc_match(utterance, "leap.year.scope.either", lang=self.lang):
return "either"
if self.voc_match(utterance, "leap.year.scope.next", lang=self.lang):
return "next"
return "current"
@staticmethod
def _get_timezone_from_builtins(location_string: str) -> Optional[datetime.tzinfo]:
"""Attempt to resolve a timezone from a location name using geocoding.
Args:
location_string (str): The location name or timezone string.
Returns:
Optional[datetime.tzinfo]: The corresponding timezone, or None if not found.
"""
if "/" not in location_string:
try:
# This handles common city names, like "Dallas" or "Paris"
# first get the lat / long.
g = geocoder.osm(location_string)
# now look it up
tf = TimezoneFinder()
timezone = tf.timezone_at(lng=g.lng, lat=g.lat)
return pytz.timezone(timezone)
except Exception:
pass
try:
# This handles codes like "America/Los_Angeles"
return pytz.timezone(location_string)
except Exception:
pass
return None
def _get_timezone_from_table(self, location_string: str) -> Optional[datetime.tzinfo]:
"""Resolve timezone using a manually defined lookup table.
Args:
location_string (str): The location string to resolve.
Returns:
Optional[datetime.tzinfo]: The corresponding timezone, or None if not found.
"""
timezones = self.resources.load_named_value_file("timezone.value", ',')
for timezone in timezones:
if location_string.lower() == timezone.lower():
# assumes translation is correct
return pytz.timezone(timezones[timezone].strip())
return None
def _get_timezone_from_fuzzymatch(self, location_string: str) -> Optional[datetime.tzinfo]:
"""Fuzzymatch a location against the pytz timezones.
The pytz timezones consists of
Location/Name pairs. For example:
["Africa/Abidjan", "Africa/Accra", ... "America/Denver", ...
"America/New_York", ..., "America/North_Dakota/Center", ...
"Cuba", ..., "EST", ..., "Egypt", ..., "Etc/GMT+3", ...
"Etc/Zulu", ... "US/Eastern", ... "UTC", ..., "Zulu"]
These are parsed and compared against the provided location.
"""
target = location_string.lower()
best = None
for name in pytz.all_timezones:
# Separate at '/'
normalized = name.lower().replace("_", " ").split("/")
if len(normalized) == 1:
pct = fuzzy_match(normalized[0], target)
elif len(normalized) >= 2:
# Check for locations like "Sydney"
pct1 = fuzzy_match(normalized[1], target)
# locations like "Sydney Australia" or "Center North Dakota"
pct2 = fuzzy_match(normalized[-2] + " " + normalized[-1],
target)
pct3 = fuzzy_match(normalized[-1] + " " + normalized[-2],
target)
pct = max(pct1, pct2, pct3)
if not best or pct >= best[0]:
best = (pct, name)
if best and best[0] > 0.8:
# solid choice
return pytz.timezone(best[1])
elif best and best[0] > 0.3:
say = speakable_timezone(best[1])
if self.ask_yesno("did.you.mean.timezone",
data={"zone_name": say}) == "yes":
return pytz.timezone(best[1])
else:
return None
def get_timezone_in_location(self, location_string: str) -> datetime.tzinfo:
"""Get the timezone for a given location using multiple fallback strategies.
This method attempts to resolve a timezone by checking built-in resources,
a custom lookup table, and finally fuzzy matching.
Args:
location_string (str): A string representing a location (e.g., city or region).
Returns:
datetime.tzinfo: The timezone object if resolved, else None.
"""
if self._is_ambiguous_location(location_string):
return None
timezone = self._get_timezone_from_builtins(location_string)
if not timezone:
timezone = self._get_timezone_from_table(location_string)
if not timezone:
timezone = self._get_timezone_from_fuzzymatch(location_string)
return timezone
######################################################################
# utils
def get_datetime(self, location: str = None,
anchor_date: datetime.datetime = None) -> Optional[datetime.datetime]:
"""Return the localized datetime for a given location or current session.
Args:
location (str, optional): A location name for timezone conversion.
anchor_date (datetime.datetime, optional): A reference date. Defaults to now.
Returns:
Optional[datetime.datetime]: The localized datetime, or None if timezone cannot be resolved.
"""
if location:
tz = self.get_timezone_in_location(location)
if not tz:
return None # tz not found
else:
# self.location_timezone comes from Session
tz = pytz.timezone(self.location_timezone)
if anchor_date:
dt = anchor_date.astimezone(tz)
else:
dt = now_local(tz)
return dt
def get_spoken_time(self, location: str = None, force_ampm=False,
anchor_date: datetime.datetime = None) -> Optional[str]:
"""Get a human-readable spoken version of the current time.
Args:
location (str, optional): Location for timezone conversion.
force_ampm (bool, optional): Whether to force AM/PM mode even if using 24-hour format.
anchor_date (datetime.datetime, optional): Specific time to use instead of now.
Returns:
str: A spoken-friendly representation of the time.
"""
dt = self.get_datetime(location, anchor_date)
if not dt:
return None
# speak AM/PM when talking about somewhere else
say_am_pm = bool(location) or force_ampm
s = nice_time(dt, lang=self.lang, speech=True,
use_24hour=self.use_24hour, use_ampm=say_am_pm)
# HACK: Mimic 2 has a bug with saying "AM". Work around it for now.
if say_am_pm:
s = s.replace("AM", "A.M.")
return s
def get_display_time(self, location: str = None, force_ampm=False,
anchor_date: datetime.datetime = None) -> Optional[str]:
"""Get a display-friendly version of the current time.
Args:
location (str, optional): Location for timezone conversion.
force_ampm (bool, optional): Whether to display time in AM/PM format.
anchor_date (datetime.datetime, optional): Specific time to use instead of now.
Returns:
str: A string representing the display time.
"""
dt = self.get_datetime(location, anchor_date)
if not dt:
return None
# speak AM/PM when talking about somewhere else
say_am_pm = bool(location) or force_ampm
return nice_time(dt, lang=self.lang,
speech=False,
use_24hour=self.use_24hour, # session aware
use_ampm=say_am_pm)
def get_display_date(self, location: str = None,
anchor_date: datetime.datetime = None) -> str:
"""Get a localized and display-friendly version of the current date.
Args:
location (str, optional): Location name for timezone context.
anchor_date (datetime.datetime, optional): Date to display instead of now.
Returns:
str: A string representing the formatted date.
"""
dt = self.get_datetime(location, anchor_date)
fmt = self.date_format # Session aware
if fmt == 'MDY':
return dt.strftime("%-m/%-d/%Y")
elif fmt == 'YMD':
return dt.strftime("%-Y/%-m/%d")
elif fmt == 'YDM':
return dt.strftime("%-Y/%-d/%m")
elif fmt == 'DMY':
return dt.strftime("%d/%-m/%-Y")
######################################################################
# Time queries / display
def speak_time(self, dialog: str, location: str = None,
anchor_date: datetime.datetime = None):
"""Speak the current time. Optionally at a location
speaks an error if timezone for requested location could not be detected"""
if location:
current_time = self.get_spoken_time(location, anchor_date=anchor_date)
if not current_time:
self.speak_dialog("time.tz.not.found", {"location": location})
return
time_string = self.get_display_time(location, anchor_date=anchor_date)
else:
current_time = self.get_spoken_time(anchor_date=anchor_date)
time_string = self.get_display_time(anchor_date=anchor_date)
# speak it
self.speak_dialog(dialog, {"time": current_time})
# and briefly show the time
if time_string:
self.show_time(time_string)
@intent_handler("what.time.is.it.intent")
def handle_query_time(self, message):
"""Handle queries about the current time."""
utt = message.data.get('utterance', "")
location = self._resolve_location(message.data.get("location"), utt)
# speak it
self.speak_time("time.current", location=location)
@intent_handler("what.time.will.it.be.intent")
def handle_query_future_time(self, message):
normalizer = UtteranceNormalizerPlugin.get_normalizer(self.lang)
utt = normalizer.normalize(message.data["utterance"])
dt, utt = extract_datetime(utt, lang=self.lang) or (None, None)
if not dt:
self.handle_query_time(message)
return
location = self._resolve_location(message.data.get("location"), utt)
# speak it
self.speak_time("time.future", location=location, anchor_date=dt)
######################################################################
# Date queries
def handle_query_date(self, message, response_type="simple"):
"""Handle queries about the current date."""
utt = message.data.get('utterance', "").lower()
now = self.get_datetime() # session aware
try:
dt, utt = extract_datetime(utt, anchorDate=now, lang=self.lang) or (now, utt)
except Exception:
self.log.exception(f"failed to extract date from '{utt}'")
dt = now
# handle questions ~ "what is the day in sydney"
location_string = self._resolve_location(message.data.get("location"), utt)
if location_string:
dt = self.get_datetime(location_string, anchor_date=dt)
if not dt:
self.speak_dialog("time.tz.not.found",
{"location": location_string})
return # failed in timezone lookup
speak_date = nice_date(dt, 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 = dt.replace(hour=0, minute=0,
second=0, microsecond=0)
today_date = now.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, lang=self.lang)
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, lang=self.lang)
self.speak_dialog("date.relative.past",
{"date": speak_date,
"num_days": speak_num_days})
# and briefly show the date
self.show_date(dt, location=location_string)
@intent_handler("current_date.intent")
def handle_current_date(self, message):
"""Handle current date queries."""
self.handle_query_date(message, response_type="simple")
@intent_handler("time.until.intent")
def handle_time_until(self, message):
self.handle_query_date(message, response_type="relative")
@intent_handler("what.day.is.it.intent")
def handle_current_day(self, message):
"""
Speaks the current day name using a localized dialog.
Args:
message: The message object triggering the intent.
"""
utt = message.data.get("utterance", "")
location = self._resolve_location(message.data.get("location"), utt)
now = self.get_datetime(location)
if location and not now:
self.speak_dialog("time.tz.not.found", {"location": location})
return
self.speak_dialog("day.current",
{"day": nice_day(now, lang=self.lang)})
# TODO - merge with weekday.for.date.intent
# use voc_match or something to disambiguate
@intent_handler("what.weekday.is.it.intent")
def handle_current_weekday(self, message):
"""
Handles queries about the current weekday and speaks the name of today's weekday.
Responds to user requests for the current weekday by retrieving the localized current date and speaking the corresponding weekday name.
"""
now = self.get_datetime() # session aware
self.speak_dialog("weekday.current",
{"weekday": nice_weekday(now, lang=self.lang)})
@intent_handler("weekday.for.date.intent")
def handle_weekday(self, message):
"""
Handles queries about the weekday for a specific date.
Extracts a date from the user's message and responds with the weekday name and a contextual dialog indicating whether the date is in the past or future. If no date can be extracted, speaks an error dialog.
"""
now = self.get_datetime() # session aware
dt, _ = extract_datetime(message.data.get("date") or message.data["utterance"],
anchorDate=now, lang=self.lang) or (None, None)
if not dt:
self.speak_dialog("extract.date.error")
return
if dt >= now:
dialog = "weekday.at.date.future"
else:
dialog = "weekday.at.date.past"
# TODO - "today" should never trigger this intent, but if it does,
# should we handle it better? nice_date will return "today" in that case
self.speak_dialog(dialog, {
"date": nice_date(dt, lang=self.lang, now=now),
"weekday": nice_weekday(dt, lang=self.lang)})
@intent_handler("weekday.matches.date.intent")
def handle_weekday_match(self, message):
"""Handle yes/no questions about whether a date matches a weekday."""
now = self.get_datetime() # session aware
utterance = message.data.get("utterance", "")
weekday = self._get_requested_weekday(message)
dt, _ = extract_datetime(message.data.get("date") or utterance,
anchorDate=now, lang=self.lang) or (None, None)
if not dt or weekday is None:
self.speak_dialog("extract.date.error")
return
expected_weekday, expected_name = weekday
data = {
"date": nice_date(dt, lang=self.lang, now=now),
"weekday": expected_name,
"actual_weekday": nice_weekday(dt, lang=self.lang)
}
if dt.date() > now.date():
dialog = (
"weekday.matches.date.future.yes"
if dt.weekday() == expected_weekday
else "weekday.matches.date.future.no"
)
elif dt.date() == now.date():
dialog = (
"weekday.matches.date.today.yes"
if dt.weekday() == expected_weekday
else "weekday.matches.date.today.no"
)
else:
dialog = (
"weekday.matches.date.past.yes"
if dt.weekday() == expected_weekday
else "weekday.matches.date.past.no"
)
self.speak_dialog(dialog, data)
@intent_handler("what.month.is.it.intent")
def handle_current_month(self, message):
"""
Handles queries about the current month and speaks its name.
Args:
message: The message object containing the user's request.
"""
now = self.get_datetime() # session aware
self.speak_dialog("month.current",
{"month": nice_month(now, lang=self.lang)})
@intent_handler("what.year.is.it.intent")
def handle_current_year(self, message):
now = self.get_datetime() # session aware
self.speak_dialog("year.current",
{"year": nice_year(now, lang=self.lang)})
@intent_handler("date.future.weekend.intent")
def handle_date_future_weekend(self, message):
"""
Handles queries about the upcoming weekend's dates.
Determines the dates for the next Saturday and Sunday, formats them for speech, and responds with a dialog containing both dates.
"""
now = self.get_datetime()
utt = message.data.get("utterance", "").lower()
weekday = now.weekday()
# On Saturday/Sunday, default to the upcoming weekend unless the user
# explicitly asked for the current weekend in this locale.
if weekday <= 5:
saturday_dt = now + datetime.timedelta(days=5 - weekday)
else:
saturday_dt = now - datetime.timedelta(days=1)
if weekday >= 5 and not self._mentions_current_weekend(utt):
saturday_dt += datetime.timedelta(days=7)
sunday_dt = saturday_dt + datetime.timedelta(days=1)
saturday_date = ', '.join(nice_date(saturday_dt, lang=self.lang).split(', ')[:2])
sunday_date = ', '.join(nice_date(sunday_dt, lang=self.lang).split(', ')[:2])
self.speak_dialog('date.future.weekend', {
'saturday_date': saturday_date,
'sunday_date': sunday_date
})
# TODO - merge date.last.weekend.intent and date.future.weekend.intent handlers
# use voc_match or something to disambiguate
@intent_handler("date.last.weekend.intent")
def handle_date_last_weekend(self, message):
# Strip year off nice_date as request is inherently close
# Don't pass `now` to `nice_date` as a
# request on Monday will return "yesterday"
"""
Handles the intent to provide the dates of the previous weekend.
Speaks a dialog with the formatted dates for last Saturday and Sunday.
"""
now = self.get_datetime()
dt = extract_datetime('last saturday',
anchorDate=now, lang='en-us')[0]
saturday_date = ', '.join(nice_date(dt, lang=self.lang).split(', ')[:2])
dt = extract_datetime('last sunday',
anchorDate=now, lang='en-us')[0]
sunday_date = ', '.join(nice_date(dt, lang=self.lang).split(', ')[:2])
self.speak_dialog('date.last.weekend', {
'saturday_date': saturday_date,
'sunday_date': sunday_date
})
@intent_handler("next.leap.year.intent")
def handle_query_next_leap_year(self, message):
"""
Handles the intent to provide the year of the next leap year.
Determines the next leap year based on the current date and speaks the result to the user.
"""
now = self.get_datetime()
leap_date = now_local().replace(month=2, day=28)
year = now.year if now <= leap_date else now.year + 1
next_leap_year = get_next_leap_year(year)
self.speak_dialog('next.leap.year', {'year': next_leap_year})
@intent_handler("is.leap.year.intent")
def handle_is_leap_year(self, message):
"""Handle yes/no questions about leap years."""
utterance = message.data.get("utterance", "")
now = self.get_datetime()
current_year = now.year
next_year = current_year + 1
scope = self._get_leap_year_query_scope(utterance)
if scope == "either":
if calendar.isleap(current_year):
self.speak_dialog("leap.year.either.yes",
{"year": current_year})
elif calendar.isleap(next_year):
self.speak_dialog("leap.year.either.yes",
{"year": next_year})
else:
self.speak_dialog("leap.year.either.no",
{"year": get_next_leap_year(next_year + 1)})
return
if scope == "next":
if calendar.isleap(next_year):
self.speak_dialog("leap.year.next.yes", {"year": next_year})
else:
self.speak_dialog("leap.year.next.no",
{"year": get_next_leap_year(next_year)})
return
if calendar.isleap(current_year):
self.speak_dialog("leap.year.current.yes",
{"year": current_year})
else:
self.speak_dialog("leap.year.current.no",
{"year": get_next_leap_year(current_year)})
######################################################################
# GUI / Faceplate
def show_date(self, dt: datetime.datetime, location: str):
"""Display date on GUI and Mark 1 faceplate."""
self.show_date_gui(dt, location)
self.show_date_mark1(dt)
def show_date_mark1(self, dt: datetime.datetime):
show = self.get_display_date(anchor_date=dt)
LOG.debug(f"sending date to mk1 {show}")
self.bus.emit(Message("ovos.mk1.display_date",
{"text": show}))
def show_date_gui(self, dt: datetime.datetime, location: str):
self.gui.clear()
self.gui['location_string'] = str(location)
self.gui['date_string'] = self.get_display_date(anchor_date=dt)
self.gui['weekday_string'] = nice_weekday(dt, lang=self.lang)
self.gui['day_string'] = dt.strftime('%d')
self.gui['month_string'] = nice_month(dt, lang=self.lang)
self.gui['year_string'] = dt.strftime("%Y")
if self.date_format == 'MDY':
self.gui['daymonth_string'] = f"{self.gui['month_string']} {self.gui['day_string']}"
else:
self.gui['daymonth_string'] = f"{self.gui['day_string']} {self.gui['month_string']}"
self.gui.show_page('date')
def show_time(self, display_time: str):
"""Display time on GUI and Mark 1 faceplate."""
self.show_time_gui(display_time)
self.show_time_mark1(display_time)
def show_time_mark1(self, display_time: str):
LOG.debug(f"Emitting ovos.mk1.display_time with time: {display_time}")
self.bus.emit(Message("ovos.mk1.display_time", {"text": display_time}))
def show_time_gui(self, display_time):
""" Display time on the GUI. """
self.gui.clear()
self.gui['time_string'] = display_time
self.gui['ampm_string'] = ''
self.gui['date_string'] = self.get_display_date()
self.gui.show_page('time')