fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
taia.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/time.h>
3#include "taia.h"
4
12/* XXX: breaks tai encapsulation */
13
14void taia_add(struct taia *t,struct taia *u,struct taia *v)
15{
16 t->sec.x = u->sec.x + v->sec.x;
17 t->nano = u->nano + v->nano;
18 t->atto = u->atto + v->atto;
19 if (t->atto > 999999999UL) {
20 t->atto -= 1000000000UL;
21 ++t->nano;
22 }
23 if (t->nano > 999999999UL) {
24 t->nano -= 1000000000UL;
25 ++t->sec.x;
26 }
27}
28
29double taia_approx(struct taia *t)
30{
31 return tai_approx(&t->sec) + taia_frac(t);
32}
33
34double taia_frac(struct taia *t)
35{
36 return (t->atto * 0.000000001 + t->nano) * 0.000000001;
37}
38
39int taia_less(struct taia *t,struct taia *u)
40{
41 if (t->sec.x < u->sec.x) return 1;
42 if (t->sec.x > u->sec.x) return 0;
43 if (t->nano < u->nano) return 1;
44 if (t->nano > u->nano) return 0;
45 return t->atto < u->atto;
46}
47
48int taia_now(struct taia *t)
49{
50 struct timeval now;
51 if (gettimeofday(&now,(struct timezone *) 0) == 0) {
52 tai_unix(&t->sec,now.tv_sec);
53 t->nano = 1000 * now.tv_usec + 500;
54 t->atto = 0;
55 return 0;
56 }
57 t->nano = 0;
58 t->atto = 0;
59 return -1;
60}
61
62void taia_pack(char *s,struct taia *t)
63{
64 unsigned long x;
65
66 tai_pack(s,&t->sec);
67 s += 8;
68
69 x = t->atto;
70 s[7] = x & 255; x >>= 8;
71 s[6] = x & 255; x >>= 8;
72 s[5] = x & 255; x >>= 8;
73 s[4] = x;
74 x = t->nano;
75 s[3] = x & 255; x >>= 8;
76 s[2] = x & 255; x >>= 8;
77 s[1] = x & 255; x >>= 8;
78 s[0] = x;
79}
80
81void taia_sub(struct taia *t,struct taia *u,struct taia *v)
82{
83 unsigned long unano = u->nano;
84 unsigned long uatto = u->atto;
85
86 t->sec.x = u->sec.x - v->sec.x;
87 t->nano = unano - v->nano;
88 t->atto = uatto - v->atto;
89 if (t->atto > uatto) {
90 t->atto += 1000000000UL;
91 --t->nano;
92 }
93 if (t->nano > unano) {
94 t->nano += 1000000000UL;
95 --t->sec.x;
96 }
97}
98
99void taia_uint(struct taia *t,unsigned int s)
100{
101 t->sec.x = s;
102 t->nano = 0;
103 t->atto = 0;
104}
int taia_now(struct taia *t)
Definition: taia.c:48
void taia_sub(struct taia *t, struct taia *u, struct taia *v)
Definition: taia.c:81
int taia_less(struct taia *t, struct taia *u)
Definition: taia.c:39
double taia_approx(struct taia *t)
Definition: taia.c:29
double taia_frac(struct taia *t)
Definition: taia.c:34
void taia_uint(struct taia *t, unsigned int s)
Definition: taia.c:99
void taia_add(struct taia *t, struct taia *u, struct taia *v)
Definition: taia.c:14
void taia_pack(char *s, struct taia *t)
Definition: taia.c:62
#define tai_unix(t, u)
Definition: tai.h:35
void tai_pack(char *, const struct tai *)
Definition: tai.c:21
#define tai_approx(t)
Definition: tai.h:49
uint64 x
Definition: tai.h:31
Definition: taia.h:13
struct tai sec
Definition: taia.h:14
unsigned long atto
Definition: taia.h:16
unsigned long nano
Definition: taia.h:15