/*
==============================================================================
| (c) 1989, Sch”nSoft & John-Marc C. Arnau       CREATION DATE:  03/05/89    |
|----------------------------------------------------------------------------|
| MODULE: Date-Time Handling                     VERSION: 1.0   REVISION: 2  |
|----------------------------------------------------------------------------|
| COPYRIGHT NOTICE                                                           |
| ================                                                           |
|                                                                            |
| Everyone is permitted to copy and distribute verbatim copies               |
| of this license and source file, but changing it is not allowed.           |
| You can also use this wording to make the terms for other programs.        |
| The license agreements of most software companies keep you at the          |
| mercy of those companies.  By contrast, my license is intended to          |
| give everyone the right to share this source file. To make sure that       |
| you get the rights I want you to have, I need to make restrictions         |
| that forbid anyone to deny you these rights or to ask you to surrender     |
| the rights.  Hence this license agreement.                                 |
|                                                                            |
| Specifically, I want to make sure that you have the right to give          |
| away copies of this source file, that you receive source code or else      |
| can get it if you want it, that you can change this source file or use     |
| pieces of it in new free programs, and that you know you can do these      |
| things.                                                                    |
|                                                                            |
| To make sure that everyone has such rights, I have to forbid you to        |
| deprive anyone else of these rights. For example, if you distribute        |
| copies of this source file, you must give the recipients all the rights    |
| that you have. You must make sure that they, too, receive or can get the   |
| source code. And you must tell them their rights.                          |
|                                                                            |
| Also, for my own protection, I must make certain that everyone             |
| finds out that there is no warranty for this source file. If this source   |
| file is modified by someone else and passed on, I want its recipients      |
| to know that what they have is not what I distributed, so that any problems| 
| introduced by others will not reflect on my reputation.                    |
|                                                                            |
| Therefore I, JOHN-MARC C. ARNAU make the following terms which say what    |
| you must do to be allowed to distribute or change this source file.        |
|                                                                            |
|                                                                            |
|			COPYING POLICIES                                     |
|                                                                            |
| 1. You may copy and distribute verbatim copies of this source code         |
| as you receive it, in any medium, provided that you conspicuously and      |
| appropriately publish on each copy a valid copyright notice "Copyright     |
| (C) 1989 John-Marc C. Arnau." (or with whatever year is appropriate);      |
| keep intact the notices on all files that refer to this License Agreement  |
| and to the absence of any warranty; and give any other recipients of the   |
| source file a copy of this License Agreement along with the program.       |
| You may charge a distribution fee for the physical act of transferring     |
| a copy.                                                                    |
|                                                                            |
| 2. You may modify your copy or copies of this source file or any portion   |
| of it, and copy and distribute such modifications under the terms of       |
| Paragraph 1 above, provided that you also do the following:                |
|                                                                            |
|   a) cause the modified files to carry prominent notices stating           |
|   that you changed the files and the date of any change; and               |
|                                                                            |
|   b) cause the whole of any work that you distribute or publish,           |
|   that in whole or in part contains or is a derivative of this source      |
|   file or any part thereof, to be licensed at no charge to all third       |
|   parties on terms identical to those contained in this License            |
|   Agreement (except that you may choose to grant more extensive            |
|   warranty protection to some or all third parties, at your option).       |
|                                                                            |
|   c) You may charge a distribution fee for the physical act of             |
|   transferring a copy, and you may at your option offer warranty           |
|   protection in exchange for a fee.                                        |
|                                                                            |
|   Mere aggregation of another unrelated program with this program          |
|   (or its derivative) on a volume of a storage or distribution medium      |
|   does not bring the other program under the scope of these terms.         |
|                                                                            |
| 3. You may copy and distribute this source file (or a portion or           |
| derivative of it, under Paragraph 2) in object code or executable form     |
| under the terms of Paragraphs 1 and 2 above provided that you also         |
| do one of the following:                                                   |
|                                                                            |
|   a) accompany it with the complete corresponding machine-readable         |
|   source code, which must be distributed under the terms of                |
|   Paragraphs 1 and 2 above; or,                                            |
|                                                                            |
|   b) accompany it with a written offer, valid for at least three           |
|   years, to give any third party free (except for a nominal                |
|   shipping charge) a complete machine-readable copy of the                 |
|   corresponding source code, to be distributed under the terms of          |
|   Paragraphs 1 and 2 above; or,                                            |
|                                                                            |
|   c) accompany it with the information you received as to where the        |
|   corresponding source code may be obtained.	(This alternative is         |
|   allowed only for noncommercial distribution and only if you              |
|   received the program in object code or executable form alone.)           |
|                                                                            |
| For an executable file, complete source code means all the source code     |
| for all modules it contains; but, as a special exception, it need not      |
| include source code for modules which are standard libraries that          |
| accompany the operating system on which the executable file runs.          |
|                                                                            |
| 4. You may not copy, sublicense, distribute or transfer this source file   |
| except as expressly provided under this License Agreement.  Any attempt    |
| otherwise to copy, sublicense, distribute or transfer this source file     |
| is void and your rights to use the program under this License agreement    |
| shall be automatically terminated.  However, parties who have received     |
| computer software programs from you with this License Agreement will       |
| not have their licenses terminated so long as such parties remain in       |
| full compliance.                                                           |
|                                                                            |
| 5. If you wish to incorporate parts of this source file into other         |
| free programs whose distribution conditions are different, write to        |
| John-Marc C. Arnau, P.O. Box 14208  08080-BARCELONA (Cathalonia) EUROPE,   |
| or else EMAIL me at Compuserve N§ 70441,2164. I will be guided by the      |
| two goals of preserving the free status of all derivatives of my free      |
| software and of promoting the sharing and reuse of software.               |
|                                                                            |
| Your comments and suggestions about my licensing policies and my           |
| software are welcome!  Please contact the John-Marc C. Arnau,              |
| P.O. Box 14208  08080-BARCELONA (Cathalonia) EUROPE, or else EMAIL me at   |
| Compuserve N§ 70441,2164.                                                  |
|                                                                            |
|         		       NO WARRANTY                                   |
|                                                                            |
| BECAUSE THIS SOURCE FILE IS LICENSED FREE OF CHARGE, I PROVIDE ABSOLUTELY  |
| NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE INTERNATIONAL LAW.EXCEPT|
| WHEN OTHERWISE STATED IN WRITING, JOHN-MARC C. ARNAU AND/OR OTHER PARTIES  |
| PROVIDE THIS SOURCE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED |
| OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF       |
| MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS   | 
| TO THE QUALITY AND PERFORMANCE OF THIS SOURCE FILE IS WITH YOU.  SHOULD    |
| THIS SOURCE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 
| REPAIR OR CORRECTION.                                                      |
|                                                                            |
| IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL JOHN-MARC C. ARNAU      |
| AND/OR ANY OTHER PARTY WHO MAY MODIFY AND REDISTRIBUTE THIS SOURCE FILE    |
| AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST       |
| PROFITS, LOST MONIES, OR OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL        |
| DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE (INCLUDING BUT NOT      |
| LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES        |
| SUSTAINED BY THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH     |
| ANY OTHER PROGRAMS) THIS SOURCE FILE, EVEN IF YOU HAVE BEEN ADVISED OF     |
| THE POSSIBILITY OF SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.      |
|                                                                            |
| This copyright notice has been modified from R. Stallman & FREE SOFTWARE   |
| FOUNDATION, INC. general public license.                                   |
|                                                                            |
| NAME                                                                       |
|   SH™N DATE-TIME HANDLING SYSTEM                REF: 001-890727            |
|                                                                            |
| DESCRIPTION                                                                |

	Many of you have probably got into trouble trying to cope with
dates and times while hoping to bring a nice piece of programming to
life. So have I. That's why I have set up this tiny date-time handling
system. There was a particular point that woried me:  should I use UNIX(R)
like date-time internal representation or, else, should I move to a
standard that would handle my grandfather's birthdate ?. Finnaly I decided
to implement both, and in the end I've got a code full of conditional
compilation commands. It works anyway !!!.

	COMPILATION HINTS
	=================

	This code has been developed and tested under Turbo C 2.0
compiler. Let's hope it will run under any ANSI compatible C compiler
with a proper standard library. By the way, I hate C compilers without 
function prototyping. Don't you ?.

	The only thing you have to care abouth when compiling this file
is getting the proper definitions according to your own needs:

	1) LANGUAGE:

	You can choose between: ENGLISH, CATHALAN AND SPANISH. No doubt 
there's room for many others if you have the time to implement them.

	2) DATE-TIME REPRESENTATION:

	Two representations are currently available: UNIX(R) like and
LOTUS(R) like. The later works with doubles, which means it's slower but
can handle dates from 1900 up to .... . If you redefine BEGINYEAR you'll
be able to deal with other time periods. Nevertheless double values have
plenty of room to hold large periods without overflow.

	3) TEST

	If you define TEST constant you'll get a nice program to show
you how all this stuff works.

	For more info refer to code DEFINES section .

	FUNCTIONS
	=========

	You have only three functions to care abouth:

	1) Sch001DateToNum( ... )
	2) Sch001NumToDate( ... )
	3) Sch001DatCnv( ... )

  	FUNCTION 1) 
        -----------

	Sch001DateToNum(int Day, int Mon, int Yea, int Sec, int Min, int Hou);

	Just suply a Day, Month, Year, Second, Minute and Hour and
you'll get a nice time_t as a return value. If you work with LOTUS(R)
representation you'll get a double.

  	FUNCTION 2)
        -----------

	static int	Sch001NumToDate(double Num,
				int *Day, int *Mon, int *Yea,
				int *WDay,
				int *Sec, int *Min, int *Hou
				);

	This function will only be usefull if you use LOTUS(R)
representation. You suply a double and you get in turn the Day, Month,
etc. Remember the integer part of the double represents the number of
days elapsed since 1900 and the fraction represents the time of the day.
Refer to 123 (R) reference manual for further explanations.

	FUNCTION 3)
        -----------

	UNIX REPRESENTATION

	char *Sch001DatCnv(char *Req, long Dat);

	LOTUS REPRESENTATION

	char *Sch001DatCnv(char *Req, double Dat);

	You suply a request string wich can hold any caracters plus any
combination of the following keywords:
	
	KEYWORD		MEANING
        --------------------------------------
	"DD",		   Day numeric   
	"HH",              Hour numeric   
	"MI",              Minutes numeric   
	"MM",              Month numeric   
	"YYYY",            Year long figure   
	"YY",   	   Year short figure   
	"DAY",		   Day string   
	"MONTH",  	   Month string   
	"SHORTMONTH",	   Month string short   
	"SS",		   Seconds numeric   
	"YEAR", 	   Year string   

	Then you suply a long or double (depends on the model you
choose) and you get a nice string with all your keywords substituted
with their actual values. Example:


	You request:    Sch001DatCnv("Today is: DD of MONTH, YYYY", 32456,234); 

	You get:	"Today is: 09 November 1988"

	Nice, isn't it. Have fun ... I'm looking forward to hearing from
you. 


                                                 John
|                                                                            |
| NOTES                                                                      |
|                                                                            |
|   1) If you ever wish to contact me regarding this source file please      |
|   state the following REFERENCE: 001-890727                                |
|                                                                            |
|   2) This is just a pice of a whole library of C code where all "callable" |
|   functions begin with "Sch" followed by a 3 alphanumeric code.            |
|                                                                            |
|   3) I am willing to cooperate in any software project concerning Medical  |
|   science. I would apreciate any suggestions concerning this possibility.  |
|                                                                            |
| AUTHOR(s)                                                                  |
|                                                                            |
|   John-Marc C. ARNAU                                                       |
|                                                                            |
|   P.O. Box 14208                                                           |
|   08080-BARCELONA (Cathalonia)                                             |
|   EUROPE                                                                   |
|                                                                            |
|   Compuserve: 70441,2164                                                   |
|                                                                            |
| ACKNOWLEDGMENTS                                                            |
|                                                                            |
|   Oliver Laumann, Technical University of Berlin, Germany.                 |
|     For his contribution to converting tm structures to t_time.            |
|                                                                            |
|  Judson D. McClendon                                                       |
|     For WeekDat function.                                                  |
|                                                                            |
|----------------------------------------------------------------------------|
| REVISIONS                                                                  |
|                                                                            |
|                                                                            |
==============================================================================
*/

/*
===========================================
INCLUDE FILES
===========================================
*/

#include <time.h>
#include <math.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
===========================================
DEFINITIONS
===========================================
*/

/* Select one language ENGLISH CATHALAN SPANISH*/

#define ENGLISH	 1

/* Select one date-time representation (UNIX_TIM or LOTUS_TIM) */

#define LOTUS_TIM 1

/* Select TEST if you are willing to test this code */

#define TEST	  1

/*
 *    **************************************************
 *    *                                                *
 *    *              Function Prototypes               *
 *    *                                                *
 *    **************************************************
         ALL LOCAL FUNCTIONS CARRY A STATIC KEYWORD
 */




#ifdef UNIX_TIM
time_t 		Sch001DateToNum(int Day, int Mon, int Yea,
			int Sec, int Min, int Hou);

char 		*Sch001DatCnv(char *Req, long Dat);
#endif

#ifdef LOTUS_TIM
double 		Sch001DateToNum(int Day, int Mon, int Yea,
			int Sec, int Min, int Hou);


int		Sch001NumToDate(double Num,
			int *Day, int *Mon, int *Yea,
			int *WDay,
			int *Sec, int *Min, int *Hou
			);

char 		*Sch001DatCnv(char *Req, double Dat);
#endif

static int  WeekDay(int Day, int Mon, int Yea);
static char *ShoDay(struct tm *Tim);
static char *ShoMon(struct tm *Tim);
static char *ShoYea(struct tm *Tim);
static char *ShoLonYea(struct tm *Tim);
static char *ShoStrDay(struct tm *Tim);
static char *ShoStrMon(struct tm *Tim);
static char *ShoShortStrMon(struct tm *Tim);
static char *ShoStrYea(struct tm *Tim);
static char *ShoHou(struct tm *Tim);
static char *ShoMin(struct tm *Tim);
static char *ShoSec(struct tm *Tim);

/*
 *    **************************************************
 *    *                                                *
 *    *                   Constants                    *
 *    *                                                *
 *    **************************************************
 */

#define BEGINYEAR  1900    /* Beginning year */
#define ENDYEAR    5000    /* Ending year    */



/* 
===========================================
LOCAL DATA
===========================================
*/

                           /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
static char DaysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static char *month[] = {
#ifdef ENGLISH
       "January",
       "February",
       "March",
       "April",
       "May",
       "June",
       "July",
       "August",
       "September",
       "October",
       "November",
       "December"
#endif
#ifdef CATHALAN
       "Gener",
       "Febrer",
       "Mar‡",
       "Abril",
       "Maig",
       "Juny",
       "Juliol",
       "Agost",
       "Septembre",
       "Octubre",
       "Novembre",
       "Decembre"       
#endif
#ifdef SPANISH
       "Enero",
       "Febrero",
       "Marzo",
       "Abril",
       "Mayo",
       "Junio",
       "Julio",
       "Agosto",
       "Setiembre",
       "Octubre",
       "Noviembre",
       "Diciembre"
#endif
};

static char *ShortMon[] = {
#ifdef ENGLISH
       "Jan",
       "Feb",
       "Mar",
       "Apr",
       "May",
       "Jun",
       "Jul",
       "Aug",
       "Sep",
       "Oct",
       "Nov",
       "Dec"
#endif
#ifdef CATHALAN
       "Gen",
       "Feb",
       "Mar",
       "Abr",
       "Mai",
       "Jun",
       "Jul",
       "Ago",
       "Sep",
       "Oct",
       "Nov",
       "Dec"
#endif
#ifdef SPANISH
       "Ene",
       "Feb",
       "Mar",
       "Abr",
       "May",
       "Jun",
       "Jul",
       "Ago",
       "Set",
       "Oct",
       "Nov",
       "Dic"
#endif
};

static char *wkday[] = {
#ifdef ENGLISH
       "Sunday",
       "Monday",
       "Tuesday",
       "Wednesday",
	"Thursday",
       "Friday",
       "Saturday"
#endif
#ifdef SPANISH
       "Domingo",
       "Lunes",
       "Martes",
       "Miercoles",
       "Jueves",
       "Viernes",
       "Sabado"
#endif              
#ifdef CATHALAN
       "Diumenge",
       "Dilluns",       
       "Dimarts",      
       "Dimecres",
       "Dijous",
       "Divendres",
       "Disabte"
#endif
};

static char *ShoWeeDay[] = {
#ifdef ENGLISH
       "Sun",
       "Mon",
       "Tue",
       "Wed",
       "Thu",
       "Fri",
       "Sat"
#endif
#ifdef SPANISH
       "Dom",
       "Lun",
       "Mar",
       "Mie",
       "Jue",
       "Vie",       
       "Sab"
#endif       
#ifdef CATHALAN
       "Diu",
       "Dil",
       "Dim",      
       "Die",
       "Dij",
       "Div",
       "Dis"
#endif
};

/* Date Time formating keywords */

static char *KeyWor[]=
{

	"DD",		/* Day numeric */
	"HH",           /* Hour numeric */
	"MI",           /* Minutes numeric */
	"MM",           /* Month numeric */
	"YYYY",         /* Year long figure */
	"YY",   	/* Year short figure */
	"DAY",		/* Day string */
	"MONTH",  	/* Month string */
	"SHORTMONTH",	/* Month string short */
	"SS",		/* Seconds numeric */
	"YEAR", 	/* Year string */
	0
};

/* Function pointers associated with above keywords */

static char *(* KeyAct [])()=
{
	ShoDay,
	ShoHou,
	ShoMin,
	ShoMon,
	ShoLonYea,
	ShoYea,
	ShoStrDay,
	ShoStrMon,
	ShoShortStrMon,
	ShoSec,
	ShoStrYea,
	0
};


/*
===========================================
LOCAL CODE * LEVEL 0
===========================================
*/


/* Return 1 if `y' is a leap year, 0 otherwise.
 */

static int IsLeap(int Yea)
{
    Yea += 1900;
    if (Yea % 400 == 0)
        return (1);
    if (Yea % 100 == 0)
        return (0);
    return (Yea % 4 == 0);
}

#ifdef UNIX_TIM

/* Return the number of days between Jan 1, 1970 and the given
 * broken-down time.
 */

static int ndays (int Day, int Mon, int Yea)
{
register n = Day;
register m, y;

    for (y = 70; y < Yea; ++y)
    {
        n += 365;
        if (IsLeap (y)) ++n;
    }
    for (m = 0; m < Mon; ++m)
	n += DaysInMonth[m] + (m == 1 && leap (y));

    return (n);
}

/* Convert a broken-down time (such as returned by localtime())
 * back into a `time_t'.
 */

static time_t Sch001DateToNum(int Day, int Mon, int Yea,
			int Sec, int Min, int Hou
			)
{
register int m1, m2;
time_t t;
struct tm *otm;

	t = (ndays (Day, Mon, Yea) - 1) * 86400L + Hou * 3600L
		+ Min * 60 + Sec;

	/*
	* Now the hard part -- correct for the time zone:
	*/


	otm = localtime (&t);

	m2 = Hou * 60 + Min;
	m1 = otm->tm_hour * 60 + otm->tm_min;
	t -= ((m1 - m2 + 720 + 1440) % 1440 - 720) * 60L;

	return (t);
}

#endif

#ifdef LOTUS_TIM

static int WeekDay(int Day, int Mon, int Yea)

{

	if (Mon < 3)
	{
		Yea -= 1;
		Mon += 12;
	}

	return( (Day + 1
		+ (2 * Mon)
		+ ((Mon + 1) * 3 / 5)
		+ (Yea)
		+ (Yea / 4)
		- (Yea / 100)
		+ (Yea / 400)
		) % 7);

}



static double Sch001DateToNum(int Day, int Mon, int Yea,
			int Sec, int Min, int Hou
			)
{
int	Cnt;
long	Ret;
double	GloRet = 0.0;

	for(Cnt = BEGINYEAR, Ret = 0; Cnt < Yea; Cnt++)
	{
	      if ((Cnt % 400) == 0)
	         Ret += 366;
	      else if ((Cnt % 100) == 0)
	         Ret += 365;
	      else if ((Cnt % 4) == 0)
		 Ret += 366;
	      else
	         Ret += 365;
	}

	if ((Yea % 400) == 0)
		DaysInMonth[1] = 29;
	else if ((Yea % 100) == 0)
		DaysInMonth[1] = 28;
	else if ((Yea % 4) == 0)
		DaysInMonth[1] = 29;
	else
		DaysInMonth[1] = 28;

	for(Cnt = 0; Cnt < Mon; Cnt++)
	{
	         Ret += DaysInMonth[Cnt];
	}

	Ret += Day + 1;

	/* Compute sec min hou */
	GloRet = 	Hou / 24.0;
	GloRet += 	Min / (24.0 * 60.0);
	GloRet += 	Sec / (24.0 * 60.0 * 60.0);

	GloRet += (double)Ret;
	return((double) GloRet);
}


static int	Sch001NumToDate(double Num,
			int *Day, int *Mon, int *Yea,
			int *WDay,
			int *Sec, int *Min, int *Hou
			)
{
int	Cnt;
long	Ret;
long 	Goal;
double  Tim, Dummy;

	Goal = (long)Num;
	Tim  = modf(Num, &Dummy);

	/* Compute Yea Mon Day */

	for(Cnt = BEGINYEAR, Ret = 0; Ret < Goal; Cnt++)
	{
	      if ((Cnt % 400) == 0)
	         Ret += 366;
	      else if ((Cnt % 100) == 0)
	         Ret += 365;
	      else if ((Cnt % 4) == 0)
		 Ret += 366;
	      else
	         Ret += 365;
	}

	Cnt--;
	*Yea = Cnt;

	if ((Cnt % 400) == 0)
	{
		Ret -= 366;
		DaysInMonth[2] = 29;
	}
	else if ((Cnt % 100) == 0)
	{
		Ret -= 365;
		DaysInMonth[1] = 28;
	}
	else if ((Cnt % 4) == 0)
	{
		Ret -= 366;
		DaysInMonth[1] = 29;
	}
	else
	{
		Ret -= 365;
		DaysInMonth[1] = 28;
	}

	for(Cnt = 0; Ret < Goal; Cnt++)
	{
	         Ret += DaysInMonth[Cnt];
	}
	Cnt--;
	*Mon = Cnt;

	Ret -= DaysInMonth[Cnt];

	*Day = (int)(Goal - Ret - 1);

	/* Compute week day */

	*WDay = WeekDay(*Day, *Mon, *Yea);

	/* Compute hour min and sec */

	Dummy 	= Tim * 24.0;
	*Hou 	= (int)Dummy;
	Tim	= modf(Dummy, &Dummy);
	Dummy	= Tim * 60;
	*Min	= (int)Dummy;
	Tim	= modf(Dummy, &Dummy);
	Dummy	= Tim * 60;
	*Sec	= (int)Dummy;
	*Sec 	+= 1;
	return(0);
}
#endif

/*
===========================================
LOCAL CODE * LEVEL 1
===========================================
*/

static char *ShoDay(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	sprintf(TmpBuf, "%02d", Tim->tm_mday);
	return(TmpBuf);
}

static char *ShoMon(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	sprintf(TmpBuf, "%02d", Tim->tm_mon + 1);
	return(TmpBuf);
}

static char *ShoYea(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

#ifdef UNIX_TIM
	sprintf(TmpBuf, "%02d", Tim->tm_year);
#endif
#ifdef LOTUS_TIM
	sprintf(TmpBuf, "%02d", Tim->tm_year-BEGINYEAR);
#endif

	return(TmpBuf);
}

static char *ShoLonYea(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

#ifdef UNIX_TIM
	sprintf(TmpBuf, "%02d%02d",
		Tim->tm_year > 70 ? 19: 20,
		Tim->tm_year);
#endif
#ifdef LOTUS_TIM
	sprintf(TmpBuf, "%04d", Tim->tm_year);
#endif

	return(TmpBuf);
}

static char *ShoStrDay(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];	

	sprintf(TmpBuf, "%s",
		wkday[Tim->tm_wday]);
	return(TmpBuf);
}

static char *ShoStrMon(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	sprintf(TmpBuf, "%s",
		month[Tim->tm_mon]);
	return(TmpBuf);
}

static char *ShoShortStrMon(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	sprintf(TmpBuf, "%s",
		ShortMon[Tim->tm_mon]);
	return(TmpBuf);
}

static char *ShoStrYea(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	strcpy(TmpBuf, "NOT AVAILABLE YET"); 		
	return(TmpBuf);
}


static char *ShoHou(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	sprintf(TmpBuf, "%02d", Tim->tm_hour);
	return(TmpBuf);
}

static char *ShoMin(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	sprintf(TmpBuf, "%02d", Tim->tm_min);
	return(TmpBuf);
}

static char *ShoSec(Tim)
struct tm *Tim;
{
static 	char 		TmpBuf[18];

	sprintf(TmpBuf, "%02d", Tim->tm_sec);
	return(TmpBuf);
}

/*
===========================================
FUNCTION
===========================================
*/

#ifdef UNIX_TIM
char *Sch001DatCnv(char *Req, long Dat)
#endif

#ifdef LOTUS_TIM
char *Sch001DatCnv(char *Req, double Dat)
#endif

{
static 	char 		Res[100];
	char 		*ReqPtr, *ResPtr;
	char		TmpBuf[100];
	int		Cnt, Ski;
	long 		Clk;
#ifdef UNIX_TIM
	struct tm 	*Tim;
#endif
#ifdef LOTUS_TIM
	struct tm 	Tim;
#endif



#ifdef UNIX_TIM
       Tim = localtime(&Dat);
#endif

#ifdef LOTUS_TIM
	Sch001NumToDate(Dat,
		&Tim.tm_mday, &Tim.tm_mon, &Tim.tm_year,
		&Tim.tm_wday,
		&Tim.tm_sec, &Tim.tm_min, &Tim.tm_hour
		);
#endif

	ReqPtr = Req;
	ResPtr = Res;

	while(*ReqPtr)
	{
		for(Ski = 0, Cnt = 0; KeyWor[Cnt]; Cnt++)
		{
			if(!strncmp(KeyWor[Cnt], ReqPtr, strlen(KeyWor[Cnt])))
			{
#ifdef UNIX_TIM				
				strcpy(TmpBuf, (* KeyAct[Cnt])(Tim));
#endif
#ifdef LOTUS_TIM
				strcpy(TmpBuf, (* KeyAct[Cnt])(&Tim));
#endif
				strcpy(ResPtr, TmpBuf);
				ReqPtr += strlen(KeyWor[Cnt]);
				ResPtr += strlen(TmpBuf);
				Ski    = 1;
			}
		}

		if(!Ski)
			*ResPtr++ = *ReqPtr++;
	}
	ResPtr++;
	*ResPtr = '\0';
	return(Res);
}


/*
===========================================
TEST CODE
===========================================
*/

#ifdef TEST
main()
{
char	Tpl[100];	/* Template */

#ifdef UNIX_TIM
long 	Dat;
struct tm 	*Tim;
#endif

#ifdef LOTUS_TIM
double	Dat;
struct tm 	Tim;
#endif

	printf("***Sch”n Date-Time Handling system***\n");
	printf("(c)1989 Sch”nSoft & John-Marc C. Arnau\n");

	printf("Template ?: ");
	scanf("%[^\n]", Tpl);
	printf("Date ....?: ");

#ifdef UNIX_TIM
	scanf("%ld", &Dat);
#endif

#ifdef LOTUS_TIM
	scanf("%lg", &Dat);
#endif

	printf("Result ...: %s\n", Sch001DatCnv(Tpl, Dat));

#ifdef LOTUS_TIM
	Sch001NumToDate(Dat,
		&Tim.tm_mday, &Tim.tm_mon, &Tim.tm_year,
		&Tim.tm_wday,
		&Tim.tm_sec, &Tim.tm_min, &Tim.tm_hour
		);
	Dat = Sch001DateToNum(Tim.tm_mday, Tim.tm_mon, Tim.tm_year,
		Tim.tm_sec, Tim.tm_min, Tim.tm_hour
		);
	printf("Rev. TEST : %5.6lg\n",
		Dat);
#endif

#ifdef UNIX_TIM
	Tim = localtime(&Dat);
	Dat = Sch001DateToNum(Tim->tm_mday, Tim->tm_mon, Tim->tm_year,
		Tim->tm_sec, Tim->tm_min, Tim->tm_hour
		);
	printf("Rev. TEST : %ld\n",
		Dat);

#endif

	printf("******** END OF TEST ***********\n");
	printf("Press CR to end\n");
	getchar();
	getchar();
}
#endif

