Looking for a tool to make UNIX-Time

John F. Haugh II jfh at rpp386.cactus.org
Tue Nov 27 01:14:49 AEST 1990


In article <1948 at necisa.ho.necisa.oz> boyd at necisa.ho.necisa.oz (Boyd Roberts) writes:
>Here's a mktime() sort of thing based on calling localtime(3) and
>binary chopping on a guessed UNIX time long.  Ok?

gack!  you can compute it directly without looping.  if you don't
care about daylight savings time corrections you can get it without
ever having to call localtime.

this is what i use in "chage" to compute "days since the epoch".  don't
forget to multiply by (3600*24) to get seconds.  i've not completely
debugged this fragment - it is part of the next version of that command.

-- slice and dice as needed ... --
/*
 * Copyright 1990, John F. Haugh II
 * All rights reserved.
 *
 * Use, duplication, and disclosure prohibited without
 * the express written permission of the author.
 */

#include <sys/types.h>
#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.  they are declared so that january is 1, not 0.
 */

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

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

/*
 * strtoday - compute the number of days since 1970.
 *
 * the total number of days prior to the current date is
 * computed.  january 1, 1970 is used as the origin with
 * it having a day number of 0.  the gmtime() routine is
 * used to prevent confusion regarding time zones.
 */

long
strtoday (str)
char	*str;
{
	int	month;
	int	day;
	int	year;
	long	total;

	/*
	 * start by separating the month, day and year.  this is
	 * a chauvanistic program - it only takes date input in
	 * the standard USA format.
	 */

	if (sscanf (str, "%d/%d/%d", &month, &day, &year) != 3)
		return -1;

	/*
	 * the month, day of the month, and year are checked for
	 * correctness and the year adjusted so it falls between
	 * 1970 and 2069.
	 */

	if (month < 1 || month > 12)
		return -1;

	if (day < 1)
		return -1;

	if ((month != 2 || (year % 4) != 0) && day > days[month])
		return -1;
	else if (day > 29)
		return -1;

	if (year < 0)
		return -1;
	else if (year < 70)
		year += 2000;
	else if (year < 99)
		year += 1900;

	if (year < 1970 || year > 2069)
		return -1;

	/*
	 * the total number of days is the total number of days in all
	 * the whole years, plus the number of leap days, plus the
	 * number of days in the whole months preceding, plus the number
	 * of days so far in the month.
	 */

	total = ((year - 1970) * 365) + (((year + 1) - 1970) / 4);
	total += juldays[month] + (month > 2 && (year % 4) == 0 ? 1:0);
	total += day - 1;

	return total;
}
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh at rpp386.cactus.org
"SCCS, the source motel!  Programs check in and never check out!"
		-- Ken Thompson



More information about the Alt.sources.d mailing list