1 | /* $Id: time-1.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Query the time and check that it always goes forward, POSIX only.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <time.h>
|
---|
43 | #include <sys/time.h>
|
---|
44 |
|
---|
45 |
|
---|
46 |
|
---|
47 | int main()
|
---|
48 | {
|
---|
49 | unsigned cErrors = 0;
|
---|
50 | #ifdef USE_CLOCK_MONOTONIC
|
---|
51 | struct timespec aTs[2];
|
---|
52 | struct timespec *pCur = &aTs[0];
|
---|
53 | struct timespec *pPrev = &aTs[1];
|
---|
54 | struct timespec *pTmp;
|
---|
55 | #else
|
---|
56 | struct timeval aTv[2];
|
---|
57 | struct timeval *pCur = &aTv[0];
|
---|
58 | struct timeval *pPrev = &aTv[1];
|
---|
59 | struct timeval *pTmp;
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | #ifdef USE_CLOCK_MONOTONIC
|
---|
63 | clock_gettime(CLOCK_MONOTONIC, pPrev);
|
---|
64 | #else
|
---|
65 | gettimeofday(pPrev, NULL);
|
---|
66 | #endif
|
---|
67 | for (;;)
|
---|
68 | {
|
---|
69 | #ifdef USE_CLOCK_MONOTONIC
|
---|
70 | clock_gettime(CLOCK_MONOTONIC, pCur);
|
---|
71 | #else
|
---|
72 | gettimeofday(pCur, NULL);
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | if ( pCur->tv_sec == pPrev->tv_sec
|
---|
76 | #ifdef USE_CLOCK_MONOTONIC
|
---|
77 | && pCur->tv_nsec < pPrev->tv_nsec
|
---|
78 | #else
|
---|
79 | && pCur->tv_usec < pPrev->tv_usec
|
---|
80 | #endif
|
---|
81 | )
|
---|
82 | {
|
---|
83 | #ifdef USE_CLOCK_MONOTONIC
|
---|
84 | printf("tv_nsec in the past: %ld.%09u < %ld.%09u - %u nsec\n",
|
---|
85 | (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
|
---|
86 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec,
|
---|
87 | (unsigned)pPrev->tv_nsec - (unsigned)pCur->tv_nsec);
|
---|
88 | #else
|
---|
89 | printf("tv_usec in the past: %ld.%06u < %ld.%06u - %u usec\n",
|
---|
90 | (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
|
---|
91 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec,
|
---|
92 | (unsigned)pPrev->tv_usec - (unsigned)pCur->tv_usec);
|
---|
93 | #endif
|
---|
94 | cErrors++;
|
---|
95 | if (cErrors > 1000)
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | else if (pCur->tv_sec < pPrev->tv_sec)
|
---|
99 | {
|
---|
100 | #ifdef USE_CLOCK_MONOTONIC
|
---|
101 | printf("tv_sec in the past: %ld.%09u < %ld.%09u\n",
|
---|
102 | (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
|
---|
103 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec);
|
---|
104 | #else
|
---|
105 | printf("tv_sec in the past: %ld.%06u < %ld.%06u\n",
|
---|
106 | (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
|
---|
107 | (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec);
|
---|
108 | #endif
|
---|
109 | cErrors++;
|
---|
110 | if (cErrors > 1000)
|
---|
111 | break;
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | /* swap */
|
---|
116 | pTmp = pPrev;
|
---|
117 | pPrev = pCur;
|
---|
118 | pCur = pTmp;
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | return 1;
|
---|
123 | }
|
---|