Datum zählt über Wert???

24/12/2009 - 20:04 von Jens Kallup | Report spam
Hallo,

wie kann ich richtigerweise ein Datum mit Addition
korrekt verarbeiten?
wenn ich "date_dplus" auf 8 setzte und das heutige
Datum (24.12.2009) zugrunde lege, dann wird buffer
in "add_dtoc" stets 32.12.09 ausgeben - was nicht
richtig ist - oder?
wie macht aan es richtig?

hier ein wenig code:

int date_dplus = 0; // tag + vorgabewert

extern "C" EXPORT char* print_dtoc(void)
{
char *buffer = (char*)malloc(30);
time_t tt;
struct tm *tod;
time(&tt);
tod = localtime(&tt);

if (year_century == 0)
{
switch (date_format)
{
case 0:
sprintf(buffer,"%02d.%02d.%02d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year%100
); break;
case 1:
sprintf(buffer,"%02d/%02d/%02d",tod->tm_mon+1,tod->tm_mday+date_dplus
,tod->tm_year%100); break;
case 2:
sprintf(buffer,"%02d.%02d.%02d",tod->tm_year%100,tod->tm_mon+1,tod->tm_mday+date_dplus
); break;
case 3:
sprintf(buffer,"%02d/%02d/%02d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year%100
); break;
case 4:
sprintf(buffer,"%02d/%02d/%02d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year%100
); break;
case 5:
sprintf(buffer,"%02d-%02d-%02d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year%100
); break;
case 6:
sprintf(buffer,"%02d/%02d/%02d",tod->tm_year%100,tod->tm_mon+1,tod->tm_mday+date_dplus
); break;
case 7:
sprintf(buffer,"%02d-%02d-%02d",tod->tm_mon+1,tod->tm_mday+date_dplus,tod->tm_year%100
); break;
case 8:
sprintf(buffer,"%02d/%02d/%02d",tod->tm_mon+1,tod->tm_mday+date_dplus,tod->tm_year%100
); break;
case 9:
sprintf(buffer,"%02d/%02d/%02d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year%100
); break;
case 10:
sprintf(buffer,"%02d/%02d/%02d",tod->tm_year%100,tod->tm_mon+1,tod->tm_mday+date_dplus
); break;
}
}
else {
switch (date_format)
{
case 0:
sprintf(buffer,"%02d.%02d.%04d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year+1900
); break;
case 1:
sprintf(buffer,"%02d/%02d/%04d",tod->tm_mon+1,tod->tm_mday+date_dplus
,tod->tm_year+1900); break;
case 2:
sprintf(buffer,"%04d.%02d.%02d",tod->tm_year+1900,tod->tm_mon+1,tod->tm_mday+date_dplus
); break;
case 3:
sprintf(buffer,"%02d/%02d/%04d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year+1900
); break;
case 4:
sprintf(buffer,"%02d/%02d/%04d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year+1900
); break;
case 5:
sprintf(buffer,"%02d-%02d-%04d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year+1900
); break;
case 6:
sprintf(buffer,"%04d/%02d/%02d",tod->tm_year+1900,tod->tm_mon+1,tod->tm_mday+date_dplus
); break;
case 7:
sprintf(buffer,"%02d-%02d-%04d",tod->tm_mon+1,tod->tm_mday+date_dplus,tod->tm_year+1900
); break;
case 8:
sprintf(buffer,"%02d/%02d/%04d",tod->tm_mon+1,tod->tm_mday+date_dplus,tod->tm_year+1900
); break;
case 9:
sprintf(buffer,"%02d/%02d/%04d",tod->tm_mday+date_dplus,tod->tm_mon+1,tod->tm_year+1900
); break;
case 10:
sprintf(buffer,"%04d/%02d/%02d",tod->tm_year+1900,tod->tm_mon+1,tod->tm_mday+date_dplus
); break;
}
}
return (char*)cat_string(buffer);
}

char* cat_dtoc(char *txt)
{
cat_string(txt);
return print_dtoc();
}

char* add_dtoc(double d, char *txt)
{
char *buffer = (char*)malloc(1024);
date_dplus = int(d);
strcpy(buffer,cat_dtoc(txt));

return buffer;
}


Gruß
Jens
 

Lesen sie die antworten

#1 Markus Wichmann
24/12/2009 - 23:27 | Warnen spam
Jens Kallup () schrieb:
Hallo,

wie kann ich richtigerweise ein Datum mit Addition
korrekt verarbeiten?
wenn ich "date_dplus" auf 8 setzte und das heutige
Datum (24.12.2009) zugrunde lege, dann wird buffer
in "add_dtoc" stets 32.12.09 ausgeben - was nicht
richtig ist - oder?
wie macht aan es richtig?




Nicht so kompliziert. Du willst also eine Anzahl von Tagen auf ein Datum
addieren. Wie geht das? So hier:

int is_leap_year(unsigned int year)
{
int retval = 0;
if (!(year % 400))
retval = 1;
else if ((year % 100) && !(year % 4))
retval = 1;
return retval;
}

struct Date {
unsigned char day, /* 0..30 */
month; /* 0..11 */
unsigned int year; /* 0..65535 */
};

struct Date add_day(struct Date* d, unsigned int inc)
{
struct Date retval;
unsigned char maxday[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
maxday[1] += is_leap_year(d->year);
retval.day = d->day + inc;
retval.month = d->month;
retval.year = d->year;

while (retval.day > maxday[retval.month])
{
retval -= maxday[retval.month];
retval.month++;
if (retval.month > 11)
{
retval.month = 0;
retval.year++;
maxday[1] = is_leap_year(retval.year)? 28: 29;
}
}

return retval;
}

Gruß
Jens



HTH,
Markus
Nur weil ein Genie nix reißt, muß ja nun nicht gleich jeder Idiot
pausieren... Bully hats ja auch geschafft.

news://freenews.netfront.net/ - complaints:

Ähnliche fragen