forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.ts
More file actions
94 lines (76 loc) · 3.12 KB
/
converter.ts
File metadata and controls
94 lines (76 loc) · 3.12 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
import i18n, { getMessagesFromLocale } from '../plugins/i18n';
import { HOUR, HOUR_IN_SECONDS, MINUTE, MINUTE_IN_SECONDS, Time } from './time';
export function convertHHMMToSeconds(hours: number, minutes: number) {
return hours * 3600 + minutes * 60;
}
export function convertHHMMToMilliSeconds(hours: number, minutes: number) {
return hours * HOUR + minutes * MINUTE;
}
export function convertSecondsToHHMM(seconds: number): Time {
const hours = Math.floor(seconds / 3600);
const totalSeconds = seconds % 3600;
const mins = Math.floor(totalSeconds / 60);
return {
hours: hours,
minutes: mins,
};
}
export function convertMilliSecondsToHHMM(seconds: number): Time {
const hours = Math.floor(seconds / HOUR);
const totalSeconds = seconds % HOUR;
const mins = Math.floor(totalSeconds / MINUTE);
return {
hours: hours,
minutes: mins,
};
}
export function convertSummaryTimeToBadgeString(summaryTime: number): string {
const sec = summaryTime;
const min = Number((summaryTime / 60).toFixed(0));
const hours = Number((summaryTime / (60 * 60)).toFixed(1));
if (sec < 60) return `${sec}s`;
else if (min < 60) return `${min}m`;
else return `${hours}h`;
}
export function convertSummaryTimeToString(summaryTime: number) {
let days = Math.floor(summaryTime / 3600 / 24);
const totalHours = summaryTime % (3600 * 24);
let hours = Math.floor(totalHours / 3600);
const totalSeconds = summaryTime % 3600;
let mins = Math.floor(totalSeconds / 60);
let seconds = totalSeconds % 60;
function appendTime(value: number, stringPrefix: string, isUseZero: boolean = false) {
return value > 0 ? `${isUseZero ? zeroAppend(value) : value} ${stringPrefix}` : '';
}
const daysStr = appendTime(days, i18n.global.t('d.message'));
const hoursStr = appendTime(hours, i18n.global.t('h.message'), daysStr == '' ? false : true);
const minsStr = appendTime(mins, i18n.global.t('m.message'), hoursStr == '' ? false : true);
const secondsStr = appendTime(seconds, i18n.global.t('s.message'), minsStr == '' ? false : true);
return `${daysStr} ${hoursStr} ${minsStr} ${secondsStr}`;
}
export function convertLimitTimeToString(summaryTime: number) {
const totalHours = summaryTime % (3600 * 24);
let hours = Math.floor(totalHours / 3600);
const totalSeconds = summaryTime % 3600;
let mins = Math.floor(totalSeconds / 60);
function appendTime(value: number, stringPrefix: string, isUseZero: boolean = false) {
return `${isUseZero ? zeroAppend(value) : value} ${stringPrefix}`;
}
return `${appendTime(hours, getMessagesFromLocale()['h']['message'])} ${appendTime(
mins,
getMessagesFromLocale()['m']['message'],
true,
)}`;
}
function zeroAppend(time: number) {
if (time < 10) return `0${time}`;
else return time;
}
export function convertStringTimeIntervalToSeconds(timeInterval: string) {
const time = timeInterval.split(':');
return Number(time[0]) * HOUR_IN_SECONDS + Number(time[1]) * MINUTE_IN_SECONDS + Number(time[2]);
}
export function convertHoursToTime(time: number) {
const timeInSeconds = Math.floor(time * MINUTE_IN_SECONDS);
return convertSummaryTimeToString(timeInSeconds);
}