Created
May 31, 2024 13:33
-
-
Save lze3/24db9d3e9e79fe95039b88bcafb21eea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Date.prototype.addDays = function(days) { | |
var date = new Date(this.valueOf()); | |
date.setDate(date.getDate() + days); | |
return date; | |
}; | |
const args = process.argv.slice(2); | |
const fs = require('fs'); | |
const first_working_date = '5/23/2024'; // can just be left usually | |
let shift_pattern = { | |
on: 5, | |
off: 3 | |
}; | |
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']; | |
const preformat_month_array = () => { | |
const ret = {}; | |
months.forEach(month => ret[month] = []); | |
return ret; | |
}; | |
const month_as_string = (month_as_number) => months[month_as_number]; | |
// allow presetting days | |
if (args.length == 2) { | |
if (parseInt(args[0]) && parseInt(args[1])) { | |
shift_pattern.on = parseInt(args[0]); | |
shift_pattern.off = parseInt(args[1]); | |
} | |
} | |
console.log(`Calculating work pattern for ${shift_pattern.on}on ${shift_pattern.off}off...\n`); | |
let working_date_as_date = new Date(first_working_date); | |
const get_working_days = () => { | |
const working_days = preformat_month_array(); | |
const rest_days = preformat_month_array(); | |
let working = 1; | |
let rest = 0; | |
const running_totals = { | |
working_: 0, | |
resting: 0 | |
}; | |
working_date_as_date.setTime(working_date_as_date.getTime() - 1) // stupid bst shit | |
for (let i = 0; i < 364; i++) { | |
working_date_as_date = working_date_as_date.addDays(1); | |
const current_month = month_as_string(working_date_as_date.getMonth()); | |
if (!rest) { | |
if (working === shift_pattern.on) { | |
rest = 1; | |
working = 0; | |
} else { | |
working += 1; | |
} | |
running_totals.working_++; | |
working_days[current_month].push(working_date_as_date.toLocaleDateString());; | |
} else { | |
if (rest === shift_pattern.off) { | |
working = 1; | |
rest = 0; | |
} else { | |
rest += 1; | |
} | |
running_totals.resting++; | |
rest_days[current_month].push(working_date_as_date.toLocaleDateString()); | |
} | |
} | |
fs.writeFile('./data/rest-days.json', JSON.stringify(rest_days), err => err); | |
fs.writeFile('./data/work-days.json', JSON.stringify(working_days), err => err); | |
const { working_, resting } = running_totals; | |
console.log(`Finished compiling, totals: | |
working: ${working_} | |
resting: ${resting} | |
== ${Math.round((working_ / (working_ + resting)) * 100)}% working`); | |
}; | |
get_working_days(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment