[comp.sys.atari.st] time.c time routines for ST

RDROYA01@ULKYVX.BITNET.UUCP (01/30/87)

 
/* time.c module to give the ST something a little more standard in
 * the way of time calls.  Add your own calls to get special time
 * arguments, or compile and make calls to time() to get the complete
 * time string.
 * Author:   Some by Robert Royar
 * Date:     Fri Jan 30 12:02:42 EST 1987 (that's the way a time string looks)
 * Compiled: Alcyon v 4.14 (the other compilers give you this free don't they?)
 * Bug reports: RDROYA01@ULKYVX.BITNET
 */
#include <stdio.h>
#include <osbind.h>
 
 
static int _day[2][12] = {
                        {0,31,59,90,120,151,181,212,243,273,304,334},
                        {0,31,60,91,121,152,182,213,244,274,305,335}
};
static char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                           "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
static char *wdays[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int __date, __time;
char *tzone = "EST";
 
typedef struct TIME {
        int     t_sec;          /* second */
        int     t_min;          /* minute */
        int     t_hr;           /* hour   */
        int     t_md;           /* day of month */
        int     t_mnt;          /* month */
        int     t_year;         /* year */
        int     t_wday;         /* day of week */
        int     t_yd;           /* day of year */
} TIME;
 
extern char *time();
extern TIME *ctime();
 
/* from the _C Programming Language_ by Kernighan and Ritchie */
 
year_day(day, month, year)
register int day, month, year;
{
        register int leap;
 
        leap = year%4 == 0 && year%100 != 0 || year%400 == 0;
        return(day+_day[leap][month]);
}
 
/* given a year and the day of the year (returned above) return an index to
 * the correct day of the week
 */
week_day(year,day)
register int year, day;
{
        /* this array covers 1980-2000. Each number indexes into the
         * days array for the first day of the year */
        static int firstdays[] = {2, 4, 5, 6, 0, 2, 3, 4, 5, 0, 1, 2, 3, 5,
                                  6, 0, 1, 3, 4, 5, 6};
        register int begdat;
 
        if (year < 1980)
                return(NULL);   /* everyday is sunday before 1980 */
        year -= 1980;
        begdat = firstdays[year];
        day = (begdat+day)-1;
        if (day < 7)
                return(day);
        else
                return(day%7);
 
}
 
/* return a pointer to a structure of type TIME after filling the structure
 * with the proper values.
 */
TIME *
ctime()
{
        static TIME tm;
 
        __date = Tgetdate();
        __time = Tgettime();
 
        tm.t_sec = (__time&31)*2;
        tm.t_min = (__time>>5)&63;
        tm.t_hr  = (__time>>11)&31;
        tm.t_md  = (__date&31);
        tm.t_mnt = ((__date>>5)&15)-1;
        tm.t_year = ((__date>>9)&31)+1980;
        tm.t_yd  = year_day(tm.t_md,tm.t_mnt,tm.t_year);
        tm.t_wday = week_day(tm.t_year,tm.t_yd);
        return(&tm);
}
 
/* create an ASCII fixed length string and copy it into timeline.  Caller
 * should make sure timeline has 30 characters space.
 */
char *
time(timeline)
char timeline[];
{
        extern TIME *ctime();
        extern char *getenv();
        register TIME *tm;
 
        /* write your own getenv(), but the ST env section is
         * a total farce with only 128 bytes for storage
         */
        tzone = getenv("timezone");
        tm = ctime();
        sprintf(timeline,"%s %s %02d %d:%02d:%02d %s %d\n",
        wdays[tm->t_wday],months[tm->t_mnt],tm->t_md,tm->t_hr,tm->t_min,
        tm->t_sec,tzone,tm->t_year);
        return(&timeline);
}