JavaScript "intlib"

         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
import {
exec_unify, exec_build, check_atom, is_pending,
check_integer, exec_deref, is_variable,
set, norm_smallint, sys_time_parse,
make_error, make_check, narrow_float, Compound
} from "../../nova/core.mjs";
/*********************************************************************/
/* os_atom_time/4 */
/*********************************************************************/
/**
* os_atom_time(A, F, O, T): internal only
* If A is a variable, the built-in succeeds in A with the millisecond
* time T formatted by the pattern F and the flag O. Otherwise the
* built-in succeeds in T with the millisecond time parsed from A
* by the pattern F and the flag O.
*/
function test_os_atom_time(args) {
let fmt = exec_build(args[1]);
check_atom(fmt);
let flag = exec_build(args[2]);
check_integer(flag);
let text = exec_deref(args[0]);
if (is_variable(text) || is_pending(text)) {
let tms = exec_build(args[3]);
check_integer(tms);
let res = sys_time_format(fmt, narrow_float(tms), flag);
if (res === null)
throw make_error(new Compound("domain_error",
["time_format", fmt]));
return exec_unify(text, res);
} else {
text = exec_build(text);
check_atom(text);
let tms = sys_time_parse(text, fmt, flag);
if (isNaN(tms))
throw make_error(new Compound("syntax_error", ["illegal_date"]));
if (tms === null)
throw make_error(new Compound("domain_error",
["time_format", fmt]));
return exec_unify(args[3], norm_smallint(tms));
}
}
/**
* <p>Format a date time string.</p>
*
* @param pat The format pattern.
* @param tms The date time in UTC milliseconds.
* @param utc The UTC calendar flag.
* @return The date time as a string.
*/
function sys_time_format(pat, tms, utc) {
let date = new Date(tms);
let buf = "";
let k = 0;
for (let i = 0; i < pat.length; i++) {
let ch = pat.charCodeAt(i);
if (ch === 37) { // %
buf += pat.substring(k, i);
i++;
if (i < pat.length) {
switch (pat.charCodeAt(i)) {
case 37: // %
buf += "%";
break;
case 97: // a
let f;
if (utc !== 0) {
f = new Intl.DateTimeFormat("en-US", {timeZone: "UTC", weekday: "short"});
} else {
f = new Intl.DateTimeFormat(undefined, {weekday: "short"});
}
buf += f.format(date);
break;
case 98: // b
let g;
if (utc !== 0) {
g = new Intl.DateTimeFormat("en-US", {timeZone: "UTC", month: "short"});
} else {
g = new Intl.DateTimeFormat(undefined, {month: "short"});
}
buf += g.format(date);
break;
case 100: // d
buf = append_padded(buf, (utc !== 0 ? date.getUTCDate():date.getDate()));
break;
case 109: // m
buf = append_padded(buf, (utc !== 0 ? date.getUTCMonth():date.getMonth())+1);
break;
case 89: // Y
buf += (utc !== 0 ? date.getUTCFullYear():date.getFullYear());
break;
case 72: // H
buf = append_padded(buf, (utc !== 0 ? date.getUTCHours():date.getHours()));
break;
case 77: // M
buf = append_padded(buf, (utc !== 0 ? date.getUTCMinutes():date.getMinutes()));
break;
case 83: // S
buf = append_padded(buf, (utc !== 0 ? date.getUTCSeconds():date.getSeconds()));
break;
default:
return null;
}
k = i + 1;
} else {
k = i;
}
}
}
buf += pat.substring(k, pat.length);
return buf;
}
/**
* Append an integer value zero padded.
*
* @param buf The string builder.
* @param value The integer value.
* @return The string builder.
*/
function append_padded(buf, value) {
if (value < 10)
buf += '0';
buf += value.toString();
return buf;
}
/*********************************************************************/
/* Int Lib Init */
/*********************************************************************/
export function main() {
set("os_atom_time", 4, make_check(test_os_atom_time));
}