2018-11-07 10:36:21 +00:00
|
|
|
/**
|
2019-09-18 08:04:11 +00:00
|
|
|
* @copyright Copyright (c) 2019 Georg Ehrke
|
2021-09-07 19:12:52 +00:00
|
|
|
*
|
2018-11-07 10:36:21 +00:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2021-09-07 19:12:52 +00:00
|
|
|
*
|
2021-09-03 18:27:40 +00:00
|
|
|
* @license AGPL-3.0-or-later
|
2018-11-07 10:36:21 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2021-09-07 19:12:52 +00:00
|
|
|
*
|
2018-11-07 10:36:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-24 17:09:16 +00:00
|
|
|
import logger from './logger.js'
|
|
|
|
|
2018-11-07 10:36:21 +00:00
|
|
|
/**
|
|
|
|
* returns a new Date object
|
|
|
|
*
|
2021-07-23 10:37:54 +00:00
|
|
|
* @return {Date}
|
2018-11-07 10:36:21 +00:00
|
|
|
*/
|
2018-11-03 20:37:52 +00:00
|
|
|
export function dateFactory() {
|
|
|
|
return new Date()
|
|
|
|
}
|
|
|
|
|
2018-11-07 10:36:21 +00:00
|
|
|
/**
|
|
|
|
* formats a Date object as YYYYMMDD
|
|
|
|
*
|
|
|
|
* @param {Date} date Date to format
|
2021-07-23 10:37:54 +00:00
|
|
|
* @return {string}
|
2018-11-07 10:36:21 +00:00
|
|
|
*/
|
2018-11-03 20:37:52 +00:00
|
|
|
export function getYYYYMMDDFromDate(date) {
|
|
|
|
return new Date(date.getTime() - (date.getTimezoneOffset() * 60000))
|
|
|
|
.toISOString()
|
|
|
|
.split('T')[0]
|
|
|
|
}
|
2019-08-20 14:01:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get unix time from date object
|
|
|
|
*
|
|
|
|
* @param {Date} date Date to format
|
2021-07-23 10:37:54 +00:00
|
|
|
* @return {number}
|
2019-08-20 14:01:28 +00:00
|
|
|
*/
|
|
|
|
export function getUnixTimestampFromDate(date) {
|
|
|
|
return Math.floor(date.getTime() / 1000)
|
|
|
|
}
|
2019-08-30 08:39:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a Date-object based on the firstday param used in routes
|
|
|
|
*
|
2021-07-23 10:37:54 +00:00
|
|
|
* @param {string} firstDayParam The firstday param from the router
|
|
|
|
* @return {Date}
|
2019-08-30 08:39:57 +00:00
|
|
|
*/
|
|
|
|
export function getDateFromFirstdayParam(firstDayParam) {
|
|
|
|
if (firstDayParam === 'now') {
|
|
|
|
return dateFactory()
|
|
|
|
}
|
|
|
|
|
2020-02-24 11:31:25 +00:00
|
|
|
const [year, month, date] = firstDayParam.split('-')
|
|
|
|
.map((str) => parseInt(str, 10))
|
2020-02-24 17:09:16 +00:00
|
|
|
|
|
|
|
if (Number.isNaN(year) || Number.isNaN(month) || Number.isNaN(date)) {
|
|
|
|
logger.error('First day parameter contains non-numerical components, falling back to today')
|
|
|
|
return dateFactory()
|
|
|
|
}
|
|
|
|
|
2020-02-24 11:31:25 +00:00
|
|
|
const dateObject = dateFactory()
|
|
|
|
dateObject.setFullYear(year, month - 1, date)
|
|
|
|
dateObject.setHours(0, 0, 0, 0)
|
|
|
|
|
|
|
|
return dateObject
|
2019-08-30 08:39:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* formats firstday param as YYYYMMDD
|
|
|
|
*
|
2021-07-23 10:37:54 +00:00
|
|
|
* @param {string} firstDayParam The firstday param from the router
|
|
|
|
* @return {string}
|
2019-08-30 08:39:57 +00:00
|
|
|
*/
|
|
|
|
export function getYYYYMMDDFromFirstdayParam(firstDayParam) {
|
|
|
|
if (firstDayParam === 'now') {
|
|
|
|
return getYYYYMMDDFromDate(dateFactory())
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstDayParam
|
|
|
|
}
|
2019-09-02 12:59:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a date object based on the given DateTimeValue
|
|
|
|
* Ignores given timezone-information
|
|
|
|
*
|
|
|
|
* @param {DateTimeValue} dateTimeValue Value to get date from
|
2021-07-23 10:37:54 +00:00
|
|
|
* @return {Date}
|
2019-09-02 12:59:35 +00:00
|
|
|
*/
|
|
|
|
export function getDateFromDateTimeValue(dateTimeValue) {
|
|
|
|
return new Date(
|
|
|
|
dateTimeValue.year,
|
|
|
|
dateTimeValue.month - 1,
|
|
|
|
dateTimeValue.day,
|
|
|
|
dateTimeValue.hour,
|
|
|
|
dateTimeValue.minute,
|
2019-10-26 20:46:37 +00:00
|
|
|
0,
|
2024-03-21 15:35:50 +00:00
|
|
|
0,
|
2019-09-02 12:59:35 +00:00
|
|
|
)
|
|
|
|
}
|
2019-09-18 10:04:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* modifies a date
|
|
|
|
*
|
|
|
|
* @param {Date} date Date object to modify
|
2021-07-23 10:37:54 +00:00
|
|
|
* @param {object} data The destructuring object
|
|
|
|
* @param {number} data.day Number of days to add
|
|
|
|
* @param {number} data.week Number of weeks to add
|
|
|
|
* @param {number} data.month Number of months to add
|
2023-05-21 09:05:57 +00:00
|
|
|
* @param data.year
|
2021-07-23 10:37:54 +00:00
|
|
|
* @return {Date}
|
2019-09-18 10:04:17 +00:00
|
|
|
*/
|
2023-05-21 09:05:57 +00:00
|
|
|
export function modifyDate(date, { day = 0, week = 0, month = 0, year = 0 }) {
|
2019-10-26 20:46:37 +00:00
|
|
|
date = new Date(date.getTime())
|
|
|
|
date.setDate(date.getDate() + day)
|
|
|
|
date.setDate(date.getDate() + week * 7)
|
|
|
|
date.setMonth(date.getMonth() + month)
|
2023-05-21 09:05:57 +00:00
|
|
|
date.setFullYear(date.getFullYear() + year)
|
2019-10-26 20:46:37 +00:00
|
|
|
|
|
|
|
return date
|
2019-09-18 10:04:17 +00:00
|
|
|
}
|