From 83f0690977a1d8cc7f04e11bfded9ee3df7abd4b Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Sun, 5 Sep 2021 16:11:10 +0100 Subject: [PATCH] nice dates (#31) * feat/get_date_strings * 0.4.5 authored-by: jarbasai --- lingua_nostra/format.py | 67 ++++++++++++++++++++++++++++++++++++++--- lingua_nostra/time.py | 13 ++++++++ setup.py | 2 +- 3 files changed, 77 insertions(+), 5 deletions(-) diff --git a/lingua_nostra/format.py b/lingua_nostra/format.py index a26b448a..b44fe03d 100755 --- a/lingua_nostra/format.py +++ b/lingua_nostra/format.py @@ -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", @@ -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 @@ -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 diff --git a/lingua_nostra/time.py b/lingua_nostra/time.py index 3b989dd5..171ba2a4 100644 --- a/lingua_nostra/time.py +++ b/lingua_nostra/time.py @@ -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) + diff --git a/setup.py b/setup.py index fa3ac6a1..ecc1e51d 100644 --- a/setup.py +++ b/setup.py @@ -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',