| delorie.com/archives/browse.cgi | search |
| From: | "Michal Strelec" <strelec AT adam DOT osu DOT cz> |
| Newsgroups: | comp.os.msdos.djgpp |
| Subject: | Strange localtime function behaviour |
| Date: | Mon, 26 Jul 1999 11:19:02 +0200 |
| Organization: | Czech Technical University |
| Lines: | 72 |
| Message-ID: | <7nh9b2$gcb$1@ns.felk.cvut.cz> |
| NNTP-Posting-Host: | cl106161.osu.cz |
| X-Newsreader: | Microsoft Outlook Express 4.72.3155.0 |
| X-MimeOLE: | Produced By Microsoft MimeOLE V4.72.3155.0 |
| To: | djgpp AT delorie DOT com |
| DJ-Gateway: | from newsgroup comp.os.msdos.djgpp |
| Reply-To: | djgpp AT delorie DOT com |
Hi,
I've written small piece of code which add one (date+time) to another and
converts output to normal format (no overriding of month, days,...).
First I fill 2 variables of my custom type. rtd1 is first time and rtd2 mean
interval to be added to rtd1.
typedef struct {
byte RTD_hour;
byte RTD_min;
byte RTD_sec;
natural RTD_year;
byte RTD_mon;
byte RTD_day;
} RTD;
RTD rtd1,rtd2;
rtd1.RTD_hour=8;
rtd1.RTD_min=42;
rtd1.RTD_sec=23;
rtd1.RTD_year=1999;
rtd1.RTD_mon=7;
rtd1.RTD_day=26;
/* Inteval to be add to rtd1 */
rtd1.RTD_hour=0;
rtd1.RTD_min=0;
rtd1.RTD_sec=0;
rtd1.RTD_year=0;
rtd1.RTD_mon=1;
rtd1.RTD_day=0;
Now I move first time to classical time structure
/* Now create time structure */
cas.tm_sec=rtd1.RTD_sec;
cas.tm_min=rtd1.RTD_min;
cas.tm_hour=rtd1.RTD_hour;
cas.tm_mday=rtd1.RTD_day;
cas.tm_mon=--rtd1.RTD_mon;
cas.tm_year=rtd1.RTD_year-1900;
cas.tm_isdst=0; /* Must be 0 otherwise VERY nondeterministic problem may
occuare */
And add interval
cas.tm_sec+=rtd2.RTD_sec;
cas.tm_min+=rtd2.RTD_min;
cas.tm_hour+=rtd2.RTD_hour;
cas.tm_mday+=rtd2.RTD_day;
cas.tm_mon+=rtd2.RTD_mon;
cas.tm_year+=rtd2.RTD_year;
At last I do conversion
/* Now convert to sec */
nsec=mktime(&cas);
/* And back to record */
cas=*localtime(&nsec);
Here it is. This program works Good. But when I have ommited the line with
setting cas.tm_isdst=0;
Conversion makes sometimes strange time+dates. And I couldn't find anywhere
WHY this has to be set. cas.tm_wday or cas.tm_yday don't need to be set so
why cas.tm_isdst do?
I have tried this example in BC3.1 and there works everything fine. (even
without setting cas.isdst=0)
Mike
| webmaster | delorie software privacy |
| Copyright © 2019 by DJ Delorie | Updated Jul 2019 |