[RFCv2 05/46] util: Add helper functions for 'bool' and 'time_t' and cooperate with xmlgen

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Shi Lei <shi_lei@xxxxxxxxxxxxxx>
---
 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;
 
 
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 *result)
 
     return 0;
 }
+
+
+int
+virStrToBool(const char *str, const char *truevalue, bool *result)
+{
+    if (STREQ(str, truevalue))
+        *result = true;
+    else
+        *result = false;
+
+    return 0;
+}
+
+
+int virStrToTime(const char *str, time_t *result)
+{
+    g_autoptr(GDateTime) then = NULL;
+    g_autoptr(GTimeZone) tz = 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:29:01 */
+    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 != '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,
+                       _("invalid time '%s', expect YYYY-MM-DDTHH:MM:SS"),
+                       str);
+        return -1;
+    }
+
+    then = g_date_time_new(tz, year, mon, mday, hour, min, sec);
+    *result = (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 = NULL;
+    g_autofree char *thenstr = NULL;
+
+    then = g_date_time_new_from_unix_utc(time);
+    thenstr = 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
 
 #include "internal.h"
+#include "virbuffer.h"
 
 #define VIR_INT64_STR_BUFLEN 21
 
@@ -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 time);
+
 /**
  * VIR_AUTOSTRINGLIST:
  *
-- 
2.25.1





[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux