[m-rev.] for review: smart recompilation
Simon Taylor
stayl at cs.mu.OZ.AU
Wed May 30 01:52:23 AEST 2001
On 29-May-2001, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 28-May-2001, Simon Taylor <stayl at cs.mu.OZ.AU> wrote:
> > library/time.m:
> > Add undocumented predicates to convert time_t to and from int.
> > They are undocumented because they rely on the assumption that
> > `time_t' fits in a Mercury `int'.
>
> I don't think this is a good idea, if it can be avoided.
> It would be better to write the code in a way which doesn't
> rely on that assumption.
>
> Also it would not be a great idea to write out time_t values as ints
> into files, since that would cause problems if the time_t representation
> is not the same on all platforms that access those files.
>
> Instead, how about writing out the time_t values as strings,
> e.g. in yyyy-mm-dd hh:mm:ss format, and then parsing them back in again?
>
> The output can be done by
>
> char buf[sizeof "yyyy-mm-dd hh:mm:ss"];
> strftime(buf, sizeof buf, "%Y-%m-%d %H:%M:%S", tm_ptr);
>
> If we could rely on X/Open, we could also use strptime() to parse it,
> but it would probably be best to roll our own.
That sounds like a good idea, but in practice it's a bit painful.
We need to write time values in UTC (otherwise known as Greenwich
Mean Time) to avoid problems with daylight saving time or users
changing timezones. The problem with that is that POSIX does not
provide a portable way to convert a broken down time (struct tm)
containing a UTC time into a time_t. (mktime() converts a `struct tm'
containing a local time into a time_t).
Some systems (e.g. Linux) have a non-standard timegm() function.
Some (e.g. DEC OSF, Solaris), don't.
>From the glibc manual:
- Function: time_t timegm (struct tm *BROKENTIME)
`timegm' is functionally identical to `mktime' except it always
takes the input values to be Coordinated Universal Time (UTC)
regardless of any local time zone setting.
Note that `timegm' is the inverse of `gmtime'.
*Portability note:* `mktime' is essentially universally
available. `timegm' is rather rare. For the most portable
conversion from a UTC broken-down time to a simple time, set the
`TZ' environment variable to UTC, call `mktime', then set `TZ'
back.
To me, messing about with environment variables to make a C function
do what I want seems like a dumb thing to do.
As far as I can tell, the only other alternative is to pass timestamps
around as type `tm', or as strings formatted as above, rather than
`time_t'. Given that we don't actually care about contents of the time
values, just comparison between time values, I prefer the formatted
strings option. The `tm' values contain fields for day of week,
day of year and daylight saving time, which aren't used, and which
cause similar problems with comparisons as the `term__context' fields
in `prog_data__type's.
Comments?
Simon.
--------------------------------------------------------------------------
mercury-reviews mailing list
post: mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------
More information about the reviews
mailing list