-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdateformat.js
More file actions
152 lines (135 loc) · 3.48 KB
/
dateformat.js
File metadata and controls
152 lines (135 loc) · 3.48 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
const Diagnostics = require('Diagnostics');
const Patches = require('Patches');
const Time = require('Time');
/**
* Global variables.
*/
const globals = {
interval: null,
};
/**
* Script options.
*/
const options = {
/**
* Default date format.
*/
format: 'YYYY-MM-DD HH:mm:ss',
/**
* Default date refresh interval in seconds.
*/
refreshInterval: 1,
/**
* Display debug info.
*/
debug: false,
};
/**
* Display debug messages on console.
*
* @param {String} context
* @param {String} message
*/
const debug = (context, message) => {
options.debug && Diagnostics.log(`[debug][${context}] ${message}`);
};
/**
* Update patch input values.
*/
const update = async () => {
try {
const dateString = formatDate(new Date(), options.format);
await Patches.inputs.setString('date', dateString);
debug('from script', `date: ${dateString}`);
} catch (err) {
Diagnostics.log(err);
}
};
/**
* Returns a date/time number in two digits format.
*
* @param {Number} date
* @returns {String}
*/
const twoDigit = (date) => String(date).padStart(2, '0');
/**
* Return hours from date to 12 hours format.
*
* @param {Date} date
* @returns {Number}
*/
const twelveHoursFormat = (date) => (date.getHours() % 12) || 12;
/**
* Format a date according to the input string.
*
* @param {Date} date
* @param {String} format
*/
const formatDate = (date, format) => {
return format
.replace(/(YYYY)/g, date.getFullYear())
.replace(/(YY)/g, String(date.getFullYear()).slice(-2))
.replace(/(MM)/g, twoDigit(date.getMonth() + 1))
.replace(/(M)/g, date.getMonth() + 1)
.replace(/(DD)/g, twoDigit(date.getDate()))
.replace(/(D)/g, date.getDate())
.replace(/(HH)/g, twoDigit(date.getHours()))
.replace(/(H)/g, date.getHours())
.replace(/(hh)/g, twoDigit(twelveHoursFormat(date)))
.replace(/(h)/g, twelveHoursFormat(date))
.replace(/(mm)/g, twoDigit(date.getMinutes()))
.replace(/(m)/g, date.getMinutes())
.replace(/(ss)/g, twoDigit(date.getSeconds()))
.replace(/(s)/g, date.getSeconds())
.replace(/(A)/g, date.getHours() >= 12 ? 'PM': 'AM')
.replace(/(a)/g, date.getHours() >= 12 ? 'pm' : 'am');
};
/**
* Patch output types mapped to monitor functions names.
*/
const outputTypes = {
'string': 'getStringOrFallback',
'number': 'getScalarOrFallback',
'boolean': 'getBooleanOrFallback',
};
/**
* Watch a patch output parameter.
*
* @param {String} name
* @param {String} type
* @param {Function} callback
*/
const watchOutput = async (name, type, callback) => {
const getOutputWatcher = Patches.outputs[outputTypes[type]];
const watcher = await getOutputWatcher(name, options[name]);
return watcher.monitor({ fireOnInitialValue: true }).subscribe(({ newValue }) => {
debug('to script', `${name}: ${newValue}`);
callback(newValue);
});
};
/**
* Main function.
*/
const main = async () => {
try {
// Update refresh interval.
watchOutput('refreshInterval', 'number', (refreshInterval) => {
if (globals.interval) {
Time.clearInterval(globals.interval);
}
options.refreshInterval = refreshInterval;
globals.interval = Time.setInterval(update, refreshInterval * 1000);
});
// Update date format.
watchOutput('format', 'string', (format) => {
options.format = format;
});
// Update debug option.
watchOutput('debug', 'boolean', (debug) => {
options.debug = debug;
});
} catch (err) {
Diagnostics.log(err);
}
};
main();