Re: using mktime()
From: Dave Thompson (david.thompson1_at_worldnet.att.net)
Date: 02/21/05
- Next message: Dave Thompson: "Re: passing array to isdigit()"
- Previous message: infobahn: "Re: bitwise operators"
- In reply to: Al Bowers: "Re: using mktime()"
- Next in thread: infobahn: "Re: using mktime()"
- Reply: infobahn: "Re: using mktime()"
- Reply: Al Bowers: "Re: using mktime()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 21 Feb 2005 07:25:45 GMT
On Sat, 12 Feb 2005 23:33:50 -0500, Al Bowers <xabowers@rapidsys.com>
wrote:
> Chris Croughton wrote:
<snip>
> > The correct way to [offset] times is to use the broken-down structure,
> > mess about with the fields and make that back into a time_t using
> > mktime(). For instance, to get the time 2:23:45 from now use:
> >
> > time_t now = time(NULL);
> > time_t then;
> > struct tm tt = *gmtime(&now);
> > tt.tm_hour += 2;;
> > tt.tm_min += 23;
> > tt.tm_sec += 45;
> > then = mktime(&tt);
> >
> > (that's a snippet, not a complete program -- some of the pedants will
> > complain about not including headers and the like if I don't say that).
> >
> > Unfortunately, the C standard doesn't say anything about the allowable
> > ranges of the fields, except that they are of type int and the normal
> > ranges are what you would expect. In particular, it doesn't say whether
> > negative values have the correct effect, so while adding to times is no
> > problem reducing them could be undefined...
> >
>
> No. What the Standard says is that function mktime will bring
> all values into range. For example the range for struct tm member
If the call is successful, yes. It (definitely) won't be if the
requested time is not representable in time_t, and it's not clear if
mktime() is allowed to fail in other cases that the implementor
decides are "too hard" -- the comments in the Olson public-domain
implementation imply to me that this might have happened.
> tm_sec is 0-59. If tm_sec has the value of say -69 the function
> mktime will bring tm_sec into range by subtracting 1 from tm_min.
Actually tm_sec is 0-60 to allow for (positive) leap seconds, which
are rarely if ever implemented. That is, leap seconds actually happen
(for now, there has been discussion of eliminating them) but (most?) C
implementations (and systems) just treat them as transient errors.
The only people I've heard of actually using them are the ones for
whom they were designed -- astronomers and space navigators, and their
only contact to most ordinary people, GPS.
Presumably you meant -60sec = -1min. -69sec = -2min leaving 51sec.
> And on up the ladder, if necessary, until finally tm_mon and
> tm_year are determined. Then tm_wday and tm_yday components of the
> struct are set appropriately. I would think that a Standard C
> that would allow you to add to a time but make reducing it undefined
> would be unwise.
- David.Thompson1 at worldnet.att.net
- Next message: Dave Thompson: "Re: passing array to isdigit()"
- Previous message: infobahn: "Re: bitwise operators"
- In reply to: Al Bowers: "Re: using mktime()"
- Next in thread: infobahn: "Re: using mktime()"
- Reply: infobahn: "Re: using mktime()"
- Reply: Al Bowers: "Re: using mktime()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|