v15i032: Routine to process ctime(3) output

Rich Salz rsalz at uunet.uu.net
Sat Jun 4 02:30:03 AEST 1988


Submitted-by: Mike Marshall <hubcap at hubcap.clemson.edu>
Posting-number: Volume 15, Issue 32
Archive-name: emitc

[  This is one of those "oh yeah, how could this not be there?" routines.
   The name is clever, and works on two levels.  --r$ ]

There may be other programs in the archives that do this, but when I was
looking around all I could find was the enormously complicated "getdate"
that is included with the news source, and the proprietary "unctime" which
comes with the BSD source. This program (emitc) is a simple minded hack
comparatively, but it works, is small, and is no slower than "unctime".

I would guess it is more or less portable.  Don't forget to change the
value of TIMEZONE if necessary (see the comments in the code).

-Mike Marshall     hubcap at hubcap.clemson.edu  ...!hubcap!hubcap


#! /bin/sh
#
# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
echo 'x - emitc.man'
if test -f emitc.man; then echo 'shar: not overwriting emitc.man'; else
sed 's/^X//' << '________This_Is_The_END________' > emitc.man
X.TH emitc 3
X.SH NAME
Xemitc \- convert ctime formatted ascii date string to long integer
X.SH SYNTAX
X.PP
X.nf
X
Xlong emitc(string)
Xchar *string;
X.SH DESCRIPTION
X.PP
XThe
X.PN emitc
Xcall converts a 25 character string (in the following form: Fri Apr 22 09:45:15 
X1988\\n),
Xpointed to by
X.PN string,
Xinto a long integer representing the time in seconds since
X00:00:00 GMT, January 1, 1970,
Xand returns that value.
X.SH AUTHOR
X.PP
XMike Marshall   hubcap at hubcap.clemson.edu   04/20/88
________This_Is_The_END________
if test `wc -l < emitc.man` -ne 23; then
	echo 'shar: emitc.man was damaged during transit (should have been 23 bytes)'
fi
fi		; : end of overwriting check
echo 'x - emitc.c'
if test -f emitc.c; then echo 'shar: not overwriting emitc.c'; else
sed 's/^X//' << '________This_Is_The_END________' > emitc.c
X/* emitc - as the name implies, emitc is the opposite of ctime(3).
X .  ctime(3) eats a long integer which represents the number of seconds
X .  since Jan 01 00:00:00 1970 and spits out the corresponding day, date
X .  and time in ascii. emitc ingests an ascii string (formatted as per
X .  ctime's output) and gives back the appropriate number of seconds.
X .     
X .  Anyone is free to use emitc anyway they please, but if you do deem
X .  it worthwhile, leave the following credit line somewhere in the code:
X .
X .  AUTHOR - Mike Marshall    hubcap at hubcap.clemson.edu      4/20/88
X . 
X .  SYSTEM CALLS USED - none
X .
X .  LIBRARY FUNCTIONS USED - localtime(3)
X */
X#include <time.h>
X#define MONTH_OFFSET 4
X#define DAY_OFFSET 8
X#define HOUR_OFFSET 11
X#define MINUTE_OFFSET 14
X#define SECOND_OFFSET 17
X#define YEAR_OFFSET 20
X#define MONTH_LENGTH 3
X#define MONTHS "JanFebMarAprMayJunJulAugSepOctNovDec"
X#define SECONDS_PER_YEAR 31536000
X#define SECONDS_PER_DAY 86400
X#define SECONDS_PER_HOUR 3600
X#define SECONDS_PER_MINUTE 60
X#define FEB 1
X#define TIMEZONE 5     /* You gotta change this, depending on where you
X                        . are located geographically. I equated TIMEZONE
X                        . to 5 since I am in the Eastern Standard Time Zone,
X                        . which is five hours west of Greenwich.
X                        */
X
Xlong emitc(string)
Xchar *string;
X{
X  struct tm *time;
X  long fudge_days;
X  long month_num;
X  long clock;
X  static char *months = MONTHS;
X  static long days_so_far[11] = {31,59,90,120,151,181,212,243,273,304,334};
X  /* determine month number */
X  for (month_num=0;
X       strncmp(string+MONTH_OFFSET,months+month_num*MONTH_LENGTH,MONTH_LENGTH);
X       month_num++);
X  /* determine the number of seconds since the beginning of the universe */
X  clock = (atoi(string+YEAR_OFFSET)-1970) * SECONDS_PER_YEAR;
X  if (month_num > 0) 
X     clock = clock + days_so_far[month_num-1] * SECONDS_PER_DAY;
X  clock = clock + (atoi(string+DAY_OFFSET)-1) * SECONDS_PER_DAY;
X  clock = clock + atoi(string+HOUR_OFFSET) * SECONDS_PER_HOUR;
X  clock = clock + atoi(string+MINUTE_OFFSET) * SECONDS_PER_MINUTE;
X  clock = clock + atoi(string+SECOND_OFFSET);
X  /* calculate in the leap year fudge factors */
X  if (atoi(string+YEAR_OFFSET) > 1971) {
X     fudge_days=0;
X     fudge_days = (atoi(string+YEAR_OFFSET)-1971)/4;
X     if (!(atoi(string+YEAR_OFFSET)%4) && (month_num > FEB)) fudge_days++;
X     clock = clock + fudge_days*SECONDS_PER_DAY;
X  }
X  /* calculate in the time shift westward from Greenwich */
X  clock = clock + TIMEZONE*SECONDS_PER_HOUR;
X  /* worry about daylight savings time */
X  time = localtime(&clock);
X  if (time->tm_isdst) clock = clock - SECONDS_PER_HOUR;
X  return(clock);
X} 
________This_Is_The_END________
if test `wc -l < emitc.c` -ne 70; then
	echo 'shar: emitc.c was damaged during transit (should have been 70 bytes)'
fi
fi		; : end of overwriting check
exit 0

-- 
Please send comp.sources.unix-related mail to rsalz at uunet.uu.net.



More information about the Comp.sources.unix mailing list