• 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 2
22 
23 static int testtimes[NUMTESTS][2] = {
24 	{INT32_MAX, 999999999},	// large number
25 	{946713600, 999999999},	// Y2K - Jan 1, 2000
26 	{951811200, 999999999},	// Y2K - Feb 29, 2000
27 	{1078041600, 999999999},	// Y2K - Feb 29, 2004
28 	{1049623200, 999999999},	// daylight savings 2003
29 };
30 
main(void)31 int main(void)
32 {
33 	struct timespec tpset, tpget, tsreset;
34 	int secdelta, nsecdelta;
35 	int failure = 0;
36 	int i;
37 
38 	if (clock_gettime(CLOCK_REALTIME, &tsreset) != 0) {
39 		perror("clock_gettime() did not return success\n");
40 		return PTS_UNRESOLVED;
41 	}
42 
43 	for (i = 0; i < NUMTESTS; i++) {
44 		tpset.tv_sec = testtimes[i][0];
45 		tpset.tv_nsec = testtimes[i][1];
46 		if (clock_settime(CLOCK_REALTIME, &tpset) == 0) {
47 			if (clock_gettime(CLOCK_REALTIME, &tpget) == -1) {
48 				printf("Error in clock_gettime()\n");
49 				return PTS_UNRESOLVED;
50 			}
51 			secdelta = tpget.tv_sec - tpset.tv_sec;
52 			nsecdelta = tpget.tv_nsec - tpset.tv_nsec;
53 			if (nsecdelta < 0) {
54 				nsecdelta = nsecdelta + 1000000000;
55 				secdelta = secdelta - 1;
56 			}
57 
58 			if ((secdelta > ACCEPTABLESECDELTA)
59 				|| (secdelta < 0)) {
60 				printf("FAIL: test(%d,%d), secdelta: %d,"
61 					" nsecdelta: %d\n",
62 					testtimes[i][0], testtimes[i][1],
63 					secdelta, nsecdelta);
64 				failure = 1;
65 			}
66 		} else {
67 			printf("clock_settime() failed\n");
68 			return PTS_UNRESOLVED;
69 		}
70 
71 		if (clock_settime(CLOCK_REALTIME, &tsreset) != 0) {
72 			perror("clock_settime() did not return success\n");
73 			return PTS_UNRESOLVED;
74 		}
75 	}
76 
77 	if (clock_settime(CLOCK_REALTIME, &tsreset) != 0) {
78 		perror("clock_settime() did not return success\n");
79 		return PTS_UNRESOLVED;
80 	}
81 
82 	if (failure) {
83 		printf("At least one test FAILED\n");
84 		return PTS_FAIL;
85 	}
86 
87 	printf("All tests PASSED\n");
88 	return PTS_PASS;
89 }
90