E
Emmanuel Delahaye
Hi,
Is there a portable way to convert the value returned by difftime (a
number of seconds of type double) in time_t, which, AFAIK, is not a
number of seconds.
For this reason, I guess, that this code of mine, supposed to give an
example about 'how to use time functions correctly' is not portable, is
it ?
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main (void)
{
time_t now = time (NULL);
struct tm tm_now = *localtime (&now);
char s[64];
strftime (s, sizeof s, "%d/%m/%Y", &tm_now);
printf ("Today is : %s\n", s);
/* next Christmas */
{
struct tm tm_xmas =
{0};
tm_xmas.tm_year = tm_now.tm_year;
tm_xmas.tm_mon = 12 - 1;
tm_xmas.tm_mday = 25;
/* ajustment */
{
time_t xmas = mktime (&tm_xmas);
strftime (s, sizeof s, "%d/%m/%Y", &tm_xmas);
printf ("next Christmas is : %s\n", s);
{
time_t diff = difftime (xmas, now);
struct tm tm_diff = *gmtime (&diff);
printf ("Only %d days remaining before Christmas\n",
tm_diff.tm_yday);
}
}
}
return 0;
}
Today is : 07/01/2006
next Christmas is : 25/12/2006
Only 351 days remaining before Christmas
Is there a portable way to convert the value returned by difftime (a
number of seconds of type double) in time_t, which, AFAIK, is not a
number of seconds.
For this reason, I guess, that this code of mine, supposed to give an
example about 'how to use time functions correctly' is not portable, is
it ?
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main (void)
{
time_t now = time (NULL);
struct tm tm_now = *localtime (&now);
char s[64];
strftime (s, sizeof s, "%d/%m/%Y", &tm_now);
printf ("Today is : %s\n", s);
/* next Christmas */
{
struct tm tm_xmas =
{0};
tm_xmas.tm_year = tm_now.tm_year;
tm_xmas.tm_mon = 12 - 1;
tm_xmas.tm_mday = 25;
/* ajustment */
{
time_t xmas = mktime (&tm_xmas);
strftime (s, sizeof s, "%d/%m/%Y", &tm_xmas);
printf ("next Christmas is : %s\n", s);
{
time_t diff = difftime (xmas, now);
struct tm tm_diff = *gmtime (&diff);
printf ("Only %d days remaining before Christmas\n",
tm_diff.tm_yday);
}
}
}
return 0;
}
Today is : 07/01/2006
next Christmas is : 25/12/2006
Only 351 days remaining before Christmas