VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/snippets/time-1.c@ 53404

Last change on this file since 53404 was 52776, checked in by vboxsync, 10 years ago

fix OSE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1/* $Id: time-1.c 52776 2014-09-17 14:51:43Z vboxsync $ */
2/** @file
3 * Query the time and check that it always goes forward, POSIX only.
4 */
5
6/*
7 * Copyright (C) 2011-2014 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <stdio.h>
31#include <time.h>
32#include <sys/time.h>
33
34
35
36int main()
37{
38 unsigned cErrors = 0;
39#ifdef USE_CLOCK_MONOTONIC
40 struct timespec aTs[2];
41 struct timespec *pCur = &aTs[0];
42 struct timespec *pPrev = &aTs[1];
43 struct timespec *pTmp;
44#else
45 struct timeval aTv[2];
46 struct timeval *pCur = &aTv[0];
47 struct timeval *pPrev = &aTv[1];
48 struct timeval *pTmp;
49#endif
50
51#ifdef USE_CLOCK_MONOTONIC
52 clock_gettime(CLOCK_MONOTONIC, pPrev);
53#else
54 gettimeofday(pPrev, NULL);
55#endif
56 for (;;)
57 {
58#ifdef USE_CLOCK_MONOTONIC
59 clock_gettime(CLOCK_MONOTONIC, pCur);
60#else
61 gettimeofday(pCur, NULL);
62#endif
63
64 if ( pCur->tv_sec == pPrev->tv_sec
65#ifdef USE_CLOCK_MONOTONIC
66 && pCur->tv_nsec < pPrev->tv_nsec
67#else
68 && pCur->tv_usec < pPrev->tv_usec
69#endif
70 )
71 {
72#ifdef USE_CLOCK_MONOTONIC
73 printf("tv_nsec in the past: %ld.%09d < %ld.%09d - %u nsec\n",
74 (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
75 (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec,
76 (unsigned)pPrev->tv_nsec - (unsigned)pCur->tv_nsec);
77#else
78 printf("tv_usec in the past: %ld.%06d < %ld.%06d - %u usec\n",
79 (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
80 (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec,
81 (unsigned)pPrev->tv_usec - (unsigned)pCur->tv_usec);
82#endif
83 cErrors++;
84 if (cErrors > 1000)
85 break;
86 }
87 else if (pCur->tv_sec < pPrev->tv_sec)
88 {
89#ifdef USE_CLOCK_MONOTONIC
90 printf("tv_sec in the past: %ld.%09d < %ld.%09d\n",
91 (long)pCur->tv_sec, (unsigned)pCur->tv_nsec,
92 (long)pPrev->tv_sec, (unsigned)pPrev->tv_nsec);
93#else
94 printf("tv_sec in the past: %ld.%06d < %ld.%06d\n",
95 (long)pCur->tv_sec, (unsigned)pCur->tv_usec,
96 (long)pPrev->tv_sec, (unsigned)pPrev->tv_usec);
97#endif
98 cErrors++;
99 if (cErrors > 1000)
100 break;
101 }
102 else
103 {
104 /* swap */
105 pTmp = pPrev;
106 pPrev = pCur;
107 pCur = pTmp;
108 }
109 }
110
111 return 1;
112}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette