summaryrefslogtreecommitdiff
path: root/src/tai.c
blob: 0079cf1636858d54772ea6b0ab8f0e976dc06a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "tai.h"

#include <time.h>

/**
  @file tai.c
  @author djb
  @ref qmail
  @brief 'temps atomic' time handling
*/

void tai_add(struct tai *t, const struct tai *u, const struct tai *v)
{
  t->x = u->x + v->x;
}

void tai_now(struct tai *t)
{
  tai_unix(t, time((time_t *)0));
}

void tai_pack(char *s, const struct tai *t)
{
  uint64 x;

  x = t->x;
  // clang-format off
  s[7] = (char)x; x >>= 8;
  s[6] = (char)x; x >>= 8;
  s[5] = (char)x; x >>= 8;
  s[4] = (char)x; x >>= 8;
  s[3] = (char)x; x >>= 8;
  s[2] = (char)x; x >>= 8;
  s[1] = (char)x; x >>= 8;
  s[0] = (char)x;
  // clang-format on
}

void tai_sub(struct tai *t, const struct tai *u, const struct tai *v)
{
  t->x = u->x - v->x;
}

void tai_uint(struct tai *t, unsigned int u)
{
  t->x = u;
}

void tai_unpack(const char *s, struct tai *t)
{
  uint64 x;

  x = (unsigned char)s[0];
  // clang-format off
  x <<= 8; x += (unsigned char)s[1];
  x <<= 8; x += (unsigned char)s[2];
  x <<= 8; x += (unsigned char)s[3];
  x <<= 8; x += (unsigned char)s[4];
  x <<= 8; x += (unsigned char)s[5];
  x <<= 8; x += (unsigned char)s[6];
  x <<= 8; x += (unsigned char)s[7];
  // clang-format on
  t->x = x;
}