From nobody Mon Feb 9 22:18:47 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1599190946; cv=none; d=zohomail.com; s=zohoarc; b=S3iibnCyczN+a5UGOraaRJsGaFIviOFGfzC/AjCgRdF8/88qnY6qRzyWIuvOlJcgN4y/ogAvkki3wKrVCr8oAwMwR8MH1qYBb/XuSVaRpWOBEowx6XVBjIEEX9M/0PBNNnIZuq/X20n/pdDRGlOq2VyI7aATwKxy8oNlXLmw0rA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599190946; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=R9QmBNrVFCH/+UDB2qhdTuGz76t1fWxzKALt0ayWhhM=; b=gvTDdlowyVhDE68r2bvVDwaHvRgkoMgdvvoMJLZlMcstqfVijOK62TMSoFP09qlJiXzxXfMf/WnTZO/J7bLv921xInq/Q6B6qqaxT4OyQ0XSQ2FfIK56O/tn7t6EJ9O/0tFCXUeYetFZ15mhF16sLMbjb+ozVEn20gZiiSp+EEo= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1599190946871758.959415474275; Thu, 3 Sep 2020 20:42:26 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-139-IOY-o7pdNj6GDbO7MsElDQ-1; Thu, 03 Sep 2020 23:42:23 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id C50EA100670C; Fri, 4 Sep 2020 03:42:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A0DE81001B2B; Fri, 4 Sep 2020 03:42:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 7004097309; Fri, 4 Sep 2020 03:42:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0843fBAw030655 for ; Thu, 3 Sep 2020 23:41:11 -0400 Received: by smtp.corp.redhat.com (Postfix) id CB4E9202348A; Fri, 4 Sep 2020 03:41:10 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C07D2201EB53 for ; Fri, 4 Sep 2020 03:41:08 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 2EBE9101A568 for ; Fri, 4 Sep 2020 03:41:08 +0000 (UTC) Received: from mail-m24147.qiye.163.com (mail-m24147.qiye.163.com [220.194.24.147]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-347-aMqHAvZhNYGMBHttNycUGA-1; Thu, 03 Sep 2020 23:41:04 -0400 Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp4 (Coremail) with SMTP id JedpCgAHcNgStlFfYHYgCg--.1426S7; Fri, 04 Sep 2020 11:35:48 +0800 (CST) X-MC-Unique: IOY-o7pdNj6GDbO7MsElDQ-1 X-MC-Unique: aMqHAvZhNYGMBHttNycUGA-1 From: Shi Lei To: libvir-list@redhat.com Subject: [RFCv2 05/46] util: Add helper functions for 'bool' and 'time_t' and cooperate with xmlgen Date: Fri, 4 Sep 2020 11:34:57 +0800 Message-Id: <20200904033538.418579-6-shi_lei@massclouds.com> In-Reply-To: <20200904033538.418579-1-shi_lei@massclouds.com> References: <20200904033538.418579-1-shi_lei@massclouds.com> MIME-Version: 1.0 X-CM-TRANSID: JedpCgAHcNgStlFfYHYgCg--.1426S7 X-Coremail-Antispam: 1Uf129KBjvJXoWxWw4fCw43uF4DtF4xGF4UJwb_yoW5ZFWUp3 97uFsxZrnYqwn3G3sFgF1kGr13Zr4FqrW3Xrsxuwn5GF4xAF1jyrn3JF9093y5WFZ7Gr4a yr95WryrCF98Jw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRoGQkUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiGxSVT1pchmm9CgAAsL X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false; X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Signed-off-by: Shi Lei --- src/libvirt_private.syms | 3 +++ src/util/virstring.c | 57 ++++++++++++++++++++++++++++++++++++++++ src/util/virstring.h | 9 +++++++ 3 files changed, 69 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 191eab0..4ad9d1e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3242,6 +3242,7 @@ virStringStripSuffix; virStringToUpper; virStringTrimOptionalNewline; virStrncpy; +virStrToBool; virStrToDouble; virStrToLong_i; virStrToLong_l; @@ -3252,6 +3253,8 @@ virStrToLong_ul; virStrToLong_ull; virStrToLong_ullp; virStrToLong_ulp; +virStrToTime; +virTimeFormatBuf; virTrimSpaces; =20 =20 diff --git a/src/util/virstring.c b/src/util/virstring.c index de2ef96..23ade40 100644 --- a/src/util/virstring.c +++ b/src/util/virstring.c @@ -1349,3 +1349,60 @@ int virStringParseYesNo(const char *str, bool *resul= t) =20 return 0; } + + +int +virStrToBool(const char *str, const char *truevalue, bool *result) +{ + if (STREQ(str, truevalue)) + *result =3D true; + else + *result =3D false; + + return 0; +} + + +int virStrToTime(const char *str, time_t *result) +{ + g_autoptr(GDateTime) then =3D NULL; + g_autoptr(GTimeZone) tz =3D g_time_zone_new_utc(); + char *tmp; + int year, mon, mday, hour, min, sec; + + /* Expect: YYYY-MM-DDTHH:MM:SS (%d-%d-%dT%d:%d:%d) eg 2010-11-28T14:2= 9:01 */ + if (/* year */ + virStrToLong_i(str, &tmp, 10, &year) < 0 || *tmp !=3D '-' || + /* month */ + virStrToLong_i(tmp+1, &tmp, 10, &mon) < 0 || *tmp !=3D '-' || + /* day */ + virStrToLong_i(tmp+1, &tmp, 10, &mday) < 0 || *tmp !=3D 'T' || + /* hour */ + virStrToLong_i(tmp+1, &tmp, 10, &hour) < 0 || *tmp !=3D ':' || + /* minute */ + virStrToLong_i(tmp+1, &tmp, 10, &min) < 0 || *tmp !=3D ':' || + /* second */ + virStrToLong_i(tmp+1, &tmp, 10, &sec) < 0 || *tmp !=3D '\0') { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("invalid time '%s', expect YYYY-MM-DDTHH:MM:SS"), + str); + return -1; + } + + then =3D g_date_time_new(tz, year, mon, mday, hour, min, sec); + *result =3D (time_t)g_date_time_to_unix(then); + return 0; +} + + +int +virTimeFormatBuf(virBufferPtr buf, const char *layout, const time_t time) +{ + g_autoptr(GDateTime) then =3D NULL; + g_autofree char *thenstr =3D NULL; + + then =3D g_date_time_new_from_unix_utc(time); + thenstr =3D g_date_time_format(then, "%Y-%m-%dT%H:%M:%S"); + virBufferAsprintf(buf, layout, thenstr); + return 0; +} diff --git a/src/util/virstring.h b/src/util/virstring.h index 561ce0c..658b575 100644 --- a/src/util/virstring.h +++ b/src/util/virstring.h @@ -19,6 +19,7 @@ #pragma once =20 #include "internal.h" +#include "virbuffer.h" =20 #define VIR_INT64_STR_BUFLEN 21 =20 @@ -179,6 +180,14 @@ int virStringParsePort(const char *str, int virStringParseYesNo(const char *str, bool *result) G_GNUC_WARN_UNUSED_RESULT; + +int virStrToBool(const char *str, const char *truevalue, bool *result); + +int virStrToTime(const char *str, time_t *result) + G_GNUC_WARN_UNUSED_RESULT; + +int virTimeFormatBuf(virBufferPtr buf, const char *layout, const time_t ti= me); + /** * VIR_AUTOSTRINGLIST: * --=20 2.25.1