• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
3  * Created by:  julie.n.fleischer REMOVE-THIS AT intel DOT com
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  *
8  * Test that the clock time can be set to a large number, Y2K
9  * critical dates, and times around daylight savings.
10  *
11  * Test for CLOCK_REALTIME.  (N/A for CLOCK_MONOTONIC as that clock
12  * cannot be set.)
13  */
14 #include <stdio.h>
15 #include <time.h>
16 #include <stdint.h>
17 #include "posixtest.h"
18 
19 #define NUMTESTS 5
20 
21 #define ACCEPTABLESECDELTA 0
22 #define ACCEPTABLENSECDELTA 5000000
23 
24 static int testtimes[NUMTESTS][2] = {
25 	{INT32_MAX, 999999999},	// large number
26 	{946713600, 999999999},	// Y2K - Jan 1, 2000
27 	{951811200, 999999999},	// Y2K - Feb 29, 2000
28 	{1078041600, 999999999},	// Y2K - Feb 29, 2004
29 	{1049623200, 999999999},	// daylight savings 2003
30 };
31 
main(int argc,char * argv[])32 int main(int argc, char *argv[])
33 {
34 	struct timespec tpset, tpget, tsreset;
35 	int secdelta, nsecdelta;
36 	int failure = 0;
37 	int i;
38 
39 	if (clock_gettime(CLOCK_REALTIME, &tsreset) != 0) {
40 		perror("clock_gettime() did not return success\n");
41 		return PTS_UNRESOLVED;
42 	}
43 
44 	for (i = 0; i < NUMTESTS; i++) {
45 		tpset.tv_sec = testtimes[i][0];
46 		tpset.tv_nsec = testtimes[i][1];
47 #ifdef DEBUG
48 		printf("Test: %ds %dns\n", testtimes[i][0], testtimes[i][1]);
49 #endif
50 		if (clock_settime(CLOCK_REALTIME, &tpset) == 0) {
51 			if (clock_gettime(CLOCK_REALTIME, &tpget) == -1) {
52 				printf("Error in clock_gettime()\n");
53 				return PTS_UNRESOLVED;
54 			}
55 			secdelta = tpget.tv_sec - tpset.tv_sec;
56 			nsecdelta = tpget.tv_nsec - tpset.tv_nsec;
57 			if (nsecdelta < 0) {
58 				nsecdelta = nsecdelta + 1000000000;
59 				secdelta = secdelta - 1;
60 			}
61 #ifdef DEBUG
62 			printf("Delta:  %ds %dns\n", secdelta, nsecdelta);
63 #endif
64 			if ((secdelta > ACCEPTABLESECDELTA) || (secdelta < 0)) {
65 				printf("clock does not appear to be set\n");
66 				failure = 1;
67 			}
68 			if ((nsecdelta > ACCEPTABLENSECDELTA) ||
69 			    (nsecdelta < 0)) {
70 				printf("clock does not appear to be set\n");
71 				failure = 1;
72 			}
73 		} else {
74 			printf("clock_settime() failed\n");
75 			return PTS_UNRESOLVED;
76 		}
77 
78 		if (clock_settime(CLOCK_REALTIME, &tsreset) != 0) {
79 			perror("clock_settime() did not return success\n");
80 			return PTS_UNRESOLVED;
81 		}
82 	}
83 
84 	if (clock_settime(CLOCK_REALTIME, &tsreset) != 0) {
85 		perror("clock_settime() did not return success\n");
86 		return PTS_UNRESOLVED;
87 	}
88 
89 	if (failure) {
90 		printf("At least one test FAILED\n");
91 		return PTS_FAIL;
92 	}
93 
94 	printf("All tests PASSED\n");
95 	return PTS_PASS;
96 }
97