Results in Debug and Release mode are different
Hi!,
I've created three functions that would work together in order to generate a date is string YYYY-MM-DD format.
The code works fine in Debug mode.The problem is when I try to run this code in the Release mode.
It (the program) gives me two different results when I run it in Debug and Release mode.
And no compile error and warning are present when I compile it in those two modes respectively.
here is the code
Code:
#include <string.h>
#include <stdio.h>
#include <time.h>
#define HOUR 261
#define MINUTE 262
#define DAY 263
#define MONTH 264
#define YEAR 265
void padUpString(char *str, char* filler, int len, int fillAtTheEnd)
{
int i;
char newstr[50]="";
int strLen = strlen(str);
int diff = len - strLen;
if (strLen < len)
{
for (i=0; i< len ; i++)
{
if (fillAtTheEnd)
{
if( i >= strLen) *(str+strLen++)=*filler;
}else
{
if( i < diff) strcat(newstr,filler);
}
}
if (!fillAtTheEnd)
{
strcat(newstr,str);
strcpy(str,newstr);
}
}
}
int getCurrentTimeValue(int timeElement)
{
struct tm *tm_today;
time_t t;
t = time(NULL);
tm_today = localtime(&t);
//printf("timeElement is %d\n",timeElement);
if (timeElement == HOUR)
{
return tm_today->tm_hour;
}
if (timeElement == MINUTE)
{
return tm_today->tm_min;
}
if (timeElement == DAY)
{
//printf("DAY is %d\n",timeElement); //for debug purposes
return tm_today->tm_mday;
}
if (timeElement == MONTH)
{
//printf("MONTH is %d\n",timeElement);//for debug purposes
return tm_today->tm_mon + 1;
}
if (timeElement == YEAR)
{
//printf("YEAR is %d\n",timeElement);//for debug purposes
return tm_today->tm_year + 1900;
}
return -1;
}
void getProcessDate(char *procDate)
{
//YYYY-MM-DD
char currentTime[11];
char year[4];
char day[2];
char month[2];
sprintf(year,"%d",getCurrentTimeValue(YEAR));
sprintf(month,"%d",getCurrentTimeValue(MONTH));
sprintf(day,"%d",getCurrentTimeValue(DAY));
printf("\nCheck date\n"); // for testing purpose only
printf("year [%s]\n",year);
printf("month [%s]\n",month);
printf("day [%s]\n",day);
padUpString(month,"0",2,0);
padUpString(day,"0",2,0);
printf("\nAfter padding\n");// for testing purpose only
printf("month [%s]\n",month);
printf("day [%s]\n",day);
sprintf(currentTime,"%s-%s-%s",year,month,day);
currentTime[10] = '\0';
memcpy(procDate,currentTime,sizeof(currentTime));
}
int main()
{
char date[30];
getProcessDate(date);
printf("%s\n",date);
return 0;
}
the output
Debug Mode
Quote:
Check date
year [2006]
month [5]
day [24]
After padding
month [05]
day [24]
2006-05-24
Release Mode
Quote:
Check date
year [2006]
month []
day [24]
After padding
month [00]
day [2400]
-00-2400
as you can see the "month" in Release mode is blank,which I suspect is the one causing the problem.
Does anyone know why the results are different?
and how would I fix such problem?
I'm using MVC6 in Win2K
Regards,
jaro