
/* 
 * File: format.c
 *       Part of the ever expanding tea time library.
 *
 * Decsription: This file contains routines to play with date formatting.
 *              tt_format():  format a date
 *              tt_weekday(): get string for day of week
 *              tt_month():   get string for month of year
 *
 * Author: Tom Coppeto  11/10/89
 *
 * $Source: /afs/.net.mit.edu/tools/src/tea/RCS/format.c,v $
 * $Author: tom $ 
 * $Locker: tom $
 * $Log:	format.c,v $
 * Revision 2.0  90/05/23  13:01:45  tom
 * fixed ultrix ifdefs
 * 
 * Revision 1.3  90/02/15  19:33:45  tom
 * ifdef timezone stuff - not in ultrix
 * 
 * Revision 1.2  89/12/17  21:16:29  tjcoppet
 * Added rules for fixed width output (2foo and 02foos)
 * 
 * Revision 1.1  89/11/13  20:49:40  tjcoppet
 * Initial revision
 * 
 * $Header: /afs/.net.mit.edu/tools/src/tea/RCS/format.c,v 2.0 90/05/23 13:01:45 tom Exp Locker: tom $
 */

#include <stdio.h>
#include <teatime.h>
#include <tzfile.h>

extern char *wday[], *month[];

/*
 * Function:    tt_format()
 * Decsription: formats a string for given date. If given format rule
 *              is omitted (NULL), a default is used. The format string
 *              uses sprintf like rules.
 * Returns:     pointer to string (max storage 100 chars)
 * Notes:       Currently ignores given control strings.
 */

char *
tt_format(time_info,format)
     struct tm *time_info;
     char *format;
{
  char buf[BUFSIZ];
  char *bufP = buf;
  char c;
  int hour;
  int i;

  hour = time_info->tm_hour;
  if(hour > 12)
    hour -= 12;

  if(format == (char *) NULL)
    format = (char *) getenv("DATE");

  if(format == (char *) NULL)
    {
      (void) sprintf(buf, "%3.3s %s%d-%3.3s-%s%d %s%d:%s%d%s",
		     wday[time_info->tm_wday],
		     time_info->tm_mday > 9 ? "" : "0",
		     time_info->tm_mday,
		     month[time_info->tm_mon],
		     time_info->tm_year > 9 ? "" : "0",
		     time_info->tm_year,
		     hour > 9 ? "" : " ",
		     hour,
		     time_info->tm_min > 9 ? "" : "0",
		     time_info->tm_min,
		     time_info->tm_hour > 11 ? "pm" : "am");
    }
  else
    {
      while(format)
	{
	  if(*format == '%')
	    {
	      ++format;
	      if(*format == '\0') 
		{
		  *buf = '\0';
		  break;
		}
	      if(*format == '%')
		{
		  *bufP++ = '%';
		  ++format;
		  continue;
		}
	      if(!strncmp(format, "day", 3))
		{
		  strcpy(bufP, wday[time_info->tm_wday]);
		  bufP += strlen(wday[time_info->tm_wday]);
		  format += 3;
		  continue;
		}
	      if(!strncmp(format, "sday", 4))
		{
		  strncpy(bufP, wday[time_info->tm_wday],3);
		  bufP += 3;
		  format += 4;
		  continue;
		}
	      if(!strncmp(format, "mday", 4))
		{
		  sprintf(bufP,"%d", time_info->tm_mday);
		  bufP += time_info->tm_mday > 9 ? 2 : 1;
		  format += 4;
		}
	      if(!strncmp(format, "2mday", 5))
		{
		  sprintf(bufP,"%s%d",time_info->tm_mday > 9 ? "" : " ",
			  time_info->tm_mday);
		  bufP += 2;
		  format += 5;
		}
	      if(!strncmp(format, "02mday", 6))
		{
		  sprintf(bufP,"%s%d",time_info->tm_mday > 9 ? "" : "0",
			  time_info->tm_mday);
		  bufP += 2;
		  format += 6;
		}
	      if(!strncmp(format, "yday", 4))
		{
		  sprintf(bufP,"%s%s%d",time_info->tm_yday > 9 ? "" : " ",
			  time_info->tm_yday > 99 ? "" : " ",
			  time_info->tm_yday);
		  bufP += 3;
		  format += 4;
		}
	      if(!strncmp(format, "month", 5))
		{
		  strcpy(bufP,month[time_info->tm_mon]);
		  bufP += strlen(month[time_info->tm_mon]);
		  format += 5;
		}
	      if(!strncmp(format, "smonth", 6))
		{
		  strncpy(bufP,month[time_info->tm_mon],3);
		  bufP += 3;
		  format += 6;
		}
	      if(!strncmp(format, "nmon", 4))
		{
		  sprintf(bufP,"%d",time_info->tm_mon+1);
		  bufP += time_info->tm_mon > 8 ? 2 : 1;
		  format += 4;
		}
	      if(!strncmp(format, "2nmon", 5))
		{
		  sprintf(bufP,"%s%d", time_info->tm_mon < 9 ? " " : "", 
			  time_info->tm_mon+1);
		  bufP += 2;
		  format += 5;
		}
              if(!strncmp(format, "02nmon", 5))
                {
                  sprintf(bufP,"%s%d", time_info->tm_mon < 9 ? "0" : "",
                          time_info->tm_mon+1);
                  bufP += 2;
                  format += 5;
                }
	      if(!strncmp(format, "year", 4))
		{
		  sprintf(bufP,"%d",time_info->tm_year);
		  bufP += 2;
		  format += 4;
		}
	      if(!strncmp(format, "byear", 5))
		{
		  sprintf(bufP,"%d",time_info->tm_year+TM_YEAR_BASE);
		  bufP += 4;
		  format += 5;
		}

#ifndef sgi
	      if(!strncmp(format, "tz", 2))
		{
		  strcpy(bufP,time_info->tm_zone);
		  bufP += strlen(time_info->tm_zone);
		  format += 2;
		}
#endif

	      if(!strncmp(format, "mhour", 5))
		{
		  sprintf(bufP,"%d",time_info->tm_hour);
		  bufP += time_info->tm_hour > 9 ? 2 : 1;
		  format += 5;
		}
	      if(!strncmp(format, "2mhour", 6))
		{
		  sprintf(bufP,"%s%d", time_info->tm_hour > 9 ? "" : " ",
			  time_info->tm_hour);
		  bufP += 2;
		  format += 6;
		}
	      if(!strncmp(format, "02mhour", 7))
		{
		  sprintf(bufP,"%d", time_info->tm_hour > 9 ? "" : "0",
			  time_info->tm_hour);
		  bufP += 2;
		  format += 7;
		}
	      if(!strncmp(format, "hour", 4))
		{
		  hour = time_info->tm_hour;
		  if(hour > 12)
		    hour -= 12;
		  sprintf(bufP,"%d",hour);
		  bufP += hour > 9 ? 2 : 1;
		  format += 4;
		}
              if(!strncmp(format, "12hour", 6))
                {
                  hour = time_info->tm_hour;
                  if(hour > 12)
                    hour -= 12;
                  if(hour == 0)
		    hour = 12;
                  sprintf(bufP,"%d",hour);
                  bufP += hour > 9 ? 2 : 1;
                  format += 6;
                }
	      if(!strncmp(format, "2hour", 5))
		{
		  hour = time_info->tm_hour;
		  if(hour > 12)
		    hour -= 12;
		  sprintf(bufP,"%s%d", hour > 9 ? "" : " ", hour);
		  bufP += 2;
		  format += 5;
		}
	      if(!strncmp(format, "02hour", 5))
		{
		  hour = time_info->tm_hour;
		  if(hour > 12)
		    hour -= 12;
		  sprintf(bufP,"%s%d", hour > 9 ? "" : "0", hour);
		  bufP += 2;
		  format += 5;
		}
	      if(!strncmp(format, "min", 3))
		{
		  sprintf(bufP,"%s%d", time_info->tm_min < 10 ? "0" : "",
			  time_info->tm_min);
		  bufP += 2;
		  format += 3;
		}
	      if(!strncmp(format, "sec", 3))
		{
		  sprintf(bufP,"%s%d",time_info->tm_sec < 10 ? "0" : "",
			  time_info->tm_sec);
		  bufP += 2;
		  format += 3;
		}
	      if(!strncmp(format, "apm", 3))
		{
		  if(time_info->tm_hour > 11)
		    strcpy(bufP,"pm");
		  else
		    strcpy(bufP,"am");
		  bufP += 2;
		  format += 3;
		}
	    }
	  else
	    {
	      *bufP++ = *format;
	      ++format;
	      if(*format == '\0')
		break;
	    }
	}
      *bufP++ = '\0';
    }

  if (*buf == '\0')
    (void) sprintf(buf,"It's tea time!");
	      
  return(buf);
}


/*
 * Function:    tt_weekday();
 * Description: Given an integer from 0-6, returns pointer to string for
 *              corresponding day of week.
 * Returns:     pointer to day string.
 */

char *
tt_weekday(day)
     int day;
{
  if((day > 0) && (day < 7))
    return(wday[day]);
  else
    return("You lose.");
}


/*
 * Function:    tt_month();
 * Description: Given an integer from 0-11, returns pointer to string for
 *              corresponding month of year.
 * Returns:     pointer to month string.
 */

char *
tt_month(m)
     int m;
{
  if((m > 0) && (m < 12))
    return(month[m]);
  else
    return("You lose.");
}
