-
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( );
}
-
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.
-
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
-
>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.
-
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
-
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
-