25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Date and time functions in DeSmet C

DeSmet C functions originally written for the Heathkit H-100 computer, to:

  • Return the weekday of a Julian date
  • Return the Julian date of a date string in MM-DD-YY format
  • Translate a Julian date to MM-DD-YY format
  • Translate a number of seconds to a time string in HH:MM:SS format
  • Translate a time string in HH:MM:SS format to number of seconds since midnight
  • Return in HH:MM:SS format the difference between two HH:MM:SS formatted time strings
/* -------------------------------------------------------------- */
/* MISCELLANEOUS DATE AND TIME ROUTINES */
/* Copyright (C)1994 Steven Whitney. */
/* Initially published by http://25yearsofprogramming.com. */
/* Published under the GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY. */
/* -------------------------------------------------------------- */
/*	dayofwk.cpp		10-13-94, 1/6/04
	for julian date 1-366, returns day of week 0 = Sun to 6 = Sat
	this function only exists to support logspred.c
	for any other use, I think I've seen functions that do this much better,
	maybe in the various C libraries?
	C++ has better methods for the same task.
*/
/* -------------------------------------------------------------- */
int dayofwk(jul, first)
int jul; 		/* jul = julian date */
int first;		/* first = 0-6 day of the first day of the year */
{
	return((((jul - 1) % 7) + first) % 7);
}
/* -------------------------------------------------------------- */
/* TOJULIAN()	10-11-94  TRANSLATES MM-DD-YY DATE TO JULIAN */
/* RETURNS JULIAN DATE (1-366) OR ZERO ON ERROR */
/* -------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "my.h"

int tojulian(date)    	/* FORMAT MUST BE MM-DD-YY */
const char *date;
{
int d, m, y, j;			/* DAY, MONTH, YEAR, JULIAN */
int fb, ny;				/* # DAYS IN FEBRUARY & IN THE YEAR */
char buf[3];

strncpy((char *)buf,date,2);
m = atoi((char *)buf);
if((m < 1) || (m > 12))
	return(0);
strncpy((char *)buf,date+3,2);
d = atoi((char *)buf);
y = atoi(date+6);
fb = ((y % 4) == 0)? 29 : 28;	/* NO 400 TEST: 2000 WILL BE A LEAP YEAR */
ny = 337 + fb;					/* NUMBER OF DAYS IN THE YEAR */
j = 0;
switch(m)
{
	case 12: j += 30;
	case 11: j += 31;
	case 10: j += 30;
	case  9: j += 31;
	case  8: j += 31;
	case  7: j += 30;
	case  6: j += 31;
	case  5: j += 30;
	case  4: j += 31;							
	case  3: j += fb;
	case  2: j += 31;
	case  1: j += d; break;
}
return ((j > 0) && (j <= ny))? j : 0;
}

/* -------------------------------------------------------------- */
/*	TODATE.c  10-11-94  ACCEPTS JULIAN DATE (1-366), */
/*	RETURNS POINTER TO START OF BUF, CONTAINING DATE AS MMDDYY */
/*  OR ZERO IF JULIAN DATE PROVIDED WAS ILLEGAL */
/* -------------------------------------------------------------- */
#include <stdio.h>
#include <stddef.h>
#include <dos.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include "my.h"

char *todate(buf, j, y)
char *buf;			/* buf = FOR WRITING DATE INTO, MUST BE 7 CHARS */
int j;				/* j = JULIAN DATE */
int y;				/* y = 2-DIGIT YEAR NEEDED FOR DETERMINING WHETHER LEAP */
{
int m, fb, ny;		/* MONTH, # OF DAYS IN FEBRUARY & IN THE YEAR */
fb = ((y % 4) == 0)? 29 : 28;			/* # DAYS IN FEB. */
ny = 337 + fb;							/* # DAYS IN THE YEAR */
if((j < 1) || (j > ny))					/* CHECK FOR INVALID JULIAN DATE */
	return(0);
m = 1;
while(1)
{
	if(j - 31 > 0) { j -= 31; m++; } else break;
	if(j - fb > 0) { j -= fb; m++; } else break;
	if(j - 31 > 0) { j -= 31; m++; } else break;
	if(j - 30 > 0) { j -= 30; m++; } else break;
	if(j - 31 > 0) { j -= 31; m++; } else break;
	if(j - 30 > 0) { j -= 30; m++; } else break;
	if(j - 31 > 0) { j -= 31; m++; } else break;
	if(j - 31 > 0) { j -= 31; m++; } else break;
	if(j - 30 > 0) { j -= 30; m++; } else break;
	if(j - 31 > 0) { j -= 31; m++; } else break;
	if(j - 30 > 0) { j -= 30; m++; } else break;
	break;												/* SAFETY EXIT */
}
sprintf(buf,"%02d%02d%02d",m,j,y);		/* DAY IS WHATEVER'S LEFT IN J */
return(buf);
}

/* -------------------------------------------------------------- */
/*	TIMESTR()	   2-25-94 */
/*	PUTS A GIVEN # OF SECONDS INTO A BUFFER IN HH:MM:SS FORMAT */
/*	RETURNS TRUE IF # < 86400.  THERE ARE 86400 SECONDS IN A DAY */
/*			FALSE IF # >= 86400 OR NEGATIVE */
/*				(AND BUFFER CONTAINS "ERROR") */
/* -------------------------------------------------------------- */
#define TRUE	(1)
#define FALSE	(0)
int timestr(seconds,buf)
long seconds;			/* # OF SECONDS TO BE CONVERTED */
char *buf;				/* BUFFER, MIN.[9], FOR HOLDING HH:MM:SS STRING */
{
long hours, minutes;
void sprintf();

if((seconds < 0L) || (seconds >= 86400L))
{
	sprintf(buf,"%s","ERROR");
	return(FALSE);
}
hours = seconds / 3600L;
seconds -= (hours * 3600L);
minutes = seconds / 60L;
seconds -= (minutes * 60L);		
sprintf(buf,"%02d:%02d:%02d",(int)hours,(int)minutes,(int)seconds);
return(TRUE);
}


/* -------------------------------------------------------------- */
/*	TIMESECS()		2-25-94 */
/*	CONVERTS A TIME STRING IN HH:MM:SS FORMAT TO SECONDS SINCE MIDNIGHT */
/*	RETURNS (LONG)(# OF SECONDS).   THERE ARE 86400 SECONDS IN A DAY */
/*	THERE ARE NO CHECKS FOR ERRORS IN THE GIVEN TIME STRING */
/* -------------------------------------------------------------- */
long timesecs(timebuf)
char *timebuf;				/* BUFFER CONTAINING HH:MM:SS */
{
long atol();

return((atol(timebuf) * 3600L) + (atol(timebuf+3) * 60L) + (atol(timebuf+6)));
}

/* -------------------------------------------------------------- */
/*	TIMEDIFF()	2-25-94 */
/*	CALCULATES DIFFERENCE BETWEEN TWO HH:MM:SS TIME STRINGS */
/*	RETURNS POINTER TO A THIRD BUFFER CONTAINING A TIME-FORMATTED STRING */
/*			IF START TIME IS AFTER FINISH, BUFFER CONTAINS "ERROR" */
/*	THIS FUNCTION IS DUPLICATED BY TIMESECS() AND TIMESTR() */
/*	ADD DAY CHANGE CHECK.  THERE ARE 86400 SECONDS IN A DAY */
/* -------------------------------------------------------------- */
char *timediff(starttime,finishtime,resultbuf)
char *starttime, *finishtime, *resultbuf;
{
long ssecs, fsecs, difference, hours, minutes, atol();
void sprintf();
char *strcpy();
					/* CONVERT BOTH TO SECONDS SINCE MIDNIGHT AND SUBTRACT */

ssecs = ((atol(starttime) * 3600)+(atol(starttime+3) * 60)+(atol(starttime+6)));
fsecs = ((atol(finishtime) * 3600)+(atol(finishtime+3) * 60)+(atol(finishtime+6)));
difference = fsecs - ssecs;
if(difference >= 0L)			/* REMEMBER DIFFERENCE IS IN SECONDS */
{
	hours = difference / 3600L;
	difference -= (hours * 3600L);
	minutes = difference / 60L;
	difference -= (minutes * 60L);		/* SECONDS = WHAT'S LEFT OVER */
	sprintf(resultbuf,"%02d:%02d:%02d",(int)hours,(int)minutes,(int)difference);
}
else
	strcpy(resultbuf,"ERROR");
return(resultbuf);
}

 

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.