diff --git a/stdlib/date_fr.catala_fr b/stdlib/date_fr.catala_fr index f6e2541f7..83853c4dd 100644 --- a/stdlib/date_fr.catala_fr +++ b/stdlib/date_fr.catala_fr @@ -122,7 +122,7 @@ déclaration énumération Mois: -- Décembre ## Renvoie le numéro de mois (à partir de 1) associé à un mois nommé. -déclaration month_to_int +déclaration mois_vers_entier contenu entier dépend de m contenu Mois égal à @@ -146,7 +146,7 @@ déclaration structure MoisDansAnnée: ## Renvoie le mois nommé correspondant au numéro de mois (à partir de 1). ## Si l'argument n'est pas entre 1 et 12, échoue avec l'erreur `impossible`. -déclaration month_of_int +déclaration mois_depuis_entier contenu Mois dépend de i contenu entier égal à @@ -166,14 +166,14 @@ déclaration month_of_int ## Transforme un `MoisDansAnnée` vers une `date` en choisissant le premier jour ## du mois. -déclaration month_of_year_to_premier_jour_du_mois +déclaration mois_dans_année_vers_premier_jour_du_mois contenu date dépend de m contenu MoisDansAnnée égal à depuis_année_mois_jour de (m.numéro_année, (month_to_int de m.nom_mois), 1) ## Extrait le mois nommé et le numéro de l'année dans une date. -déclaration to_month_of_year +déclaration vers_mois_dans_année contenu MoisDansAnnée dépend de d contenu date égal à diff --git a/stdlib/period_en.catala_en b/stdlib/period_en.catala_en index 87ef46a79..4378403a1 100644 --- a/stdlib/period_en.catala_en +++ b/stdlib/period_en.catala_en @@ -14,6 +14,31 @@ declaration structure Period: # The end date is included in the period data end content date +## Returns a period ranging over the given month of the given year. +declaration of_month_and_year + content Period + depends on + pmonth content Date.Month, + pyear content integer + equals + let pbegin equals + Date.of_year_month_day of (pyear, (Date.month_to_int of pmonth), 1) + in + Period { + -- begin: pbegin + -- end: Date.last_day_of_month of pbegin + } + +## Returns a period ranging over the given year. +declaration of_year + content Period + depends on pyear content integer + equals + Period { + -- begin: Date.of_year_month_day of (pyear, 1, 1) + -- end: Date.of_year_month_day of (pyear, 12, 31) + } + ## Ensures that the period is coherent (it begins before its end). declaration valid content boolean diff --git a/stdlib/period_fr.catala_fr b/stdlib/period_fr.catala_fr index 62f5ee341..31a498213 100644 --- a/stdlib/period_fr.catala_fr +++ b/stdlib/period_fr.catala_fr @@ -14,6 +14,31 @@ déclaration structure Période: # La date de fin est incluse dans la période par convention. donnée fin contenu date +## Retourne la période englobant le mois donné de l'année donnée. +déclaration depuis_mois_et_année + contenu Période + dépend de + pmois contenu Date.Mois, + pannée contenu entier + égal à + soit pbegin égal à + Date.depuis_année_mois_jour de (pannée, (Date.mois_vers_entier de pmois), 1) + dans + Période { + -- début: pbegin + -- fin: Date.dernier_jour_du_mois de pbegin + } + +## Retourne la période englobant l'année donnée. +déclaration depuis_année + contenu Période + dépend de pannée contenu entier + égal à + Période { + -- début: Date.depuis_année_mois_jour de (pannée, 1, 1) + -- fin: Date.depuis_année_mois_jour de (pannée, 12, 31) + } + ## Vérifie si la période est bien cohérente (elle débute avant sa fin). déclaration valide contenu booléen diff --git a/tests/stdlib/period.catala_en b/tests/stdlib/period.catala_en index 9ac1462e7..52d99a7af 100644 --- a/tests/stdlib/period.catala_en +++ b/tests/stdlib/period.catala_en @@ -1,3 +1,64 @@ +```catala +declaration mk_p + content Period.Period + depends on b content date, e content date + equals Period.Period { -- begin: b -- end: e } + +#[test] +declaration scope Tunit: + output p_one_day content Period.Period + output p_bad content Period.Period + output p_succ_one_day content Period.Period + output p_in_succ_one_day content Period.Period + output p_from_month_and_year content Period.Period + output p_from_year content Period.Period + +scope Tunit: + definition p_one_day equals mk_p of (|2020-01-01|, |2020-01-01|) + assertion (Period.valid of p_one_day and Period.duration of p_one_day = 1 day) + + definition p_bad equals mk_p of (|2020-01-02|, |2020-01-01|) + assertion (not (Period.valid of p_bad)) + + definition p_succ_one_day equals mk_p of (|2020-01-02|, |2020-01-03|) + assertion ( + Period.valid of p_succ_one_day + and Period.are_adjacent of p_one_day, p_succ_one_day + and (Period.intersection of p_one_day, p_succ_one_day) with pattern Absent + and Period.join of p_one_day, p_succ_one_day = mk_p of |2020-01-01|, |2020-01-03| + and Period.contained of p_succ_one_day, |2020-01-02| + ) + + definition p_in_succ_one_day equals mk_p of (|2020-01-03|, |2020-01-03|) + assertion ( + Period.valid of p_in_succ_one_day + and not (Period.are_adjacent of p_succ_one_day, p_in_succ_one_day) + and ( + (Period.intersection of p_succ_one_day, p_in_succ_one_day) with pattern Present content p + and p = (mk_p of |2020-01-03|, |2020-01-03|) + ) + and Period.join of p_succ_one_day, p_in_succ_one_day = p_succ_one_day + ) + + assertion ( + (Period.find_period of [ p_one_day ; p_succ_one_day ; p_in_succ_one_day ], |2020-01-04|) with pattern Absent + and ( + ( + Period.find_period of + [ p_one_day ; p_succ_one_day ; p_in_succ_one_day ], + |2020-01-03| + ) with pattern Present content p + and p = p_succ_one_day + ) + ) + + definition p_from_month_and_year equals Period.of_month_and_year of Date.Month.May, 1968 + assertion p_from_month_and_year = Period.Period { -- begin: |1968-05-01| -- end: |1968-05-31| } + + definition p_from_year equals Period.of_year of 2002 + assertion p_from_year = Period.Period { -- begin: |2002-01-01| -- end: |2002-12-31| } +``` + ```catala #[test] declaration scope Tsort: