Julian date routines needed

Garry Garrett garry at ceco.ceco.com
Thu Feb 21 10:59:13 AEST 1991


In article <1780 at manta.NOSC.MIL>, grantk at manta.NOSC.MIL (Kelly J. Grant) writes:
> In article <1753 at manta.NOSC.MIL>, grantk at manta.NOSC.MIL (Kelly J. Grant) writes:
> > Howdy networld
> > 
> > About 100,000 years ago I had some Julian date routines (in dBASE) to
> > convert YY/MM/DD into a format suitable for math, and also to convert
> > back to normal dates.  We called these JULTOCAL and CALTOJUL.  I now
> > have a need for these algorithms for a UNIX program.  Would any of 
> > you kind souls have these routines lying around in your book of tricks
> > [right next to your hanoi.c file maybe :-)]
> 
> Kelly Grant        grantk at manta.nosc.mil   (619) 225-2508


	There is an easy way to do this.  Let UNIX do all the work FOR you.

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

main()
{
	struct tm *tmptr;
	size_t	  sec70;

	tmptr = (struct tm *) malloc(sizeof(struct tm));
	/* ... */
	sscanf(datestr,"%d/%d/%d",&tmptr->tm_year,&tmptr->tm_mon,&tmptr->tm->mday);
	/* my UNIX system doesn't seem to mind that the rest of the */
	/* fields of tmptr are not filled out.  I think that malloc */
	/* on my system fills the memory with 0's.  Not true on all */
	/* systems though.					    */
	sec70 = mktime(tmptr);
	tmptr = localtime(sec70);
	/* tmptr->tm_yday now contains the julian day */
}

Generally speaking, on a unix system, if you can convert the time that you
are given, in whatever format you are given to either structure tm (defined
in time.h) or into the number of seconds elapsed since jan 1, 1970 at 00:00:00,
then you can do anything you want to with it.  Unix is very good about that,
but other C compilers do not always have all functions that are available  
on Unix.  Prime is one of those systems.  That's why I would like to find 
the code to mktime() and strftime() (as described in K&R, 2nd edition, p255)
so that I can manipulate various reprentations of time.  

Garry Garrett
garry at ceco.ceco.com
...!uunet!ceco.ceco.com!garry



More information about the Comp.lang.c mailing list