This repository was archived by the owner on Nov 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimes.js
83 lines (68 loc) · 1.98 KB
/
times.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"use strict";
if(logTimeStats === undefined)
var logTimeStats = {};
/* Parse login time data from the JSON into more useful variables */
logTimeStats.Times = function() {
this.clear = () => {
this.firstDay = "";
this.lastDay = "";
this.startDate = "";
this.endDate = "";
this.actDays = 0;
this.totalDays = 0;
this.timeTotalMs = 0;
};
this.clear();
this.update = (data) => {
if (!data) {
this.clear();
return;
}
this.actDays = 0;
this.totalDays = 0;
this.timeTotalMs = 0;
const dataArray = Object.keys(data);
this.lastDay = dataArray[0];
this.firstDay = dataArray.pop();
if (!this.startDate)
this.startDate = this.firstDay;
if (!this.endDate)
this.endDate = logTimeStats.dateToString(new Date());
const end = new Date(this.endDate + "T00:00:00.0000");
const date = new Date(this.startDate + "T00:00:00.0000");
while (date.getTime() <= end.getTime()) {
const dateString = logTimeStats.dateToString(date);
if (data[dateString]) {
this.actDays++;
const dateArray = data[dateString].split(':');
this.timeTotalMs += dateArray[0] * 60 * 60 * 1000;
this.timeTotalMs += dateArray[1] * 60 * 1000;
this.timeTotalMs += dateArray[2] * 1000;
}
this.totalDays++;
date.setDate(date.getDate() + 1);
}
};
};
logTimeStats.msToTime = function(ms) {
if (!ms)
return null;
let seconds = Math.floor(ms / 1000);
let minutes = Math.floor(seconds / 60);
let hours = Math.floor(minutes / 60);
seconds = seconds % 60;
minutes = minutes % 60;
return [hours, minutes, seconds];
};
// Date.prototype.toISOString() uses UTC timezone
// which can get messy, so let's do this manually
logTimeStats.dateToString = function(date) {
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
if (month < 10)
month = '0' + month;
if (day < 10)
day = '0' + day;
return (year + '-' + month + '-' + day);
};