/*static char *SCCSID = "@(#)convtime.c	6.1 90/11/18";*/
/*static char *SCCSID = "@(#)convtime.c	13.2 90/01/25";*/
/***	convtime.c - map internal to external times and vice-versa
 */

#include <os2.h>

#include "api.h"
#include "const.h"
#include "chain.h"
#include "misc.h"
#include "buf.h"
#include "dir.h"
#include "pr_wrsub.h"
#include "pr_debug.h"

#define _70_to_80_bias	0x012CEA600L
#define SECS_IN_HALFDAY 0x0A8C0L
#define FOURYEARS	(3*365+366)

USHORT YrTab[] = { 366, 365, 365, 365 };

USHORT MonTab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

void Net_To_Dos_Date (TIME time, PDOSDATE pdd, PDOSTIME pdt)
{
    time -= _70_to_80_bias;

    {
	ULONG secs = time % (SECS_IN_HALFDAY * 2);
	USHORT usTmp;
	USHORT usTmp2;

	/* pdt->hours = (USHORT) (secs / (60 * 60)); */
#ifdef DEBUG
	if ((secs / 2) > 0x0000ffff) {
		dprintf ("Net_To_Dos_Date: secs too big\n");
	}
#endif
	pdt->hours = usTmp = ((USHORT) (secs / 2)) / 1800;

	/* usTmp = (USHORT) (secs % (60 * 60)); */
	usTmp = (USHORT) (secs - (ULONG) usTmp * (ULONG) (60 * 60));

	pdt->minutes = usTmp2 = usTmp / 60;

	/* pdt->seconds2 = (usTmp % 60) / 2 */
	pdt->seconds2 = (usTmp - (usTmp2 * 60)) / 2;
    }

    {
	ULONG days = time / (SECS_IN_HALFDAY * 2);
	USHORT i, j;
	USHORT usDays;

#ifdef DEBUG
	if ((days / FOURYEARS) * 4 > 0x0000ffff) {
		dprintf ("Net_To_Dos_Date: days too big\n");
	}
#endif
	pdd->years = (i = (USHORT) (days / FOURYEARS)) * 4;

	/* usDays = (USHORT) (days % FOURYEARS); */
	usDays = (USHORT) (days - (ULONG) i * (ULONG) FOURYEARS);

	/*
	 * Since usDays must be between 0 and FOURYEARS (sum of days in 4
	 * years), then we shouldn't need to do the mod in the loop below.
	 */
	i = 0;
	while (usDays >= YrTab[i]) {
	    usDays -= YrTab[i];

	    /* i = (i + 1) % 4; */
	    i++;
#ifdef DEBUG
	    if (i > 3) {
		dprintf ("Net_To_Dos_Date: going beyond YrTab\n");
	    }
#endif
	}

	/*  (days) = offset of days into year
	 *  (i) = offset into 4-year period
	 */

	pdd->years += i;

	MonTab[1] += i == 0;	    /* Make Feb longer some years */

	j = 0;
	while (usDays >= MonTab[j])
	    usDays -= MonTab[j++];

	MonTab[1] -= i == 0;	    /* Make Feb normal */

	/*  (usDays) = offset of days into month
	 *  (j) = month of year - 1
	 */

	pdd->months = j + 1;
	pdd->days = usDays + 1;
    }
}

TIME  Dos_To_Net_Date (DOSDATE dd, DOSTIME dt)
{
    LONG time;
    USHORT i;

    time = (dd.years / 4) * FOURYEARS +     /*	days in four-year blocks */
	   (dd.years % 4) * 365;	    /*	days in normal years */

    /*	If we're past the leap year in the four-year block or if
     *	we're past February then account for leap day
     */
    if ((dd.years % 4) != 0 || dd.months > 2)
	time++;

    /*	(time) is the number of days to the beginning of this year
     */
    for (i = 1; i < dd.months; i++)
	time += MonTab[i - 1];

    /*	(time) is the number of days to the beginning of this month
     */

    time += dd.days - 1;
    return ((time * 24 + dt.hours) * 60 + dt.minutes) * 60 + dt.seconds2 * 2 + _70_to_80_bias;
}
