[slang-users] TIme Routines.
John E. Davis
davis at space.mit.edu
Mon Jul 18 16:21:13 EDT 2005
jmrobert5 at mchsi.com <jmrobert5 at mchsi.com> wrote:
>A puzzler...
Apparantly, newer versions of glibc return -1 when the tm struct has a
value before the epoch. This is because various standards require
mktime to return -1 upon error. As a result, how does one distinguish
one second before the epoch from an error?
The way solaris handles this is via errno, i.e.,
errno=0;
if (((time_t)-1 == (secs = mktime(&tm)))
&& errno)
{
/* error occurred */
.
.
}
I would have preferred glibc to adopt a similar approach.
For now one work-around would be to write a slang routine to "solve"
for the value of t such that
tm = localtime (t);
where tm is given. A binary search (bisection) algorithm would
probably work best. In fact, before mktime was added to slang, I
wrote my own version of mktime using such a method:
define my_mktime (tm)
{
variable secs1 = _time ();
variable secs0 = 0;
while (secs1 != secs0)
{
variable tm_1 = localtime (secs1);
variable tm_0 = localtime (secs0);
variable secs = (secs0 + secs1)/2;
variable tm2 = localtime (secs);
if (tm2.tm_year > tm.tm_year)
{
secs1 = secs;
continue;
}
if (tm2.tm_year < tm.tm_year)
{
secs0 = secs;
continue;
}
if (tm2.tm_mon > tm.tm_mon)
{
secs1 = secs;
continue;
}
if (tm2.tm_mon < tm.tm_mon)
{
secs0 = secs;
continue;
}
if (tm2.tm_mday > tm.tm_mday)
{
secs1 = secs;
continue;
}
if (tm2.tm_mday < tm.tm_mday)
{
secs0 = secs;
continue;
}
if (tm2.tm_hour > tm.tm_hour)
{
secs1 = secs;
continue;
}
if (tm2.tm_hour < tm.tm_hour)
{
secs0 = secs;
continue;
}
if (tm2.tm_min > tm.tm_min)
{
secs1 = secs;
continue;
}
if (tm2.tm_min < tm.tm_min)
{
secs0 = secs;
continue;
}
if (tm2.tm_sec > tm.tm_sec)
{
secs1 = secs;
continue;
}
if (tm2.tm_sec < tm.tm_sec)
{
secs0 = secs;
continue;
}
break;
}
return secs0;
}
More information about the slang-users-l
mailing list