diff --git a/dmdiagnostics.c b/dmdiagnostics.c index dcd1cb82..d37b349c 100644 --- a/dmdiagnostics.c +++ b/dmdiagnostics.c @@ -90,8 +90,8 @@ static void send_transfer_complete_event(const char *command, const char *obj_pa char fault_string[128] = {0}; unsigned fault_code = 0; - strftime(start_time, sizeof(start_time), "%Y-%m-%dT%H:%M:%SZ", localtime(&start_t)); - strftime(complete_time, sizeof(complete_time), "%Y-%m-%dT%H:%M:%SZ", localtime(&complete_t)); + strftime(start_time, sizeof(start_time), "%Y-%m-%dT%H:%M:%SZ", gmtime(&start_t)); + strftime(complete_time, sizeof(complete_time), "%Y-%m-%dT%H:%M:%SZ", gmtime(&complete_t)); if (!get_response_code_status(transfer_url, res_code)) { fault_code = USP_FAULT_GENERAL_FAILURE; diff --git a/dmtree/tr104/servicesvoiceservicesip.c b/dmtree/tr104/servicesvoiceservicesip.c index f0e64a89..85789ab4 100644 --- a/dmtree/tr104/servicesvoiceservicesip.c +++ b/dmtree/tr104/servicesvoiceservicesip.c @@ -392,7 +392,7 @@ static int get_ServicesVoiceServiceSIPClientContact_ExpireTime(char *refparam, s } time_expires = time_last + period; - if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", localtime(&time_expires)) == 0) + if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", gmtime(&time_expires)) == 0) return -1; *value = dmstrdup(buf); diff --git a/dmtree/tr181/deviceinfo.c b/dmtree/tr181/deviceinfo.c index 92aeac34..c1be9a75 100644 --- a/dmtree/tr181/deviceinfo.c +++ b/dmtree/tr181/deviceinfo.c @@ -313,7 +313,7 @@ static int get_vcf_date(char *refparam, struct dmctx *ctx, void *data, char *ins snprintf(path, sizeof(path), "%s%s", DEFAULT_CONFIG_DIR, d_file->d_name); stat(path, &attr); - strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%SZ", localtime(&attr.st_mtime)); + strftime(date, sizeof(date), "%Y-%m-%dT%H:%M:%SZ", gmtime(&attr.st_mtime)); *value = dmstrdup(date); } } diff --git a/dmtree/tr181/dhcpv4.c b/dmtree/tr181/dhcpv4.c index 139f6280..1e3bd53c 100644 --- a/dmtree/tr181/dhcpv4.c +++ b/dmtree/tr181/dhcpv4.c @@ -1714,7 +1714,7 @@ static int get_DHCPv4ServerPoolClientIPv4Address_LeaseTimeRemaining(char *refpar { const struct client_args *args = data; - return dm_time_format(args->lease->ts, value); + return dm_time_utc_format(args->lease->ts, value); } static int get_DHCPv4ServerPoolClientIPv4Address_IPAddress(char *refparam, struct dmctx *ctx, void *data, char *instance, char **value) diff --git a/dmtree/tr181/dhcpv6.c b/dmtree/tr181/dhcpv6.c index 60556682..6f82b347 100644 --- a/dmtree/tr181/dhcpv6.c +++ b/dmtree/tr181/dhcpv6.c @@ -60,7 +60,7 @@ static int get_value_in_date_time_format(json_object *json_obj, char *option_nam if (option_value && *option_value != '\0' && atoi(option_value) > 0) { time_t time_value = atoi(option_value); char s_now[sizeof "AAAA-MM-JJTHH:MM:SSZ"]; - if (strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%SZ", localtime(&time_value)) == 0) + if (strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%SZ", gmtime(&time_value)) == 0) return -1; *value = dmstrdup(s_now); // MEM WILL BE FREED IN DMMEMCLEAN } @@ -1081,7 +1081,7 @@ static int get_DHCPv6ServerPoolClientIPv6Prefix_PreferredLifetime(char *refparam if (preferred && *preferred != '\0' && atoi(preferred) > 0) { time_t time_value = atoi(preferred); char s_now[sizeof "AAAA-MM-JJTHH:MM:SSZ"]; - if (strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%SZ", localtime(&time_value)) == 0) + if (strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%SZ", gmtime(&time_value)) == 0) return -1; *value = dmstrdup(s_now); // MEM WILL BE FREED IN DMMEMCLEAN } @@ -1097,7 +1097,7 @@ static int get_DHCPv6ServerPoolClientIPv6Prefix_ValidLifetime(char *refparam, st if (valid && *valid != '\0' && atoi(valid) > 0) { time_t time_value = atoi(valid); char s_now[sizeof "AAAA-MM-JJTHH:MM:SSZ"]; - if (strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%SZ", localtime(&time_value)) == 0) + if (strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%SZ", gmtime(&time_value)) == 0) return -1; *value = dmstrdup(s_now); } diff --git a/dmtree/tr181/dynamicdns.c b/dmtree/tr181/dynamicdns.c index c876909e..e294f577 100644 --- a/dmtree/tr181/dynamicdns.c +++ b/dmtree/tr181/dynamicdns.c @@ -689,7 +689,7 @@ static int get_DynamicDNSClientHostname_LastUpdate(char *refparam, struct dmctx uptime = "0"; epoch_time = now - atoi(uptime) + atoi(last_time); - if ((ts = localtime(&epoch_time)) == NULL) + if ((ts = gmtime(&epoch_time)) == NULL) return -1; if (strftime(current_time, sizeof(current_time), "%Y-%m-%dT%H:%M:%SZ", ts) == 0) diff --git a/dmtree/tr181/ip.c b/dmtree/tr181/ip.c index 32f0d906..0677ba36 100644 --- a/dmtree/tr181/ip.c +++ b/dmtree/tr181/ip.c @@ -2052,7 +2052,7 @@ static int get_IPInterfaceIPv6Address_PreferredLifetime(char *refparam, struct d else preferred = dmjson_get_value(((struct intf_ip_args *)data)->interface_obj, 1, "preferred"); - if (preferred && *preferred && get_shift_time_time(atoi(preferred), local_time, sizeof(local_time)) == -1) + if (preferred && *preferred && get_shift_utc_time(atoi(preferred), local_time, sizeof(local_time)) == -1) return 0; *value = (*local_time) ? dmstrdup(local_time) : "9999-12-31T23:59:59Z"; @@ -2082,7 +2082,7 @@ static int get_IPInterfaceIPv6Address_ValidLifetime(char *refparam, struct dmctx else preferred = dmjson_get_value(((struct intf_ip_args *)data)->interface_obj, 1, "valid"); - if (preferred && *preferred && get_shift_time_time(atoi(preferred), local_time, sizeof(local_time)) == -1) + if (preferred && *preferred && get_shift_utc_time(atoi(preferred), local_time, sizeof(local_time)) == -1) return 0; *value = (*local_time) ? dmstrdup(local_time) : "9999-12-31T23:59:59Z"; @@ -2265,7 +2265,7 @@ static int get_IPInterfaceIPv6Prefix_PreferredLifetime(char *refparam, struct dm char local_time[32] = {0}; char *preferred = dmjson_get_value(((struct intf_ip_args *)data)->interface_obj, 1, "preferred"); - if (preferred && *preferred && get_shift_time_time(atoi(preferred), local_time, sizeof(local_time)) == -1) + if (preferred && *preferred && get_shift_utc_time(atoi(preferred), local_time, sizeof(local_time)) == -1) return 0; *value = (*local_time) ? dmstrdup(local_time) : "9999-12-31T23:59:59Z"; diff --git a/dmtree/tr181/routing.c b/dmtree/tr181/routing.c index 18ebf6be..31fadd8e 100644 --- a/dmtree/tr181/routing.c +++ b/dmtree/tr181/routing.c @@ -942,7 +942,7 @@ static int get_RoutingRouteInformationInterfaceSetting_RouteLifetime(char *refpa if (valid && *valid != '\0' && atoi(valid) > 0) { char local_time[32] = {0}; - if (get_shift_time_time(atoi(valid), local_time, sizeof(local_time)) == -1) + if (get_shift_utc_time(atoi(valid), local_time, sizeof(local_time)) == -1) return 0; *value = dmstrdup(local_time); } diff --git a/dmtree/tr181/security.c b/dmtree/tr181/security.c index 5609a4a5..a7d05a22 100644 --- a/dmtree/tr181/security.c +++ b/dmtree/tr181/security.c @@ -331,7 +331,7 @@ static int get_SecurityCertificate_LastModif(char *refparam, struct dmctx *ctx, struct stat b; char t[sizeof("AAAA-MM-JJTHH:MM:SSZ")] = "0001-01-01T00:00:00Z"; if (!stat(cert_profile->path, &b)) - strftime(t, sizeof(t), "%Y-%m-%dT%H:%M:%SZ", localtime(&b.st_mtime)); + strftime(t, sizeof(t), "%Y-%m-%dT%H:%M:%SZ", gmtime(&b.st_mtime)); *value = dmstrdup(t); return 0; } diff --git a/dmtree/tr181/wifi.c b/dmtree/tr181/wifi.c index 17f77e88..0e885b12 100644 --- a/dmtree/tr181/wifi.c +++ b/dmtree/tr181/wifi.c @@ -2709,15 +2709,15 @@ static int get_WiFiAccessPointAssociatedDevice_AssociationTime(char *refparam, s char *in_network = dmjson_get_value((json_object *)data, 1, "in_network"); if (in_network && *in_network != '\0' && atoi(in_network) > 0) { time_t t_time = time(NULL) - atoi(in_network); - if (localtime(&t_time) == NULL) + if (gmtime(&t_time) == NULL) return -1; - char local_time[32] = {0}; + char utc_time[32] = {0}; - if (strftime(local_time, sizeof(local_time), "%Y-%m-%dT%H:%M:%SZ", localtime(&t_time)) == 0) + if (strftime(utc_time, sizeof(utc_time), "%Y-%m-%dT%H:%M:%SZ", gmtime(&t_time)) == 0) return -1; - *value = dmstrdup(local_time); + *value = dmstrdup(utc_time); } return 0; } diff --git a/libbbf_api/dmcommon.c b/libbbf_api/dmcommon.c index a1bec7f7..a9e51787 100644 --- a/libbbf_api/dmcommon.c +++ b/libbbf_api/dmcommon.c @@ -931,6 +931,22 @@ bool elt_exists_in_array(char **str_array, char *str, int length) return false; } +int get_shift_utc_time(int shift_time, char *utc_time, int size) +{ + struct tm *t_tm; + time_t now = time(NULL); + + now = now + shift_time; + t_tm = gmtime(&now); + if (t_tm == NULL) + return -1; + + if (strftime(utc_time, size, "%Y-%m-%dT%H:%M:%SZ", t_tm) == 0) + return -1; + + return 0; +} + int get_shift_time_time(int shift_time, char *local_time, int size) { time_t t_time; @@ -1047,6 +1063,24 @@ int get_net_iface_sysfs(const char *uci_iface, const char *name, char **value) return get_net_device_sysfs(device, name, value); } +int dm_time_utc_format(time_t ts, char **dst) +{ + char time_buf[32] = { 0, 0 }; + struct tm *t_tm; + + *dst = "0001-01-01T00:00:00Z"; + + t_tm = gmtime(&ts); + if (t_tm == NULL) + return -1; + + if(strftime(time_buf, sizeof(time_buf), "%Y-%m-%dT%H:%M:%SZ", t_tm) == 0) + return -1; + + *dst = dmstrdup(time_buf); + return 0; +} + int dm_time_format(time_t ts, char **dst) { char time_buf[32] = { 0, 0 };