/** * Various date helper methods. * * Copyright (c) 2022 Markus Sipilä. * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ /** * Exports. */ module.exports = { getEntsoStart: getEntsoStart, getEntsoEnd: getEntsoEnd, getCurrentHour: getCurrentHour, getMidnight: getMidnight }; /** * Returns the start datetime for Entso API request. * * Nordpool publishes the day ahead prices at 12:45 CET/CEST = 13:45 EET/EEST. If the script is * executed at 14:00 or later (local time), tomorrow's prices will be requested. * Otherwise today's prices will be requested. * * @return string * Start datetime for the API call in format YYYYMMDD0000. */ function getEntsoStart() { let date = new Date(); if (date.getHours() >= 14) { date.setDate(date.getDate() + 1); } return date.toLocaleDateString('en-GB').replaceAll('-','') + '0000'; } /** * Returns the end datetime for Entso API request. * * Nordpool publishes the day ahead prices at 12:45 CET/CEST = 13:45 EET/EEST. If the script is * executed at 14:00 or later (local time), tomorrow's prices will be requested. * Otherwise today's prices will be requested. * * @return string * End datetime for the API call in in format YYYYMMDD2200. */ function getEntsoEnd() { let date = new Date(); if (date.getHours() >= 14) { date.setDate(date.getDate() + 1); } // Entso API uses CET/CEST. 23.00 EET/EEST = 22.00 CET/CEST. return date.toLocaleDateString('en-GB').replaceAll('-','') + '2200'; } /** * Returns current hour. * * @return Date * Current date with minutes, seconds and milliseconds set to 0. */ function getCurrentHour() { let date = new Date(); date.setMinutes(0); date.setSeconds(0); date.setMilliseconds(0); return date; } /** * Returns the start/stop midnight. * * Nordpool publishes the day ahead prices at 12:45 CET/CEST = 13:45 EET/EEST. If the script is * executed at 14:00 or later (local time), 'start' midnight will be tomorrow 00:00 local time. * Otherwise 'start' midnight will be today 00:00 local time. * * Stop midnight: 24 hours afer the start midnight. * * @param string type * 'start' or 'stop. * * @return Date */ function getMidnight(type) { let date = new Date(); if (date.getHours() >= 14) { date.setDate(date.getDate() + 1); } // Set time to 00:00:00.000 date.setHours(0); date.setMinutes(0); date.setSeconds(0); date.setMilliseconds(0); if (type == 'stop') { date.setDate(date.getDate() + 1); } return date; }