forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtime.js
More file actions
executable file
·139 lines (116 loc) · 4.77 KB
/
Copy pathtime.js
File metadata and controls
executable file
·139 lines (116 loc) · 4.77 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import dayjs from './dayjs.js';
const defaultTimeFormat = '[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]';
const conditionalRegex = /(\[\%([^\>\]]*)\>([^\]]*)\])/ig;
const roundedRegex = /(\[\%([^\>\]]*)\:([^\]]*)\])/ig;
const conditionalSimpleRegex = /([0-9]*)\>(.*)/ig;
const defaultRegex = /(\[\%([^\]]*)\])/ig;
function padLeft(value, n, str) {
return Array(Math.max(0, n - String(value).length + 1)).join(str || '0') + value;
}
/**
* time model
*/
class Time {
/**
* construct
* @param date
* @param data raw noteable payload (author, created_at, noteable_type)
* @param config
* @param {number} seconds
* @param {string|null} [note]
* @param {number} [chargeRatio]
*/
constructor(date = null, data, config, seconds, note = null, chargeRatio = 1.0) {
this.data = data;
this._date = date;
this.config = config;
this.note = note;
this.chargeRatio = chargeRatio;
this.seconds = seconds;
}
/*
* properties
*/
static get defaultTimeFormat() {
return defaultTimeFormat;
}
get user() {
return this.data.author.username;
}
get date() {
return this._date ? dayjs(this._date) : dayjs(this.data.created_at);
}
get type() {
return this.data.noteable_type;
}
get time() {
return Time.toHumanReadable(this.seconds, this._hoursPerDay, this._timeFormat);
}
get _timeFormat() {
return this.config && this.config.get('timeFormat', 'records') ? this.config.get('timeFormat', 'records') : '';
}
get _hoursPerDay() {
return this.config && this.config.get('hoursPerDay') ? parseInt(this.config.get('hoursPerDay')) : 8;
}
get _daysPerWeek() {
return this.config && this.config.get('daysPerWeek') ? parseInt(this.config.get('daysPerWeek')) : 5;
}
get _weeksPerMonth() {
return this.config && this.config.get('weeksPerMonth') ? parseInt(this.config.get('weeksPerMonth')) : 4;
}
/**
* get human readable
* @param input
* @param hoursPerDay
* @param format
* @returns {string}
*/
static toHumanReadable(input, hoursPerDay = 8, format = Time.defaultTimeFormat) {
let sign = parseInt(input) < 0 ? '-' : '', output = format, match;
input = Math.abs(input);
let secondsInADay = 60 * 60 * hoursPerDay;
let secondsInAnHour = 60 * 60;
let secondsInAMinute = 60;
let inserts = {};
inserts.sign = sign;
inserts.days_overall = input / secondsInADay;
inserts.days_overall_comma = inserts.days_overall.toString().replace('.', ',');
inserts.days = Math.floor(inserts.days_overall);
inserts.Days = padLeft(inserts.days, 2, '0');
inserts.hours_overall = input / secondsInAnHour;
inserts.hours_overall_comma = inserts.hours_overall.toString().replace('.', ',');
inserts.hours = Math.floor((input % secondsInADay) / secondsInAnHour);
inserts.Hours = padLeft(inserts.hours, 2, '0');
inserts.minutes_overall = input / secondsInAMinute;
inserts.minutes_overall_comma = (inserts.minutes_overall).toString().replace('.', ',');
inserts.minutes = Math.floor(((input % secondsInADay) % secondsInAnHour) / secondsInAMinute);
inserts.Minutes = padLeft(inserts.minutes, 2, '0');
inserts.seconds_overall = input;
inserts.seconds = ((input % secondsInADay) % secondsInAnHour) % secondsInAMinute;
inserts.Seconds = padLeft(inserts.seconds, 2, '0');
// rounded
while ((match = roundedRegex.exec(format)) !== null) {
if (match.index === roundedRegex.lastIndex) roundedRegex.lastIndex++;
let time, conditionalMatch, decimals = match[3];
if ((conditionalMatch = conditionalSimpleRegex.exec(decimals)) !== null) {
decimals = conditionalMatch[1]
}
let iDecimals = parseInt(decimals);
time = Math.ceil(inserts[match[2]] * Math.pow(10, iDecimals)) / Math.pow(10, iDecimals);
output = output.replace(match[0], (time !== 0 && conditionalMatch ? time + conditionalMatch[2] : time).toString());
}
// conditionals
while ((match = conditionalRegex.exec(format)) !== null) {
if (match.index === conditionalRegex.lastIndex) conditionalRegex.lastIndex++;
output = output.replace(match[0], inserts[match[2]] > 0 ? inserts[match[2]] + match[3] : '');
}
// default
format = output;
while ((match = defaultRegex.exec(format)) !== null) {
if (match.index === defaultRegex.lastIndex) defaultRegex.lastIndex++;
output = output.replace(match[0], inserts[match[2]]);
}
return output.trim();
}
}
export default Time;