Why my print current date time (C language) gives different answer
I want to get the current date (day, mon and year). I found out there are some functions in C to do that like ctime (get the string of ti开发者_StackOverflowme), localtime and gmtime. I tried with following code but the output are different. I get this output:
The date and time is Tue Apr 20 2010 (which is correct)
The year is : 110
The year is : 110.
Does anybody know why?
int main(int argc, char** argv)
{
time_t now;
if((now = time(NULL)) == (time_t)-1)
{
puts("Failure in getting time");
}
else {
printf("The date and time is: %s\n", ctime(&now));
printf("The year is: %ld\n", localtime(&now)->tm_year);
printf("The year is: %ld\n", gmtime(&now)->tm_year);
}
getchar();
}
Look at the man page for ctiime - the year field is years from 1900.
http://linux.die.net/man/3/ctime
tm_year
The number of years since 1900.
精彩评论