"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.beginningOfDay = beginningOfDay;
exports.beginningOfHour = beginningOfHour;
exports.beginningOfMinute = beginningOfMinute;
exports.beginningOfSecond = beginningOfSecond;
exports.endOfDay = endOfDay;
exports.endOfHour = endOfHour;
exports.endOfMinute = endOfMinute;
exports.endOfSecond = endOfSecond;
exports.findNextMonth = findNextMonth;
exports.nextDay = nextDay;
exports.nextDayOfMonth = nextDayOfMonth;
exports.nextHour = nextHour;
exports.nextMinute = nextMinute;
exports.nextSecond = nextSecond;

// -----------------------------------------------------------------------------
// Seconds
// -----------------------------------------------------------------------------
function beginningOfSecond(now) {
  const date = new Date(now);
  return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), 0).getTime();
}

function endOfSecond(now) {
  const date = new Date(beginningOfSecond(now));
  return date.setSeconds(date.getSeconds() + 1);
}

function nextSecond(now, second) {
  const date = new Date(now);

  if (date.getSeconds() == second) {
    return now;
  }

  if (date.getSeconds() < second) {
    return date.setSeconds(second);
  } // Set seconds to the provided value and add one minute


  const newDate = new Date(date.setSeconds(second));
  return newDate.setTime(newDate.getTime() + 1000 * 60);
} // -----------------------------------------------------------------------------
// Minutes
// -----------------------------------------------------------------------------


function beginningOfMinute(now) {
  const date = new Date(now);
  return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), 0, 0).getTime();
}

function endOfMinute(now) {
  const date = new Date(beginningOfMinute(now));
  return date.setMinutes(date.getMinutes() + 1);
}

function nextMinute(now, minute) {
  const date = new Date(now);

  if (date.getMinutes() == minute) {
    return now;
  }

  if (date.getMinutes() < minute) {
    return date.setMinutes(minute);
  } // Set minutes to the provided value and add one hour


  const newDate = new Date(date.setMinutes(minute));
  return newDate.setTime(newDate.getTime() + 1000 * 60 * 60);
} // -----------------------------------------------------------------------------
// Hours
// -----------------------------------------------------------------------------


function beginningOfHour(now) {
  const date = new Date(now);
  return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), 0, 0, 0).getTime();
}

function endOfHour(now) {
  const date = new Date(beginningOfHour(now));
  return date.setHours(date.getHours() + 1);
}

function nextHour(now, hour) {
  const date = new Date(now);

  if (date.getHours() == hour) {
    return now;
  }

  if (date.getHours() < hour) {
    return date.setHours(hour);
  } // Set hours to the provided value and add one day


  const newDate = new Date(date.setHours(hour));
  return newDate.setTime(newDate.getTime() + 1000 * 60 * 60 * 24);
} // -----------------------------------------------------------------------------
// Days
// -----------------------------------------------------------------------------


function beginningOfDay(now) {
  const date = new Date(now);
  return new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0, 0).getTime();
}

function endOfDay(now) {
  const date = new Date(beginningOfDay(now));
  return date.setDate(date.getDate() + 1);
}

function nextDay(now, day) {
  const date = new Date(now);

  if (date.getDay() === day) {
    return now;
  }

  return date.setDate(date.getDate() + (7 + 7 - date.getDay()) % 7);
}

function nextDayOfMonth(now, day) {
  const date = new Date(now);

  if (date.getDate() === day) {
    return now;
  }

  if (date.getDate() < day) {
    return date.setDate(day);
  }

  return findNextMonth(now, day, 1);
}

function findNextMonth(now, day, months) {
  const d = new Date(now);
  const tmp1 = new Date(d.setDate(day));
  const tmp2 = new Date(tmp1.setMonth(tmp1.getMonth() + months));

  if (tmp2.getDate() === day) {
    const d2 = new Date(now);
    const tmp3 = new Date(d2.setDate(day));
    return tmp3.setMonth(tmp3.getMonth() + months);
  }

  return findNextMonth(now, day, months + 1);
}
//# sourceMappingURL=time.js.map