ZAPPLE said:
Hello,
Great information. C a great Programming language. There are a number
of things that I dont know in C. Just now I am coming to know about
time_t, struct tm, daylight savings in C, difftime etc..
Thanks a lot for posting such usefull information. When I tried
running it in my computer, it showed 765426211 seconds.
Is there any way of finding the date in the past from the given
seconds. That is if we give 765426211 the out put should be 13:34 jan
25 1980.
Here's an example going the other way, counting up. You can work out
the other way, I'm sure. You may want to delete the block beginning
with if (!try && INT_MAX != SHRT_MAX) , since it might output a _lot_ of
lines. I have removed some 46720 lines of output from my run.
Redirecting output to a file will keep it from zipping past.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
int main(void)
{
struct tm epoch = /* Jan 25 1980 13:34 */ {.tm_sec = 0,.tm_min =
34,.tm_hour =
13,.tm_mday = 25,.tm_mon = 0,.tm_year = 80,.tm_isdst = -1
};
struct tm newtime; /* so we can leave epoch alone */
time_t then, now;
long addend = 765426211L, taddend, tmp;
int try = 0;
newtime = epoch; /* copying old value */
taddend = addend;
tmp = taddend + newtime.tm_sec;
printf("I'm going to try to add %ld seconds to the time\n"
"represented by %s"
"The struct member tm_sec is an int, so it is possible that\n"
"this cannot be done at once.\n"
"On my machine, INT_MAX is %d,\n"
"so I will need %ld (or more) tries. Your milage may
vary.\n\n",
taddend, asctime(&epoch), INT_MAX,
tmp / INT_MAX + (tmp % INT_MAX != 0));
while (taddend + newtime.tm_sec > INT_MAX) {
tmp = INT_MAX - newtime.tm_sec;
printf(" ... partial update %d, adding %ld\n", ++try, tmp);
newtime.tm_sec = INT_MAX;
taddend -= tmp;
if (mktime(&newtime) == (time_t) - 1) {
printf("Oops! mktime failed, quitting ...\n");
exit(EXIT_FAILURE);
}
printf(" Time has been updated to %s", asctime(&newtime));
}
if (!taddend)
printf("Done!\n");
else {
printf("Final addition (%ld)\n", taddend);
newtime.tm_sec += taddend;
if (mktime(&newtime) == (time_t) - 1) {
printf("Oops! mktime failed, quitting ...\n");
exit(EXIT_FAILURE);
}
printf(" Time has been updated to %s", asctime(&newtime));
then = mktime(&epoch);
now = mktime(&newtime);
printf("Check: the difference is %.0f seconds\n\n",
difftime(now, then));
}
if (!try && INT_MAX != SHRT_MAX) {
newtime = epoch; /* copying old value */
taddend = addend;
tmp = taddend + newtime.tm_sec;
printf("That didn't show any intermediate steps.\n"
"Let's pretend INT_MAX is actually %d.\n"
"Then I will need %ld (or more) tries. "
"Your milage may vary.\n\n",
SHRT_MAX, tmp / SHRT_MAX + (tmp % SHRT_MAX != 0));
while (taddend + newtime.tm_sec > SHRT_MAX) {
tmp = SHRT_MAX - newtime.tm_sec;
printf(" ... partial update %d, adding %ld\n", ++try, tmp);
newtime.tm_sec = SHRT_MAX;
taddend -= tmp;
if (mktime(&newtime) == (time_t) - 1) {
printf("Oops! mktime failed, quitting ...\n");
exit(EXIT_FAILURE);
}
printf(" Time has been updated to %s", asctime(&newtime));
}
if (!taddend)
printf("Done!\n");
else {
printf("Final addition (%ld)\n", taddend);
newtime.tm_sec += taddend;
if (mktime(&newtime) == (time_t) - 1) {
printf("Oops! mktime failed, quitting ...\n");
exit(EXIT_FAILURE);
}
printf(" Time has been updated to %s", asctime(&newtime));
then = mktime(&epoch);
now = mktime(&newtime);
printf("Check: the difference is %.0f seconds\n\n",
difftime(now, then));
}
}
return 0;
}
I'm going to try to add 765426211 seconds to the time
represented by Sun Jan 25 13:34:00 1980
The struct member tm_sec is an int, so it is possible that
this cannot be done at once.
On my machine, INT_MAX is 2147483647,
so I will need 1 (or more) tries. Your milage may vary.
Final addition (765426211)
Time has been updated to Tue Apr 27 15:57:31 2004
Check: the difference is 765426211 seconds
That didn't show any intermediate steps.
Let's pretend INT_MAX is actually 32767.
Then I will need 23360 (or more) tries. Your milage may vary.
... partial update 1, adding 32767
Time has been updated to Fri Jan 25 22:40:07 1980
... partial update 2, adding 32760
Time has been updated to Sat Jan 26 07:46:07 1980
[23360 updates deleted]
... partial update 23363, adding 32760
Time has been updated to Tue Apr 27 00:52:07 2004
... partial update 23364, adding 32760
Time has been updated to Tue Apr 27 09:58:07 2004
Final addition (21564)
Time has been updated to Tue Apr 27 15:57:31 2004
Check: the difference is 765426211 seconds