Path: gmdzi!unido!mcsun!sunic!uupsi!rpi!zaphod.mps.ohio-state.edu!
samsung!cs.utexas.edu!chinacat!sequoia!rpp386!jfh
From: j...@rpp386.cactus.org (John F. Haugh II)
Newsgroups: alt.sources
Subject: UNIX-Time the right way ...
Message-ID: <18760@rpp386.cactus.org>
Date: 29 Nov 90 16:12:15 GMT
Reply-To: j...@rpp386.cactus.org (John F. Haugh II)
Followup-To: alt.sources.d
Organization: Lone Star Cafe and BBS Service
Lines: 197
Posted: Thu Nov 29 17:12:15 1990
X-Clever-Slogan: Recycle or Die.
X-Archive-Name: mktime.demo

There is nothing worse than dumb, stupid, slow code.

These is also nothing worse than people whose IQ is so low
that they can't take a hint and do something productive
with it.  Please, people, some of you claim to be programmers,
how about living up to those claims?  I'm really sorry I feel
this need to insult some people, but gee guys, at least take
the effort to try my suggestions before attacking me as
writing buggy or inaccurate code.  Of course, having not been
paid to write this code I will tell you that under certain
conditions it may loop infinitely, but then you get what you
pay for.  Try 2/31/88 for a nice time.

Test results after the source code.  You will notice that this
code correctly handles daylight savings time, as well as
hours, minutes, and seconds.  You will also notice that it
calls localtime 1/15th or so less often.  It even does leap
days.

There is nothing wrong with being "clever", but binary
approximation is as old as Newton, if not older.
--
/*
 * Copyright 1990, John F. Haugh II
 * All rights reserved.
 *
 * Use, duplication, and disclosure prohibited without
 * the express written permission of the author.
 *
 * Non-commercial (profit-making) source distribution permitted
 * provided this notice remains intact.
 */

#include <time.h>

/*
 * days and juldays are used to compute the number of days in the
 * current month, and the cummulative number of days in the preceding
 * months.
 */

static	short	days[12] = {
	31,	28,	31,	30,	31,	30,	/* JAN - JUN */
	31,	31,	30,	31,	30,	31 };	/* JUL - DEC */

static	short	juldays[12] = {
	0,	31,	59,	90,	120,	151,	/* JAN - JUN */
	181,	212,	243,	273,	304,	334 };	/* JUL - DEC */

static long
dtime (tm, time)
struct	tm	*tm;
long	time;	/* no C */
{
	struct	tm	*sm;
	long	diff;
	long	julian1, julian2;

	sm = localtime (&time);

	julian1 = ((tm->tm_year - 70) * 365) +	/* days in whole years */
		(((tm->tm_year + 1) - 70) / 4);	/* days in leap years */
	julian1 += juldays[tm->tm_mon] +	/* days in whole months */
		(tm->tm_mon > 1 && (tm->tm_year % 4) == 0 ? 1:0); /* leap day */
	julian1 += tm->tm_mday - 1;		/* days so far this month */

	julian2 = ((sm->tm_year - 70) * 365) +	/* days in whole years */
		(((sm->tm_year + 1) - 70) / 4);	/* days in leap years */
	julian2 += juldays[sm->tm_mon] +	/* days in whole months */
		(sm->tm_mon > 1 && (sm->tm_year % 4) == 0 ? 1:0); /* leap day */
	julian2 += sm->tm_mday - 1;		/* days so far this month */

	diff = (julian1 - julian2) * (24L*3600);	/* add the days */
	diff += (tm->tm_hour - sm->tm_hour) * (3600L);	/* add the hours */
	diff += (tm->tm_min - sm->tm_min) * (60L);	/* add the minutes */
	diff += (tm->tm_sec - sm->tm_sec);		/* add the seconds */

	return diff;				/* that's how far off */
}

long
mktime (tm)
struct	tm	*tm;
{
	long	old, diff, new;
	struct	tm	*t;

	for (old = 0L;(diff = dtime (tm, old)) != 0;old += diff) {
#ifdef	TEST
		new = old + diff;
		printf ("old = %ld, new = %ld\n", old, new);

		t = localtime (&old);
		printf ("old = %d/%d/%d %d:%d:%d\n",
			t->tm_mon + 1, t->tm_mday, t->tm_year,
			t->tm_hour, t->tm_min, t->tm_sec);

		t = localtime (&new);
		printf ("new = %d/%d/%d %d:%d:%d\n",
			t->tm_mon + 1, t->tm_mday, t->tm_year,
			t->tm_hour, t->tm_min, t->tm_sec);
#else
		;
#endif
	}
	return old;
}
#ifdef	TEST
main ()
{
	long	t;
	struct	tm	tm;
	struct	tm	*ptm;
	char	buf[100];

	while (gets (buf)) {
		memset (&tm, 0, sizeof tm);

		sscanf (buf, "%d/%d/%d %d:%d:%d",
			&tm.tm_mon, &tm.tm_mday, &tm.tm_year,
			&tm.tm_hour, &tm.tm_min, &tm.tm_sec);

		tm.tm_mon--;

		t = mktime (&tm);

		ptm = localtime (&t);
		printf ("guess = %d or %d/%d/%d %d:%d:%d\n",
			t, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_year,
			ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
	}
}
#endif
--
Script is typescript, started Thu Nov 29 10:00:25 1990
rpp386-> ./mktime
1/1/70 0:0:0
old = 0, new = 21600
old = 12/31/69 18:0:0
new = 1/1/70 0:0:0
guess = 21600 or 1/1/70 0:0:0
12/31/69 18:0:0
guess = 0 or 12/31/69 18:0:0
11/29/90 10:00:25
old = 0, new = 659894425
old = 12/31/69 18:0:0
new = 11/29/90 10:0:25
guess = 659894425 or 11/29/90 10:0:25
6/1/90 12:00:00
old = 0, new = 644263200
old = 12/31/69 18:0:0
new = 6/1/90 13:0:0
old = 644263200, new = 644259600
old = 6/1/90 13:0:0
new = 6/1/90 12:0:0
guess = 644259600 or 6/1/90 12:0:0
10/21/90 01:00:00
old = 0, new = 656492400
old = 12/31/69 18:0:0
new = 10/21/90 2:0:0
old = 656492400, new = 656488800
old = 10/21/90 2:0:0
new = 10/21/90 1:0:0
guess = 656488800 or 10/21/90 1:0:0
10/21/90 02:00:00
old = 0, new = 656496000
old = 12/31/69 18:0:0
new = 10/21/90 3:0:0
old = 656496000, new = 656492400
old = 10/21/90 3:0:0
new = 10/21/90 2:0:0
guess = 656492400 or 10/21/90 2:0:0
10/21/90 3:0:0
old = 0, new = 656499600
old = 12/31/69 18:0:0
new = 10/21/90 4:0:0
old = 656499600, new = 656496000
old = 10/21/90 4:0:0
new = 10/21/90 3:0:0
guess = 656496000 or 10/21/90 3:0:0
10/28/90 1:0:0
old = 0, new = 657097200
old = 12/31/69 18:0:0
new = 10/28/90 1:0:0
guess = 657097200 or 10/28/90 1:0:0
10/28/90 2:0:0
old = 0, new = 657100800
old = 12/31/69 18:0:0
new = 10/28/90 2:0:0
guess = 657100800 or 10/28/90 2:0:0
rpp386-> exit
Script done Thu Nov 29 10:03:31 1990
-- 
John F. Haugh II                     UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                   Domain: j...@rpp386.cactus.org
"SCCS, the source motel!  Programs check in and never check out!"
		-- Ken Thompson

Path: gmdzi!unido!isaak!escher!nadia!smurf!ira.uka.de!
sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!think.com!mintaka!
olivea!tymix!cirrusl!sunstorm!dhesi
From: dhesi%...@oliveb.ATC.olivetti.com (Rahul Dhesi)
Newsgroups: alt.sources.d
Subject: Re: UNIX-Time the right way ...
Message-ID: <2757@cirrusl.UUCP>
Date: 30 Nov 90 09:46:24 GMT
References: <18760@rpp386.cactus.org>
Sender: news@cirrusl.UUCP
Organization: Cirrus Logic Inc.
Lines: 21
Posted: Fri Nov 30 10:46:24 1990

In alt.sources j...@rpp386.cactus.org (John F. Haugh II) writes:

> * Use, duplication, and disclosure prohibited without
> * the express written permission of the author.
> *
> * Non-commercial (profit-making) source distribution permitted
> * provided this notice remains intact.

If use, duplication, and disclosure is prohibited without express
written permission, and no such express written permission has been
supplied, what's the point of posting this to alt.sources?  Even if
such permission is available, it's quite meaningless to ask for
non-disclosure of any code that's posted to Usenet.

"Non-commercial (profit-making)"...isn't that an oxymoronic phrase?

What has Usenet come to?  (At least, what has the alt.* hierarchy come
to?)
--
Rahul Dhesi < dhesi%...@oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi

Path: gmdzi!unido!mcsun!sunic!hagbard!eru!bloom-beacon!mit-eddie!
rutgers!ucsd!sdd.hp.com!wuarchive!cs.utexas.edu!execu!sequoia!
rpp386!jfh
From: j...@rpp386.cactus.org (John F. Haugh II)
Newsgroups: alt.sources.d
Subject: copyright notices (was: Re: UNIX-Time the right way ...)
Message-ID: <18765@rpp386.cactus.org>
Date: 30 Nov 90 14:19:19 GMT
References: <18760@rpp386.cactus.org> <2757@cirrusl.UUCP>
Reply-To: j...@rpp386.cactus.org (John F. Haugh II)
Organization: Lone Star Cafe and BBS Service
Lines: 36
Posted: Fri Nov 30 15:19:19 1990
X-Clever-Slogan: Recycle or Die.

In article < 2757@cirrusl.UUCP> dhesi%...@oliveb.ATC.olivetti.com 
(Rahul Dhesi) writes:
>If use, duplication, and disclosure is prohibited without express
>written permission, and no such express written permission has been
>supplied, what's the point of posting this to alt.sources?  Even if
>such permission is available, it's quite meaningless to ask for
>non-disclosure of any code that's posted to Usenet.

It's called "a formality".  I actually have a vi key bound to a
command which inserts copyright notices at the very top of source
code files.  Honest people don't have problems with copyright
notices.

In several years of posting source code I have only =once= refused
distribution or duplication permission to anyone.  That was to
a company which wanted to use a dialer package I posted without
giving any credit to me for authorship or without paying a token
royalty.  The dialer package was to be part of a communications
application which they were selling for profit.

>"Non-commercial (profit-making)"...isn't that an oxymoronic phrase?

Hmmm.  Yes, I suppose you might have a point there - it probably
should read "Non-commercial (not profit making) distribution
permitted".

>What has Usenet come to?  (At least, what has the alt.* hierarchy come
>to?)

Just because it's the alt.net you get to steal other people's work?
Please, if that's the case I'll stop posting code, as will many
others who don't believe in plagiarism or outright theft.
-- 
John F. Haugh II                     UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                   Domain: j...@rpp386.cactus.org
"SCCS, the source motel!  Programs check in and never check out!"
		-- Ken Thompson

Path: gmdzi!unido!mcsun!uunet!olivea!tymix!cirrusl!sun600!dhesi
From: dhesi%...@oliveb.ATC.olivetti.com (Rahul Dhesi)
Newsgroups: alt.sources.d
Subject: Re: copyright notices (was: Re: UNIX-Time the right way ...)
Message-ID: <2761@cirrusl.UUCP>
Date: 3 Dec 90 02:36:14 GMT
References: <18760@rpp386.cactus.org> <2757@cirrusl.UUCP> 
<18765@rpp386.cactus.org>
Sender: news@cirrusl.UUCP
Organization: Cirrus Logic Inc.
Lines: 20
Posted: Mon Dec  3 03:36:14 1990

In < 18...@rpp386.cactus.org> j...@rpp386.cactus.org (John F. Haugh II) 
writes:

>Just because it's the alt.net you get to steal other people's work?
>Please, if that's the case I'll stop posting code, as will many
>others who don't believe in plagiarism or outright theft.

Well, you can't really have it two ways.  If you consider it plagiarism
or outright theft for people to use the code that you post in
alt.sources, then you shouldn't be posting it in the first place.  If
not so, then you shouldn't be posting the code here and insisting on
that strange copyright notice that prohibits disclosure.  Do you
seriously me to make others sign a non-disclosure agreement before I
install some utility that uses your code for use on our machines?

If the purpose of that copyright notice is to ensure correct author
attribution, then it is enough to state that, e.g., "permission is
granted to use...etc...provided this copyright notice is preserved."
--
Rahul Dhesi <dhesi%...@oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi

Path: gmdzi!unido!mcsun!uunet!cs.utexas.edu!natinst!sequoia!rpp386!jfh
From: j...@rpp386.cactus.org (John F. Haugh II)
Newsgroups: alt.sources.d
Subject: Re: copyright notices (was: Re: UNIX-Time the right way ...)
Message-ID: <18770@rpp386.cactus.org>
Date: 3 Dec 90 14:03:05 GMT
References: <18760@rpp386.cactus.org> <2757@cirrusl.UUCP> 
<18765@rpp386.cactus.org> <2761@cirrusl.UUCP>
Reply-To: j...@rpp386.cactus.org (John F. Haugh II)
Organization: Lone Star Cafe and BBS Service
Lines: 16
Posted: Mon Dec  3 15:03:05 1990
X-Clever-Slogan: Recycle or Die.

In article < 2761@cirrusl.UUCP> dhesi%...@oliveb.ATC.olivetti.com 
(Rahul Dhesi) writes:
>If the purpose of that copyright notice is to ensure correct author
>attribution, then it is enough to state that, e.g., "permission is
>granted to use...etc...provided this copyright notice is preserved."

if you or anyone else have real suggestions (like the exact wording),
i'd appreciate seeing them.  however, as this is the first time i've
seen any complaints, and as it is the exact same wording i've been
using for three years, i am having a hard time believing that anyone
is really having trouble with that copyright notice.

seriously, if you have some "favorite" 4 or 5 line copyright notice
that you think is better, mail it to me.
-- 
John F. Haugh II                     UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                   Domain: j...@rpp386.cactus.org

Path: gmdzi!unido!mcsun!uunet!olivea!tymix!cirrusl!sunstorm!dhesi
From: dhesi%...@oliveb.ATC.olivetti.com (Rahul Dhesi)
Newsgroups: alt.sources.d
Subject: Re: copyright notices (was: Re: UNIX-Time the right way ...)
Message-ID: <2762@cirrusl.UUCP>
Date: 3 Dec 90 19:10:44 GMT
References: <18760@rpp386.cactus.org> <2757@cirrusl.UUCP> 
<18765@rpp386.cactus.org> <2761@cirrusl.UUCP> <18770@rpp386.cactus.org>
Sender: news@cirrusl.UUCP
Organization: Cirrus Logic Inc.
Lines: 14
Posted: Mon Dec  3 20:10:44 1990

In < 18...@rpp386.cactus.org> j...@rpp386.cactus.org (John F. Haugh II) 
writes:

>seriously, if you have some "favorite" 4 or 5 line copyright notice
>that you think is better, mail it to me.

Here you are (posted rather than mailed):

     (C) Copyright 1990 < your name>, All rights reserved.  Permission
     is granted to copy and create derivate works for any purpose,
     whether commercial or noncomemrcial, provided this copyright
     message is preserved in all copies.
--
Rahul Dhesi < dhesi%...@oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi

Path: gmdzi!unido!mcsun!uunet!cs.utexas.edu!execu!sequoia!rpp386!jfh
From: j...@rpp386.cactus.org (John F. Haugh II)
Newsgroups: alt.sources.d
Subject: Re: copyright notices (was: Re: UNIX-Time the right way ...)
Message-ID: <18780@rpp386.cactus.org>
Date: 4 Dec 90 14:22:35 GMT
References: <18760@rpp386.cactus.org> <2757@cirrusl.UUCP> 
<18765@rpp386.cactus.org> <2761@cirrusl.UUCP> <18770@rpp386.cactus.org> 
<2762@cirrusl.UUCP>
Reply-To: j...@rpp386.cactus.org (John F. Haugh II)
Organization: Lone Star Cafe and BBS Service
Lines: 19
Posted: Tue Dec  4 15:22:35 1990
X-Clever-Slogan: Recycle or Die.

In article < 2762@cirrusl.UUCP> dhesi%...@oliveb.ATC.olivetti.com 
(Rahul Dhesi) writes:
>Here you are (posted rather than mailed):

Thanks.  I'm always willing to consider a little constructive commentary.
How about this one -

/*
 * Copyright 1990, John F. Haugh II
 * All rights reserved.
 *
 * Permission is granted to copy and create derivative works for any
 * non-commercial purpose, provided this copyright notice is preserved
 * in all copies of source code, or included in human readable form
 * and conspicuously displayed on all copies of object code or
 * distribution media.
 */
-- 
John F. Haugh II                     UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                   Domain: j...@rpp386.cactus.org

Path: gmdzi!unido!mcsun!uunet!olivea!tymix!cirrusl!sun600!dhesi
From: dhesi%...@oliveb.ATC.olivetti.com (Rahul Dhesi)
Newsgroups: alt.sources.d
Subject: Re: copyright notices (was: Re: UNIX-Time the right way ...)
Message-ID: <2768@cirrusl.UUCP>
Date: 4 Dec 90 20:53:15 GMT
References: <18760@rpp386.cactus.org> <2757@cirrusl.UUCP> 
<18765@rpp386.cactus.org> <2761@cirrusl.UUCP> <18770@rpp386.cactus.org> 
<2762@cirrusl.UUCP> <18780@rpp386.cactus.org>
Sender: news@cirrusl.UUCP
Organization: Cirrus Logic Inc.
Lines: 8
Posted: Tue Dec  4 21:53:15 1990

In < 18...@rpp386.cactus.org> j...@rpp386.cactus.org (John F. Haugh II) 
writes:

[a new improved copyright statement]

GOOD!  Big improvement -- I like it!
--
Rahul Dhesi < dhesi%...@oliveb.ATC.olivetti.com>
UUCP:  oliveb!cirrusl!dhesi