Thread: time

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    28

    time

    how would i access tm_year in this program

    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main( )
    {
        time_t today;
        char *now, buff[20];
        
        struct tm* gettime;
        
        time( &today );
        
        gettime = localtime( &today );
        now = asctime(( const struct tm* ) gettime );
        
        strftime(buff, 80, now, gettime);
        
        printf( "%s %d", buff, gettime );
      
        getch( );
    }
    Last edited by cigcode; 02-29-2004 at 05:10 AM.

  2. #2
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main(void)
    {
    	struct tm* tmp;
    
    	time_t t = time(NULL);
    
    	tmp = localtime(&t);
    
    	printf("%d", tmp->tm_year);
    
    	return 0;
    }
    In your example code gettime->tm_year holds years since 1900.
    R.I.P C89

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    ahh i c what you did.. you also added a NULL in the time fuction.. hmm so NULL == current time correct.. i should of thought of this before its like mysql with a colum of datetime.. now i get it.. 10x
    Last edited by cigcode; 02-29-2004 at 07:48 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >hmm so NULL == current time correct..
    To a certain extent. The time function returns the current calendar time or (time_t)-1 if the time is not available. Time also takes a pointer to time_t as an argument. If that pointer is not NULL then the current calendar time is assigned to it as well. So tests that use the return value directly:
    Code:
    time_t now;
    
    if ( ( now = time ( NULL ) ) != (time_t)-1 ) {
      /* Work with now */
    }
    Can be written so that they're easier to read:
    Code:
    time_t now;
    
    if ( time ( &now ) != (time_t)-1 ) {
      /* Work with now */
    }
    now = time ( NULL );

    and

    time ( &now );

    do the same thing, they just get there in a different way.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    ah ok i get.. but what i dont get is why this is giving me the value of 104 instead of 04 or 2004

    Code:
    struct tm* gettime;
        
        time_t t = time( NULL );
        char *now, buff[20];
        
        gettime = localtime( &t );
        
        printf( "Year: %d\n\n", gettime->tm_year );
    should'nt it just give me 2004 or 04

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    1900 + 104 == 2004.

    The tm_year member of struct tm yields years since 1900.

    Take a peek. http://www-ccs.ucsd.edu/c/time.html#tm
    Last edited by c99; 02-29-2004 at 09:44 AM.
    R.I.P C89

  7. #7
    Registered User
    Join Date
    Feb 2004
    Posts
    28
    ic thank you again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 02:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM