help to new C programmer with time.h

Jonathan I. Kamens jik at athena.mit.edu
Fri Mar 1 18:16:15 AEST 1991


In article <5284 at vela.acs.oakland.edu>, swood at vela.acs.oakland.edu ( EVENSONG) writes:
|> #include "stdio.h"
|> #include "time.h"
|> 
|> main()
|> {
|>   struct tm tt;
|>   time_t lt;
|>   int d, m;
|> 
|>   time(&lt);
|>   d = tt.tm_mday;
|>   m = tt.tm_mon;
|> 
|>   printf("Day %d Month %d\n", d, m);
|> }

   First of all, this is an OS-specific question, not a C question, because
the C language does not define how to get the time; any functions for getting
the time in C are OS-specific library functions, not standard C functions. 
Since the functions and structures you are using appear to me to be Unix
library structures and functions, I have cross-posted my response to
comp.unix.programmer and directed followups there.

  Second, how exactly do you expect tt to be filled in?  Do you expect a call
to the time library function which doesn't even pass in tt to magically fill
it in somehow?  I think what you want to do is this:

	#include <stdio.h>
	#include <sys/types.h>
	#include <time.h>

	main()
	{
		struct tm *tt;
		time_t lt;
		int d, m;

		time(&lt);
		tt = localtime(&lt);
		/* Remember that the return value of localtime points to
		   static data which will be destroyed on the next call to it.
		*/ 
		d = tt->tm_mday; /* this is the day of the month */
		m = tt->tm_mon;  /* this is the month */

		printf("Day %d Month %d\n", d, m);
	}

Of course, something here may be wrong, if the OS you're working on is
significantly different from what I'm using, which is why I say that this is
an OS-specific question.

  By the way, it usually helps, when asking about something that you cannot
get to work, to explain *how* it's not working.  Does the program not compile
at all?  Does it compile but produce bad output?  If so, what output does it
produce?

  The more specific you are, the more likely it is that other people will be
able to help you.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik at Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710



More information about the Comp.unix.programmer mailing list