Git-Url: 
http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=82190f3cf971d5483867b0969d1e9fe4946cb65c

commit 82190f3cf971d5483867b0969d1e9fe4946cb65c
Author: Michel Hermier <herm...@frugalware.org>
Date:   Wed Nov 13 18:48:27 2013 +0100

libpacman: Add util/time.{c,h} utilities.

diff --git a/lib/libpacman/Makefile.am b/lib/libpacman/Makefile.am
index 7c06423..b9e19d1 100644
--- a/lib/libpacman/Makefile.am
+++ b/lib/libpacman/Makefile.am
@@ -27,7 +27,8 @@ PACKAGE_TARGETS = \
UTIL_TARGETS = \
util/list.c \
util/log.c \
-       util/stringlist.c
+       util/stringlist.c \
+       util/time.c

TARGETS = \
$(DB_TARGETS) \
diff --git a/lib/libpacman/util/time.c b/lib/libpacman/util/time.c
new file mode 100644
index 0000000..17d16b3
--- /dev/null
+++ b/lib/libpacman/util/time.c
@@ -0,0 +1,70 @@
+/*
+ *  time.c
+ *
+ *  Copyright (c) 2013 by Michel Hermier <herm...@frugalware.org>
+ *
+ *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ *  USA.
+ */
+
+#include "config.h"
+
+#ifndef __USE_XOPEN2K8
+#define __USE_XOPEN2K8
+#endif
+
+#include <locale.h>
+
+/* pacman-g2 */
+#include "util/time.h"
+
+#include "util.h"
+
+/* Return the C locale
+ */
+static
+locale_t _pacman_locale_c(void)
+{
+       static locale_t locale_c = NULL;
+
+       if(locale_c == NULL) {
+               locale_c = newlocale(LC_ALL_MASK, "C", NULL);
+               /* BUG_ON(locale_c == NULL); */
+       }
+       return locale_c;
+}
+
+struct tm *_pacman_localtime(const time_t *timep)
+{
+       time_t now;
+
+       if(timep == NULL &&
+               (now = time(NULL)) != ((time_t) -1)) {
+               timep = &now;
+       }
+       return localtime(timep);
+}
+
+size_t _pacman_strftime_lc(char *s, size_t max, const char *format, const 
struct tm *tm)
+{
+       return strftime_l(s, max, format, tm, _pacman_locale_c());
+}
+
+char *_pacman_strptime_lc(const char *s, const char *format, struct tm *tm)
+{
+       return strptime_l(s, format, tm, _pacman_locale_c());
+}
+
+/* vim: set ts=2 sw=2 noet: */
diff --git a/lib/libpacman/util/time.h b/lib/libpacman/util/time.h
new file mode 100644
index 0000000..a482976
--- /dev/null
+++ b/lib/libpacman/util/time.h
@@ -0,0 +1,40 @@
+/*
+ *  time.h
+ *
+ *  Copyright (c) 2013 by Michel Hermier <herm...@frugalware.org>
+ *
+ *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+ *  USA.
+ */
+#ifndef _PACMAN_TIME_H
+#define _PACMAN_TIME_H
+
+#include <time.h>
+
+/* Return the localtime for timep. If timep is NULL, return the conversion for 
time(NULL) (libc returns NULL instead).
+ */
+struct tm *_pacman_localtime(const time_t *timep);
+
+/* _lc means localised C */
+size_t _pacman_strftime_lc(char *s, size_t max, const char *format, const 
struct tm *tm);
+char *_pacman_strptime_lc(const char *s, const char *format, struct tm *tm);
+
+#define PM_RFC1123_FORMAT "%a, %d %b %Y %H:%M:%S %z"
+#define _pacman_strftime_rfc1123_lc(s, max, tm) _pacman_strftime_lc((s), 
(max), PM_RFC1123_FORMAT, (tm))
+#define _pacman_strptime_rfc1123_lc(s, tm) _pacman_strptime_lc((s), 
PM_RFC1123_FORMAT, (tm))
+
+#endif /* _PACMAN_TIME_H */
+
+/* vim: set ts=2 sw=2 noet: */
_______________________________________________
Frugalware-git mailing list
Frugalware-git@frugalware.org
http://frugalware.org/mailman/listinfo/frugalware-git

Reply via email to