Skip to content

Instantly share code, notes, and snippets.

@retorillo
Last active May 2, 2021 04:59
Show Gist options
  • Save retorillo/b8410eff64d87f3e8cffb4b7b7730e87 to your computer and use it in GitHub Desktop.
Save retorillo/b8410eff64d87f3e8cffb4b7b7730e87 to your computer and use it in GitHub Desktop.
Premiere's Time object computation (Adobe ExtendedScript compatible)
// for Premiere's Time object computation (Adobe ExtendedScript Compatible)
// WORKING IN PROGRESS, NOT WELL TESTED
// License: Public Domain or CC0
// Copyleft (ɔ) Retorillo
function bigint_pad(x, L) {
// NOTE: ExtendedScript does not support String.padStart
var y = x.split(/(?=.)/);
while(y.length < L) y.unshift('0');
return y.join('');
}
function bigint_add(x, y) {
if (!/^[0-9]+$/.test(x) || !/^[0-9]+$/.test(y))
throw new Error("must be positive number");
var L = Math.max(x.length, y.length);
x = bigint_pad(x, L);
y = bigint_pad(y, L);
var z = [];
var inc = false;
for (var c = L - 1; c >= 0; c--) {
var digit = parseInt(x[c]) + parseInt(y[c]) + (inc ? 1 : 0)
inc = digit > 9;
z.unshift(digit % 10);
}
while (z[0] === 0) z.shift();
if (inc) z.unshift(1);
return z.join('');
}
function bigint_less(x, y) {
if (!/^[0-9]+$/.test(x) || !/^[0-9]+$/.test(y))
throw new Error("must be positive number");
if (x.length > y.length) return false;
if (x.length < y.length) return true;
for (var c = 0; c < x.length; c++) {
if (parseInt(x[c]) > parseInt(y[c])) return false;
if (parseInt(x[c]) < parseInt(y[c])) return true;
}
return false;
}
function bigint_delta(x, y) {
if (!/^[0-9]+$/.test(x) || !/^[0-9]+$/.test(y))
throw new Error("must be positive number");
if (bigint_less(x, y)) {
var x_swap = x;
x = y;
y = x_swap;
}
var L = Math.max(x.length, y.length);
x = bigint_pad(x, L);
y = bigint_pad(y, L);
var z = [];
var dec1 = false, dec2 = false;
for (var c = L - 1; c >= 0; c--) {
var xc = parseInt(x[c]);
dec2 = false;
if (dec1) {
if (xc-- < 0) {
xc += 10;
dec2 = true;
}
}
var yc = parseInt(y[c]);
var digit = xc - yc;
if (digit < 0) {
dec1 = true;
digit += 10;
}
else if (!dec2) dec1 = false;
z.unshift(digit);
}
while (z[0] === 0) z.shift();
return z.join('');
}
// For Node.js
function bigint_test() {
console.log(bigint_delta('2540160000000', '25401600000000'));
console.log(bigint_delta('2540160000000', '25401600000000') == "22861440000000");
console.log(bigint_delta('1000000000000', '1'));
console.log(bigint_delta('1000000000000', '1') == "999999999999");
}
bigint_test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment