time: normalize GetNetworkTime() response to local time + timezone info (bgo #697372)
The GetNetworkTime() response is defined to be an ISO8601 string, which is in turn defined to be in local time. Make sure that's reflected in the documentation, and append the timezone offset to UTC where we have it. Oddly, Icera devices return their time info in UTC with an offset to the local timezone, so we have to jump through some hoops there to convert the response to localtime based on the reported offset. Some additional fixes by Aleksander Morgado <aleksander@lanedo.com>. https://bugzilla.gnome.org/show_bug.cgi?id=697372
This commit is contained in:
@@ -1493,7 +1493,9 @@ parse_tlts_query_reply (const gchar *response,
|
||||
gint second;
|
||||
gchar sign;
|
||||
gint offset;
|
||||
GDateTime *utc, *adjusted;
|
||||
|
||||
/* TLTS reports UTC time with the TZ offset to *local* time */
|
||||
response = mm_strip_tag (response, "*TLTS: ");
|
||||
if (sscanf (response,
|
||||
"\"%02d/%02d/%02d,%02d:%02d:%02d%c%02d\"",
|
||||
@@ -1504,42 +1506,70 @@ parse_tlts_query_reply (const gchar *response,
|
||||
&minute,
|
||||
&second,
|
||||
&sign,
|
||||
&offset) == 8) {
|
||||
/* Offset comes in 15-min intervals */
|
||||
offset *= 15;
|
||||
/* Apply sign to offset */
|
||||
if (sign == '-')
|
||||
offset *= -1;
|
||||
|
||||
/* If asked for it, build timezone information */
|
||||
if (tz) {
|
||||
*tz = mm_network_timezone_new ();
|
||||
mm_network_timezone_set_offset (*tz, offset);
|
||||
}
|
||||
|
||||
if (iso8601) {
|
||||
/* Icera modems only report a 2-digit year, while ISO-8601 requires
|
||||
* a 4-digit year. Assume 2000.
|
||||
*/
|
||||
if (year < 100)
|
||||
year += 2000;
|
||||
|
||||
/* don't give tz info in the date/time string, we have another
|
||||
* property for that */
|
||||
*iso8601 = g_strdup_printf ("%04d/%02d/%02d %02d:%02d:%02d",
|
||||
year, month, day,
|
||||
hour, minute, second);
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
&offset) != 8) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Unknown *TLTS response: %s",
|
||||
response);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Unknown *TLTS response: %s",
|
||||
response);
|
||||
return FALSE;
|
||||
/* Icera modems only report a 2-digit year, while ISO-8601 requires
|
||||
* a 4-digit year. Assume 2000.
|
||||
*/
|
||||
if (year < 100)
|
||||
year += 2000;
|
||||
|
||||
/* Offset comes in 15-min units */
|
||||
offset *= 15;
|
||||
/* Apply sign to offset; */
|
||||
if (sign == '-')
|
||||
offset *= -1;
|
||||
|
||||
utc = g_date_time_new_utc (year, month, day, hour, minute, second);
|
||||
if (!utc) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Invalid *TLTS date/time: %s",
|
||||
response);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Convert UTC time to local time by adjusting by the timezone offset */
|
||||
adjusted = g_date_time_add_minutes (utc, offset);
|
||||
g_date_time_unref (utc);
|
||||
if (!adjusted) {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
MM_CORE_ERROR_FAILED,
|
||||
"Failed to convert modem time to local time (offset %d)",
|
||||
offset);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/* Convert offset from minutes-to-UTC to minutes-from-UTC */
|
||||
offset *= -1;
|
||||
|
||||
if (tz) {
|
||||
*tz = mm_network_timezone_new ();
|
||||
mm_network_timezone_set_offset (*tz, offset);
|
||||
}
|
||||
|
||||
if (iso8601) {
|
||||
*iso8601 = mm_new_iso8601_time (g_date_time_get_year (adjusted),
|
||||
g_date_time_get_month (adjusted),
|
||||
g_date_time_get_day_of_month (adjusted),
|
||||
g_date_time_get_hour (adjusted),
|
||||
g_date_time_get_minute (adjusted),
|
||||
g_date_time_get_second (adjusted),
|
||||
TRUE,
|
||||
offset);
|
||||
}
|
||||
|
||||
g_date_time_unref (adjusted);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static MMNetworkTimezone *
|
||||
|
||||
@@ -1012,9 +1012,8 @@ parse_nwltime_reply (const char *response,
|
||||
mm_get_uint_from_match_info (match_info, 6, &second) &&
|
||||
mm_get_int_from_match_info (match_info, 8, &utc_offset)) {
|
||||
|
||||
/* Return ISO-8601 format date/time string */
|
||||
result = g_strdup_printf ("%04d/%02d/%02d %02d:%02d:%02d",
|
||||
year, month, day, hour, minute, second);
|
||||
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);
|
||||
|
||||
@@ -1337,9 +1337,7 @@ parse_time (const gchar *response,
|
||||
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)) {
|
||||
/* Return ISO-8601 format date/time string */
|
||||
result = g_strdup_printf ("%04d/%02d/%02d %02d:%02d:%02d",
|
||||
year, month, day, hour, minute, second);
|
||||
result = mm_new_iso8601_time (year, month, day, hour, minute, second, FALSE, 0);
|
||||
} else {
|
||||
g_set_error (error,
|
||||
MM_CORE_ERROR,
|
||||
|
||||
Reference in New Issue
Block a user