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
·141 lines (118 loc) · 4.45 KB
/
time.js
File metadata and controls
executable file
·141 lines (118 loc) · 4.45 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
const _ = require('underscore');
const moment = require('moment');
const defaultTimeFormat = '[%sign][%days>d ][%hours>h ][%minutes>m ][%seconds>s]';
const mappings = ['complete', 'sign', 'weeks', 'days', 'hours', 'minutes', 'seconds'];
const regex = /^(?:([-])\s*)?(?:(\d+)w\s*)?(?:(\d+)d\s*)?(?:(\d+)h\s*)?(?:(\d+)m\s*)?(?:(\d+)s\s*)?$/;
const conditionalRegex = /(\[\%([^\>\]]*)\>([^\]]*)\])/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, note, parent, config) {
this.data = note;
this.parent = parent;
this.config = config;
this.seconds = time.parse(timeString, this._hoursPerDay);
}
/*
* properties
*/
static get defaultTimeFormat() {
return defaultTimeFormat;
}
get user() {
return this.data.author.username;
}
get date() {
return 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') ? this.config.get('timeFormat') : '';
}
get _hoursPerDay() {
return this.config && this.config.get('hoursPerDay') ? parseInt(this.config.get('hoursPerDay')) : 8;
}
/**
* parse human readable to seconds
* @param string
* @param hoursPerDay
* @returns {*}
*/
static parse(string, hoursPerDay = 8) {
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) * 5 * 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);
// 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;