[comp.unix.programmer] help to new C programmer with time.h

jik@athena.mit.edu (Jonathan I. Kamens) (03/01/91)

In article <5284@vela.acs.oakland.edu>, swood@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@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

pf@geech.ai.mit.edu (03/02/91)

jik@athena.mit.edu (Jonathan I. Kamens) wrote:
>   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

Sure about this?  struct tm, time_t, and localtime are in K&RII, p256.
They look like part of the standard library to me.  A nonstandard
approach would be assuming that time_t is a longword containing the
number of seconds since whenever.  But using time_t as an abstract
datatype seems allowed by K&R2 (which, I know, is not the ANSI spec).
--
Paul Falstad, pfalstad@phoenix.princeton.edu PLink:HYPNOS GEnie:P.FALSTAD
"So how DO you delete a file called - ?"  For viewers at home, the answer
is coming up on your screen.  For those of you who wish to play it the hard
way, stand upside down with your head in a bucket of piranha fish.

scs@adam.mit.edu (Steve Summit) (03/02/91)

In article <1991Mar1.071615.18895@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>   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. 

Surprisingly enough, the functions time() and ctime() (as well as
asctime, localtime, gmtime, and the new, more flexible strftime),
*are* in the ANSI C Standard, X3.159.  (See section 4.12 .)
Therefore, these functions *should* be used in C programs which
wish to manipulate dates and times, in preference to the redundant,
system-specific, nonportable alternatives several vendors
incongruously provide.

                                            Steve Summit
                                            scs@adam.mit.edu

jik@athena.mit.edu (Jonathan I. Kamens) (03/02/91)

  OK, so in the past two postings I've made to this newsgroup, I've screwed up
in my description of the ANSI C standard (once about time.h and once about
old-style vs. new-style declarations).  Obviously, I'm completely clueless (or
at least partially clueless :-).  Now I understand why I waited so long before
being willing to post in comp.lang.c; I guess I should have waited longer.

  I'd like to apologize for the mistakes.... I'll try to be a bit more careful
in the future (I'll just wait a couple days to let other people answer
questions before I take a stab at them :-).

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

DOCTORJ@SLACVM.SLAC.STANFORD.EDU (Jon J Thaler) (03/02/91)

In article <1991Mar1.165132.7053@athena.mit.edu>, scs@adam.mit.edu (Steve
Summit) says:

>Surprisingly enough, the functions time() and ctime() (as well as
>asctime, localtime, gmtime, and the new, more flexible strftime),
>*are* in the ANSI C Standard, X3.159.  (See section 4.12 .)
>Therefore, these functions *should* be used in C programs which
>wish to manipulate dates and times, in preference to the redundant,
>system-specific, nonportable alternatives several vendors
>incongruously provide.

My usual computing environment is more-or-less system specific (VMS), but
multi-language (C and Fortran).  I find that in this situation DEC's
nonportable, but language independent time functions are far more useful.
                              Jon Thaler