Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/lingua nostra - default_tz #6

Merged
merged 3 commits into from
Mar 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mycroft/skills/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
wait_for_exit_signal
)
from mycroft.util.lang import set_default_lang
from mycroft.util.time import set_default_tz
from mycroft.util.log import LOG
from mycroft.skills.core import FallbackSkill
from mycroft.skills.event_scheduler import EventScheduler
Expand Down Expand Up @@ -192,6 +193,9 @@ def main(ready_hook=on_ready, error_hook=on_error, stopping_hook=on_stopping,
# Set the active lang to match the configured one
set_default_lang(config.get('lang', 'en-us'))

# Set the default timezone to match the configured one
set_default_tz()

# Connect this process to the Mycroft message bus
bus = _start_message_bus_client()
_register_intent_services(bus)
Expand Down
5 changes: 4 additions & 1 deletion mycroft/skills/intent_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from mycroft.configuration import Configuration
from mycroft.util.lang import set_default_lang
from mycroft.util.time import set_default_tz
from mycroft.util.log import LOG
from mycroft.util.parse import normalize
from mycroft.metrics import report_timing, Stopwatch
Expand Down Expand Up @@ -156,7 +157,8 @@ def get_skill_name(self, skill_id):
def reset_converse(self, message):
"""Let skills know there was a problem with speech recognition"""
lang = _get_message_lang(message)
set_default_lang(lang)
set_default_lang(lang) # restore default lang
set_default_tz() # restore default timezone
for skill in copy(self.active_skills):
self.do_converse(None, skill[0], lang, message)

Expand Down Expand Up @@ -277,6 +279,7 @@ def handle_utterance(self, message):
try:
lang = _get_message_lang(message)
set_default_lang(lang)
set_default_tz() # set default timezone

utterances = message.data.get('utterances', [])
combined = _normalize_all_utterances(utterances)
Expand Down
2 changes: 1 addition & 1 deletion mycroft/skills/skill_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from .skill_loader import SkillLoader
from .skill_updater import SkillUpdater

from lingua_franca import load_languages, set_default_lang
from lingua_nostra import load_languages, set_default_lang

SKILL_MAIN_MODULE = '__init__.py'

Expand Down
4 changes: 2 additions & 2 deletions mycroft/util/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
from calendar import leapdays
from enum import Enum

from lingua_franca import get_default_lang
from lingua_nostra import get_default_lang
# These are the main functions we are using lingua franca to provide
from lingua_franca.format import (NUMBER_TUPLE, DateTimeFormat, join_list,
from lingua_nostra.format import (NUMBER_TUPLE, DateTimeFormat, join_list,
date_time_format, expand_options,
_translate_word,
nice_number, nice_time, pronounce_number,
Expand Down
2 changes: 1 addition & 1 deletion mycroft/util/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
lingua-franca (https://github.com/mycroftai/lingua-franca) selected language
"""

from lingua_franca import set_default_lang, get_default_lang
from lingua_nostra import set_default_lang, get_default_lang
8 changes: 4 additions & 4 deletions mycroft/util/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
from difflib import SequenceMatcher
from warnings import warn

import lingua_franca.parse
from lingua_franca import get_default_lang, get_primary_lang_code
from lingua_franca.parse import extract_number, extract_numbers, \

from lingua_nostra import get_default_lang, get_primary_lang_code
from lingua_nostra.parse import extract_number, extract_numbers, \
extract_duration, get_gender, normalize
from lingua_franca.parse import extract_datetime as lf_extract_datetime
from lingua_nostra.parse import extract_datetime as lf_extract_datetime

from .time import now_local
from .log import LOG
Expand Down
80 changes: 10 additions & 70 deletions mycroft/util/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@
system. This time is based on the setting in the Mycroft config and may or
may not match the system locale.
"""
from datetime import datetime
from dateutil.tz import gettz, tzlocal
from dateutil.tz import gettz
from lingua_nostra.time import set_default_tz as _set_default_tz, now_utc, \
now_local, to_local, to_utc, to_system, default_timezone as _default_tz


def set_default_tz(tz=None):
if not tz:
tz = default_timezone()
_set_default_tz(tz)


def default_timezone():
Expand All @@ -42,72 +49,5 @@ def default_timezone():
return gettz(code)
except Exception:
# Just go with system default timezone
return tzlocal()


def now_utc():
"""Retrieve the current time in UTC

Returns:
(datetime): The current time in Universal Time, aka GMT
"""
return to_utc(datetime.utcnow())


def now_local(tz=None):
"""Retrieve the current time

Arguments:
tz (datetime.tzinfo, optional): Timezone, default to user's settings
return _default_tz()

Returns:
(datetime): The current time
"""
if not tz:
tz = default_timezone()
return datetime.now(tz)


def to_utc(dt):
"""Convert a datetime with timezone info to a UTC datetime

Arguments:
dt (datetime): A datetime (presumably in some local zone)
Returns:
(datetime): time converted to UTC
"""
tzUTC = gettz("UTC")
if dt.tzinfo:
return dt.astimezone(tzUTC)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tzUTC)


def to_local(dt):
"""Convert a datetime to the user's local timezone

Arguments:
dt (datetime): A datetime (if no timezone, defaults to UTC)
Returns:
(datetime): time converted to the local timezone
"""
tz = default_timezone()
if dt.tzinfo:
return dt.astimezone(tz)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tz)


def to_system(dt):
"""Convert a datetime to the system's local timezone

Arguments:
dt (datetime): A datetime (if no timezone, assumed to be UTC)
Returns:
(datetime): time converted to the operation system's timezone
"""
tz = tzlocal()
if dt.tzinfo:
return dt.astimezone(tz)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tz)
6 changes: 2 additions & 4 deletions requirements/minimal.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
requests
pyee
#lingua-franca
git+https://github.com/HelloChatterbox/[email protected]
git+https://github.com/HelloChatterbox/lingua-nostra
pyxdg
#mycroft-messagebus-client
git+https://github.com/HelloChatterbox/mycroft-messagebus-client
mycroft-messagebus-client>=0.9.1
inflection
psutil
fasteners
Expand Down
4 changes: 2 additions & 2 deletions requirements/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ python-dateutil==2.6.0
fasteners==0.14.1
PyYAML==5.1.2

lingua-franca==0.2.2
git+https://github.com/HelloChatterbox/lingua-nostra
msm==0.8.8
msk==0.3.16
adapt-parser==0.3.7
Expand All @@ -25,4 +25,4 @@ padaos==0.1.9
precise-runner==0.2.1
petact==0.1.2
pyxdg==0.26
git+https://github.com/MycroftAI/mycroft-messagebus-client
mycroft-messagebus-client>=0.9.1