diff options
| author | Joel Kronqvist <work.joelkronqvist@gmail.com> | 2022-11-03 17:15:40 +0200 | 
|---|---|---|
| committer | Joel Kronqvist <work.joelkronqvist@gmail.com> | 2022-11-03 17:15:40 +0200 | 
| commit | 5f74d40fa736745651514853afdca3ed44e5ae74 (patch) | |
| tree | 84dba0e9a48ae71fb61fb8134d2b895cac8ed34a /Functions | |
| parent | cf1dd1d7bd128bf770159032975bc014497507ca (diff) | |
| download | LYLLRuoka-5f74d40fa736745651514853afdca3ed44e5ae74.tar.gz LYLLRuoka-5f74d40fa736745651514853afdca3ed44e5ae74.zip  | |
Made server update foods at monday morning instead of updating them just randomly once a week.
Diffstat (limited to 'Functions')
| -rw-r--r-- | Functions/dateFuncs.js | 26 | 
1 files changed, 24 insertions, 2 deletions
diff --git a/Functions/dateFuncs.js b/Functions/dateFuncs.js index 9b1237a..55a9372 100644 --- a/Functions/dateFuncs.js +++ b/Functions/dateFuncs.js @@ -35,8 +35,30 @@ function weekdayToNumber(s)  	}  } + +function run_at_monday_mornings(func) { +	const ms_in_h = 60/*min*/ * 60/*s*/ * 1000/*ms*/; + +	const d = new Date(); +	const hour = d.getHours(); // eg. fri 17:20 -> 17 +	const weekday = d.getDay() || 7; // 1 = monday, 7 = sunday + +	days_to_elapse = 8 - weekday; +	ms_to_elapse = ms_in_h * ( +		days_to_elapse * 24/*hours in a day*/ +		- hour // removes unneccessary hours so that we update it at 0 am +	) + 1 /*+1 so that we know that it won't be exactly midnight +	and possibly sunday. idk if this actually is needed.*/; + +	setTimeout(() => run_at_monday_mornings(func), ms_to_elapse); + +	if (weekday === 1) +		func(); +} +  module.exports = {      fromString: stringToDate,      between: isBetweenDates, -    weekdayToNumber: weekdayToNumber -}
\ No newline at end of file +    weekdayToNumber: weekdayToNumber, +    run_at_monday_mornings: run_at_monday_mornings +}  | 
