[comp.sources.wanted] System V-compatible ctime

wnp@iiasa.AT (wolf paul) (08/30/90)

In order to compile the recently-posted "ago" program, I am looking
for a System V or POSIX-compatible ctime(3) library. Specifically,
I need a cftime() or strftime() routine.

Thanks!
-- 
Wolf N. Paul, IIASA, A - 2361 Laxenburg, Austria, Europe
PHONE: +43-2236-71521-465     FAX: +43-2236-71313      UUCP: uunet!iiasa.at!wnp
INTERNET: wnp%iiasa.at@uunet.uu.net      BITNET: tuvie!iiasa!wnp@awiuni01.BITNET
       * * * * Kurt Waldheim for President (of Mars, of course!) * * * *

erik@rochgate.FIDONET.ORG (Erik VanRiper) (08/31/90)

 > From: wnp@iiasa.AT (wolf paul)
 > Date: 30 Aug 90 12:40:16 GMT
 > Organization: IIASA, Laxenburg/Vienna, Austria, Europe
 > Message-ID: <862@iiasa.UUCP>
 > Newsgroups: alt.sources.d,comp.sources.wanted,comp.sources.d,comp.lang.c
 >
 > In order to compile the recently-posted "ago" program, I am looking
 > for a System V or POSIX-compatible ctime(3) library. Specifically,
 > I need a cftime() or strftime() routine.
 >
 > Thanks!
 > --
 > Wolf N. Paul, IIASA, A - 2361 Laxenburg, Austria, Europe
 > PHONE: +43-2236-71521-465     FAX: +43-2236-71313      UUCP:
 > uunet!iiasa.at!wnp
 > INTERNET: wnp%iiasa.at@uunet.uu.net      BITNET:
 > tuvie!iiasa!wnp@awiuni01.BITNET
 >        * * * * Kurt Waldheim for President (of Mars, of course!) * * * *


      Here is a routine for you.....strftime()

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

char months[][10]={"January","February","March","April","May",
                   "June","July","August","September","October",
                   "November","December"};


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

char weekday[][10]={"Sunday","Monday","Tuesday","Wednesday","Thursday",
                    "Friday","Saturday"};

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

/* Note:  TZ environment variable MUST be defined to use the %Z function! */

int strftime(char *string,int maxsize,char *format,struct tm *current_time)
{
  char *in,
       *out,
       *scrptr;

  char temp[250];

  maxsize=min(maxsize,230);

  for (in=format,out=temp;*in;in++)
  {
    if ((int)(out-(int)temp) >= maxsize)
      break;

    if (*in=='%')
    {
      switch(*++in)
      {
        case 'a':
          strcpy(out,weekday_ab[current_time->tm_wday]);
          break;

        case 'A':
          strcpy(out,weekday[current_time->tm_wday]);
          break;

        case 'b':
          strcpy(out,months_ab[current_time->tm_mon]);
          break;

        case 'B':
          strcpy(out,months[current_time->tm_mon]);
          break;

        case 'c':
          sprintf(out,"%02d-%02d-%02d %02d:%02d:%02d",
                  current_time->tm_mon+1,
                  current_time->tm_mday,
                  current_time->tm_year,
                  current_time->tm_hour,
                  current_time->tm_min,
                  current_time->tm_sec);
          break;

        case 'd':
          sprintf(out,"%02d",current_time->tm_mday);
          break;

        case 'H':
          sprintf(out,"%02d",current_time->tm_hour);
          break;

        case 'I':
          sprintf(out,"%02d",
                  (current_time->tm_hour >= 0 && current_time->tm_hour <= 12 ?
                   current_time->tm_hour :
                   current_time->tm_hour-12));
          break;

        case 'j':
          sprintf(out,"%03d",current_time->tm_yday+1);
          break;

        case 'm':
          sprintf(out,"%02d",(current_time->tm_mon)+1);
          break;

        case 'M':
          sprintf(out,"%02d",current_time->tm_min);
          break;

        case 'p':
          strcpy(out,(current_time->tm_hour < 12 ? "am" : "pm"));
          break;

        case 'S':
          sprintf(out,"%02d",current_time->tm_sec);
          break;

        case 'U': /* ??? */
          sprintf(out,"%02d",(current_time->tm_yday)/7);
          break;

        case 'w':
          sprintf(out,"%d",current_time->tm_wday);
          break;

        case 'W': /* ??? */
          sprintf(out,"%02d",(current_time->tm_yday)/7);
          break;

        case 'x':
          sprintf(out,"%02d-%02d-%02d",
                  (current_time->tm_mon)+1,
                  current_time->tm_mday,
                  current_time->tm_year);
          break;

        case 'X':
          sprintf(out,"%02d:%02d:%02d",
                  current_time->tm_hour,
                  current_time->tm_min,
                  current_time->tm_sec);
          break;

        case 'y':
          sprintf(out,"%02d",current_time->tm_year);
          break;

        case 'Y':
          sprintf(out,"%02d",current_time->tm_year+1900);
          break;

        case 'Z': /* ??? */

          if ((scrptr=getenv("TZ")) != 0)
          {
            scrptr[3]=0;
            strcpy(out,strupr(scrptr));
          }
          else strcpy(string,"??T");
          break;

        case '%':
          strcpy(out,"%");
          break;
      }

      out += strlen(out);
    }
    else *out++=*in;
  }

  *out='\0';

  strcpy(string,temp);

  return(strlen(string));
}

--- FD 1.99c
 * Origin: ENC BBS.  A Nodeling in Disguise  HST/V42b (1:260/230)
--  
---------------------------------------------
Programmers Distribution Network Coordinator
FidoNet: 1:260/230
---------------------------------------------

bdb@becker.UUCP (Bruce D. Becker) (09/01/90)

In article <862@iiasa.UUCP> wnp@iiasa.UUCP (wolf paul) writes:
>In order to compile the recently-posted "ago" program, I am looking
>for a System V or POSIX-compatible ctime(3) library. Specifically,
>I need a cftime() or strftime() routine.

	Although not perfect, the code included below
	ought to handle the situation OK...


 --------- 8< --------- 8< --------- 8< --------- 8< --------- 8< ---------

#ifdef BSD
#include	<sys/time.h>
#else
#include	<time.h>
#endif

static char	*amon[] = {	"Jan","Feb","Mar","Apr","May","Jun",
				"Jul","Aug","Sep","Oct","Nov","Dec" };
static char	*lmon[] = {	"January","February","March",
				"April","May","June",
				"July","August","September",
				"October","November","December" };
static char	*awkd[] = {	"Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
static char	*lwkd[] = {	"Sunday","Monday","Tuesday","Wednesday",
				"Thursday","Friday","Saturday" };

int	cftime(buf, fmt, clock)
char	*buf, *fmt;
long	*clock;
{

	char		c, *ctmp, *bb;
	int		i;
	struct tm	*lt;

	if (fmt == (char *)0 || *fmt == '\0') fmt = "%a %b %d %T %Z %Y";
	lt = localtime(clock);
	bb = buf;
	while(c = *(fmt++)) {
		if (c != '%') *(buf++) = c;
		else switch(c = *(fmt++)) {
		case 'a':
			ctmp = awkd[lt->tm_wday];
			while (*(buf++) = *(ctmp++));
			--buf;
			break;
		case 'A':
			ctmp = lwkd[lt->tm_wday];
			while (*(buf++) = *(ctmp++));
			--buf;
			break;
		case 'b':
		case 'h':
			ctmp = amon[lt->tm_mon];
			while (*(buf++) = *(ctmp++));
			--buf;
			break;
		case 'B':
			ctmp = lmon[lt->tm_mon];
			while (*(buf++) = *(ctmp++));
			--buf;
			break;
		case 'd':
			sprintf(buf, "%02d", lt->tm_mday);
			buf += 2;
			break;
		case 'D':
		case 'x':
			sprintf(buf, "%02d/%02d/%02d",
			lt->tm_mon+1, lt->tm_mday, lt->tm_year);
			buf += 8;
			break;
		case 'e':
			sprintf(buf, "%2d", lt->tm_mday);
			buf += 2;
			break;
		case 'H':
			sprintf(buf, "%02d", lt->tm_hour);
			buf += 2;
			break;
		case 'I':
			i = lt->tm_hour % 12;
			sprintf(buf, "%02d", i?i:12);
			buf += 2;
			break;
		case 'j':
			sprintf(buf, "%03d", lt->tm_yday+1);
			buf += 3;
			break;
		case 'm':
			sprintf(buf, "%2d", lt->tm_mon+1);
			buf += 2;
			break;
		case 'M':
			sprintf(buf, "%02d", lt->tm_min);
			buf += 2;
			break;
		case 'n':
			*(buf++) = '\n';
			break;
		case 'r':
			i = lt->tm_hour % 12;
			sprintf(buf, "%02d:%02d:%02d ",
			i?i:12, lt->tm_min, lt->tm_sec);
			buf += 9;
		case 'p':
			if (lt->tm_hour <12 ) *(buf++) = 'A';
			else *(buf++) = 'P';
			*(buf++) = 'M';
			break;
		case 'R':
			sprintf(buf, "%02d:%02d",
			lt->tm_hour, lt->tm_min);
			buf += 5;
			break;
		case 'S':
			sprintf(buf, "%2d", lt->tm_sec);
			buf += 2;
			break;
		case 't':
			*(buf++) = '\t';
			break;
		case 'T':
		case 'X':
			sprintf(buf, "%02d:%02d:%02d",
			lt->tm_hour, lt->tm_min, lt->tm_sec);
			buf += 8;
			break;
		case 'U':
			sprintf(buf, "%2d", ((lt->tm_yday+7-lt->tm_wday)/7)+1);
			buf += 2;
			break;
		case 'w':
			sprintf(buf, "%1d", lt->tm_wday);
			buf += 1;
			break;
		case 'W':
			sprintf(buf, "%2d", ((lt->tm_yday+6-lt->tm_wday)/7)+1);
			buf += 2;
			break;
		case 'y':
			sprintf(buf, "%2d", lt->tm_year);
			buf += 2;
			break;
		case 'Y':
			if ((i = lt->tm_year) < 70) i += 100;
			i += 1900; 
			sprintf(buf, "%4d", i);
			buf += 4;
			break;
		case 'Z':
#ifdef BSD
			{
				struct timeval	tp;
				struct timezone	tz;

				gettimeofday(&tp, &tz);
				ctmp = (char *)timezone(tz.tz_minuteswest,
					lt->tm_isdst);
			}
#else
			tzset();
			i = (lt->tm_isdst)? 1: 0;
			ctmp = tzname[i];
#endif
			while (*(buf++) = *(ctmp++));
			--buf;
			break;
		case '%':
			*(buf++) = c;
			break;
		default:
			sprintf(buf, "bad format character - %c", c);
			return (-1);
		}
	}
	return (buf-bb);
}

 --------- 8< --------- 8< --------- 8< --------- 8< --------- 8< ---------

-- 
  ,u,	 Bruce Becker	Toronto, Ontario
a /i/	 Internet: bdb@becker.UUCP, bruce@gpu.utcs.toronto.edu
 `\o\-e	 UUCP: ...!uunet!mnetor!becker!bdb
 _< /_	 "I still have my phil-os-o-phy" - Meredith Monk