unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Blake Jones <blakej@foo.net>
To: Tomi Ollila <tomi.ollila@iki.fi>
Cc: notmuch@notmuchmail.org
Subject: Re: [PATCH 10/10] timegm: add portable implementation (Solaris support) 
Date: Mon, 05 Nov 2012 07:47:22 -0800	[thread overview]
Message-ID: <2592.1352130442@foo.net> (raw)
In-Reply-To: Your message of "Mon, 05 Nov 2012 14:09:33 +0200." <m2txt4jloi.fsf@guru.guru-group.fi>

[-- Attachment #1: Type: text/plain, Size: 1268 bytes --]

> Yet another idea for an alternative. Compile by entering 'sh xtimegm.c'
> and then run ./xtimegm
> 
> Simple cases seems to work. Dst change may (or then may not) give one
> hour difference to the expected. The test "coverage" could be easily
> expanded to that ;)
> 
> Hmm, I also found this:
> http://lists.gnu.org/archive/html/bug-gnulib/2003-09/msg00004.html
> which does 2 mktime's and one gmtime_r (without allocating tg!)...
> Pick any of these 3 -- or something different (e.g. less NIH if there is)

Of these two, I would probably lean slightly toward the latter, in that
it relies more on libc for the time zone handling logic.

But in general, this seems to me like a case where an explicit
implementation like mine is less prone to failure, because it doesn't
need to worry about time zones at all.  The other approaches rely on
letting libc do all the hard work of time zone manipulation, and then
reading the tea leaves to find a way to undo it.  I would guess that if
some change in the time standards is going to break one of these
implementations, it's going to be some new time zone specification, not
a change in the number of days in a month. :)

For what it's worth, I used the attached program to test my
implementation, and it passed.

Blake


[-- Attachment #2: Type: text/plain, Size: 2553 bytes --]

#include <time.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 parse-time-string.c -- 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.
 */
static 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);
}

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void
tm_test(int niter)
{
	const 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		i;
	struct tm	tm, *tmp;
	time_t		t;

	for (i = 0; i < niter; i++) {
		tm.tm_year = (lrand48() % 67) + 70;
		tm.tm_mon = lrand48() % 12;
		do {
			t = (lrand48() % 31) + 1;
		} while (t > monthlen[leapyear(1900 + tm.tm_year)][tm.tm_mon]);
		tm.tm_mday = t;
		tm.tm_hour = lrand48() % 24;
		tm.tm_min = lrand48() % 60;
		tm.tm_sec = lrand48() % 60;

		t = timegm(&tm);
		tmp = gmtime(&t);

		if (tmp->tm_sec != tm.tm_sec ||
		    tmp->tm_min != tm.tm_min ||
		    tmp->tm_hour != tm.tm_hour ||
		    tmp->tm_mday != tm.tm_mday ||
		    tmp->tm_mon != tm.tm_mon ||
		    tmp->tm_year != tm.tm_year) {
			printf("%4d-%02d-%02d %02d:%02d:%02d -> "
			    "%4d-%02d-%02d %02d:%02d:%02d\n",
			    tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
			    tm.tm_hour, tm.tm_min, tm.tm_sec,
			    tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
			    tmp->tm_hour, tmp->tm_min, tmp->tm_sec);

		}
	}
}

void
time_test(int niter)
{
	int		i;
	time_t		st, et;
	struct tm	*tmp;

	for (i = 0; i < niter; i++) {
		st = (time_t)lrand48();

		tmp = gmtime(&st);
		et = timegm(tmp);

		if (st != et) {
			printf("%d -> %d (%4d-%02d-%02d %02d:%02d:%02d)\n",
			    st, et,
			    tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
			    tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
		}
	}
}

int
main(void)
{
	const int	niter = 10000000;

	srand48(getpid());

	tm_test(niter);
	time_test(niter);

	return (0);
}

  reply	other threads:[~2012-11-05 15:47 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-04  3:15 [PATCH 00/10] Solaris support Blake Jones
2012-11-04  3:15 ` [PATCH 01/10] getpwuid: check for standards compliance (Solaris support) Blake Jones
2012-11-04  3:15 ` [PATCH 02/10] asctime: " Blake Jones
2012-11-04  3:15 ` [PATCH 03/10] gethostbyname: check for libnsl " Blake Jones
2012-11-04  3:15 ` [PATCH 04/10] configure: check for -Wl,-rpath " Blake Jones
2012-11-04  3:15 ` [PATCH 05/10] install: check for non-SysV version " Blake Jones
2012-11-04 21:31   ` Jani Nikula
2012-11-05  5:27     ` Blake Jones
2012-11-05 11:29       ` Jani Nikula
2012-11-05 14:52         ` Blake Jones
2012-11-04  3:15 ` [PATCH 06/10] strsep: check for availability " Blake Jones
2012-11-04  3:15 ` [PATCH 07/10] gen-version-script: parse Solaris "nm" output " Blake Jones
2012-11-04  3:16 ` [PATCH 08/10] notmuch-config: header for index() prototype " Blake Jones
2012-11-04 21:16   ` Jani Nikula
2012-11-04 21:47     ` Tomi Ollila
2012-11-05  4:52       ` Blake Jones
2012-11-04  3:16 ` [PATCH 09/10] debugger.c: correct return type from getppid() " Blake Jones
2012-11-04  3:16 ` [PATCH 10/10] timegm: add portable implementation " Blake Jones
2012-11-04 10:21   ` Jani Nikula
2012-11-04 15:40     ` Blake Jones
2012-11-04 20:58       ` Jani Nikula
2012-11-05  4:50         ` Blake Jones
2012-11-05 12:09       ` Tomi Ollila
2012-11-05 15:47         ` Blake Jones [this message]
2012-11-05 17:36           ` Tomi Ollila
2012-11-05 18:33             ` Blake Jones
2012-11-04 21:35 ` [PATCH 00/10] Solaris support Jani Nikula

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2592.1352130442@foo.net \
    --to=blakej@foo.net \
    --cc=notmuch@notmuchmail.org \
    --cc=tomi.ollila@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).