timemngr: Added support for SupportedZones object

(cherry picked from commit fea3e1e4ef)

fea3e1e4 timemngr: Added support for SupportedZones object

Co-authored-by: Amin Ben Romdhane <amin.benromdhane@iopsys.eu>
This commit is contained in:
Vivek Dutta 2025-10-29 21:04:20 +05:30 committed by Vivek Kumar Dutta
parent 38d8d01f27
commit ac0b85f837
No known key found for this signature in database
GPG key ID: 4E09F5AD8265FD4C
4 changed files with 96 additions and 2 deletions

View file

@ -5,13 +5,13 @@
include $(TOPDIR)/rules.mk include $(TOPDIR)/rules.mk
PKG_NAME:=timemngr PKG_NAME:=timemngr
PKG_VERSION:=1.1.9 PKG_VERSION:=1.1.10
LOCAL_DEV:=0 LOCAL_DEV:=0
ifneq ($(LOCAL_DEV),1) ifneq ($(LOCAL_DEV),1)
PKG_SOURCE_PROTO:=git PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/timemngr.git PKG_SOURCE_URL:=https://dev.iopsys.eu/bbf/timemngr.git
PKG_SOURCE_VERSION:=c0c15beee2b60925f51b8ba78be516d2f5536c65 PKG_SOURCE_VERSION:=2da5a2a1270d59fed49d576afda0a6ffab264576
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz
PKG_MIRROR_HASH:=skip PKG_MIRROR_HASH:=skip
endif endif
@ -81,6 +81,7 @@ endif
$(INSTALL_DATA) ./files/etc/config/time $(1)/etc/config/ $(INSTALL_DATA) ./files/etc/config/time $(1)/etc/config/
ifeq ($(CONFIG_TIMEMNGR_NTPD),y) ifeq ($(CONFIG_TIMEMNGR_NTPD),y)
$(CP) ./files/etc/timemngr/ntpd_config.sh $(1)/etc/timemngr/time.sh $(CP) ./files/etc/timemngr/ntpd_config.sh $(1)/etc/timemngr/time.sh
$(CP) ./files/etc/timemngr/generate_supported_zones.sh $(1)/etc/timemngr/generate_supported_zones.sh
endif endif
endif endif

View file

@ -2,3 +2,4 @@ config global 'global'
option enable '1' option enable '1'
option dst '1' option dst '1'
option loglevel '2' option loglevel '2'
option supported_zones_file '/etc/timemngr/supported_zones.json'

View file

@ -4,10 +4,13 @@ START=20
STOP=01 STOP=01
. /etc/timemngr/time.sh . /etc/timemngr/time.sh
. /etc/timemngr/generate_supported_zones.sh
USE_PROCD=1 USE_PROCD=1
PROG_UBUS=/usr/sbin/timemngr PROG_UBUS=/usr/sbin/timemngr
DEFAULT_SUPPORTED_ZONES_PATH="/etc/timemngr/supported_zones.json"
log() { log() {
echo "${@}"|logger -t timemngr.init -p info echo "${@}"|logger -t timemngr.init -p info
} }
@ -31,6 +34,20 @@ boot() {
sed -i '/#timemngr-dst/d' /etc/crontabs/root sed -i '/#timemngr-dst/d' /etc/crontabs/root
fi fi
config_get file_path global supported_zones_file
# If option is not configured, use the default path
if [ -z "${file_path}" ]; then
file_path="${DEFAULT_SUPPORTED_ZONES_PATH}"
uci -q set time.global.supported_time_zone_file="${DEFAULT_SUPPORTED_ZONES_PATH}"
uci -q commit time
fi
# If the JSON file doesn't exist, generate it
if [ ! -f "${file_path}" ]; then
generate_supported_zones > "${file_path}"
fi
start start
} }

View file

@ -0,0 +1,75 @@
#!/bin/sh
. /usr/share/libubox/jshn.sh
# Default temporary file for timezone mapping
TMP_MAP="/tmp/timezone_map.txt"
# -----------------------------------------------------------------------------
# Function: generate_supported_zones
# Description: Generates a JSON array of supported zones in the format:
# [
# { "time_zone": "TZ_STRING", "zone_name": "Region/City,Region/City" },
# ...
# ]
# Output: Prints the JSON to stdout (caller can redirect to a file)
# -----------------------------------------------------------------------------
generate_supported_zones() {
: > "$TMP_MAP"
# Step 1: Collect zonename -> timezone mappings into TMP_MAP
for entry in /usr/share/zoneinfo/*; do
[ -d "$entry" ] || continue
region=$(basename "$entry")
for zonefile in "$entry"/*; do
[ -f "$zonefile" ] || continue
city=$(basename "$zonefile")
zonename="${region}/${city}"
tz_string=$(tail -n 1 "$zonefile" 2>/dev/null)
[ -n "$tz_string" ] && echo "$tz_string|$zonename" >> "$TMP_MAP"
done
done
# Step 2: Group zone names by tz_string and format to intermediate file
TMP_LINES="/tmp/timezone_lines.txt"
awk -F'|' '
{
tz = $1
zn = $2
if (tz in tzmap) {
tzmap[tz] = tzmap[tz] "," zn
} else {
tzmap[tz] = zn
}
}
END {
for (tz in tzmap) {
printf("TZSEP%sSEPZN%s\n", tz, tzmap[tz])
}
}
' "$TMP_MAP" > "$TMP_LINES"
# Step 3: Convert the grouped result to JSON output
json_init
json_add_array "supported_zones"
while IFS= read -r line; do
timezone=$(echo "$line" | sed -n 's/^TZSEP\(.*\)SEPZN.*/\1/p')
zonenames=$(echo "$line" | sed -n 's/^TZSEP.*SEPZN\(.*\)/\1/p')
[ -n "$timezone" ] || continue
json_add_object ""
json_add_string "time_zone" "$timezone"
json_add_string "zone_name" "$zonenames"
json_close_object
done < "$TMP_LINES"
json_close_array
json_dump
# Cleanup
rm -f "$TMP_MAP" "$TMP_LINES"
}