Skip to content

Commit

Permalink
488/add support for names in different languages (#490)
Browse files Browse the repository at this point in the history
* add en holiday names

* fix(#488): add support for names in different languages

closes #488
  • Loading branch information
MauricioRobayo authored Feb 15, 2023
1 parent a36aaf5 commit e80a1d6
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 108 deletions.
35 changes: 25 additions & 10 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,33 @@ function isEasterHoliday(holiday: Holiday): holiday is EasterHoliday {
function getHolidayDate(holiday: Holiday, year: number): Date {
if (isEasterHoliday(holiday)) {
const { month, day } = pascua(year);
const date = new Date(`${year}-${String(month).padStart(2, "0")}-01`);
const date = new Date(generateUtcStringFromDateParts(year, month, 1));
date.setUTCDate(day + holiday.offset);
return date;
}

const [month, day] = holiday.date.split("-");
return new Date(`${year}-${month.padStart(2, "0")}-${day.padStart(2, "0")}`);
return new Date(
generateUtcStringFromDateParts(year, holiday.month, holiday.day)
);
}

function formatDate(date: Date): string {
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, "0");
const day = String(date.getUTCDate()).padStart(2, "0");
return `${year}-${month}-${day}`;
function generateUtcStringFromDateParts(
year: number,
month: number,
day: number
) {
return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(
2,
"0"
)}`;
}

function generateUtcStringFromDate(date: Date): string {
return generateUtcStringFromDateParts(
date.getUTCFullYear(),
date.getUTCMonth() + 1,
date.getUTCDate()
);
}

function getHoliday(
Expand Down Expand Up @@ -73,10 +86,12 @@ function getHoliday(
: holidayDate;

return {
date: returnNativeDate ? holidayDate : formatDate(holidayDate),
date: returnNativeDate
? holidayDate
: generateUtcStringFromDate(holidayDate),
celebrationDate: returnNativeDate
? celebrationDate
: formatDate(celebrationDate),
: generateUtcStringFromDate(celebrationDate),
name: holiday.name,
nextMonday: year >= NEW_HOLIDAY_SCHEMA_START_YEAR && holiday.nextMonday,
};
Expand Down
151 changes: 133 additions & 18 deletions src/holidays.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,114 @@
import type { DateHoliday, EasterHoliday } from "./types";

const dateHolidays: DateHoliday[] = [
{ date: "01-01", name: "Año Nuevo", nextMonday: false },
{ date: "01-06", name: "Reyes Magos", nextMonday: true },
{ date: "03-19", name: "San José", nextMonday: true },
{ date: "05-01", name: "Día del Trabajo", nextMonday: false },
{ date: "06-29", name: "San Pedro y San Pablo", nextMonday: true },
{ date: "07-20", name: "Grito de la Independencia", nextMonday: false },
{ date: "08-07", name: "Batalla de Boyacá", nextMonday: false },
{ date: "08-15", name: "Asunción de la Virgen", nextMonday: true },
{ date: "10-12", name: "Día de la Raza", nextMonday: true },
{ date: "11-01", name: "Todos los Santos", nextMonday: true },
{ date: "11-11", name: "Independencia de Cartagena", nextMonday: true },
{ date: "12-08", name: "Inmaculada Concepción", nextMonday: false },
{ date: "12-25", name: "Navidad", nextMonday: false },
{
month: 1,
day: 1,
name: {
es: "Año Nuevo",
en: "New Year's day",
},
nextMonday: false,
},
{
month: 1,
day: 6,
name: {
es: "Reyes Magos",
en: "Epiphany",
},
nextMonday: true,
},
{
month: 3,
day: 19,
name: {
es: "San José",
en: "Saint Joseph's Day",
},
nextMonday: true,
},
{
month: 5,
day: 1,
name: {
es: "Día del Trabajo",
en: "Labour Day",
},
nextMonday: false,
},
{
month: 6,
day: 29,
name: {
es: "San Pedro y San Pablo",
en: "Saint Peter and Saint Paul",
},
nextMonday: true,
},
{
month: 7,
day: 20,
name: {
es: "Grito de la Independencia",
en: "Declaration of Independence",
},
nextMonday: false,
},
{
month: 8,
day: 7,
name: {
es: "Batalla de Boyacá",
en: "Battle of Boyacá",
},
nextMonday: false,
},
{
month: 8,
day: 15,
name: {
es: "Asunción de la Virgen",
en: "Assumption of Mary",
},
nextMonday: true,
},
{
month: 10,
day: 12,
name: {
es: "Día de la Raza",
en: "Columbus Day",
},
nextMonday: true,
},
{
month: 11,
day: 1,
name: {
es: "Todos los Santos",
en: "All Saints’ Day",
},
nextMonday: true,
},
{
month: 11,
day: 11,
name: { es: "Independencia de Cartagena", en: "Independence of Cartagena" },
nextMonday: true,
},
{
month: 12,
day: 8,
name: { es: "Inmaculada Concepción", en: "Immaculate Conception" },
nextMonday: false,
},
{
month: 12,
day: 25,
name: { es: "Navidad", en: "Christmas" },
nextMonday: false,
},
];

// We could simplify the calculation by setting the offset to match Monday.
Expand All @@ -23,11 +118,31 @@ const dateHolidays: DateHoliday[] = [
// and then get the next monday instead of directly using 64 as the offset.
// https://www.alcaldiabogota.gov.co/sisjur/normas/Norma1.jsp?i=4954
const easterHolidays: EasterHoliday[] = [
{ offset: -3, name: "Jueves Santo", nextMonday: false },
{ offset: -2, name: "Viernes Santo", nextMonday: false },
{ offset: 39, name: "Ascensión del Señor", nextMonday: true },
{ offset: 60, name: "Corpus Christi", nextMonday: true },
{ offset: 68, name: "Sagrado Corazón de Jesús", nextMonday: true },
{
offset: -3,
name: { es: "Jueves Santo", en: "Maundy Thursday" },
nextMonday: false,
},
{
offset: -2,
name: { es: "Viernes Santo", en: "Good Friday" },
nextMonday: false,
},
{
offset: 39,
name: { es: "Ascensión del Señor", en: "Ascension of Jesus" },
nextMonday: true,
},
{
offset: 60,
name: { es: "Corpus Christi", en: "Corpus Christi" },
nextMonday: true,
},
{
offset: 68,
name: { es: "Sagrado Corazón de Jesús", en: "Sacred Heart" },
nextMonday: true,
},
];

export default [...dateHolidays, ...easterHolidays];
Loading

0 comments on commit e80a1d6

Please sign in to comment.