/*
- * Copyright (C) 2010-2014 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, <))
+ memset(<, 0, sizeof(struct tm));
+ if (!gmtime_r(t, <))
return NULL;
- str = malloc(64);
+ return tm_to_ISO8601_time(<);
+}
- if (strftime(str, 64, "%s", <)) {
- return str;
- } else {
- free(str);
+char *time_to_ISO8601_date(time_t *t)
+{
+ struct tm lt;
+
+ memset(<, 0, sizeof(struct tm));
+ if (!gmtime_r(t, <))
return NULL;
- }
+
+ return tm_to_ISO8601_date(<);
+}
+
+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);
}