swood@vela.acs.oakland.edu ( EVENSONG) (03/01/91)
I am delving into learning C programming this week, and I was in the process of converting one of my old basic programs over to C, and got to noting that instead of input day and month directly, I could take it directly off of the system clock. But, I can not seem to get it to work. Here is one of multiple combinations that I tried: ---------------------cut here------------------ #include "stdio.h" #include "time.h" main() { struct tm tt; time_t lt; int d, m; time(<); d = tt.tm_mday; m = tt.tm_mon; printf("Day %d Month %d\n", d, m); } ----------------------------cut here------------------- swood -- ---- Insert favorite .signature here ---- | swood@argo.acs.oakland.edu | swood@vela.acs.oakland.edu Bitnet: swood@Oakland | swood@unix.secs.oakland.edu UUCP: ...!uunet!umich!{vela, argo, unix, nucleus}!swood
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(<); |> 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(<); tt = localtime(<); /* 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
pfalstad@phoenix.Princeton.EDU (Paul Falstad) (03/01/91)
swood@vela.acs.oakland.edu ( EVENSONG) wrote: >main() >{ > struct tm tt; > time_t lt; > int d, m; > > time(<); > d = tt.tm_mday; > m = tt.tm_mon; > > printf("Day %d Month %d\n", d, m); >} Well, obviously the call to time isn't doing a thing to struct tm tt. You need to explicitly set it. struct tm tt; time_t lt; time(<); tt = *localtime(<); d = ... Or (better) struct tm *tt; time_t lt; time(<); tt = localtime(<); d = tt->tm_mday; m = tt->tm_mon; -- 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.
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
garry@ceco.ceco.com (Garry Garrett) (03/02/91)
In article <5284@vela.acs.oakland.edu>, swood@vela.acs.oakland.edu ( EVENSONG) writes: > > I am delving into learning C programming this week, and I was in the process > of converting one of my old basic programs over to C, and got to noting that > instead of input day and month directly, I could take it directly off of the > system clock. But, I can not seem to get it to work. Here is one of > multiple combinations that I tried: > > ---------------------cut here------------------ > #include "stdio.h" > #include "time.h" > > main() > { > struct tm tt; struct tm *tmptr; > time_t lt; > int d, m; > > time(<); tmptr = localtime(<); d = tmptr->tm_mday; m = tmptr->tm_mon; /* > d = tt.tm_mday; > m = tt.tm_mon; */ > > printf("Day %d Month %d\n", d, m); > } > ----------------------------cut here------------------- > calling the function time merely puts the elapsed number of seconds since jan 1, 1970 at 00:00:00 into "lt" It does nothing to "tt". If you want to get a structure tm filled with the current time, then you must use localtime() to convert this SEC70 representation of time into a struct tm representation of time. localtime returns a pointer to a struct tm, however, rather than copying that information into tt, we might as well go ahead and use it in this example.
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