X-Git-Url: http://wpitchoune.net/gitweb/?p=psensor-pkg-debian.git;a=blobdiff_plain;f=src%2Flib%2Fptime.c;h=2c06c7cadd0cee96b9a666d6f5ea6b333b504fb0;hp=95c33423288723add11ed797da821532dbf148ae;hb=e248180cc4ebc258fdd5778a26c08287089165b1;hpb=162a992f0f857ef0969d64206b9762f0d054528f diff --git a/src/lib/ptime.c b/src/lib/ptime.c index 95c3342..2c06c7c 100644 --- a/src/lib/ptime.c +++ b/src/lib/ptime.c @@ -1,36 +1,74 @@ /* - * Copyright (C) 2010-2014 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 - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301 USA + Copyright (C) 2010-2014 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 + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA */ #include +#include + +#include + +const int P_TIME_VER = 2; + +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; -#include "ptime.h" + memset(<, 0, sizeof(struct tm)); + if (!gmtime_r(t, <)) + return NULL; -char *time_to_str(time_t *t) + return tm_to_ISO8601_time(<); +} + +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; - if (!localtime_r(t, <)) + str = malloc(ISO8601_DATE_LENGTH + 1); + + if (strftime(str, ISO8601_DATE_LENGTH + 1, "%F", tm)) { + return str; + } else { + free(str); return NULL; + } +} + +char *tm_to_ISO8601_time(struct tm *tm) +{ + char *str; - str = malloc(64); + str = malloc(ISO8601_TIME_LENGTH + 1); - if (strftime(str, 64, "%s", <)) { + if (strftime(str, ISO8601_TIME_LENGTH + 1, "%FT%T", tm)) { return str; } else { free(str); @@ -38,10 +76,10 @@ char *time_to_str(time_t *t) } } -char *get_time_str() +char *get_current_ISO8601_time() { time_t t; t = time(NULL); - return time_to_str(&t); + return time_to_ISO8601_time(&t); }