luci-mod-system: use localized time

use localized time

Signed-off-by: Andy Chiang <AndyChiang_git@outlook.com>
This commit is contained in:
Andy Chiang
2025-10-16 00:48:45 +07:00
committed by Paul Donald
parent e67465da5e
commit b0004ad39c
2 changed files with 29 additions and 21 deletions

View File

@@ -8,7 +8,7 @@
'require tools.widgets as widgets'; 'require tools.widgets as widgets';
var callRcList, callRcInit, callTimezone, var callRcList, callRcInit, callTimezone,
callGetLocaltime, callSetLocaltime, CBILocalTime; callGetUnixtime, callSetLocaltime, CBILocalTime;
callRcList = rpc.declare({ callRcList = rpc.declare({
object: 'rc', object: 'rc',
@@ -29,10 +29,10 @@ callRcInit = rpc.declare({
expect: { result: false } expect: { result: false }
}); });
callGetLocaltime = rpc.declare({ callGetUnixtime = rpc.declare({
object: 'system', object: 'luci',
method: 'info', method: 'getUnixtime',
expect: { localtime: 0 } expect: { result: 0 }
}); });
callSetLocaltime = rpc.declare({ callSetLocaltime = rpc.declare({
@@ -49,16 +49,17 @@ callTimezone = rpc.declare({
}); });
function formatTime(epoch) { function formatTime(epoch) {
var date = new Date(epoch * 1000); var date = new Date(epoch * 1000),
zn = uci.get('system', '@system[0]', 'zonename')?.replaceAll(' ', '_') || 'UTC',
ts = uci.get('system', '@system[0]', 'clock_timestyle'),
hc = uci.get('system', '@system[0]', 'clock_hourcycle');
return '%04d-%02d-%02d %02d:%02d:%02d'.format( return new Intl.DateTimeFormat(undefined, {
date.getUTCFullYear(), dateStyle: 'medium',
date.getUTCMonth() + 1, timeStyle: (ts == 0) ? 'long' : 'full',
date.getUTCDate(), hourCycle: hc,
date.getUTCHours(), timeZone: zn
date.getUTCMinutes(), }).format(date);
date.getUTCSeconds()
);
} }
CBILocalTime = form.DummyValue.extend({ CBILocalTime = form.DummyValue.extend({
@@ -97,7 +98,7 @@ return view.extend({
return Promise.all([ return Promise.all([
callRcList('sysntpd'), callRcList('sysntpd'),
callTimezone(), callTimezone(),
callGetLocaltime(), callGetUnixtime(),
uci.load('luci'), uci.load('luci'),
uci.load('system') uci.load('system')
]); ]);
@@ -106,7 +107,7 @@ return view.extend({
render: function(rpc_replies) { render: function(rpc_replies) {
var ntpd_enabled = rpc_replies[0], var ntpd_enabled = rpc_replies[0],
timezones = rpc_replies[1], timezones = rpc_replies[1],
localtime = rpc_replies[2], unixtime = rpc_replies[2],
m, s, o; m, s, o;
m = new form.Map('system', m = new form.Map('system',
@@ -129,7 +130,7 @@ return view.extend({
*/ */
o = s.taboption('general', CBILocalTime, '_systime', _('Local Time')); o = s.taboption('general', CBILocalTime, '_systime', _('Local Time'));
o.cfgvalue = function() { return localtime }; o.cfgvalue = function() { return unixtime };
o.ntpd_support = ntpd_enabled; o.ntpd_support = ntpd_enabled;
o = s.taboption('general', form.Value, 'hostname', _('Hostname')); o = s.taboption('general', form.Value, 'hostname', _('Hostname'));
@@ -155,6 +156,14 @@ return view.extend({
uci.set('system', section_id, 'timezone', tz); uci.set('system', section_id, 'timezone', tz);
}; };
o = s.taboption('general', form.Flag, 'clock_timestyle', _('Full TimeZone Name'), _('Unchecked means the timezone offset (E.g. GMT+1) is displayed'));
o.default = o.enabled;
o = s.taboption('general', form.ListValue, 'clock_hourcycle', _('Time Format'));
o.value('', _('Default'));
o.value('h12', _('12-Hour Clock'));
o.value('h23', _('24-Hour Clock'));
/* /*
* Logging * Logging
*/ */
@@ -309,7 +318,7 @@ return view.extend({
return m.render().then(function(mapEl) { return m.render().then(function(mapEl) {
poll.add(function() { poll.add(function() {
return callGetLocaltime().then(function(t) { return callGetUnixtime().then(function(t) {
mapEl.querySelector('#localtime').value = formatTime(t); mapEl.querySelector('#localtime').value = formatTime(t);
}); });
}); });

View File

@@ -3,9 +3,8 @@
"description": "Grant access to system configuration", "description": "Grant access to system configuration",
"read": { "read": {
"ubus": { "ubus": {
"luci": [ "getLEDs", "getTimezones", "getUSBDevices" ], "luci": [ "getLEDs", "getTimezones", "getUSBDevices", "getUnixtime" ],
"rc": [ "list" ], "rc": [ "list" ]
"system": [ "info" ]
}, },
"uci": [ "luci", "system" ] "uci": [ "luci", "system" ]
}, },