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/timezone_config #180

Closed
Closed
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions lingua_franca/config.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
load_langs_on_demand = False
inject_timezones = True
chrisveilleux marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 15 additions & 4 deletions lingua_franca/internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from functools import wraps
from importlib import import_module
from inspect import signature
from sys import version
from warnings import warn

from warnings import warn
from datetime import datetime
from lingua_franca import config
from lingua_franca.time import to_local


_SUPPORTED_LANGUAGES = ("ca", "cs", "da", "de", "en", "es", "fr", "hu",
"it", "nl", "pl", "pt", "sl", "sv")
Expand Down Expand Up @@ -456,10 +458,19 @@ def _call_localized_function(func, *args, **kwargs):
lang_param_index = func_params.index('lang')
full_lang_code = None

# Check if we need to add timezone awareness to any datetime object
if config.inject_timezones:
for k, v in kwargs.items():
if isinstance(v, datetime) and v.tzinfo is None:
kwargs[k] = to_local(v)
for idx, v in enumerate(args):
if isinstance(v, datetime) and v.tzinfo is None:
args = args[:idx] + (to_local(v),) + args[idx + 1:]
krisgesling marked this conversation as resolved.
Show resolved Hide resolved
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved

# Check if we're passing a lang as a kwarg
if 'lang' in kwargs.keys():
lang_param = kwargs['lang']
if lang_param == None:
if lang_param is None:
warn(NoneLangWarning)
lang_code = get_default_lang()
else:
Expand All @@ -468,7 +479,7 @@ def _call_localized_function(func, *args, **kwargs):
# Check if we're passing a lang as a positional arg
elif lang_param_index < len(args):
lang_param = args[lang_param_index]
if lang_param == None:
if lang_param is None:
warn(NoneLangWarning)
lang_code = get_default_lang()
elif lang_param in _SUPPORTED_LANGUAGES or \
Expand Down
8 changes: 6 additions & 2 deletions lingua_franca/lang/parse_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
TODO: numbers greater than 999999
TODO: date time ca
"""

from datetime import datetime
from dateutil.relativedelta import relativedelta
from lingua_franca.time import now_local
from lingua_franca.lang.parse_common import is_numeric, look_for_fractions
from lingua_franca.lang.common_data_ca import _NUMBERS_CA, \
_FEMALE_DETERMINANTS_CA, _FEMALE_ENDINGS_CA, \
Expand Down Expand Up @@ -337,9 +337,10 @@ def date_found():
minAbs or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
JarbasAl marked this conversation as resolved.
Show resolved Hide resolved
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
Expand Down Expand Up @@ -995,6 +996,9 @@ def date_found():
datestr = datestr.replace(monthsShort[idx], en_month)

temp = datetime.strptime(datestr, "%B %d")
if extractedDate.tzinfo:
temp = temp.replace(tzinfo=extractedDate.tzinfo)

if not hasYear:
temp = temp.replace(year=extractedDate.year)
if extractedDate < temp:
Expand Down
3 changes: 2 additions & 1 deletion lingua_franca/lang/parse_cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,10 @@ def date_found():
minAbs or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
chrisveilleux marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
7 changes: 6 additions & 1 deletion lingua_franca/lang/parse_da.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
extract_numbers_generic, Normalizer
from lingua_franca.lang.common_data_da import _DA_NUMBERS
from lingua_franca.lang.format_da import pronounce_number_da
from lingua_franca.time import now_local


def extract_number_da(text, short_scale=True, ordinals=False):
Expand Down Expand Up @@ -141,9 +142,10 @@ def date_found():
minAbs or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
Expand Down Expand Up @@ -707,6 +709,9 @@ def date_found():
datestr = datestr.replace(monthsShort[idx], en_month)

temp = datetime.strptime(datestr, "%B %d")
if extractedDate.tzinfo:
temp = temp.replace(tzinfo=extractedDate.tzinfo)

if not hasYear:
temp = temp.replace(year=extractedDate.year)
if extractedDate < temp:
Expand Down
8 changes: 7 additions & 1 deletion lingua_franca/lang/parse_de.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
extract_numbers_generic, Normalizer
from lingua_franca.lang.common_data_de import _DE_NUMBERS
from lingua_franca.lang.format_de import pronounce_number_de
from lingua_franca.time import now_local


de_numbers = {
'null': 0,
Expand Down Expand Up @@ -260,9 +262,10 @@ def date_found():
minAbs or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
Expand Down Expand Up @@ -833,6 +836,9 @@ def date_found():
datestr = datestr.replace(monthsShort[idx], en_month)

temp = datetime.strptime(datestr, "%B %d")
if extractedDate.tzinfo:
temp = temp.replace(tzinfo=extractedDate.tzinfo)

if not hasYear:
temp = temp.replace(year=extractedDate.year)
if extractedDate < temp:
Expand Down
5 changes: 4 additions & 1 deletion lingua_franca/lang/parse_en.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from dateutil.relativedelta import relativedelta

from lingua_franca.time import now_local
from lingua_franca.lang.parse_common import is_numeric, look_for_fractions, \
invert_dict, ReplaceableNumber, partition_list, tokenize, Token, Normalizer
from lingua_franca.lang.common_data_en import _ARTICLES_EN, _NUM_STRING_EN, \
Expand Down Expand Up @@ -671,8 +672,10 @@ def date_found():
hrAbs or minOffset != 0 or
minAbs or secOffset != 0
)

if not anchorDate:
anchorDate = datetime.now()
anchorDate = now_local()

if text == "":
return None

Expand Down
20 changes: 11 additions & 9 deletions lingua_franca/lang/parse_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#
from datetime import datetime
from dateutil.relativedelta import relativedelta
from dateutil.tz import gettz

from lingua_franca.time import now_local
from lingua_franca.lang.format_es import pronounce_number_es
from lingua_franca.lang.parse_common import *
from lingua_franca.lang.common_data_es import _ARTICLES_ES, _STRING_NUM_ES
Expand Down Expand Up @@ -367,7 +368,7 @@ def date_found():
if text == "":
return None
if anchorDate is None:
anchorDate = datetime.now()
anchorDate = now_local()

found = False
daySpecified = False
Expand Down Expand Up @@ -1020,17 +1021,18 @@ def date_found():
datestr = datestr.replace(monthsShort[idx], en_month)

temp = datetime.strptime(datestr, "%B %d")
temp = temp.replace(tzinfo=None)
if extractedDate.tzinfo:
temp = temp.replace(tzinfo=extractedDate.tzinfo)
# temp = to_local(temp)

if not hasYear:
temp = temp.replace(year=extractedDate.year)

if extractedDate < temp:
extractedDate = extractedDate.replace(year=int(currentYear),
month=int(
temp.strftime(
"%m")),
day=int(temp.strftime(
"%d")))
extractedDate = extractedDate.replace(
year=int(currentYear),
month=int(temp.strftime("%m")),
day=int(temp.strftime("%d")))
else:
extractedDate = extractedDate.replace(
year=int(currentYear) + 1,
Expand Down
9 changes: 8 additions & 1 deletion lingua_franca/lang/parse_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
# limitations under the License.
#
import re
from dateutil.tz import gettz
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from lingua_franca.lang.parse_common import is_numeric, look_for_fractions, \
extract_numbers_generic, Normalizer
from lingua_franca.lang.format_fr import pronounce_number_fr
from lingua_franca.lang.common_data_fr import _ARTICLES_FR, _NUMBERS_FR, \
_ORDINAL_ENDINGS_FR
from lingua_franca.time import now_local


def extract_duration_fr(text):
"""
Expand Down Expand Up @@ -491,9 +494,10 @@ def date_found():
hrOffset != 0 or minOffset != 0 or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
Expand Down Expand Up @@ -939,6 +943,9 @@ def date_found():
if datestr != "":
if not hasYear:
temp = datetime.strptime(datestr, "%B %d")
if extractedDate.tzinfo:
temp = temp.replace(tzinfo=gettz("UTC"))
temp = temp.astimezone(extractedDate.tzinfo)
temp = temp.replace(year=extractedDate.year)
if extractedDate < temp:
extractedDate = extractedDate.replace(year=int(currentYear),
Expand Down
2 changes: 1 addition & 1 deletion lingua_franca/lang/parse_hu.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime, timedelta
from lingua_franca.time import now_local
from lingua_franca.lang.parse_common import Normalizer


Expand Down
5 changes: 3 additions & 2 deletions lingua_franca/lang/parse_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import collections
from datetime import datetime
from dateutil.relativedelta import relativedelta
from lingua_franca.time import now_local
from lingua_franca.lang.parse_common import is_numeric, look_for_fractions, \
extract_numbers_generic, Normalizer
from lingua_franca.lang.format_it import _LONG_SCALE_IT, _SHORT_SCALE_IT, \
Expand Down Expand Up @@ -493,9 +494,9 @@ def date_found():
month_offset != 0 or day_offset is True or hr_offset != 0 or
hr_abs or min_offset != 0 or min_abs or sec_offset != 0)

if text == '' or not anchorDate:
if text == '':
return None

anchorDate = anchorDate or now_local()
found = False
day_specified = False
day_offset = False
Expand Down
4 changes: 3 additions & 1 deletion lingua_franca/lang/parse_nl.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
_LONG_SCALE_NL, _MULTIPLIES_LONG_SCALE_NL, _MULTIPLIES_SHORT_SCALE_NL,\
_NEGATIVES_NL, _SHORT_SCALE_NL, _STRING_LONG_ORDINAL_NL, _STRING_NUM_NL, \
_STRING_SHORT_ORDINAL_NL, _SUMS_NL
from lingua_franca.time import now_local
import re


Expand Down Expand Up @@ -560,9 +561,10 @@ def date_found():
minAbs or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
chrisveilleux marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
5 changes: 3 additions & 2 deletions lingua_franca/lang/parse_pl.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
_SHORT_SCALE_PL, _SHORT_ORDINAL_PL, _FRACTION_STRING_PL, _TIME_UNITS_CONVERSION, \
_TIME_UNITS_NORMALIZATION, _MONTHS_TO_EN, _DAYS_TO_EN, _ORDINAL_BASE_PL, \
_ALT_ORDINALS_PL

from lingua_franca.time import now_local
import re


Expand Down Expand Up @@ -710,9 +710,10 @@ def date_found():
minAbs or secOffset != 0
)

if string == "" or not dateNow:
if string == "":
return None

dateNow = dateNow or now_local()
found = False
daySpecified = False
dayOffset = False
Expand Down
7 changes: 6 additions & 1 deletion lingua_franca/lang/parse_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
_MALE_DETERMINANTS_PT, _MALE_ENDINGS_PT, _GENDERS_PT
from lingua_franca.internal import resolve_resource_file
from lingua_franca.lang.parse_common import Normalizer
from lingua_franca.time import now_local
import json
import re

Expand Down Expand Up @@ -286,9 +287,10 @@ def date_found():
minAbs or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
Expand Down Expand Up @@ -953,6 +955,9 @@ def date_found():
datestr = datestr.replace(monthsShort[idx], en_month)

temp = datetime.strptime(datestr, "%B %d")
if extractedDate.tzinfo:
temp = temp.replace(tzinfo=extractedDate.tzinfo)

if not hasYear:
temp = temp.replace(year=extractedDate.year)
if extractedDate < temp:
Expand Down
4 changes: 3 additions & 1 deletion lingua_franca/lang/parse_sv.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#
from datetime import datetime
from dateutil.relativedelta import relativedelta
from lingua_franca.time import now_local
from .parse_common import is_numeric, look_for_fractions, Normalizer


Expand Down Expand Up @@ -155,9 +156,10 @@ def date_found():
minAbs or secOffset != 0
)

if text == "" or not anchorDate:
if text == "":
return None

anchorDate = anchorDate or now_local()
found = False
daySpecified = False
dayOffset = False
Expand Down
Loading