mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-07-17 15:17:17 +00:00
src: remove use of g_date_time_new_from_iso8601 function
The g_date_time_new_from_iso8601() function was introduced as a replacement for strptime in commit810613a60e
Author: Daniel P. Berrangé <berrange@redhat.com> Date: Mon Dec 23 15:37:26 2019 +0000 src: replace strptime()/timegm()/mktime() with GDateTime APIs set Unfortunately g_date_time_new_from_iso8601 isn't available until glib 2.56, and backporting it requires alot of code copying and poking at private glib structs. This reverts domain_conf.c back to its original parsing logic prior to810613a60e
, but using g_date_time_new() instead of gmtime(). The other files are then adapted to follow a similar approach. Reviewed-by: Pavel Hrdina <phrdina@redhat.com> Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
parent
d6f7d1a4d6
commit
fcea025d0e
@ -13675,15 +13675,31 @@ virDomainGraphicsAuthDefParseXML(xmlNodePtr node,
|
|||||||
if (validTo) {
|
if (validTo) {
|
||||||
g_autoptr(GDateTime) then = NULL;
|
g_autoptr(GDateTime) then = NULL;
|
||||||
g_autoptr(GTimeZone) tz = g_time_zone_new_utc();
|
g_autoptr(GTimeZone) tz = g_time_zone_new_utc();
|
||||||
|
char *tmp;
|
||||||
|
int year, mon, mday, hour, min, sec;
|
||||||
|
|
||||||
then = g_date_time_new_from_iso8601(validTo, tz);
|
/* Expect: YYYY-MM-DDTHH:MM:SS (%d-%d-%dT%d:%d:%d) eg 2010-11-28T14:29:01 */
|
||||||
if (!then) {
|
if (/* year */
|
||||||
virReportError(VIR_ERR_INVALID_ARG,
|
virStrToLong_i(validTo, &tmp, 10, &year) < 0 || *tmp != '-' ||
|
||||||
_("password validity time '%s' values out of range"), validTo);
|
/* month */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &mon) < 0 || *tmp != '-' ||
|
||||||
|
/* day */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &mday) < 0 || *tmp != 'T' ||
|
||||||
|
/* hour */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &hour) < 0 || *tmp != ':' ||
|
||||||
|
/* minute */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &min) < 0 || *tmp != ':' ||
|
||||||
|
/* second */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &sec) < 0 || *tmp != '\0') {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("cannot parse password validity time '%s', expect YYYY-MM-DDTHH:MM:SS"),
|
||||||
|
validTo);
|
||||||
|
VIR_FREE(def->passwd);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
def->validTo = (int)g_date_time_to_unix(then);
|
then = g_date_time_new(tz, year, mon, mday, hour, min, sec);
|
||||||
|
def->validTo = (time_t)g_date_time_to_unix(then);
|
||||||
def->expires = true;
|
def->expires = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1473,8 +1473,10 @@ int
|
|||||||
esxVI_DateTime_ConvertToCalendarTime(esxVI_DateTime *dateTime,
|
esxVI_DateTime_ConvertToCalendarTime(esxVI_DateTime *dateTime,
|
||||||
long long *secondsSinceEpoch)
|
long long *secondsSinceEpoch)
|
||||||
{
|
{
|
||||||
|
char *tmp;
|
||||||
g_autoptr(GDateTime) then = NULL;
|
g_autoptr(GDateTime) then = NULL;
|
||||||
g_autoptr(GTimeZone) tz = NULL;
|
g_autoptr(GTimeZone) tz = NULL;
|
||||||
|
int year, mon, mday, hour, min, sec, milliseconds;
|
||||||
|
|
||||||
if (!dateTime || !secondsSinceEpoch) {
|
if (!dateTime || !secondsSinceEpoch) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Invalid argument"));
|
||||||
@ -1489,22 +1491,64 @@ esxVI_DateTime_ConvertToCalendarTime(esxVI_DateTime *dateTime,
|
|||||||
*
|
*
|
||||||
* map negative years to 0, since the base for time_t is the year 1970.
|
* map negative years to 0, since the base for time_t is the year 1970.
|
||||||
*/
|
*/
|
||||||
if (*(dateTime->value) == '-') {
|
if (dateTime->value[0] == '-') {
|
||||||
*secondsSinceEpoch = 0;
|
*secondsSinceEpoch = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
tz = g_time_zone_new_utc();
|
if (/* year */
|
||||||
then = g_date_time_new_from_iso8601(dateTime->value, tz);
|
virStrToLong_i(dateTime->value, &tmp, 10, &year) < 0 || *tmp != '-' ||
|
||||||
|
/* month */
|
||||||
if (!then) {
|
virStrToLong_i(tmp+1, &tmp, 10, &mon) < 0 || *tmp != '-' ||
|
||||||
|
/* day */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &mday) < 0 || *tmp != 'T' ||
|
||||||
|
/* hour */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &hour) < 0 || *tmp != ':' ||
|
||||||
|
/* minute */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &min) < 0 || *tmp != ':' ||
|
||||||
|
/* second */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &sec) < 0) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("xsd:dateTime value '%s' has unexpected format"),
|
_("xsd:dateTime value '%s' has unexpected format"),
|
||||||
dateTime->value);
|
dateTime->value);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
*secondsSinceEpoch = g_date_time_to_unix(then);
|
if (*tmp != '\0') {
|
||||||
|
/* skip .ssssss part if present */
|
||||||
|
if (*tmp == '.' &&
|
||||||
|
virStrToLong_i(tmp + 1, &tmp, 10, &milliseconds) < 0) {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("xsd:dateTime value '%s' has unexpected format"),
|
||||||
|
dateTime->value);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* parse timezone offset if present. if missing assume UTC */
|
||||||
|
if (*tmp == '+' || *tmp == '-') {
|
||||||
|
tz = g_time_zone_new(tmp);
|
||||||
|
} else if (STREQ(tmp, "Z")) {
|
||||||
|
tz = g_time_zone_new_utc();
|
||||||
|
} else {
|
||||||
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
|
_("xsd:dateTime value '%s' has unexpected format"),
|
||||||
|
dateTime->value);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
tz = g_time_zone_new_utc();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* xsd:dateTime represents local time relative to the optional timezone
|
||||||
|
* given as offset. pretend the local time is in UTC and use timegm in
|
||||||
|
* order to avoid interference with the timezone to this computer.
|
||||||
|
* apply timezone correction afterwards, because it's simpler than
|
||||||
|
* handling all the possible over- and underflows when trying to apply
|
||||||
|
* it to the tm struct.
|
||||||
|
*/
|
||||||
|
then = g_date_time_new(tz, year, mon, mday, hour, min, sec);
|
||||||
|
*secondsSinceEpoch = (long long)g_date_time_to_unix(then);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -4608,16 +4608,30 @@ static long long
|
|||||||
prlsdkParseDateTime(const char *str)
|
prlsdkParseDateTime(const char *str)
|
||||||
{
|
{
|
||||||
g_autoptr(GDateTime) then = NULL;
|
g_autoptr(GDateTime) then = NULL;
|
||||||
g_autoptr(GTimeZone) tz = g_time_zone_new_local();
|
g_autoptr(GTimeZone) tz = g_time_zone_new_utc();
|
||||||
|
char *tmp;
|
||||||
|
int year, mon, mday, hour, min, sec;
|
||||||
|
|
||||||
then = g_date_time_new_from_iso8601(str, tz);
|
/* Expect: YYYY-MM-DD HH:MM:SS (%d-%d-%dT%d:%d:%d) eg 2010-11-28 14:29:01 */
|
||||||
if (!then) {
|
if (/* year */
|
||||||
|
virStrToLong_i(str, &tmp, 10, &year) < 0 || *tmp != '-' ||
|
||||||
|
/* month */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &mon) < 0 || *tmp != '-' ||
|
||||||
|
/* day */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &mday) < 0 || *tmp != ' ' ||
|
||||||
|
/* hour */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &hour) < 0 || *tmp != ':' ||
|
||||||
|
/* minute */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &min) < 0 || *tmp != ':' ||
|
||||||
|
/* second */
|
||||||
|
virStrToLong_i(tmp+1, &tmp, 10, &sec) < 0 || *tmp != '\0') {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("unexpected DateTime format: '%s'"), str);
|
_("unexpected DateTime format: '%s'"), str);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return g_date_time_to_unix(then);
|
then = g_date_time_new(tz, year, mon, mday, hour, min, sec);
|
||||||
|
return (long long)g_date_time_to_unix(then);
|
||||||
}
|
}
|
||||||
|
|
||||||
static virDomainSnapshotObjListPtr
|
static virDomainSnapshotObjListPtr
|
||||||
|
Loading…
Reference in New Issue
Block a user