aboutsummaryrefslogtreecommitdiffstats
path: root/utils.c
blob: fd8a6ae2098a505f8ccd2838e0a7b27a59ccc2f7 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* ==========================================================================
    Licensed under BSD 2clause license See LICENSE file for more information
    Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
   ==========================================================================
         -------------------------------------------------------
        / This file contains some common utils code that can be \
        \ shared between lib: programs and tests                /
         -------------------------------------------------------
          \                           .       .
           \                         / `.   .' "
            \                .---.  <    > <    >  .---.
             \               |    \  \ - ~ ~ - /  /    |
                 _____          ..-~             ~-..-~
                |     |   \~~~\.'                    `./~~~/
               ---------   \__/                        \__/
              .'  O    \     /               /       \  "
             (_____,    `._.'               |         }  \/~~~/
              `----.          /       }     |        /    \__/
                    `-.      |       /      |       /      `. ,~~|
                        ~-.__|      /_ - ~ ^|      /- _      `..-'
                             |     /        |     /     ~-.     `-. _  _  _
                             |_____|        |_____|         ~ - . _ _ _ _ _>
   ==========================================================================
          _               __            __         ____ _  __
         (_)____   _____ / /__  __ ____/ /___     / __/(_)/ /___   _____
        / // __ \ / ___// // / / // __  // _ \   / /_ / // // _ \ / ___/
       / // / / // /__ / // /_/ // /_/ //  __/  / __// // //  __/(__  )
      /_//_/ /_/ \___//_/ \__,_/ \__,_/ \___/  /_/  /_//_/ \___//____/

   ========================================================================== */

#include <sys/time.h>
#include <time.h>

/* ==========================================================================
               ____                     __   _
              / __/__  __ ____   _____ / /_ (_)____   ____   _____
             / /_ / / / // __ \ / ___// __// // __ \ / __ \ / ___/
            / __// /_/ // / / // /__ / /_ / // /_/ // / / /(__  )
           /_/   \__,_//_/ /_/ \___/ \__//_/ \____//_/ /_//____/

   ========================================================================== */


/* ==========================================================================
    Converts millisecond into absolute timespec time. If ms is 0, return tp
    with values set to 0, which is 1970.01.01, when this is passed to
    mq_timedreceive(), function will timeout immediately
   ========================================================================== */


void psmq_ms_to_tp
(
	size_t            ms,    /* time in milliseconds */
	struct timespec  *tp     /* ms will be converted to tp here */
)
{
	size_t            nsec;  /* number of nanoseconds to add to timer */
	/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


	tp->tv_sec = 0;
	tp->tv_nsec = 0;

	if (!ms)  return;

	clock_gettime(CLOCK_REALTIME, tp);

	nsec = ms % 1000;
	nsec *= 1000000l;

	tp->tv_sec += ms / 1000;
	tp->tv_nsec += nsec;

	if (tp->tv_nsec >= 1000000000l)
	{
		/* overflow on nsec part */
		tp->tv_nsec -= 1000000000l;
		tp->tv_sec += 1;
	}
}