#include <stdio.h>
#include <time.h>
int main(){
printf("%d\n",time(NULL));
printf("Second: %ld\n",time(NULL)/60);
printf("Minute: %ld\n",time(NULL)/(60*60));
printf("Hour: %ld\n",time(NULL)/(60*60*60));
printf("Day %ld\n",time(NULL)/(60*60*60*24));
return 0;
}
Today is November 19. And it is 323. day of the year. When I run the program it print 321. what is the reason?
>Solution :
time(NULL) returns seconds since January 1:st 1970.
Your calculations are wrong in that you assume that they are from the beginning of current the year and you divide by seconds once too many.
Amazingly, the two errors almost take each other out and you end up with a result which is nearly, but not exactly right. In fact, had you been running this program two years ago (i.e. sixty years after 1970) you would have gotten the "right" result.