From mboxrd@z Thu Jan 1 00:00:00 1970 MIME-Version: 1.0 In-Reply-To: References: <20150929194253.GC57214@wopr.sciops.net> <950857732c74f611ab0f684badc7d987@brasstown.quanstro.net> Date: Wed, 30 Sep 2015 08:59:40 +0100 Message-ID: From: Charles Forsyth To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net> Content-Type: multipart/alternative; boundary=001a11c384c411678d0520f250fa Subject: Re: [9fans] off topic - a good Git reference Topicbox-Message-UUID: 6cdf70c0-ead9-11e9-9d60-3106f5b1d025 --001a11c384c411678d0520f250fa Content-Type: text/plain; charset=UTF-8 On 30 September 2015 at 08:47, Charles Forsyth wrote: > I was being sarcastic about the portability of so much contemporary C code. Here's a small but representative example. #if HAVE_SYS_TIME_H #include #endif #if HAVE_SYS_CLOCK_GETTIME time_t time_now(void) { struct timespec timespec_value; (void) clock_gettime(CLOCK_REALTIME, ×pec_value); return timespec_value.tv_seconds; } #elif HAVE_SYS_GETTIMEOFDAY time_t time_now(void) { struct timeval timeval_value; (void) gettimeofday(&timeval_value, (struct timezone *) NULL); return timeval_value.tv_seconds; } #elif HAVE_SYS_TIME time_t time_now(void) { time_t seconds_since_epoch; (void) time(&seconds_since_epoch); return seconds_since_epoch; } #endif ./configure # work out which HAVE_... definitions to use Usually there are a few more alternatives enumerated. Surprisingly often, the microseconds or nanoseconds value is discarded, to get the seconds. You could just use #include and call time(NULL) to get that, but where's the fun? --001a11c384c411678d0520f250fa Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

= On 30 September 2015 at 08:47, Charles Forsyth <charles.forsyth@gm= ail.com> wrote:
I was being sarcastic abo= ut the portability of so much contemporary C code.

He= re's a small but representative example.

#if HAVE_SYS_TIME_H
#include <sys/time.h
#e= lif HAVE_TIME_H
#include <time.h>
#endif

#if HAVE_SYS_CLOCK_GETTIME
time_t
time_now(void)
{=C2=A0 struct timespec timespec_value;
=C2= =A0 (void) clock_gettime(CLOCK_REALTIME, &timespec_value);
=C2=A0 return timespec_value.tv_seconds;
}
#elif HAVE_SYS_GETTIMEO= FDAY
time_t
time_now(void)
{
=C2=A0 struct timeval timeval_value;
=C2=A0 (void) gettimeofday(&timeval_value, (struct ti= mezone *) NULL);
=C2=A0 return timeval_valu= e.tv_seconds;
}
#elif HAVE_SYS_TIME
time_t
time_now(void)
{
=C2=A0 time_t seconds_since_epo= ch;
=C2=A0 (void) time(&seconds_since_e= poch);
=C2=A0 return seconds_since_epoch;
}
#endif

./configu= re =C2=A0 =C2=A0# work out which HAVE_... definitions to use

Usually there are a = few more alternatives enumerated.=C2=A0 Surprisingly often, the microsecond= s or nanoseconds
value is discarded, to get= the seconds. You could just use #include <time.h> and call time(NULL= ) to get that, but where's the fun?
--001a11c384c411678d0520f250fa--