Skip to content
Open
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
10 changes: 3 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# Changelog

## [0.6.5a1](https://github.com/OpenVoiceOS/ovos-date-parser/tree/0.6.5a1) (2025-08-04)
## [0.7.0a1](https://github.com/OpenVoiceOS/ovos-date-parser/tree/0.7.0a1) (2025-08-04)

[Full Changelog](https://github.com/OpenVoiceOS/ovos-date-parser/compare/0.6.4...0.6.5a1)

**Closed issues:**

- pt - grammatical gender for time words [\#94](https://github.com/OpenVoiceOS/ovos-date-parser/issues/94)
[Full Changelog](https://github.com/OpenVoiceOS/ovos-date-parser/compare/0.6.5...0.7.0a1)

**Merged pull requests:**

- fix: pt number grammatical gender [\#95](https://github.com/OpenVoiceOS/ovos-date-parser/pull/95) ([JarbasAl](https://github.com/JarbasAl))
- feat: optional weekday in nice\_date [\#97](https://github.com/OpenVoiceOS/ovos-date-parser/pull/97) ([JarbasAl](https://github.com/JarbasAl))



Expand Down
54 changes: 43 additions & 11 deletions ovos_date_parser/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,19 @@ def _number_format_hundreds(self, number, number_tuple, lang,

def _number_format_thousand(self, number, number_tuple, lang,
formatted_decade, formatted_hundreds):
"""
Format the thousands part of a year using language-specific templates.

Parameters:
number (int): The year value to format.
number_tuple: A named tuple containing precomputed string representations of number components.
lang (str): Language code for localization.
formatted_decade (str): Preformatted decade string.
formatted_hundreds (str): Preformatted hundreds string.

Returns:
str: The formatted thousands part of the year as a localized string.
"""
s = self._format_string(number % 10000, 'thousand_format', lang)
return s.format(x_in_x00=number_tuple.x_in_x00,
xx00=number_tuple.xx00,
Expand All @@ -426,7 +439,19 @@ def _number_format_thousand(self, number, number_tuple, lang,
formatted_hundreds=formatted_hundreds,
number=str(number % 10000))

def date_format(self, dt, lang, now):
def date_format(self, dt, lang, now, include_weekday=True):
"""
Format a datetime object as a localized date string according to language-specific templates.

Parameters:
dt (datetime): The date to format.
lang (str): Language code for localization.
now (datetime): Reference date for relative formatting (e.g., today, tomorrow).
include_weekday (bool): If True, includes the weekday name in the output.

Returns:
str: The formatted date string, localized and optionally including the weekday.
"""
format_str = 'date_full'
lang = lang.split("-")[0]
if now:
Expand All @@ -444,11 +469,17 @@ def date_format(self, dt, lang, now):
elif yesterday.date() == dt.date():
format_str = 'yesterday'

return self.lang_config[lang]['date_format'][format_str].format(
weekday=self.lang_config[lang]['weekday'][str(dt.weekday())],
month=self.lang_config[lang]['month'][str(dt.month)],
day=self.lang_config[lang]['date'][str(dt.day)],
formatted_year=self.year_format(dt, lang, False))
unformatted = self.lang_config[lang]['date_format'][format_str]
args = dict(
month=self.lang_config[lang]['month'][str(dt.month)],
day=self.lang_config[lang]['date'][str(dt.day)],
formatted_year=self.year_format(dt, lang, False)
)
if include_weekday:
args["weekday"] = self.lang_config[lang]['weekday'][str(dt.weekday())]
else:
unformatted = re.sub(r"{weekday}\s*,?\s*", "", unformatted).strip(", ")
return unformatted.format(**args)

def date_time_format(self, dt, lang, now, use_24hour, use_ampm):
lang = lang.split("-")[0]
Expand Down Expand Up @@ -485,7 +516,7 @@ def year_format(self, dt, lang, bc):
date_time_format = DateTimeFormat(os.path.join(os.path.dirname(__file__), 'res'))


def nice_date(dt, lang, now=None):
def nice_date(dt, lang, now=None, include_weekday=True):
"""
Format a datetime to a pronounceable date

Expand All @@ -499,19 +530,20 @@ def nice_date(dt, lang, now=None):
will be shortened accordingly: No year is returned if now is in the
same year as td, no month is returned if now is in the same month
as td. If now and td is the same day, 'today' is returned.
include_weekday (bool, optional): Whether to include the weekday name in the output. Defaults to True.

Returns:
(str): The formatted date string
"""
lang = lang.lower().split("-")[0]
if lang.startswith("pt"):
return nice_date_pt(dt, now)
return nice_date_pt(dt, now, include_weekday)
if lang.startswith("es"):
return nice_date_es(dt, now)
return nice_date_es(dt, now, include_weekday)
if lang.startswith("gl"):
return nice_date_gl(dt, now)
return nice_date_gl(dt, now, include_weekday)
date_time_format.cache(lang)
return date_time_format.date_format(dt, lang, now)
return date_time_format.date_format(dt, lang, now, include_weekday)


def nice_date_time(dt, lang, now=None, use_24hour=False,
Expand Down
12 changes: 8 additions & 4 deletions ovos_date_parser/dates_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def nice_date_time_es(dt, now=None, use_24hour=False,
return f"{nice_date_es(dt, now)} a las {nice_time_es(dt, use_24hour=use_24hour, use_ampm=use_ampm)}"


def nice_date_es(dt: datetime, now: datetime = None):
def nice_date_es(dt: datetime, now: datetime = None, include_weekday=True):
"""
Formatea una fecha en una forma pronunciable.

Expand All @@ -100,14 +100,14 @@ def nice_date_es(dt: datetime, now: datetime = None):
now (datetime): Fecha actual. Si se proporciona, la fecha devuelta se acortará en consecuencia:
No se devuelve el año si ahora está en el mismo año que `dt`, no se devuelve el mes
si ahora está en el mismo mes que `dt`. Si `now` y `dt` son el mismo día, se devuelve 'hoy'.
include_weekday (bool, optional): Whether to include the weekday name in the output. Defaults to True.

Returns:
(str): La cadena de fecha formateada
"""
weekday = nice_weekday_es(dt)
day = pronounce_number_es(dt.day)
if now is not None:
nice = f"{weekday}, {day}"
nice = day
if dt.day == now.day:
return "hoy"
if dt.day == now.day + 1:
Expand All @@ -119,7 +119,11 @@ def nice_date_es(dt: datetime, now: datetime = None):
if dt.year != now.year:
nice = nice + ", " + nice_year_es(dt)
else:
nice = f"{weekday}, {day} de {nice_month_es(dt)}, {nice_year_es(dt)}"
nice = f"{day} de {nice_month_es(dt)}, {nice_year_es(dt)}"

if include_weekday:
weekday = nice_weekday_es(dt)
nice = f"{weekday}, {nice}"
return nice


Expand Down
12 changes: 8 additions & 4 deletions ovos_date_parser/dates_gl.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def nice_date_time_gl(dt, now=None, use_24hour=False, use_ampm=False):
return f"{nice_date_gl(dt, now)} ás {nice_time_gl(dt, use_24hour=use_24hour, use_ampm=use_ampm)}"


def nice_date_gl(dt: datetime, now: datetime = None):
def nice_date_gl(dt: datetime, now: datetime = None, include_weekday=True):
"""
Formatea unha data nunha forma pronunciable.

Expand All @@ -99,14 +99,14 @@ def nice_date_gl(dt: datetime, now: datetime = None):
now (datetime): Data actual. Se se proporciona, a data devolta acurtarase en consecuencia:
Non se devolve o ano se now está no mesmo ano que `dt`, non se devolve o mes
se now está no mesmo mes que `dt`. Se `now` e `dt` son o mesmo día, devélvese 'hoxe'.
include_weekday (bool, optional): Whether to prepend the weekday name to the formatted date. Defaults to True.

Returns:
(str): A cadea de data formatada
"""
weekday = nice_weekday_gl(dt)
day = pronounce_number_gl(dt.day)
if now is not None:
nice = f"{weekday}, {day}"
nice = day
if dt.day == now.day:
return "hoxe"
if dt.day == now.day + 1:
Expand All @@ -118,7 +118,11 @@ def nice_date_gl(dt: datetime, now: datetime = None):
if dt.year != now.year:
nice = nice + ", " + nice_year_gl(dt)
else:
nice = f"{weekday}, {day} de {nice_month_gl(dt)}, {nice_year_gl(dt)}"
nice = f"{day} de {nice_month_gl(dt)}, {nice_year_gl(dt)}"

if include_weekday:
weekday = nice_weekday_gl(dt)
nice = f"{weekday}, {nice}"
return nice


Expand Down
17 changes: 8 additions & 9 deletions ovos_date_parser/dates_pt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def nice_date_time_pt(dt, now=None, use_24hour=False,
return f"{nice_date_pt(dt, now)} ás {nice_time_pt(dt, use_24hour=use_24hour, use_ampm=use_ampm)}"


def nice_date_pt(dt: datetime, now: datetime = None):
def nice_date_pt(dt: datetime, now: datetime = None, include_weekday=True):
"""
Format a datetime to a pronounceable date

Expand All @@ -105,14 +105,14 @@ def nice_date_pt(dt: datetime, now: datetime = None):
will be shortened accordingly: No year is returned if now is in the
same year as td, no month is returned if now is in the same month
as td. If now and td is the same day, 'today' is returned.
include_weekday (bool, optional): Whether to include the weekday name in the output. Defaults to True.

Returns:
(str): The formatted date string
"""
weekday = nice_weekday_pt(dt)
day = pronounce_number_pt(dt.day, gender=GrammaticalGender.MASCULINE)
if now is not None:
nice = f"{weekday}, {day}"
nice = day
if dt.day == now.day:
return "hoje"
if dt.day == now.day + 1:
Expand All @@ -124,7 +124,11 @@ def nice_date_pt(dt: datetime, now: datetime = None):
if dt.year != now.year:
nice = nice + ", " + nice_year_pt(dt)
else:
nice = f"{weekday}, {day} de {nice_month_pt(dt)}, {nice_year_pt(dt)}"
nice = f"{day} de {nice_month_pt(dt)}, {nice_year_pt(dt)}"

if include_weekday:
weekday = nice_weekday_pt(dt)
nice = f"{weekday}, {nice}"
return nice


Expand Down Expand Up @@ -193,11 +197,6 @@ def nice_time_pt(dt, speech=True, use_24hour=False, use_ampm=False):
speak += "meia noite"
elif hour == 12:
speak += "meio dia"
# 1 and 2 are pronounced in female form when talking about hours
elif hour == 1 or hour == 13:
speak += "uma"
elif hour == 2 or hour == 14:
speak += "duas"
elif hour < 13:
speak = pronounce_number_pt(hour, gender=GrammaticalGender.FEMININE)
else:
Expand Down
6 changes: 3 additions & 3 deletions ovos_date_parser/version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# START_VERSION_BLOCK
VERSION_MAJOR = 0
VERSION_MINOR = 6
VERSION_BUILD = 5
VERSION_ALPHA = 0
VERSION_MINOR = 7
VERSION_BUILD = 0
VERSION_ALPHA = 1
# END_VERSION_BLOCK
Loading