Skip to content

Commit

Permalink
timezone issues (MycroftAI#32)
Browse files Browse the repository at this point in the history
Co-authored-by: jarbasal <[email protected]>
  • Loading branch information
JarbasAl and JarbasAl authored Jun 23, 2021
1 parent fabb889 commit a92df40
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 408 deletions.
7 changes: 4 additions & 3 deletions lingua_nostra/lang/parse_fa.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import json
from lingua_nostra.internal import resolve_resource_file
from lingua_nostra.parse import normalize_decimals
from lingua_nostra.time import now_local


def _is_number(s):
Expand Down Expand Up @@ -215,7 +216,7 @@ def extract_datetime_fa(text, anchorDate=None, default_time=None):
.replace('بعد از ظهر', 'بعدازظهر')

if not anchorDate:
anchorDate = datetime.now()
anchorDate = now_local()
today = anchorDate.replace(hour=0, minute=0, second=0, microsecond=0)
today_weekday = int(anchorDate.strftime("%w"))
weekday_names = [
Expand Down Expand Up @@ -369,10 +370,10 @@ def extract_number_fa(text, short_scale=True, ordinals=False, decimal='.'):

class FarsiNormalizer(Normalizer):
# TODO
with open(resolve_resource_file("text/en-us/normalize.json")) as f:
with open(resolve_resource_file("text/fa-ir/normalize.json")) as f:
_default_config = json.load(f)


def normalize_fa(text, remove_articles=True):
""" English string normalization """
""" Farsi string normalization """
return FarsiNormalizer().normalize(text, remove_articles)
3 changes: 1 addition & 2 deletions lingua_nostra/lang/parse_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -960,8 +960,7 @@ def date_found():
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(tzinfo=extractedDate.tzinfo)
temp = temp.replace(year=extractedDate.year)
if extractedDate < temp:
extractedDate = extractedDate.replace(year=int(currentYear),
Expand Down
39 changes: 33 additions & 6 deletions lingua_nostra/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def now_utc():
Returns:
(datetime): The current time in Universal Time, aka GMT
"""
return to_utc(datetime.utcnow())
return datetime.now(gettz("UTC"))


def now_local(tz=None):
Expand All @@ -63,6 +63,18 @@ def now_local(tz=None):
return datetime.now(tz)


def now_system():
""" Retrieve the current time in system timezone
Args:
tz (datetime.tzinfo, optional): Timezone, default to user's settings
Returns:
(datetime): The current time
"""
return datetime.now(tzlocal())


def to_utc(dt):
""" Convert a datetime with timezone info to a UTC datetime
Expand All @@ -71,11 +83,16 @@ def to_utc(dt):
Returns:
(datetime): time converted to UTC
"""
tzUTC = gettz("UTC")
tz = gettz("UTC")
if dt.tzinfo:
return dt.astimezone(tzUTC)
return dt.astimezone(tz)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tzUTC)
# naive datetimes assumed to be in default timezone already!
# in the case of datetime.now this corresponds to tzlocal()
# otherwise timezone is undefined and can not be guessed, we assume
# the user means "my timezone" and that LN was configured to use it
# beforehand, if unconfigured default == tzlocal()
return dt.replace(tzinfo=default_timezone()).astimezone(tz)


def to_local(dt):
Expand All @@ -90,7 +107,12 @@ def to_local(dt):
if dt.tzinfo:
return dt.astimezone(tz)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tz)
# naive datetimes assumed to be in default timezone already!
# in the case of datetime.now this corresponds to tzlocal()
# otherwise timezone is undefined and can not be guessed, we assume
# the user means "my timezone" and that LN was configured to use it
# beforehand, if unconfigured default == tzlocal()
return dt.replace(tzinfo=tz)


def to_system(dt):
Expand All @@ -105,4 +127,9 @@ def to_system(dt):
if dt.tzinfo:
return dt.astimezone(tz)
else:
return dt.replace(tzinfo=gettz("UTC")).astimezone(tz)
# naive datetimes assumed to be in default timezone already!
# in the case of datetime.now this corresponds to tzlocal()
# otherwise timezone is undefined and can not be guessed, we assume
# the user means "my timezone" and that LN was configured to use it
# beforehand, if unconfigured default == tzlocal()
return dt.replace(tzinfo=default_timezone()).astimezone(tz)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def required(requirements_file):

setup(
name='lingua_nostra',
version='0.4.3',
version='0.4.4',
packages=['lingua_nostra', 'lingua_nostra.lang'],
url='https://github.com/HelloChatterbox/lingua-nostra',
license='Apache2.0',
Expand Down
40 changes: 23 additions & 17 deletions test/test_format_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from lingua_nostra.format import nice_time
from lingua_nostra.format import pronounce_number
from lingua_nostra.lang.format_ca import TimeVariantCA
from lingua_nostra.time import default_timezone


def setUpModule():
Expand Down Expand Up @@ -134,7 +135,7 @@ def test_convert_decimals(self):
class TestNiceDateFormat(unittest.TestCase):
def test_pm(self):
dt = datetime.datetime(2017, 1, 31,
13, 22, 3)
13, 22, 3, tzinfo=default_timezone())

# Verify defaults haven't changed
self.assertEqual(nice_time(dt, lang="ca-es"),
Expand All @@ -155,7 +156,7 @@ def test_pm(self):
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True,
use_ampm=False), "les tretze i vint-i-dos")

dt = datetime.datetime(2017, 1, 31, 13, 0, 3)
dt = datetime.datetime(2017, 1, 31, 13, 0, 3, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca"), "la una en punt")
self.assertEqual(nice_time(dt, lang="ca", use_ampm=True),
"la una en punt de la tarda")
Expand All @@ -169,7 +170,8 @@ def test_pm(self):
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True,
use_ampm=True), "les tretze")

dt = datetime.datetime(2017, 1, 31, 13, 2, 3)
dt = datetime.datetime(2017, 1, 31, 13, 2, 3,
tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True),
"les tretze i dos")
self.assertEqual(nice_time(dt, lang="ca", use_ampm=True),
Expand All @@ -186,7 +188,8 @@ def test_pm(self):
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True,
use_ampm=False), "les tretze i dos")

dt = datetime.datetime(2017, 1, 31, 12, 15, 0)
dt = datetime.datetime(2017, 1, 31, 12, 15, 0,
tzinfo=default_timezone())
# Default Watch system
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True,
use_ampm=False), "les dotze i quinze")
Expand All @@ -204,7 +207,8 @@ def test_pm(self):
use_ampm=False, variant=TimeVariantCA.BELL),
"un quart d'una de la tarda")

dt = datetime.datetime(2017, 1, 31, 00, 14, 0)
dt = datetime.datetime(2017, 1, 31, 00, 14, 0,
tzinfo=default_timezone())
# Default Watch system
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True,
use_ampm=False), "les zero i catorze")
Expand All @@ -225,7 +229,7 @@ def test_pm(self):

def test_midnight(self):
dt = datetime.datetime(2017, 1, 31,
0, 2, 3)
0, 2, 3, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca"),
"les dotze i dos")
self.assertEqual(nice_time(dt, lang="ca", use_ampm=True),
Expand All @@ -248,7 +252,7 @@ def test_midnight(self):

def test_midday(self):
dt = datetime.datetime(2017, 1, 31,
12, 15, 9)
12, 15, 9, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es"),
"les dotze i quinze")
self.assertEqual(nice_time(dt, lang="ca-es", use_ampm=True),
Expand All @@ -274,7 +278,7 @@ def test_midday(self):
def test_minutes_to_hour(self):
# "twenty minutes to midnight"
dt = datetime.datetime(2017, 1, 31,
19, 40, 49)
19, 40, 49, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es"),
"les set i quaranta")
self.assertEqual(nice_time(dt, lang="ca-es", use_ampm=True),
Expand All @@ -300,39 +304,39 @@ def test_minutes_to_hour(self):
def test_minutes_past_hour(self):
# "quarter past ten"
dt = datetime.datetime(2017, 1, 31,
1, 15, 00)
1, 15, 00, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es", use_24hour=True),
"la una i quinze")
self.assertEqual(nice_time(dt, lang="ca-es"),
"la una i quinze")

dt = datetime.datetime(2017, 1, 31,
1, 35, 00)
1, 35, 00, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es"),
"la una i trenta-cinc")

dt = datetime.datetime(2017, 1, 31,
1, 45, 00)
1, 45, 00, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es"),
"la una i quaranta-cinc")

dt = datetime.datetime(2017, 1, 31,
4, 50, 00)
4, 50, 00, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es"),
"les quatre i cinquanta")

dt = datetime.datetime(2017, 1, 31,
5, 55, 00)
5, 55, 00, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es"),
"les cinc i cinquanta-cinc")

dt = datetime.datetime(2017, 1, 31,
5, 30, 00)
5, 30, 00, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es", use_ampm=True),
"les cinc i trenta de la matinada")

dt = datetime.datetime(2017, 1, 31,
23, 15, 9)
23, 15, 9, tzinfo=default_timezone())
self.assertEqual(nice_time(dt, lang="ca-es", use_24hour=True,
use_ampm=True),
"les vint-i-tres i quinze")
Expand All @@ -341,7 +345,8 @@ def test_minutes_past_hour(self):
"les onze i quinze de la nit")

def test_variant_strings(self):
dt = datetime.datetime(2017, 1, 31, 12, 15, 0)
dt = datetime.datetime(2017, 1, 31, 12, 15, 0,
tzinfo=default_timezone())
# Default variant
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True,
use_ampm=False, variant="default"),
Expand All @@ -350,7 +355,8 @@ def test_variant_strings(self):
use_ampm=False),
"les dotze i quinze")

dt = datetime.datetime(2017, 1, 31, 00, 14, 0)
dt = datetime.datetime(2017, 1, 31, 00, 14, 0,
tzinfo=default_timezone())
# Spanish-like time system
self.assertEqual(nice_time(dt, lang="ca", use_24hour=True,
use_ampm=False,
Expand Down
Loading

0 comments on commit a92df40

Please sign in to comment.