mirror of
https://git.notmuchmail.org/git/notmuch
synced 2025-02-17 23:53:15 +01:00
timegm: add portable implementation (Solaris support)
The timegm(3) function is a non-standard extension to libc which is available in GNU libc and on some BSDs. Although SunOS had this function in its libc, Solaris (unfortunately) removed it. This patch implements a very simple version of timegm() which is good enough for parse-time-string.c. Signed-off-by: Vladimir Marek <vlmarek@volny.cz>
This commit is contained in:
parent
43843745dc
commit
8c6b2e7e9d
6 changed files with 84 additions and 0 deletions
|
@ -17,4 +17,8 @@ ifneq ($(HAVE_STRSEP),1)
|
||||||
notmuch_compat_srcs += $(dir)/strsep.c
|
notmuch_compat_srcs += $(dir)/strsep.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifneq ($(HAVE_TIMEGM),1)
|
||||||
|
notmuch_compat_srcs += $(dir)/timegm.c
|
||||||
|
endif
|
||||||
|
|
||||||
SRCS := $(SRCS) $(notmuch_compat_srcs)
|
SRCS := $(SRCS) $(notmuch_compat_srcs)
|
||||||
|
|
|
@ -57,6 +57,11 @@ char* strcasestr(const char *haystack, const char *needle);
|
||||||
char *strsep(char **stringp, const char *delim);
|
char *strsep(char **stringp, const char *delim);
|
||||||
#endif /* !HAVE_STRSEP */
|
#endif /* !HAVE_STRSEP */
|
||||||
|
|
||||||
|
#if !HAVE_TIMEGM
|
||||||
|
#include <time.h>
|
||||||
|
time_t timegm (struct tm *tm);
|
||||||
|
#endif /* !HAVE_TIMEGM */
|
||||||
|
|
||||||
/* Silence gcc warnings about unused results. These warnings exist
|
/* Silence gcc warnings about unused results. These warnings exist
|
||||||
* for a reason; any use of this needs to be justified. */
|
* for a reason; any use of this needs to be justified. */
|
||||||
#ifdef __GNUC__
|
#ifdef __GNUC__
|
||||||
|
|
7
compat/have_timegm.c
Normal file
7
compat/have_timegm.c
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#include <time.h>
|
||||||
|
#include "compat.h"
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
return (int) timegm((struct tm *)0);
|
||||||
|
}
|
56
compat/timegm.c
Normal file
56
compat/timegm.c
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
/* timegm.c --- Implementation of replacement timegm function.
|
||||||
|
|
||||||
|
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 3, 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 2013 Blake Jones. */
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
#include "compat.h"
|
||||||
|
|
||||||
|
static int
|
||||||
|
leapyear (int year)
|
||||||
|
{
|
||||||
|
return ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is a simple implementation of timegm() which does what is needed
|
||||||
|
* by create_output() -- just turns the "struct tm" into a GMT time_t.
|
||||||
|
* It does not normalize any of the fields of the "struct tm", nor does
|
||||||
|
* it set tm_wday or tm_yday.
|
||||||
|
*/
|
||||||
|
time_t
|
||||||
|
timegm (struct tm *tm)
|
||||||
|
{
|
||||||
|
int monthlen[2][12] = {
|
||||||
|
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
|
||||||
|
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
|
||||||
|
};
|
||||||
|
int year, month, days;
|
||||||
|
|
||||||
|
days = 365 * (tm->tm_year - 70);
|
||||||
|
for (year = 70; year < tm->tm_year; year++) {
|
||||||
|
if (leapyear(1900 + year)) {
|
||||||
|
days++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (month = 0; month < tm->tm_mon; month++) {
|
||||||
|
days += monthlen[leapyear(1900 + year)][month];
|
||||||
|
}
|
||||||
|
days += tm->tm_mday - 1;
|
||||||
|
|
||||||
|
return ((((days * 24) + tm->tm_hour) * 60 + tm->tm_min) * 60 + tm->tm_sec);
|
||||||
|
}
|
11
configure
vendored
11
configure
vendored
|
@ -530,6 +530,17 @@ else
|
||||||
fi
|
fi
|
||||||
rm -f compat/have_strsep
|
rm -f compat/have_strsep
|
||||||
|
|
||||||
|
printf "Checking for timegm... "
|
||||||
|
if ${CC} -o compat/have_timegm "$srcdir"/compat/have_timegm.c > /dev/null 2>&1
|
||||||
|
then
|
||||||
|
printf "Yes.\n"
|
||||||
|
have_timegm="1"
|
||||||
|
else
|
||||||
|
printf "No (will use our own instead).\n"
|
||||||
|
have_timegm="0"
|
||||||
|
fi
|
||||||
|
rm -f compat/have_timegm
|
||||||
|
|
||||||
printf "Checking for standard version of getpwuid_r... "
|
printf "Checking for standard version of getpwuid_r... "
|
||||||
if ${CC} -o compat/check_getpwuid "$srcdir"/compat/check_getpwuid.c > /dev/null 2>&1
|
if ${CC} -o compat/check_getpwuid "$srcdir"/compat/check_getpwuid.c > /dev/null 2>&1
|
||||||
then
|
then
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "compat.h"
|
||||||
#include "parse-time-string.h"
|
#include "parse-time-string.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
Loading…
Add table
Reference in a new issue