Merge tag 'upstream/1.2.0'
[psensor-pkg-debian.git] / src / lib / ptime.c
index 34ae92e..05ca1a1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2013 jeanfi@gmail.com
+ * Copyright (C) 2010-2016 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  * 02110-1301 USA
  */
 #include <stdlib.h>
+#include <string.h>
 
-#include "ptime.h"
+#include <ptime.h>
 
-char *time_to_str(time_t *t)
+const int P_TIME_VER = 3;
+
+static const int ISO8601_TIME_LENGTH = 19; /* YYYY-MM-DDThh:mm:ss */
+static const int ISO8601_DATE_LENGTH = 10; /* YYYY-MM-DD */
+
+char *time_to_ISO8601_time(time_t *t)
 {
        struct tm lt;
-       char *str;
 
-       if (!localtime_r(t, &lt))
+       memset(&lt, 0, sizeof(struct tm));
+       if (!gmtime_r(t, &lt))
                return NULL;
 
-       str = malloc(64);
+       return tm_to_ISO8601_time(&lt);
+}
 
-       if (strftime(str, 64, "%s", &lt)) {
-               return str;
-       } else {
-               free(str);
+char *time_to_ISO8601_date(time_t *t)
+{
+       struct tm lt;
+
+       memset(&lt, 0, sizeof(struct tm));
+       if (!gmtime_r(t, &lt))
                return NULL;
-       }
+
+       return tm_to_ISO8601_date(&lt);
+}
+
+char *tm_to_ISO8601_date(struct tm *tm)
+{
+       char *str;
+
+       str = malloc(ISO8601_DATE_LENGTH + 1);
+
+       if (strftime(str, ISO8601_DATE_LENGTH + 1, "%F", tm))
+               return str;
+
+       free(str);
+       return NULL;
+}
+
+char *tm_to_ISO8601_time(struct tm *tm)
+{
+       char *str;
+
+       str = malloc(ISO8601_TIME_LENGTH + 1);
+
+       if (strftime(str, ISO8601_TIME_LENGTH + 1, "%FT%T", tm))
+               return str;
+
+       free(str);
+       return NULL;
 }
 
-char *get_time_str()
+char *get_current_ISO8601_time(void)
 {
        time_t t;
 
        t = time(NULL);
-       return time_to_str(&t);
+       return time_to_ISO8601_time(&t);
 }