Skip to content

Commit

Permalink
init func: setOmerInterval (need more work)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeANi-SHILIX committed Apr 28, 2024
1 parent aac60fb commit 82caae3
Showing 1 changed file with 60 additions and 9 deletions.
69 changes: 60 additions & 9 deletions helpers/hebrewDate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { HebrewCalendar, HDate, Location, Event, Zmanim, OmerEvent, months } from '@hebcal/core';
import { GLOBAL } from '../src/storeMsg';
import { sendMsgQueue } from '../src/QueueObj';

/**
*
* @param {Date} date
*/
function isHebrewHolyday(date) {
export function isHebrewHolyday(date) {
let hebToday = new HDate(date);
console.log(hebToday.toString());

Expand Down Expand Up @@ -65,38 +67,87 @@ function isHebrewHolyday(date) {
* the range is from 15 Nisan to 6 Sivan, otherwise will throw an error
* @returns {OmerEvent}
*/
function getOmerDay(today = new Date()) {
export function getOmerDay(today = new Date()) {
let today_he = new HDate(today);

// get sunset
let location = Location.lookup('Jerusalem');
let zmanim = new Zmanim(today, location.getLatitude(), location.getLongitude());
if (today >= zmanim.sunset()) today_he = today_he.next();
if (today >= zmanim.tzeit()) today_he = today_he.next();

const firstDayInOmer = new HDate(15, months.NISAN, today_he.getFullYear());
return new OmerEvent(today_he, today_he.deltaDays(firstDayInOmer));
}

export function setOmerInterval(force = false) {
try {
// if the bot is restarted, check if the day is in Omer range
getOmerDay()
} catch (err) {
// not in Omer range, and not forced - do nothing
if (!force) return;
}

let today = new Date();
let location = Location.lookup('Jerusalem');
let zmanim = new Zmanim(today, location.getLatitude(), location.getLongitude());

// set interval at sunset (tzeit hakochavim)
setTimeout(() => {
GLOBAL.omerReminder.omerInternal = setInterval(() => {
// send every day the reminder
// TODO: dont send on Shabbat
try {
let omer = getOmerDay()
let todayis = omer.getTodayIs("he")
let arr = todayis.split(",")
let todayis_he = arr[1]
? arr[0] + " לָעוֹמֶר," + arr[1].replace("לָעוֹמֶר", "")
: arr[0];

GLOBAL.omerReminder.chats.forEach(chat => {
// TODO: add more text
sendMsgQueue(chat, todayis_he)
})
} catch (error) {
if (!force){
clearInterval(GLOBAL.omerReminder.omerInternal)
GLOBAL.omerReminder.omerInternal = null
}
}
})
}, today - zmanim.tzeit())
}

function testOmerDay() {
const days = [
new Date(),
new Date(2024, 3, 23), // טו בניסן
new Date(2024, 3, 24),
new Date(2024, 4, 5),
new Date(2024, 5, 11),
new Date(2024, 5, 12), // ו' בסיון
];

for (const day of days) {
try {
console.log(getOmerDay(day).render('he'));
let omer = getOmerDay(day)
//console.log(omer)
console.log(omer.render("he"))
let todayis = omer.getTodayIs("he")
console.log(todayis)

// to Eedot Hamizrah example
let arr = todayis.split(",")
todayis = arr[1]
? arr[0] + " לָעוֹמֶר," + arr[1].replace("לָעוֹמֶר", "")
: arr[0]
console.log(todayis)

}
catch (err) {
console.error("not in Omer range")
}
}
}
//testOmerDay();

module.exports = {
getOmerDay,
isHebrewHolyday
}

0 comments on commit 82caae3

Please sign in to comment.