a bug in vc.net

T

tom

for the following code, gcc compiler will display
it is new day: 20040201
while, VC.NET will display "20040131".
It seems that other comiler will get the same output as gcc.

Does VC.NET break the C standard code?



#include <time.h>
#include <stdio.h>

int main()
{
struct tm today;
time_t currentTime;
char currentDay[10+256+1];

today.tm_year = 104;
today.tm_mon = 1;
today.tm_mday = 1;
today.tm_sec=0; today.tm_min = 0; today.tm_hour =0;

if( mktime(&today) != -1)
{
strftime(currentDay, 16, "%Y%m%d", &today);
printf("it is new day: %s\n", currentDay);
}
}
 
M

muchan

tom said:
for the following code, gcc compiler will display
it is new day: 20040201
while, VC.NET will display "20040131".
It seems that other comiler will get the same output as gcc.

Does VC.NET break the C standard code?



#include <time.h>
#include <stdio.h>

int main()
{
struct tm today;
time_t currentTime;
char currentDay[10+256+1];

today.tm_year = 104;
today.tm_mon = 1;
today.tm_mday = 1;
today.tm_sec=0; today.tm_min = 0; today.tm_hour =0;

if( mktime(&today) != -1)
{
strftime(currentDay, 16, "%Y%m%d", &today);
printf("it is new day: %s\n", currentDay);
}
}

In this code you didn't initialise all the member var of the today.
That is, tm_wday, tm_yday, and tm_isdst may contain _any_ value.
mktime() ignores tm_wday, tm_yday, but about tm_isdst MSDN says:

When specifying a tm structure time, set the tm_isdst field to 0
to indicate that standard time is in effect, or to a value greater
than 0 to indicate that daylight savings time is in effect, or to a
value less than zero to have the C run-time library code comput
whether standard time or daylight savings time is in effect.
(The C run-time library assumes the United States’s rules for
implementing the calculation of Daylight Saving Time). tm_isdst
is a required field. If not set, its value is undefined and the
return value from mktime is unpredictable. If timeptr points to
a tm structure returned by a previous call to asctime, gmtime,
or localtime, the tm_isdst field contains the correct value.

By putting either
memset(&today, 0, sizeof(today));
or
time_t t;
struct tm *lt;

time(&t);
lt = localtime(&t);
today = *lt;
before your code setting
today.tm_year = 104;
today.tm_mon = 1;
today.tm_mday = 1;
today.tm_sec=0; today.tm_min = 0; today.tm_hour =0;
mktime() properly returns Feb 1, 2004.


muchan
 
T

tom

muchan said:
tom said:
for the following code, gcc compiler will display
it is new day: 20040201
while, VC.NET will display "20040131".
It seems that other comiler will get the same output as gcc.

Does VC.NET break the C standard code?



#include <time.h>
#include <stdio.h>

int main()
{
struct tm today;
time_t currentTime;
char currentDay[10+256+1];

today.tm_year = 104;
today.tm_mon = 1;
today.tm_mday = 1;
today.tm_sec=0; today.tm_min = 0; today.tm_hour =0;

if( mktime(&today) != -1)
{
strftime(currentDay, 16, "%Y%m%d", &today);
printf("it is new day: %s\n", currentDay);
}
}


In this code you didn't initialise all the member var of the today.
That is, tm_wday, tm_yday, and tm_isdst may contain _any_ value.
mktime() ignores tm_wday, tm_yday, but about tm_isdst MSDN says:

When specifying a tm structure time, set the tm_isdst field to 0
to indicate that standard time is in effect, or to a value greater
than 0 to indicate that daylight savings time is in effect, or to a
value less than zero to have the C run-time library code comput
whether standard time or daylight savings time is in effect.
(The C run-time library assumes the United States’s rules for
implementing the calculation of Daylight Saving Time). tm_isdst
is a required field. If not set, its value is undefined and the
return value from mktime is unpredictable. If timeptr points to
a tm structure returned by a previous call to asctime, gmtime,
or localtime, the tm_isdst field contains the correct value.

By putting either
memset(&today, 0, sizeof(today));
or
time_t t;
struct tm *lt;

time(&t);
lt = localtime(&t);
today = *lt;
before your code setting
today.tm_year = 104;
today.tm_mon = 1;
today.tm_mday = 1;
today.tm_sec=0; today.tm_min = 0; today.tm_hour =0;
mktime() properly returns Feb 1, 2004.


muchan

Thanks so much. it helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top