Skip to content

Instantly share code, notes, and snippets.

@expalmer
Last active January 21, 2020 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save expalmer/f40f8af11be27dac0780a830cc37b3e2 to your computer and use it in GitHub Desktop.
Save expalmer/f40f8af11be27dac0780a830cc37b3e2 to your computer and use it in GitHub Desktop.
Horas Mensais
(function() {
function timeDiff(start, end) {
var diff = end - start;
var units = [
1000 * 60 * 60 *24,
1000 * 60 * 60,
1000 * 60,
1000
];
var rv = []; // h, m, s array
for (var i = 0; i < units.length; ++i) {
rv.push(Math.floor(diff / units[i]));
diff = diff % units[i];
}
var thisFullYear = end.getFullYear();
var daysInLastMonth = new Date(thisFullYear, end.getMonth(), 0).getDate();
var thisMonth = end.getMonth();
thisFullYear = thisFullYear - start.getFullYear();
thisMonth = thisMonth - start.getMonth();
subAddDays = daysInLastMonth - start.getDate();
thisDay = end.getDate();
thisMonth = thisMonth - 1;
if(thisMonth < 0){
thisFullYear = thisFullYear - 1;
thisMonth = 12 + thisMonth;
}
subAddDays = daysInLastMonth - start.getDate();
subAddDays = thisDay + subAddDays;
if(subAddDays >= daysInLastMonth){
subAddDays = subAddDays - daysInLastMonth;
thisMonth++;
if (thisMonth > 11){
thisFullYear++;
thisMonth = 0;
}
}
return {
hours: rv[1],
minutes: rv[2],
};
}
function createDate(a) {
const [hour, minute] = a.split(':')
return new Date(`2020-01-01 ${hour}:${minute}:00`)
}
function split(arr, n) {
n = n != null ? n : arr.length;
var len = arr.length;
var groups = [];
for (var i = 0; i < len; i += n) {
groups.push(arr.slice(i, i + n));
}
return groups;
}
function sumHours(a, b) {
const hours = a.hours + b.hours
const minutes = a.minutes + b.minutes
const h = hours + Math.floor(minutes / 60);
const m = minutes % 60;
return { hours: h, minutes: m }
}
function extractHoursFromDOM() {
const array = [];
const trs = document.querySelectorAll('.dataTable tbody tr')
const len = trs.length
const values = [];
for (let index = 0; index < len; index++) {
const element = trs[index];
const len2 = element.children.length
values[index] = [];
for (let index2 = 0; index2 < len2; index2++) {
const el = element.children[index2];
if (el.className.match('is_justification') && el.childNodes[1] && el.childNodes[1].innerHTML) {
const value = el.childNodes[1].innerHTML
if (value) {
values[index].push({
el,
next: el.nextSibling.nextElementSibling,
value,
date: createDate(value),
})
array.push(el.childNodes[1].innerHTML)
}
}
}
}
return values.filter(x => x.length === 4)
}
function getTotalByDay(i) {
const [a, b, c, d] = i
const morning = timeDiff(a.date, b.date)
const evening = timeDiff(c.date, d.date)
const total = sumHours(morning, evening)
return total
}
function getTotal(hours) {
const total = hours.map(getTotalByDay).reduce((acc, i) => (sumHours(acc, i)), { hours: 0, minutes: 0 })
return total
}
function showTotal(array, total) {
const last = array[array.length - 1]
const [,,,d] = last
d.el.nextSibling.nextSibling.nextElementSibling.innerHTML = `<strong style="color: purple;">${total.hours}h ${total.minutes}m</strong>`
}
const hours = extractHoursFromDOM()
hours.forEach(i => {
const [,,,last] = i
const total = getTotalByDay(i)
if (last.el && last.el.nextSibling && last.el.nextSibling.nextElementSibling.innerHTML) {
last.el.nextSibling.nextElementSibling.innerHTML = `<spam style="color: blue;">${total.hours}h ${total.minutes}m</span>`
}
})
showTotal(hours, getTotal(hours))
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment