mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
43 lines
2.1 KiB
JavaScript
43 lines
2.1 KiB
JavaScript
function round(n, dp) {
|
|
if (dp===undefined) dp=1;
|
|
var p = Math.min(dp,dp - Math.floor(Math.log(n)/Math.log(10)));
|
|
return n.toFixed(p);
|
|
}
|
|
var _is12Hours;
|
|
function is12Hours() {
|
|
if (_is12Hours === undefined) _is12Hours = (require('Storage').readJSON('setting.json',1)||{})["12hour"];
|
|
return _is12Hours;
|
|
}
|
|
exports = { name : "system", currencySym:"£",
|
|
translate : str=>str, // as-is
|
|
date : (d,short) => short?("0"+d.getDate()).substr(-2)+"/"+("0"+(d.getMonth()+1)).substr(-2)+"/"+d.getFullYear():d.toString().substr(4,11).trim(), // Date to "Feb 28 2020" or "28/02/2020"(short)
|
|
time : (d,short) => { // Date to "4:15.28" or "15:42"(short)
|
|
var h = d.getHours(), m = d.getMinutes()
|
|
if (is12Hours())
|
|
h = (h%12==0) ? 12 : h%12; // 12 hour
|
|
return (" "+h).substr(-2)+":"+("0"+m).substr(-2) + (short?"":("."+("0"+d.getSeconds()).substr(-2)));
|
|
},
|
|
dow : (d,short) => "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday".split(",")[d.getDay()].substr(0, short ? 3 : 10), // Date to "Monday" or "Mon"(short)
|
|
month : (d,short) => "January,February,March,April,May,June,July,August,September,October,November,December".split(",")[d.getMonth()].substr(0, short ? 3 : 10), // Date to "February" or "Feb"(short)
|
|
number: (n, dec) => {
|
|
if (dec == null) dec = 2;
|
|
var w = n.toFixed(dec),
|
|
k = w|0,
|
|
b = n < 0 ? 1 : 0,
|
|
u = Math.abs(w-k),
|
|
d = (''+u.toFixed(dec)).substr(2, dec),
|
|
s = ''+k,
|
|
i = s.length,
|
|
r = '';
|
|
while ((i-=3) > b)
|
|
r = ',' + s.substr(i, 3) + r;
|
|
return s.substr(0, i + 3) + r + (d ? '.' + d: '');
|
|
},
|
|
currency : n => {console.log("Warning: Currency information is deprecated");return "£"+n.toFixed(2)}, // number to "£1.00"
|
|
distance : (m,dp) => (m<1000)?round(m,dp)+"m":round(m/1000,dp)+"km", // meters to "123m" or "1.2km" depending on size
|
|
speed : (s,dp) => round(s/1.60934,dp)+"mph",// kph to "123mph"
|
|
temp : (t,dp) => round(t,dp)+"'C", // degrees C to degrees C
|
|
meridian: (d,force) => (force||is12Hours()) ? (d.getHours() <= 12) ? "am":"pm" : "", // Date to am/pm
|
|
is12Hours,
|
|
};
|