<commit_before>'use strict'

const sunCalc = require('./sunCalc');
const moment = require('moment-timezone');

var _nawRuzOffsetFrom21 = [];
var _twinHolyBirthdays = [];

const sunCalcReady = false;

fillDatePresets();

function addSunTimes(profile, answers) {
  var coord = profile.coord;
  if (!coord) {
    answers.push('Sorry. I don\'t know where you are, so can\'t tell you when sunset is.');
    return;
  }
  var readableFormat = 'MMM D, HH:mm';

  var zoneName = profile.tzInfo.zoneName;
  var nowTz = moment.tz(zoneName);
  var noonTz = moment(nowTz).hour(12).minute(0).second(0);
  var tomorrowNoonTz = moment(noonTz).add(24, 'hours');

  var sun1 = sunCalc.getTimes(noonTz, coord.lat, coord.lng);
  var sunrise1Tz = moment.tz(sun1.sunrise, zoneName)
  var sunset1Tz = moment.tz(sun1.sunset, zoneName)

  if (nowTz.isAfter(sunset1Tz)) {
    // eve of day1 into day2
    answers.push(`Starting Sunset: ${sunset1Tz.format(readableFormat)}`);

    var sun2 = sunCalc.getTimes(tomorrowNoonTz, coord.lat, coord.lng);
    var sunrise2Tz = moment.tz(sun2.sunrise, zoneName)
    var sunset2Tz = moment.tz(sun2.sunset, zoneName)

    if (nowTz.isBefore(sunrise2Tz)) {
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
      answers.push(`Sunrise: ${sunrise2Tz.format(readableFormat)}`);
    } else {
      answers.push(`Sunrise: ${sunrise2Tz.format(readableFormat)}`);
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
    }
    answers.push(`Ending Sunset: ${sunset2Tz.format(readableFormat)}`);
  } else {
    // get prior sunset
    var sun0 = sunCalc.getTimes(moment(noonTz).subtract(24, 'hours'), coord.lat, coord.lng);
    var sunset0 = moment.tz(sun0.sunset, zoneName)

    answers.push(`Starting Sunset: ${sunset0.format(readableFormat)}`);
    if (nowTz.isBefore(sunrise1Tz)) {
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
      answers.push(`Sunrise: ${sunrise1Tz.format(readableFormat)}`);
    } else {
      answers.push(`Sunrise: ${sunrise1Tz.format(readableFormat)}`);
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
    }
    answers.push(`Ending Sunset: ${sunset1Tz.format(readableFormat)}`);
  }
}

//function getSunTimes(profile) {
//  var offset = profile.tzInfo.serverDiff;
//  var coord = profile.coord;
//  var noon = new Date();
//  noon.setHours(12 + offset, 0, 0, 0);
//
//  var sunTimes = sunCalc.getTimes(noon, coord.lat, coord.lng);
//  addHours(sunTimes.sunrise, offset);
//  addHours(sunTimes.sunset, offset);
//
//  // times are right for the user, but ignore the timezone!
//  return sunTimes;
//}

function addTodayInfoToAnswers(profile, answers) {
  var zoneName = profile.tzInfo.zoneName;
  var nowTz = moment.tz(zoneName);

  var coord = profile.coord;
  var bDateInfo = getBDateInfo(nowTz, coord, zoneName);
  var bDate = bDateInfo.bDate;

  //  var noon = new Date(now.getTime());
  //  noon.setHours(12, 0, 0, 0);
  //  var sun = sunCalc.getTimes(noon, coord.lat, coord.lng);
  //  addHours(sun.sunset, offset);

  var nowHours = nowTz.hours();
  var greeting;
  if (nowHours >= 5 && nowHours <= 10) {
    greeting = (`Good morning, ${profile.first_name}.`);
  } else if (nowHours >= 19 && nowHours <= 22) {
    greeting = (`Good evening, ${profile.first_name}.`);
  } else {
    greeting = (`Hello, ${profile.first_name}.`);
  }

  answers.push(greeting + ` Today is ${monthMeaning[bDate.m]} ${bDate.d} (${monthMeaning[bDate.d]}) in the Wondrous calendar! (aka ${monthAr[bDate.m]} ${bDate.d})`);

  if (nowTz.isSame(bDateInfo.startingSunset, 'minute')) {
    answers.push(`It just started with sunset at ${bDateInfo.startingSunset.format('HH:mm')}!`);
  } else if (bDate.eve) {
    answers.push(`It started with sunset at ${bDateInfo.startingSunset.format('HH:mm')}!`);
  } else {
    answers.push(`It lasts until sunset at ${bDateInfo.endingSunset.format('HH:mm')}.`);
  }
  //
  //  if (bDateInfo.endingSunset) {
  //    var endingSunset = bDateInfo.endingSunset;
  //    var nowWhen = moment(nowTz).format('HHmm');
  //    console.log(nowWhen);
  //    var sunsetWhen = moment(endingSunset).format('HHmm');
  //    console.log(sunsetWhen);
  //    if (nowWhen == sunsetWhen) {
  //      answers.push(`It just started with sunset at ${moment(endingSunset).format('HH:mm')}!`);
  //    } else {
  //      answers.push(`It lasts until sunset at ${moment(endingSunset).format('HH:mm')}.`);
  //    }
  //  } else {
  //    answers.push(`It started with sunset at ${moment(bDateInfo.startingSunset).format('HH:mm')}.`);
  //  }
}

//function getUserNowTime(serverDiff) {
//  var now = new Date();
//  if (serverDiff) {
//    now.setHours(now.getHours() + serverDiff);
//  }
//  return now;
//}


var getBDateInfo = function (nowTz, coord, zoneName) {

  var noonTz = moment(nowTz).hour(12).minute(0).second(0);

  var sun1 = sunCalc.getTimes(noonTz, coord.lat, coord.lng);

  console.log('local now ' + nowTz.format());
  console.log('local noon ' + noonTz.format());
  console.log(sun1);
  var sunsetTz = moment.tz(sun1.sunset, zoneName)

  console.log('local sunset ' + sunsetTz.format());

  var afterSunset = nowTz.isAfter(sunsetTz);

  if (afterSunset) {
    noonTz.add(24, 'hours');
  }
  console.log('noon of day ' + noonTz.format());


  var gYear = noonTz.toDate().getFullYear();
  var gDayOfNawRuz = getNawRuz(gYear, true);
  var gDayLoftiness1 = copyAndAddDays(gDayOfNawRuz, -19);

  var bYear = gYear - (nowTz >= gDayOfNawRuz ? 1843 : 1844);
  var bMonth, bDay;

  var isBeforeLoftiness = nowTz < gDayLoftiness1;
  if (isBeforeLoftiness) {
    // back: Jan --> end of AyyamiHa
    var gDayLoftiness1LastYear = copyAndAddDays(getNawRuz(gYear - 1, true), -19);

    var daysAfterLoftiness1LastYear = Math.round((nowTz - gDayLoftiness1LastYear) / 864e5);
    var numMonthsFromLoftinessLastYear = Math.floor(daysAfterLoftiness1LastYear / 19);

    bDay = 1 + daysAfterLoftiness1LastYear - numMonthsFromLoftinessLastYear * 19;
    bMonth = numMonthsFromLoftinessLastYear;

    if (bMonth === 19) {
      bMonth = 0;
    }
  } else {
    // forward: Loftiness --> Dec
    var bDaysAfterLoftiness1 = Math.round((nowTz - gDayLoftiness1) / 864e5);
    var bNumMonthsFromLoftiness = Math.floor(bDaysAfterLoftiness1 / 19);

    bDay = 1 + bDaysAfterLoftiness1 - bNumMonthsFromLoftiness * 19;
    bMonth = bNumMonthsFromLoftiness;

    if (bMonth === 0) {
      bMonth = 19;
    }
  }

  var sun0 = !afterSunset ? sunCalc.getTimes(moment(noonTz).subtract(24, 'hours'), coord.lat, coord.lng) : null;
  var sun2 = afterSunset ? sunCalc.getTimes(moment(noonTz).add(24, 'hours'), coord.lat, coord.lng) : null;

  return {
    bDate: { y: bYear, m: bMonth, d: bDay, eve: afterSunset },
    startingSunset: afterSunset ? sunsetTz : moment.tz(sun0.sunset, zoneName),
    sunrise: afterSunset ? moment.tz(sun2.sunrise, zoneName) : moment.tz(sun1.sunrise, zoneName),
    endingSunset: afterSunset ? moment.tz(sun2.sunset, zoneName) : sunsetTz
  };
};

function getNawRuz(gYear, frag2DateOnly) {
  // get NawRuz for this gregorian year<commit_msg>add console logs to debug an issue<commit_after>'use strict'

const sunCalc = require('./sunCalc');
const moment = require('moment-timezone');

var _nawRuzOffsetFrom21 = [];
var _twinHolyBirthdays = [];

const sunCalcReady = false;

fillDatePresets();

function addSunTimes(profile, answers) {
  var coord = profile.coord;
  if (!coord) {
    answers.push('Sorry. I don\'t know where you are, so can\'t tell you when sunset is.');
    return;
  }
  var readableFormat = 'MMM D, HH:mm';

  var zoneName = profile.tzInfo.zoneName;
  var nowTz = moment.tz(zoneName);
  var noonTz = moment(nowTz).hour(12).minute(0).second(0);
  var tomorrowNoonTz = moment(noonTz).add(24, 'hours');

  var sun1 = sunCalc.getTimes(noonTz, coord.lat, coord.lng);
  var sunrise1Tz = moment.tz(sun1.sunrise, zoneName)
  var sunset1Tz = moment.tz(sun1.sunset, zoneName)

  if (nowTz.isAfter(sunset1Tz)) {
    // eve of day1 into day2
    answers.push(`Starting Sunset: ${sunset1Tz.format(readableFormat)}`);

    var sun2 = sunCalc.getTimes(tomorrowNoonTz, coord.lat, coord.lng);
    var sunrise2Tz = moment.tz(sun2.sunrise, zoneName)
    var sunset2Tz = moment.tz(sun2.sunset, zoneName)

    if (nowTz.isBefore(sunrise2Tz)) {
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
      answers.push(`Sunrise: ${sunrise2Tz.format(readableFormat)}`);
    } else {
      answers.push(`Sunrise: ${sunrise2Tz.format(readableFormat)}`);
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
    }
    answers.push(`Ending Sunset: ${sunset2Tz.format(readableFormat)}`);
  } else {
    // get prior sunset
    var sun0 = sunCalc.getTimes(moment(noonTz).subtract(24, 'hours'), coord.lat, coord.lng);
    var sunset0 = moment.tz(sun0.sunset, zoneName)

    answers.push(`Starting Sunset: ${sunset0.format(readableFormat)}`);
    if (nowTz.isBefore(sunrise1Tz)) {
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
      answers.push(`Sunrise: ${sunrise1Tz.format(readableFormat)}`);
    } else {
      answers.push(`Sunrise: ${sunrise1Tz.format(readableFormat)}`);
      answers.push(`Now: ${nowTz.format(readableFormat)}`);
    }
    answers.push(`Ending Sunset: ${sunset1Tz.format(readableFormat)}`);
  }
}

//function getSunTimes(profile) {
//  var offset = profile.tzInfo.serverDiff;
//  var coord = profile.coord;
//  var noon = new Date();
//  noon.setHours(12 + offset, 0, 0, 0);
//
//  var sunTimes = sunCalc.getTimes(noon, coord.lat, coord.lng);
//  addHours(sunTimes.sunrise, offset);
//  addHours(sunTimes.sunset, offset);
//
//  // times are right for the user, but ignore the timezone!
//  return sunTimes;
//}

function addTodayInfoToAnswers(profile, answers) {
  var zoneName = profile.tzInfo.zoneName;
  var nowTz = moment.tz(zoneName);

  var coord = profile.coord;
  var bDateInfo = getBDateInfo(nowTz, coord, zoneName);
  var bDate = bDateInfo.bDate;

  //  var noon = new Date(now.getTime());
  //  noon.setHours(12, 0, 0, 0);
  //  var sun = sunCalc.getTimes(noon, coord.lat, coord.lng);
  //  addHours(sun.sunset, offset);

  var nowHours = nowTz.hours();
  var greeting;
  if (nowHours >= 5 && nowHours <= 10) {
    greeting = (`Good morning, ${profile.first_name}.`);
  } else if (nowHours >= 19 && nowHours <= 22) {
    greeting = (`Good evening, ${profile.first_name}.`);
  } else {
    greeting = (`Hello, ${profile.first_name}.`);
  }

  answers.push(greeting + ` Today is ${monthMeaning[bDate.m]} ${bDate.d} (${monthMeaning[bDate.d]}) in the Wondrous calendar! (aka ${monthAr[bDate.m]} ${bDate.d})`);

  console.log('local now: ' + nowTz.format())
  console.log('start of day: ' + bDateInfo.startingSunset.format());

  if (nowTz.isSame(bDateInfo.startingSunset, 'minute')) {
    answers.push(`It just started with sunset at ${bDateInfo.startingSunset.format('HH:mm')}!`);
  } else if (bDate.eve) {
    answers.push(`It started with sunset at ${bDateInfo.startingSunset.format('HH:mm')}!`);
  } else {
    answers.push(`It lasts until sunset at ${bDateInfo.endingSunset.format('HH:mm')}.`);
  }
  //
  //  if (bDateInfo.endingSunset) {
  //    var endingSunset = bDateInfo.endingSunset;
  //    var nowWhen = moment(nowTz).format('HHmm');
  //    console.log(nowWhen);
  //    var sunsetWhen = moment(endingSunset).format('HHmm');
  //    console.log(sunsetWhen);
  //    if (nowWhen == sunsetWhen) {
  //      answers.push(`It just started with sunset at ${moment(endingSunset).format('HH:mm')}!`);
  //    } else {
  //      answers.push(`It lasts until sunset at ${moment(endingSunset).format('HH:mm')}.`);
  //    }
  //  } else {
  //    answers.push(`It started with sunset at ${moment(bDateInfo.startingSunset).format('HH:mm')}.`);
  //  }
}

//function getUserNowTime(serverDiff) {
//  var now = new Date();
//  if (serverDiff) {
//    now.setHours(now.getHours() + serverDiff);
//  }
//  return now;
//}


var getBDateInfo = function (nowTz, coord, zoneName) {

  var noonTz = moment(nowTz).hour(12).minute(0).second(0);

  var sun1 = sunCalc.getTimes(noonTz, coord.lat, coord.lng);

  console.log('local now ' + nowTz.format());
  console.log('local noon ' + noonTz.format());
  console.log(sun1);
  var sunsetTz = moment.tz(sun1.sunset, zoneName)

  console.log('local sunset ' + sunsetTz.format());

  var afterSunset = nowTz.isAfter(sunsetTz);

  if (afterSunset) {
    console.log('after sunset');
    noonTz.add(24, 'hours');
  }
  console.log('noon of target day ' + noonTz.format());


  var gYear = noonTz.year();
  var gDayOfNawRuz = getNawRuz(gYear, true);
  var gDayLoftiness1 = copyAndAddDays(gDayOfNawRuz, -19);

  var bYear = gYear - (noonTz >= gDayOfNawRuz ? 1843 : 1844);
  var bMonth, bDay;

  var isBeforeLoftiness = noonTz < gDayLoftiness1;
  if (isBeforeLoftiness) {
    // back: Jan --> end of AyyamiHa
    var gDayLoftiness1LastYear = copyAndAddDays(getNawRuz(gYear - 1, true), -19);

    var daysAfterLoftiness1LastYear = Math.round((noonTz - gDayLoftiness1LastYear) / 864e5);
    var numMonthsFromLoftinessLastYear = Math.floor(daysAfterLoftiness1LastYear / 19);

    bDay = 1 + daysAfterLoftiness1LastYear - numMonthsFromLoftinessLastYear * 19;
    bMonth = numMonthsFromLoftinessLastYear;

    if (bMonth === 19) {
      bMonth = 0;
    }
  } else {
    // forward: Loftiness --> Dec
    var bDaysAfterLoftiness1 = Math.round((noonTz - gDayLoftiness1) / 864e5);
    var bNumMonthsFromLoftiness = Math.floor(bDaysAfterLoftiness1 / 19);

    bDay = 1 + bDaysAfterLoftiness1 - bNumMonthsFromLoftiness * 19;
    bMonth = bNumMonthsFromLoftiness;

    if (bMonth === 0) {
      bMonth = 19;
    }
  }

  var sun0 = !afterSunset ? sunCalc.getTimes(moment(noonTz).subtract(24, 'hours'), coord.lat, coord.lng) : null;
  var sun2 = afterSunset ? sunCalc.getTimes(moment(noonTz).add(24, 'hours'), coord.lat, coord.lng) : null;

  return {
    bDate: { y: bYear, m: bMonth, d: bDay, eve: afterSunset },
    startingSunset: afterSunset ? sunsetTz : moment.tz(sun0.sunset, zoneName),
    sunrise: afterSunset ? moment.tz(sun2.sunrise, zoneName) : moment.tz(sun1.sunrise, zoneName),
    endingSunset: afterSunset ? moment.tz(sun2.sunset, zoneName) : sunsetTz
  };
};

function getNawRuz(gYear, frag2DateOnly) {
  // get NawRuz for this gregorian year
{'fraction_non_alphanumeric': 0.16065020054887058, 'fraction_numerical': 0.01702906199422982, 'mean_word_length': 3.7579511215266153, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4775539', 'n_tokens_mistral': 5484, 'n_tokens_neox': 5296, 'n_words': 1017}