Skip to content

Commit

Permalink
nice dates (MycroftAI#31)
Browse files Browse the repository at this point in the history
* feat/get_date_strings

* 0.4.5

authored-by: jarbasai <[email protected]>
  • Loading branch information
JarbasAl authored Sep 5, 2021
1 parent a92df40 commit 83f0690
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
67 changes: 63 additions & 4 deletions lingua_nostra/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
import os
import re
from collections import namedtuple
from warnings import warn
from os.path import join
from warnings import warn

from quantulum3 import parser as quantity_parser

from lingua_nostra.bracket_expansion import SentenceTreeParser
from lingua_nostra.internal import localized_function, \
populate_localized_function_dict, get_active_langs, \
get_full_lang_code, get_default_lang, get_default_loc, \
is_supported_full_lang, _raise_unsupported_language, \
UnsupportedLanguageError, NoneLangWarning, InvalidLangWarning, \
get_full_lang_code, get_default_loc, \
is_supported_full_lang, UnsupportedLanguageError, NoneLangWarning, \
InvalidLangWarning, \
FunctionNotLocalizedError, get_primary_lang_code
from lingua_nostra.time import now_local

_REGISTERED_FUNCTIONS = ("nice_number",
"nice_time",
Expand Down Expand Up @@ -429,6 +430,35 @@ def nice_date_time(dt, lang='', now=None, use_24hour=False,
use_ampm)


def nice_day(dt, date_format='MDY', include_month=True, lang=""):
if include_month:
month = nice_month(dt, date_format, lang)
if date_format == 'MDY':
return "{} {}".format(month, dt.strftime("%d"))
else:
return "{} {}".format(dt.strftime("%d"), month)
return dt.strftime("%d")


def nice_weekday(dt, lang=""):
if lang in date_time_format.lang_config.keys():
localized_day_names = list(
date_time_format.lang_config[lang]['weekday'].values())
weekday = localized_day_names[dt.weekday()]
else:
weekday = dt.strftime("%A")
return weekday.capitalize()


def nice_month(dt, date_format='MDY', lang=""):
if lang in date_time_format.lang_config.keys():
localized_month_names = date_time_format.lang_config[lang]['month']
month = localized_month_names[str(int(dt.strftime("%m")))]
else:
month = dt.strftime("%B")
return month.capitalize()


def nice_year(dt, lang='', bc=False):
"""
Format a datetime to a pronounceable year
Expand All @@ -451,6 +481,35 @@ def nice_year(dt, lang='', bc=False):
return date_time_format.year_format(dt, full_code, bc)


def get_date_strings(dt=None, date_format='MDY', time_format="full", lang=""):
lang = get_primary_lang_code(lang)
dt = dt or now_local()
timestr = nice_time(dt, lang, speech=False,
use_24hour=time_format == "full")
monthstr = nice_month(dt, date_format, lang)
weekdaystr = nice_weekday(dt, lang)
yearstr = dt.strftime("%Y")
daystr = nice_day(dt, date_format, include_month=False, lang=lang)
if date_format == 'MDY':
return {
"date_string": dt.strftime("%-m/%-d/%Y"),
"time_string": timestr,
"month_string": monthstr,
"day_string": daystr,
'year_string': yearstr,
"weekday_string": weekdaystr
}
else:
return {
"date_string": dt.strftime("%Y/%-d/%-m"),
"time_string": timestr,
"month_string": monthstr,
"day_string": daystr,
'year_string': yearstr,
"weekday_string": weekdaystr
}


@localized_function(run_own_code_on=[FunctionNotLocalizedError])
def nice_duration(duration, lang='', speech=True):
""" Convert duration in seconds to a nice spoken timespan
Expand Down
13 changes: 13 additions & 0 deletions lingua_nostra/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,16 @@ def to_system(dt):
# 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 is_leap_year(year):
return (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0))


def get_next_leap_year(year):
next_year = year + 1
if is_leap_year(next_year):
return next_year
else:
return get_next_leap_year(next_year)

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.4',
version='0.4.5',
packages=['lingua_nostra', 'lingua_nostra.lang'],
url='https://github.com/HelloChatterbox/lingua-nostra',
license='Apache2.0',
Expand Down

0 comments on commit 83f0690

Please sign in to comment.