[comp.unix.ultrix] ULTRIX 1.2,2.2 date/time routines

doug@wbcs.UUCP (Doug Kratky) (04/21/89)

I am posting this for Jim Kamisky. He is unable to get the same results
using the time functions of ULTRIX 1.2 that he gets with the time
functions of ULTRIX 2.2. Please send your replies directly to me, and I
will pass them on to him. Thanks.


The program 'seconds.c' computes the number of seconds since the base
date of 01-JAN-1970 00:00:00 for a given input ascii date.

'Seconds.c' yields consistent results on three processors:
one with VMS V4.7, one with ULTRIX V2.2, and the other with ULTRIX V1.2.



Given a value for seconds, i.e., the current date as computed by
'seconds.c', the program 'get_date.c' returns inconsistent results on the
three processors.

The VMS machine returns the correct date and time (midnight).

The ULTRIX V2.2 machine returns a date and time exactly 6 hours
earlier than the correct date and time - viz., 18:00 of the previous day.

The ULTRIX V1.2 machine returns a time exactly 6 hours 40 minutes
earlier than the correct date and time - viz., 17:20 of the previous day.

Entering a value of '0' for the get_date program will return the true
base date of 01_JAN_1970_00:00:00 on the VMS system; it will return
the date 31_DEC-1969_18:00:00 on the ULTRIX V2.2 system; it will
return the date 31_DEC_1969_17:20:00 on the ULTRIX V1.2 system.



This is clearly a problem on the ULTRIX systems where accurate
time conversions are necessary for database queries.

I appreciate your help in resolving this problem.
                                                   === Jim ===




/*
   seconds.c
   input date - output seconds since base date: 01 JAN 70
   J. J. Kaminsky
   11-Apr-89
*/

#include <stdio.h>
#include <string.h>
#include <ctype.h>

main()
{

	float sec_per_year = 31536000.0;
	float sec_per_day = 86400.0;

	char in_string[81];
	char *delimiters = ",.-/ "; /* comma, period, dash, slash, space */
	char *mon;
	char lmon[4];
	char *day;
	char *year;

	double seconds = 0L;
	int idx, i;
	int flag = 0;
	int int_day, int_year, int_mon;
	int leap_year;
	int xyz;

	static int days_in_month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	static char *month_names[12] = {"jan","feb","mar","apr","may","jun",
                                        "jul","aug","sep","oct","nov","dec"};

	while (1)
	{
	seconds = 0L;     /* reinit value  */
	printf("\nEnter date mmm-dd-yyyy: ");
	gets(in_string);
	mon = strtok(in_string,delimiters);

	/* convert month input to lowercase  */
  	for (idx = 0; idx < 3; idx++) lmon[idx]=tolower(mon[idx]);

	day = strtok('\0',delimiters);
	year = strtok('\0',delimiters);
	printf("\nEntered date is %3s-%2s-%4s",mon,day,year);
	int_day = atoi(day);

	/* validate the date  */
	if (int_day < 1 || int_day > 31)
	{  printf("\nError in day field: %d\n",int_day);
	   exit(99); }

	int_year = atoi(year);

	/* validate the year  */
	if (int_year < 1900 || int_year > 3000)
	{  printf("\nError in year field: %d\n",int_year);
	   exit(99); }

	/* validate the month */
	for ( idx = 0; idx < 12; idx++ )
		 if ( strcmp(lmon,month_names[idx]) == 0 )
		 goto go_on;
	printf("\nerror in month field: %s",lmon);
	exit(99);

	go_on:

	/* accumulate the seconds per elapsed year here  */
	for (idx = 1970; idx < int_year; idx ++)
	{
	  seconds += sec_per_year;
	  if ( idx % 4 == 0 && idx % 100 != 0 || idx % 400 == 0 )
	     seconds += sec_per_day;
	}

	/* is the entered year a leap year ? */
	if ( int_year % 4 == 0 && int_year % 100 != 0 || int_year % 400 == 0 )
		leap_year = 1;
	else
		leap_year = 0;

	/* accumulate seconds for months in entered year  */
	for ( idx = 0; idx < 12; idx++ )
	{
		if ( strcmp(lmon,month_names[idx]) == 0 )
		{
		   for (i = 0; i < idx; i++)
		   {
		   seconds += days_in_month[i] * sec_per_day;
		   }
		/* add a day if a leap year and month > FEB  */
		if (leap_year && idx > 1) seconds += sec_per_day;
		}
	}

	/* add seconds for days in current month  */
	seconds += (int_day - 1) * sec_per_day;

	printf("\n\nSeconds: %f",seconds);
	}

exit();
}

/*****************************************************************************/

/* get_date.c
   input seconds since base date - output ascii date: dd_mmm_yy_hh:mm:ss_julian
   J. J. Kaminsky 12-Dec-88
*/

#include stdio
#include time

main()
{
  int time_value;

  static char *month[12] = {"JAN","FEB","MAR","APR",
                            "MAY","JUN","JUL","AUG",
                            "SEP","OCT","NOV","DEC"};

  struct tm *time_structure;

  while ( 1 )

  {

  printf("\n\nEnter seconds: ");
  scanf("%d",&time_value);

  time_structure = localtime(&time_value);

      printf("%02d-%s-%02d_%02d:%02d:%02d - %03d",
                    time_structure -> tm_mday,
                    month[time_structure -> tm_mon],
                    time_structure -> tm_year,
                    time_structure -> tm_hour,
                    time_structure -> tm_min,
                    time_structure -> tm_sec,
                    (time_structure -> tm_yday) + 1);
  }
  exit();
}

-- 
... US MAIL ....................... UUCP ...................................
Boeing Computer Services	...!scubed!ncr-sd!ncrwic!wbcs!doug
PO Box 7730, MS K79-32		...!bellcore!fenix!ncrlnk!ncrwic!wbcs!doug
Wichita, KS 67277-7730		...!hplabs!hp-sdd!ncr-sd!ncrwic!wbcs!doug