Home | History | Annotate | Download | only in clocks
      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 0, 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 6
     20 
     21 #define ACCEPTABLESECDELTA 0
     22 #define ACCEPTABLENSECDELTA 5000
     23 
     24 static int testtimes[NUMTESTS][2] = {
     25 	{0, 0},			// zero
     26 	{INT32_MAX, 999999999},	// large number
     27 	{946713600, 999999999},	// Y2K - Jan 1, 2000
     28 	{951811200, 999999999},	// Y2K - Feb 29, 2000
     29 	{1078041600, 999999999},	// Y2K - Feb 29, 2004
     30 	{1049623200, 999999999},	// daylight savings 2003
     31 };
     32 
     33 int main(int argc, char *argv[])
     34 {
     35 	struct timespec tpset, tpget, tsreset;
     36 	int secdelta, nsecdelta;
     37 	int failure = 0;
     38 	int i;
     39 
     40 	if (clock_gettime(CLOCK_REALTIME, &tsreset) != 0) {
     41 		perror("clock_gettime() did not return success\n");
     42 		return PTS_UNRESOLVED;
     43 	}
     44 
     45 	for (i = 0; i < NUMTESTS; i++) {
     46 		tpset.tv_sec = testtimes[i][0];
     47 		tpset.tv_nsec = testtimes[i][1];
     48 #ifdef DEBUG
     49 		printf("Test: %ds %dns\n", testtimes[i][0], testtimes[i][1]);
     50 #endif
     51 		if (clock_settime(CLOCK_REALTIME, &tpset) == 0) {
     52 			if (clock_gettime(CLOCK_REALTIME, &tpget) == -1) {
     53 				printf("Error in clock_gettime()\n");
     54 				return PTS_UNRESOLVED;
     55 			}
     56 			secdelta = tpget.tv_sec - tpset.tv_sec;
     57 			nsecdelta = tpget.tv_nsec - tpset.tv_nsec;
     58 			if (nsecdelta < 0) {
     59 				nsecdelta = nsecdelta + 1000000000;
     60 				secdelta = secdelta - 1;
     61 			}
     62 #ifdef DEBUG
     63 			printf("Delta:  %ds %dns\n", secdelta, nsecdelta);
     64 #endif
     65 			if ((secdelta > ACCEPTABLESECDELTA) || (secdelta < 0)) {
     66 				printf("clock does not appear to be set\n");
     67 				failure = 1;
     68 			}
     69 			if ((nsecdelta > ACCEPTABLENSECDELTA) ||
     70 			    (nsecdelta < 0)) {
     71 				printf("clock does not appear to be set\n");
     72 				failure = 1;
     73 			}
     74 		} else {
     75 			printf("clock_settime() failed\n");
     76 			return PTS_UNRESOLVED;
     77 		}
     78 
     79 		if (clock_settime(CLOCK_REALTIME, &tsreset) != 0) {
     80 			perror("clock_settime() did not return success\n");
     81 			return PTS_UNRESOLVED;
     82 		}
     83 	}
     84 
     85 	if (clock_settime(CLOCK_REALTIME, &tsreset) != 0) {
     86 		perror("clock_settime() did not return success\n");
     87 		return PTS_UNRESOLVED;
     88 	}
     89 
     90 	if (failure) {
     91 		printf("At least one test FAILED\n");
     92 		return PTS_FAIL;
     93 	}
     94 
     95 	printf("All tests PASSED\n");
     96 	return PTS_PASS;
     97 }
     98