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
> wie kann ich richtigerweise ein Datum mit Addition
> korrekt verarbeiten?
Mit mktime() und den sonstigen Funktionen, die Du in <time.h> findest.
> extern "C" EXPORT char* print_dtoc(void)
Das ist kein gᅵltiges C. Das sieht stark nach C++ aus.
Gruᅵ. Claus
--
) )
(,) Ich wᅵnsche allen Mitlesern (,)
__|__ ein gesegnetes Weihnachtsfest __|__
| | | |
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.
-- gUnter nanonüm in de.alt.anime
--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
> 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?
Irgendwie sieht mir der Code recht unstrukturiert
aus, eine wilde Mischung mit Parametern und globalen
Variablen.
Hier mal ein Beispiel, wie ich es unter Beibehaltung der
Struktur machen würde. Warnung: Da das Orignal auch nicht
kompilierbar war, habe ich es auch sein gelassen. Die
Struktur sollte aber funktionieren.
Die wesentlichen Änderungen:
- einige Fehlerprüfungen hinzugefügt
(Bereichsprüfung, Stringlänge).
- date_dplus vor der Feldzerlegung angewendet.
- die Gemeinsamkeiten der sehr ähnlichen Formate im Code
zusammengefasst und die Abweichungen in eine Tabelle
ausgelagert.
- die recht unbekannte Formatvariante %1$02d und ähnliche
benutzt.
#define ELEMS(a) (sizeof (a)/sizeof ((a)[0]))
extern char *print_dtoc (void)
{
enum { DAY = 1, MONTH = 2, YEAR = 3 };
struct date_format_parameter
{
unsigned char pos_1;
unsigned char pos_2;
unsigned char pos_3;
char separator;
};
static const struct date_format_parameter date_formats []
= { { DAY, MONTH, YEAR, '.' }, // 0
{ MONTH, DAY, YEAR, '/' }, // 1
{ YEAR, MONTH, DAY, '.' }, // 2
{ DAY, MONTH, YEAR, '/' }, // 3
{ DAY, MONTH, YEAR, '/' }, // 4
{ DAY, MONTH, YEAR, '-' }, // 5
{ YEAR, MONTH, DAY, '/' }, // 6
{ MONTH, DAY, YEAR, '-' }, // 7
{ MONTH, DAY, YEAR, '/' }, // 8
{ DAY, MONTH, YEAR, '/' }, // 9
{ YEAR, MONTH, DAY, '/' } // 10
};
char *buffer = malloc (30);
time_t tt;
struct tm *tod;
const struct date_format_parameter *format;
char format_string [32];
time (&tt);
tt += (time_t) 86400 * date_dplus; // 86400 = Sekunden/Tag
tod = localtime (&tt);
if (date_format < 0 && date_format >= ELEMS (date_formats))
strcpy (buffer, "");
else
{
format = date_formats [date_format];
snprintf (format_string, sizeof format_string,
"%%%d$%02dd%c%%%d$%02dd%c%%%d$%02dd",
format->pos_1, year_century == 0 ? 2 : 4,
format->separator,
format->pos_2, year_century == 0 ? 2 : 4,
format->separator,
format->pos_3, year_century == 0 ? 2 : 4);
snprintf (buffer, 30, format_string,
tod->tm_mday, tod->tm_mon + 1,
year_century == 0 ? tod->tm_year % 100
: tod->tm_year + 1900);
}
return (char *) cat_string (buffer);
}
--
Viele Grüße,
Jens Schmidt
ein dickes Danke an Euch!
habe es nun hinbekommen.
Gruᅵ
Jens