Letni a zimni cas

Igor Bujna igor.bujna na maxi-tip.cz
Úterý Říjen 29 15:59:07 CET 2002


Zdravim,
chtel jsem se zeptat jestli nekdo nenarazil na inetu na casove funkce 
dostounix/unixtodos(byli v borlandu)-prevadi z dosove struktury time a 
date na unixovy time_t a naopak.
Tyto funkce jsou jednoduse implementovatelny, akorat jak se vyporadat, 
kdyz lokalni cas je zimni a pretocit pres tyto cas, ktery je letni.
Priklad:
mam lokalni cas 29.10.2002 11:20 a potrebuji predelat cas 27.10.2002 
23:00(definovany strukturami date/time) na time_t.
Podle meho to nebude spravne fungovat v zavislosti na letnim/zimnim case.
Priklad mnou vytvorene dane funkce
------------------------------------------------------------------------
struct date
{
short da_year; /* current year */
char da_day; /* day of the month */
char da_mon; /* month (1=Jan) */
};

struct time
{
unsigned char ti_min; /* minutes */
unsigned char ti_hour; /* hours */
unsigned char ti_hund; /* hundredths of seconds */
unsigned char ti_sec; /* seconds */

};

/* vraci dany cas v time_t ze struktur data+time */
time_t
dostounix (struct date *d, struct time *t)
{
struct tm utm, *ptm;
time_t the_time = time (0);

ptm = gmtime (&the_time);
memset (&utm, N_C, sizeof (struct tm));
memcpy ((void *) &utm, (void *) ptm, sizeof (struct tm));

utm.tm_sec = t->ti_sec;
utm.tm_min = t->ti_min;
utm.tm_hour = t->ti_hour;
utm.tm_mday = d->da_day;
utm.tm_mon = d->da_mon - 1;
utm.tm_year = d->da_year - 1900;

the_time = mktime (&utm);
ptm = localtime (&the_time);
memset (&utm, N_C, sizeof (struct tm));
memcpy ((void *) &utm, (void *) ptm, sizeof (struct tm));

utm.tm_sec = t->ti_sec;
utm.tm_min = t->ti_min;
utm.tm_hour = t->ti_hour;
utm.tm_mday = d->da_day;
utm.tm_mon = d->da_mon - 1;
utm.tm_year = d->da_year - 1900;

return (mktime (&utm));
}

/* Prekonvertuje dany cas z time_t do struktur date+time */
void
unixtodos (time_t the_time, struct date *d, struct time *t)
{
struct tm utm, *ptm;

memset (&utm, N_C, sizeof (struct tm));
ptm = localtime (&the_time);
memcpy ((void *) &utm, (void *) ptm, sizeof (struct tm));

t->ti_sec = utm.tm_sec;
t->ti_hund = 0;
t->ti_min = utm.tm_min;
t->ti_hour = utm.tm_hour;
d->da_day = utm.tm_mday;
d->da_mon = utm.tm_mon + 1;
d->da_year = utm.tm_year + 1900;
}






Další informace o konferenci Linux