I believe you are really supposed to initialize tm_isdst also before
feeding the struct tm to mktime(). 0 is normally a pretty good value, unless
you're dealing with something like logs around the time of a daylight-savings-time
transition, where you can have two occurrences of the time without the timezone
specifier being considered.
Yes. If I enter the month, day, year, hour, minute and second into
the tm structure and call mktime() The day of the year, day of the
week and daylight savings time fields are automatically filled in.
The next step is that I would like to know somethings about the next
date. I can take the time_t date from the above example and add
24*60*60 (sec/hr) seconds to it to get a time 24 hours from the first
time.
No, you are not guaranteed to be able to do any type of math on a
time_t. A time_t is not guaranteed to be of the form "number of
<time unit> since <epoch>". As a horrible example, consider the
following format:
HHmmSSMMDDYYYYY
HH = hour (0-23)
mm = minute (0-59)
SS = second (0-61) [*]
MM = month (1-12)
DD = day (1-31)
YYYYY = year (Y100K problem!)
[*] wierdness here due to leap seconds
fill in the values with decimal digits, now treat it as a decimal
integer. Try fiddling with this, and you realize that subtracting
these gives complete mush (you can't even do a comparison to figure
out which time is earlier, as 15:01 is "earlier" than 15:02 regardless
of what century it's in.)
Try subtracting 1 second (or 1000000000) from 000000010102004 and
see what you get. You SHOULD get 235959123102003 if you really
subtracted 1 second (I'm assuming this particular time did not have
any leap seconds without looking it up), but you won't.
There is no good way to portably add 24 hours to a timestamp, when
you want to talk about the PASSAGE OF TIME, not WHAT THE CLOCK
READS. These differ when you cross a daylight-savings-time transition
or a leap second. 15:00 on consecutive days may be 23 or 25 hours
apart. Adding 1 to tm_day or adding 24 to tm_hour works most of
the time. Then again, even a stopped clock reads correctly twice
a day.
MS-DOS file stamps are not too unlike this (using bit fields to
represent year, month, day, hour, minute, and seconds divided by
two within a 32-bit integer), although at least they put the
high-order time elements in the high-order part of the integer, so
you actually can compare time stamps using > or < .
Next I want to convert the time_t date to a structure. If I call
localtime() I get returned pointer to a tm sturcture and in that
structure is the correct data.
But where is that structure located?
The structure returned by localtime() is static data that may be
overwritten on a subsequent call. This is specified by ANSI C and
should be in any decent documentation of localtime(). (If you write
functions like this, you'd better also carefully document this.)
When in doubt MAKE A COPY OF IT (the structure, not the pointer).
You do NOT have to allocate your copy with malloc() when an auto
variable will do.
If I call localtime() again will
I get a different pointer.
This is not specified. localtime() could play tricks with rotating
several buffers. Or, more commonly, it could return the same pointer
every time.
If these routines only create one tm
structure ever then I can live with that.
If however the run malloc()
to create a structure but never free(), Then I'll run out of ram.
You must not free() the struct tm returned by localtime().
Gordon L. Burditt