From: twtick@corral.uwyo.edu (Tom Wilson)
Newsgroups: comp.os.vms
Subject: strftime() for VMS
Date: 4 Oct 92 04:40:45 GMT
Organization: The Internet

Since it was discovered that using strftime() is not very easy, I decided to
make up my own version of this quite handy function.

The calling format is pretty simple.  You must first get the time using the
time() and localtime() functions:

  struct tm *time_values;
  time_t    current_time;

  current_time = time(NULL);
  time_values = localtime(&current_time);

then call strftime() like:

  strftime(return_string,format_string,time_values);

and the time string you request through the format string is returned in the
return_string.

The format characters are (with an example):

Code                 What is returned                 Example
----                 ----------------                 -------
 %a   		     abbreviated weekday name		Sat
 %A		     full weekday name			Saturday
 %b		     abbreviated month name		Oct
 %B		     full month name			October
 %c		     date and time			Oct 03 22:52:10 1992
 %d		     day of the month			03
 %H		     hour of the day (24 hour)		22
 %h		     hour of the day (12 hour)		10
 %j		     day of the year			276
 %m		     month of the year			10
 %M		     minutes after the hour		52
 %p		     AM or PM				PM
 %s		     seconds after the hour		10
 %U		     week of the year			40
 %w		     day of the week			6
 %x		     date				Oct 03 1992
 %X		     time				22:52:10
 %y		     year of the century		92
 %Y		     year				1992
 %%		     a percent character		%

I wasn't sure of a good way to come up with the timezone, other than reading
some logicals out of the system table.  If you want to add that, Multinet,
PMDF, and ANU news all have a timezone logical.

The code is not really that compact.  Feel free to improve upon it.  If you
do find any mistakes, please let me know either via E-mail or this list.

This is definately a good routine to add to your personal .olb file. 

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Tom Wilson 		       +  University of Wyoming   - Just a lowly      -
+ twtick@corral.uwyo.edu       -    Computer Science      + student programmer+
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+


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

strftime(char *ret_string,char *format,struct tm *time_vals)
{
  char tmp2[4],tmp[256],day[4],month[4],date[3],time_s[9],year[5];
  int i;
  static char *days[7] = {"Sunday","Monday","Tuesday","Wednesday","Thursday",
			 "Friday","Saturday"};
  static char *months[12] = {"January","February","March","April","May",
			    "June","July","August","September","October",
			    "November","December"};

  strcpy(tmp,"\0");
  sscanf(asctime(time_vals),"%s %s %s %s %s",day,month,date,time_s,year);
  if(atoi(date)<10) {
    sprintf(tmp2,"0%s",date);
    strcpy(date,tmp2);
  }
  for(i=0;i<=strlen(format);i++) {
    if(format[i]!='%')sprintf(tmp,"%s%c",tmp,format[i]);
    else {
      i += 1;
      switch(format[i]) {
        case 'a' : strcat(tmp,day);
		   break;
        case 'A' : strcat(tmp,days[time_vals->tm_wday]);
		   break;
        case 'b' : strcat(tmp,month);
		   break;
        case 'B' : strcat(tmp,months[time_vals->tm_mon]);
		   break;
	case 'c' : sprintf(tmp,"%s%s %s %s %s",tmp,month,date,time_s,year);
		   break;
        case 'd' : strcat(tmp,date);
		   break;
        case 'h' : if(time_vals->tm_hour>12) {
		     if(time_vals->tm_hour-12<10) 
		       sprintf(tmp,"%s0%d",tmp,time_vals->tm_hour-12);
		     else sprintf(tmp,"%s%d",tmp,time_vals->tm_hour-12);
		     break;
		   }
        case 'H' : if(time_vals->tm_hour<10) sprintf(tmp,"%s0%d",tmp,time_vals->tm_hour);
		   else sprintf(tmp,"%s%d",tmp,time_vals->tm_hour);
		   break;
	case 'j' : sprintf(tmp,"%s%d",tmp,time_vals->tm_yday);
		   break;
        case 'm' : sprintf(tmp,"%s%d",tmp,time_vals->tm_mon);
		   break;
        case 'M' : sprintf(tmp,"%s%d",tmp,time_vals->tm_min);
		   break;
	case 'p' : if(time_vals->tm_hour>11) strcat(tmp,"PM");
		   else strcat(tmp,"AM");
		   break;
	case 's' : sprintf(tmp,"%s%d",tmp,time_vals->tm_sec);
		   break;
	case 'U' : sprintf(tmp,"%s%d",tmp,time_vals->tm_yday/7+1);
		   break;
        case 'w' : sprintf(tmp,"%s%d",tmp,time_vals->tm_wday);
		   break;
        case 'x' : sprintf(tmp,"%s%s %s %s",tmp,month,date,year);
		   break;
        case 'X' : strcat(tmp,time_s);
		   break;
        case 'y' : sprintf(tmp,"%s%d",tmp,time_vals->tm_year);
		   break;
        case 'Y' : strcat(tmp,year);
		   break;
        case '%' : strcat(tmp,"%");
		   break;
      }
    }
  }
  strcpy(ret_string,tmp);
}