summaryrefslogtreecommitdiff
path: root/src/libstddjb/sysclock_set.c
blob: 57e2abe44259d47ff208768b0e2c60046d3d87d0 (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
/* ISC license. */

/* MT-unsafe */

#include <skalibs/sysdeps.h>

#ifndef SKALIBS_HASCLOCKRT
# ifndef SKALIBS_HASSETTIMEOFDAY
#  error "neither clockrt nor settimeofday sysdeps are present. How do your set your system clock?"
# endif
#endif


#ifdef SKALIBS_HASCLOCKRT

#include <time.h>
#include <skalibs/tai.h>

int sysclock_set (tain_t const *a)
{
  struct timespec now ;
  tain_t aa ;
  tain_add(&aa, a, &tain_nano500) ;
  if (!timespec_from_tain(&now, &aa)) return 0 ;
  if (clock_settime(CLOCK_REALTIME, &now) < 0) return 0 ;
  return 1 ;
}

#else

#include <skalibs/nonposix.h>
#include <sys/time.h>
#include <skalibs/tai.h>

int sysclock_set (tain_t const *a)
{
  struct timeval now ;
  tain_t aa ;
  tain_add(&aa, a, &tain_nano500) ;
  if (!timeval_from_tain(&now, &aa)) return 0 ;
  if (settimeofday(&now, 0) < 0) return 0 ;
  return 1 ;
}

#endif