time

This is a discussion on time within the C Programming forums, part of the General Programming Boards category; how would i access tm_year in this program Code: #include <time.h> #include <stdio.h> int main( ) { time_t today; char ...

  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 04:10 AM.

  2. #2
    c99
    c99 is offline
    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 06:48 AM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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
    c99
    c99 is offline
    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 08: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-02-2006, 11:46 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 05:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21