/**** 
 ****   pp  Amiga PagePrint Program
 ****   Version 1.0
 ****/
 
/***
 ***    (C) Copyright 1986  Phil Mercurio (Quicksilver Software)
 ***    All Rights Reserved
 ***
 ***    This software is released as a shareware product.  It may
 ***    be copied providing that no charge is made to the recipient
 ***    and that this copyright notice remains intact.
 ***
 ***    If you use and find value in this software, please send $10
 ***    to the author at this address:
 ***
 ***        Phil Mercurio
 ***        Quicksilver Software
 ***        2515 #9 Camino Del Mar
 ***        Del Mar, CA  92014
 ***
 ***
 ***    Be sure to include your name and address, as this will place
 ***    you on the Quicksilver Software mailing list.  Your support
 ***    of shareware products will encourage the further production
 ***    of low-cost software.
 ***/

/**
 ** Routines to parse a DateStamp structure into useful bits.
 **
 ** Quicksilver Software
 **/

#include <exec/types.h>
#include <libraries/dos.h>


#define DayOffset	(0)
#define Leap(yr)    ((yr-1976) % 4 == 0)

char *Weekday[] = {
    "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    };

char *Month[] = {
    "Jan", "Feb", "Mar", "Apr", "May", "Jun",
    "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
    };

int DaysPerMonth[] = {
    31, 28, 31, 30, 31, 30,
    31, 31, 30, 31, 30, 31
    };

int LeapDaysPerMonth[] = {
    31, 29, 31, 30, 31, 30,
    31, 31, 30, 31, 30, 31
    };

/*
 *  Parse the date from the DateStamp *ds into the current year,
 *  month, day, and weekday.  The ranges will be as follows:
 *
 *      Year:       [1978..?]
 *      Month:      [1..12] == [Jan..Dec]
 *      Day:        [1..31]
 *      Weekday:    [0..6] == [Sun..Sat]
 *
 */
parseDate(ds,pY,pM,pD,pWD)
struct DateStamp *ds;
int *pY, *pM, *pD, *pWD;
{
    register int year, month, day, weekday;
#ifdef Trace
    printf("parseDate(%lx,%lx,%lx,%lx,%lx)\n",ds, pY, pM, pD, pWD);
#endif Trace

    day = ds->ds_Days;
    weekday = day % 7 + DayOffset;

    for(year=1978; ; year++) {
        if(Leap(year)) {
	    if(day < 366)
	        break;
	    else
	        day -= 366;
	    }
	else {
	    if(day < 365)
	        break;
	    else
	        day -= 365;
	    }
        }
        
    if(Leap(year)) {
        for(month=0; day > LeapDaysPerMonth[month]; month++)
            day -= LeapDaysPerMonth[month];
        }
    else {
        for(month=0; day > DaysPerMonth[month]; month++)
            day -= DaysPerMonth[month];
        }

    month++;
    day++;
    
    *pY = year;
    *pM = month;
    *pD = day;
    *pWD = weekday;
}

/*
 *  Form a string containing the date from the DateStamp *ds.
 */
dateStr(s,ds)
char *s;
struct DateStamp *ds;
{
    int year, month, day, weekday;
#ifdef Trace
    printf("dateStr(%s,%lx)\n",s,ds);
#endif Trace

    parseDate(ds,&year,&month,&day,&weekday);
    
    sprintf(s,"%s %s %d, %d",Weekday[weekday],Month[month-1],day,year);
}

/*
 *  Parse the DateStamp *ds into hours, minutes and seconds
 */
parseTime(ds,pH,pM,pS)
struct DateStamp *ds;
int *pH, *pM, *pS;
{
    register int hours, minutes, seconds;
#ifdef Trace
    printf("parseTime(%lx,%lx,%lx,%lx)\n",ds,pH,pM,pS);
#endif Trace

    seconds = ds->ds_Tick / 50;
    seconds %= 60;
    
    minutes = ds->ds_Minute;
    hours = minutes / 60;
    minutes %= 60;
    
    *pH = hours;
    *pM = minutes;
    *pS = seconds;
}
    
/*
 *  Get the time from the DateStamp *ds and write it into
 *  the string s.
 */
timeStr(s,ds)
char  *s;
struct DateStamp *ds;
{
    int hours, minutes, seconds;
#ifdef Trace
    printf("timeStr(%s,%lx)\n",s,ds);
#endif Trace

    parseTime(ds,&hours,&minutes,&seconds);

    sprintf(s,"%02d:%02d:%02d",hours,minutes,seconds);
}

/*
 * Get the FileInfoBlock *fib of the file *fileName.
 */
getFIB(fileName,fib)
char *fileName;
struct FileInfoBlock *fib;
{
    BPTR fileLock;
#ifdef Trace
    printf("getFIB(%s,%lx)\n",fileName,fib);
#endif Trace

    fileLock = Lock(fileName,ACCESS_READ);

    if(fileLock == NULL || !Examine(fileLock,fib))
        return(0);

    UnLock(fileLock);
    return(1);
}
