forked from kriskbx/gitlab-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.js
More file actions
executable file
·169 lines (141 loc) · 5.81 KB
/
time.js
File metadata and controls
executable file
·169 lines (141 loc) · 5.81 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const _ = require('underscore');
const moment = require('moment');
const defaultTimeFormat = '[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]';
const mappings = ['complete', 'sign', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
const regex = /^(?:([-])\s*)?(?:(\d+)mo\s*)?(?:(\d+)w\s*)?(?:(\d+)d\s*)?(?:(\d+)h\s*)?(?:(\d+)m\s*)?(?:(\d+)s\s*)?$/;
const conditionalRegex = /(\[\%([^\>\]]*)\>([^\]]*)\])/ig;
const roundedRegex = /(\[\%([^\>\]]*)\:([^\]]*)\])/ig;
const conditionalSimpleRegex = /([0-9]*)\>(.*)/ig;
const defaultRegex = /(\[\%([^\]]*)\])/ig;
Number.prototype.padLeft = function (n, str) {
return Array(n - String(this).length + 1).join(str || '0') + this;
};
/**
* time model
*/
class time {
/**
* construct
* @param timeString
* @param note
* @param parent
* @param config
*/
constructor(timeString, date = null, note, parent, config) {
this.data = note;
this._date = date;
this.parent = parent;
this.config = config;
this.seconds = time.parse(timeString, this._hoursPerDay, this._daysPerWeek, this._weeksPerMonth);
}
/*
* properties
*/
static get defaultTimeFormat() {
return defaultTimeFormat;
}
get user() {
return this.data.author.username;
}
get date() {
return this._date ? moment(this._date) : moment(this.data.created_at);
}
get type() {
return this.data.noteable_type;
}
get project_id() {
return this.parent.data.project_id;
}
get iid() {
return this.parent.iid;
}
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;
}
/**
* parse human readable to seconds
* @param string
* @param hoursPerDay
* @param daysPerWeek
* @param weeksPerMonth
* @returns {*}
*/
static parse(string, hoursPerDay = 8, daysPerWeek = 5, weeksPerMonth = 4) {
let match, parsed;
if ((match = regex.exec(string)) === null) return false;
parsed = _.object(mappings, match.map(i => i === undefined ? 0 : i));
return (parsed.sign ? -1 : 1) * (parseInt(parsed.seconds)
+ (parseInt(parsed.minutes) * 60)
+ (parseInt(parsed.hours) * 60 * 60)
+ (parseInt(parsed.days) * hoursPerDay * 60 * 60)
+ (parseInt(parsed.weeks) * daysPerWeek * hoursPerDay * 60 * 60)
+ (parseInt(parsed.months) * weeksPerMonth * daysPerWeek * hoursPerDay * 60 * 60));
}
/**
* 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 = inserts.days.padLeft(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 = inserts.hours.padLeft(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 = inserts.minutes.padLeft(2, 0);
inserts.seconds_overall = input;
inserts.seconds = ((input % secondsInADay) % secondsInAnHour) % secondsInAMinute;
inserts.Seconds = inserts.seconds.padLeft(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]
}
decimals = parseInt(decimals);
time = Math.ceil(inserts[match[2]] * Math.pow(10, decimals)) / Math.pow(10, decimals);
output = output.replace(match[0], time !== 0 && conditionalMatch ? time + conditionalMatch[2] : time);
}
// 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();
}
}
module.exports = time;