Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Executes Javascript, Typescript Scripts.
<!--
### **WORK IN PROGRESS**
-->
### **WORK IN PROGRESS**

* (@klein0r) Added possibility to escape chars in formatTimeDiff

### 8.9.2 (2025-04-27)

* (@GermanBluefox) Updated packages for GUI
Expand Down
2 changes: 2 additions & 0 deletions docs/en/javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -1299,6 +1299,8 @@ formatTimeDiff(milliseconds, format);
* ss, сс(cyrillic) - full seconds, e.g. "05"
* s, с(cyrillic) - short seconds, e.g., "5"

You can use escape charachter `\` to avoid the replacement. e.g. `DD \Day\s, h \hour\s, m \minute, ss \second\s`

#### Example

```js
Expand Down
34 changes: 22 additions & 12 deletions lib/sandbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -3537,50 +3537,60 @@ function sandBox(script, name, verbose, debug, context) {
const neg = diff < 0;
diff = Math.abs(diff);

if (/DD|TT|ДД|D|T|Д/.test(text)) {
if (/(?<!\\)(D|T|Д)/.test(text)) {
const days = Math.floor(diff / day);

text = text.replace(/DD|TT|ДД/, days < 10 ? `0${days}` : days);
text = text.replace(/D|T|Д/, days);
text = text
.replace(/(?<!\\)(DD|TT|ДД)/g, days < 10 ? `0${days}` : days)
.replace(/(?<!\\)(D|T|Д)/g, days);

sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, days=${days})`, 'debug');

diff -= days * day;
}

if (/hh|SS|чч|h|S|ч/.test(text)) {
if (/(?<!\\)(h|S|ч)/.test(text)) {
const hours = Math.floor(diff / hour);

text = text.replace(/hh|SS|чч/, hours < 10 ? `0${hours}` : hours);
text = text.replace(/h|S|ч/, hours);
text = text
.replace(/(?<!\\)(hh|SS|чч)/g, hours < 10 ? `0${hours}` : hours)
.replace(/(?<!\\)(h|S|ч)/g, hours);

sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, hours=${hours})`, 'debug');

diff -= hours * hour;
}

if (/mm|мм|m|м/.test(text)) {
if (/(?<!\\)(m|м)/.test(text)) {
const minutes = Math.floor(diff / minute);

text = text.replace(/mm|мм/, minutes < 10 ? `0${minutes}` : minutes);
text = text.replace(/m|м/, minutes);
text = text
.replace(/(?<!\\)(mm|мм)/g, minutes < 10 ? `0${minutes}` : minutes)
.replace(/(?<!\\)(m|м)/g, minutes);

sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, minutes=${minutes})`, 'debug');

diff -= minutes * minute;
}

if (/ss|сс|мм|s|с/.test(text)) {
if (/(?<!\\)(s|с)/.test(text)) {
const seconds = Math.floor(diff / second);

text = text.replace(/ss|сс/, seconds < 10 ? `0${seconds}` : seconds);
text = text.replace(/s|с/, seconds);
text = text
.replace(/(?<!\\)(ss|сс)/g, seconds < 10 ? `0${seconds}` : seconds)
.replace(/(?<!\\)(s|с)/g, seconds);

sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text}, seconds=${seconds})`, 'debug');

diff -= seconds * second;
}

text = text
.replace(/\\(D|T|Д)/g, '$1')
.replace(/\\(h|S|ч)/g, '$1')
.replace(/\\(m|м)/g, '$1')
.replace(/\\(s|с)/g, '$1');

sandbox.verbose && sandbox.log(`formatTimeDiff(format=${format}, text=${text})`, 'debug');

return neg ? `-${text}` : text;
Expand Down
6 changes: 5 additions & 1 deletion test/testFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1946,7 +1946,8 @@ describe.only('Test JS', function () {
source: `createState('test_formatTimeDiff', { type: 'string', role: 'json', read: true, write: false }, () => {\n` +
` const diff1 = formatTimeDiff(172800000 + 10800000 + 540000 + 15000, 'hh:mm:ss');\n` +
` const diff2 = formatTimeDiff((172800000 + 10800000 + 540000 + 15000) * -1, 'mm:ss');\n` +
` setState('test_formatTimeDiff', { val: JSON.stringify({ diff1, diff2 }), ack: true });\n` +
` const diff3 = formatTimeDiff(374501000, 'DD \\\\Day\\\\s, h \\\\hour\\\\s, m \\\\minute, ss \\\\second\\\\s');\n` +
` setState('test_formatTimeDiff', { val: JSON.stringify({ diff1, diff2, diff3 }), ack: true });\n` +
`});`,
},
native: {},
Expand All @@ -1961,6 +1962,9 @@ describe.only('Test JS', function () {
expect(obj.diff2).to.be.a('string');
expect(obj.diff2).to.be.equal('-3069:15');

expect(obj.diff3).to.be.a('string');
expect(obj.diff3).to.be.equal('04 Days, 8 hours, 1 minute, 41 seconds');

removeStateChangedHandler(onStateChanged);
done();
}
Expand Down
Loading