/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: s_date.c,v 1.4 1997/09/25 17:58:19 ae59 Exp $ */
/*
$Log: s_date.c,v $
 * Revision 1.4  1997/09/25  17:58:19  ae59
 * Copyright-message
 *
 * Revision 1.3  1995/01/26  14:30:52  ae59
 * add time for GNU systems, time, strfmtime proto for sun gnu
 *
 * Revision 1.2  1994/09/20  08:38:54  ae59
 * time.h for GNU/EMX
 *
 * Revision 1.1  1994/01/27  13:24:35  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_date.c                              */
/*                                                              */
/*      Entries         : s_trng s_date(fmt)                    */
/*                        s_trng fmt                            */
/*                                                              */
/*      Arguments       : fmt  - format string                  */
/*                                                              */
/*      Description     : Return date and time from system      */
/*                        fmt-'' ctime result                   */
/*                        fmt='...' format string for strftime  */
/*                        function                              */
/*                                                              */
/*      Date            : 1993-12-30                            */
/*      1993-12-30      : time.h for hp added/undef of CLO. =b= */
/*      1993-12-28      : add LINT_ARGS            =b=          */
/*      1993-01-18      : new function             =b=          */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif
#if GNU_C+IBM_EMX_C+HP_9000_C+SUN4_GNU_C+SUN4_OS5_GNU_C
#ifdef CLOCKS_PER_SEC
#undef CLOCKS_PER_SEC
#endif
#include <time.h>
#endif
#if SUN4_GNU_C
/* only defined when c++ flag defined in 2.5.8 */
extern long unsigned int strftime (char *, long unsigned int , 
							const char *, const struct tm *);
extern long time (long *);
#endif
#ifdef LINT_ARGS
local s_trng s_date(s_trng fmt)
#else
local s_trng s_date(fmt)
s_trng fmt;
#endif
{
        time_t tsys;     /* date/time stucture */
        s_trng datestr;  /* p string structure */
        char *p;         /* help str           */
        size_t len;

        E_TPUSH("s_date")
        /*
	    * get time/date from system
	    */
        time(&tsys);

        if ( fmt.clen == 0 )        
	   {
	   	   	/*
	    		 * empty format string
	    		 * only simple date output
	    		 */
        		p = (char *) ctime(&tsys);

			/* skip cr , len should be 25*/
         	 	len = (p!=NULL) ? (strlen(p)-1) : 0; 
        }
	   else
	   {
			/* 
			 * formatted date/time requested
			 */
			 len = fmt.clen *2 +50; /* estimation for strlen */
			 if ((p = (char*) malloc(len * sizeof(char)))==NULL) len = 0;
			 else
			 {
				struct tm *tmnow;
				char help;
				tmnow = (struct tm*) gmtime( &tsys);
#ifdef HEAP_CHECK
b_geth((a_char *)&p,(a_char *)p,(a_char *)"s_date");
#endif
				/* real length of formatted string */
				help=fmt.ptr[fmt.clen];
				fmt.ptr[fmt.clen] = '\0';
			 	len = (size_t) strftime(p,len,(char*)fmt.ptr,tmnow);
				fmt.ptr[fmt.clen] = help;
			 }
	   }
        s_init(&datestr, len);

	   if (datestr.ptr!=NULL)
        {    /* this implies len > 0 */
        		memcpy((char*)datestr.ptr,p,len);
              	datestr.ptr[len] = '\0';
              	datestr.clen = len;
        }
	   else datestr.clen = 0;

	   /*
	    * free allocated buffer
	    */
	   if (fmt.clen!=0 && p!= NULL) 
	   {
#ifdef HEAP_CHECK
b_freh((a_char *)&p,(a_char *)p,(a_char *)"s_date");
#endif
			free(p);
	   }
	   /*
	    * clear temporary object
	    */
	   if (fmt.tmp) s_free(&fmt);
        E_TPOPP("s_date")
        return datestr;
}

