home *** CD-ROM | disk | FTP | other *** search
- /*
- l_time.c
- */
- /* Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
-
- Distributed under the terms of the GNU General Public License
- version 2 of june 1991 as published by the Free Software
- Foundation, Inc.
-
- This file is part of M0.
-
- M0 is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY. No author or distributor accepts responsibility to anyone for
- the consequences of using it or for whether it serves any particular
- purpose or works at all, unless he says so in writing. Refer to the GNU
- General Public License for full details.
-
- Everyone is granted permission to copy, modify and redistribute M0, but
- only under the conditions described in the GNU General Public License.
- A copy of this license is supposed to have been given to you along with
- M0 so you can know your rights and responsibilities. It should be in a
- file named LICENSE. Among other things, the copyright notice and this
- notice must be preserved on all copies. */
-
-
- #include "l_proto.h"
-
-
- eindex
- time_now(mproc p)
- {
- eindex ei = new_element(p, T_TIME);
- eptr ep = eaddr(p,ei);
-
- get_utc(&(ep->V.tim.sec), &(ep->V.tim.usec));
-
- return ei;
- }
-
-
- eindex
- time_addint(mproc p, eindex t, sint delta)
- {
- eindex ei = new_element(p, T_TIME);
- eptr ep, tp, ip;
- sint usec;
-
- tp = eaddr(p,t);
- ep = eaddr(p,ei);
-
- ep->V.tim.sec = tp->V.tim.sec + delta/1000000;
- if (delta < 0)
- usec = -( (-delta) % 1000000L );
- else
- usec = delta % 1000000L;
- ep->V.tim.usec = tp->V.tim.usec + usec;
- if (ep->V.tim.usec < 0) {
- ep->V.tim.sec--;
- ep->V.tim.usec += 1000000L;
- } else if (ep->V.tim.usec > 1000000L) {
- ep->V.tim.sec++;
- ep->V.tim.usec -= 1000000L;
- }
- return ei;
- }
-
-
- eindex
- time_diff(mproc p, eindex t1, eindex t2)
- {
- eindex ei;
- eptr ep, p1, p2;
-
- ei = new_element(p, T_INT);
- ep = eaddr(p,ei);
- p1 = eaddr(p,t1);
- p2 = eaddr(p,t2);
-
- ep->V.i = 1000000L * (p1->V.tim.sec - p2->V.tim.sec)
- + p1->V.tim.usec - p2->V.tim.usec;
- return ei;
- }
-
-
- int
- time_eq(struct time_s *t1, struct time_s *t2)
- {
- return (t1->sec != t2->sec
- || t1->usec != t2->usec) ? 0 : 1;
- }
-
-
- int
- time_gt(struct time_s *t1, struct time_s *t2)
- {
- sint d;
-
- d = t1->sec - t2->sec;
-
- if (d > 0)
- return 1;
- if (d < 0)
- return 0;
- return (t1->usec > t2->usec) ? 1 : 0;
- }
-
-
- long
- next_timeout()
- {
- eindex to;
- eptr ep;
- uint sec, usec;
-
- if (!time_queue)
- return -1;
- to = time_queue->timeout;
- ep = eaddr(time_queue, to);
-
- get_utc(&sec, &usec);
-
- if (ep->V.tim.sec < sec ||
- (ep->V.tim.sec == sec && ep->V.tim.usec <= usec))
- return 0;
-
- return ep->V.tim.usec - usec + 1000000L*(ep->V.tim.sec - sec);
- }
-