From ac0b85f837b152b9c3590f7e7b4984667c70c4fd Mon Sep 17 00:00:00 2001 From: Vivek Dutta Date: Wed, 29 Oct 2025 21:04:20 +0530 Subject: [PATCH] timemngr: Added support for SupportedZones object (cherry picked from commit fea3e1e4ef489830357317341c25937b021cb168) fea3e1e4 timemngr: Added support for SupportedZones object Co-authored-by: Amin Ben Romdhane --- timemngr/Makefile | 5 +- timemngr/files/etc/config/time | 1 + timemngr/files/etc/init.d/timemngr | 17 +++++ .../etc/timemngr/generate_supported_zones.sh | 75 +++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100755 timemngr/files/etc/timemngr/generate_supported_zones.sh diff --git a/timemngr/Makefile b/timemngr/Makefile index 1f01b2f01..76526976c 100644 --- a/timemngr/Makefile +++ b/timemngr/Makefile @@ -5,13 +5,13 @@ include $(TOPDIR)/rules.mk PKG_NAME:=timemngr -PKG_VERSION:=1.1.9 +PKG_VERSION:=1.1.10 LOCAL_DEV:=0 ifneq ($(LOCAL_DEV),1) PKG_SOURCE_PROTO:=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_MIRROR_HASH:=skip endif @@ -81,6 +81,7 @@ endif $(INSTALL_DATA) ./files/etc/config/time $(1)/etc/config/ ifeq ($(CONFIG_TIMEMNGR_NTPD),y) $(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 diff --git a/timemngr/files/etc/config/time b/timemngr/files/etc/config/time index 07b12e6e7..0e24753b3 100644 --- a/timemngr/files/etc/config/time +++ b/timemngr/files/etc/config/time @@ -2,3 +2,4 @@ config global 'global' option enable '1' option dst '1' option loglevel '2' + option supported_zones_file '/etc/timemngr/supported_zones.json' diff --git a/timemngr/files/etc/init.d/timemngr b/timemngr/files/etc/init.d/timemngr index f1938ccbb..0b14ec669 100755 --- a/timemngr/files/etc/init.d/timemngr +++ b/timemngr/files/etc/init.d/timemngr @@ -4,10 +4,13 @@ START=20 STOP=01 . /etc/timemngr/time.sh +. /etc/timemngr/generate_supported_zones.sh USE_PROCD=1 PROG_UBUS=/usr/sbin/timemngr +DEFAULT_SUPPORTED_ZONES_PATH="/etc/timemngr/supported_zones.json" + log() { echo "${@}"|logger -t timemngr.init -p info } @@ -30,6 +33,20 @@ boot() { else sed -i '/#timemngr-dst/d' /etc/crontabs/root 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 } diff --git a/timemngr/files/etc/timemngr/generate_supported_zones.sh b/timemngr/files/etc/timemngr/generate_supported_zones.sh new file mode 100755 index 000000000..fc241221a --- /dev/null +++ b/timemngr/files/etc/timemngr/generate_supported_zones.sh @@ -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" +} +