MMNetworkTimezone * mm_network_timezone_new_from_dictionary (GVariant *dictionary, GError **error) { GError *inner_error = NULL; GVariantIter iter; gchar *key; GVariant *value; MMNetworkTimezone *self; self = mm_network_timezone_new (); if (!dictionary) return self; if (!g_variant_is_of_type (dictionary, G_VARIANT_TYPE ("a{sv}"))) { g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, "Cannot create Network Timezone from dictionary: " "invalid variant type received"); g_object_unref (self); return NULL; } g_variant_iter_init (&iter, dictionary); while (!inner_error && g_variant_iter_next (&iter, "{sv}", &key, &value)) { /* All currently supported properties are signed integers, * so we just check the value type here */ if (!g_variant_is_of_type (value, G_VARIANT_TYPE_INT32)) { /* Set inner error, will stop the loop */ inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, "Invalid status dictionary, unexpected value type '%s'", g_variant_get_type_string (value)); } else if (g_str_equal (key, "offset")) self->priv->offset = g_variant_get_int32 (value); else if (g_str_equal (key, "dst-offset")) self->priv->dst_offset = g_variant_get_int32 (value); else if (g_str_equal (key, "leap-seconds")) self->priv->leap_seconds = g_variant_get_int32 (value); else { /* Set inner error, will stop the loop */ inner_error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_INVALID_ARGS, "Invalid status dictionary, unexpected key '%s'", key); } g_free (key); g_variant_unref (value); } /* If error, destroy the object */ if (inner_error) { g_propagate_error (error, inner_error); g_object_unref (self); return NULL; } return self; }
static gboolean parse_nwltime_reply (const char *response, gchar **out_iso_8601, MMNetworkTimezone **out_tz, GError **error) { GRegex *r; GMatchInfo *match_info = NULL; GError *match_error = NULL; guint year, month, day, hour, minute, second; gchar *result = NULL; gint utc_offset = 0; gboolean success = FALSE; /* Sample reply: 2013.3.27.15.47.19.2.-5 */ r = g_regex_new ("(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.(\\d+)\\.([\\-\\+\\d]+)$", 0, 0, NULL); g_assert (r != NULL); if (!g_regex_match_full (r, response, -1, 0, 0, &match_info, &match_error)) { if (match_error) { g_propagate_error (error, match_error); g_prefix_error (error, "Could not parse $NWLTIME results: "); } else { g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Couldn't match $NWLTIME reply"); } } else { /* Remember that g_match_info_get_match_count() includes match #0 */ g_assert (g_match_info_get_match_count (match_info) >= 9); if (mm_get_uint_from_match_info (match_info, 1, &year) && mm_get_uint_from_match_info (match_info, 2, &month) && mm_get_uint_from_match_info (match_info, 3, &day) && mm_get_uint_from_match_info (match_info, 4, &hour) && mm_get_uint_from_match_info (match_info, 5, &minute) && mm_get_uint_from_match_info (match_info, 6, &second) && mm_get_int_from_match_info (match_info, 8, &utc_offset)) { result = mm_new_iso8601_time (year, month, day, hour, minute, second, TRUE, utc_offset * 60); if (out_tz) { *out_tz = mm_network_timezone_new (); mm_network_timezone_set_offset (*out_tz, utc_offset * 60); } success = TRUE; } else { g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Failed to parse $NWLTIME reply"); } } if (out_iso_8601) *out_iso_8601 = result; else g_free (result); if (match_info) g_match_info_free (match_info); g_regex_unref (r); return success; }
gboolean mm_huawei_parse_nwtime_response (const gchar *response, gchar **iso8601p, MMNetworkTimezone **tzp, GError **error) { GRegex *r; GMatchInfo *match_info = NULL; GError *match_error = NULL; guint year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, dt = 0; gint tz = 0; gboolean ret = FALSE; g_assert (iso8601p || tzp); /* at least one */ r = g_regex_new ("\\^NWTIME:\\s*(\\d+)/(\\d+)/(\\d+),(\\d+):(\\d+):(\\d*)([\\-\\+\\d]+),(\\d+)$", 0, 0, NULL); g_assert (r != NULL); if (!g_regex_match_full (r, response, -1, 0, 0, &match_info, &match_error)) { if (match_error) { g_propagate_error (error, match_error); g_prefix_error (error, "Could not parse ^NWTIME results: "); } else { g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Couldn't match ^NWTIME reply"); } } else { /* Remember that g_match_info_get_match_count() includes match #0 */ g_assert (g_match_info_get_match_count (match_info) >= 9); if (mm_get_uint_from_match_info (match_info, 1, &year) && mm_get_uint_from_match_info (match_info, 2, &month) && mm_get_uint_from_match_info (match_info, 3, &day) && mm_get_uint_from_match_info (match_info, 4, &hour) && mm_get_uint_from_match_info (match_info, 5, &minute) && mm_get_uint_from_match_info (match_info, 6, &second) && mm_get_int_from_match_info (match_info, 7, &tz) && mm_get_uint_from_match_info (match_info, 8, &dt)) { /* adjust year */ if (year < 100) year += 2000; /* * tz = timezone offset in 15 minute intervals * dt = daylight adjustment, 0 = none, 1 = 1 hour, 2 = 2 hours * other values are marked reserved. */ if (iso8601p) { /* Return ISO-8601 format date/time string */ *iso8601p = mm_new_iso8601_time (year, month, day, hour, minute, second, TRUE, (tz * 15) + (dt * 60)); } if (tzp) { *tzp = mm_network_timezone_new (); mm_network_timezone_set_offset (*tzp, tz * 15); mm_network_timezone_set_dst_offset (*tzp, dt * 60); } ret = TRUE; } else { g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Failed to parse ^NWTIME reply"); } } if (match_info) g_match_info_free (match_info); g_regex_unref (r); return ret; }