github.com/Konstantin8105/c4go@v0.0.0-20240505174241-768bb1c65a51/tests/time.c (about) 1 #include <stdio.h> 2 #include <sys/time.h> 3 #include <time.h> 4 5 #include "tests.h" 6 7 #define START_TEST(t) \ 8 diag(#t); \ 9 test_##t(); 10 11 void test_time() 12 { 13 // time_t now; 14 // time_t tloc; 15 // 16 // now = time(NULL); 17 // is_not_eq(now, 0); 18 // 19 // now = time(&tloc); 20 // is_not_eq(now, 0); 21 // is_eq(now, tloc); 22 } 23 24 void test_ctime() 25 { 26 // char* s; 27 // 28 // // 1999-12-31 11:59:58 29 // time_t now = 946670398; 30 // s = ctime(&now); 31 // is_not_null(s); 32 // 33 // // Hours/minutes will vary based on local time. Ignore them. 34 // s[11] = 'H'; 35 // s[12] = 'H'; 36 // s[14] = 'm'; 37 // s[15] = 'm'; 38 // is_streq(s, "Fri Dec 31 HH:mm:58 1999\n"); 39 } 40 41 void test_gmtime() 42 { 43 // struct tm* timeinfo; 44 // time_t rawtime = 80000; 45 // timeinfo = gmtime(&rawtime); 46 // is_eq(timeinfo->tm_sec, 20); 47 // is_eq(timeinfo->tm_min, 13); 48 // is_eq(timeinfo->tm_hour, 22); 49 // is_eq(timeinfo->tm_mday, 1); 50 // is_eq(timeinfo->tm_mon, 0); 51 // is_eq(timeinfo->tm_year, 70); 52 // is_eq(timeinfo->tm_wday, 4); 53 // is_eq(timeinfo->tm_yday, 0); 54 // is_eq(timeinfo->tm_isdst, 0); 55 } 56 57 void test_mktime() 58 { 59 // struct tm timeinfo; 60 // 61 // timeinfo.tm_year = 2000 - 1900; 62 // timeinfo.tm_mon = 5 - 1; 63 // timeinfo.tm_mday = 20; 64 // timeinfo.tm_sec = 0; 65 // timeinfo.tm_min = 0; 66 // timeinfo.tm_hour = 0; 67 // 68 // mktime(&timeinfo); 69 // 70 // // is_eq(timeinfo.tm_wday, 6); 71 // is_eq(timeinfo.tm_year, 100); 72 // is_eq(timeinfo.tm_mon, 4); 73 // is_eq(timeinfo.tm_mday, 20); 74 } 75 76 void test_asctime() 77 { 78 // time_t rawtime = 80000; 79 // struct tm* timeinfo; 80 // timeinfo = gmtime(&rawtime); 81 // is_streq(asctime(timeinfo), "Thu Jan 1 22:13:20 1970\n"); 82 } 83 84 void test_timeval() 85 { 86 // struct timeval tv; 87 // tv.tv_sec = 1; 88 // tv.tv_usec = 75000; 89 // is_eq(tv.tv_sec, 1); 90 // is_eq(tv.tv_usec, 75000); 91 } 92 93 void test_timezone() 94 { 95 // struct timezone tv; 96 // tv.tz_minuteswest = 1; 97 // tv.tz_dsttime = 75000; 98 // is_eq(tv.tz_minuteswest, 1); 99 // is_eq(tv.tz_dsttime, 75000); 100 } 101 102 void test_gettime() 103 { 104 // struct timeval tv; 105 // struct timezone tz; 106 // int r = gettimeofday(&tv, &tz); 107 // printf("timezone: %d %d\n", tz.tz_minuteswest, tz.tz_dsttime); 108 // (void)tv; 109 // (void)r; 110 } 111 112 void test_clock() 113 { 114 // clock_t c = clock(); 115 // unsigned long l = c; 116 // is_true(l > 0); 117 } 118 119 void test_difftime() 120 { 121 // time_t now; 122 // struct tm newyear; 123 // double seconds; 124 // 125 // time(&now); 126 // 127 // double d = now; 128 // d /= 1000; 129 // long l = d; 130 // l *= 1000; 131 // now = ((time_t)(l)); 132 // 133 // newyear = *localtime(&now); 134 // 135 // newyear.tm_hour = 0; 136 // newyear.tm_min = 0; 137 // newyear.tm_sec = 0; 138 // newyear.tm_mon = 0; 139 // newyear.tm_mday = 1; 140 // 141 // seconds = difftime(now, mktime(&newyear)); 142 // 143 // printf("%.f seconds since new year in the current timezone.\n", seconds); 144 } 145 146 void test_CLOCKS_PER_SEC() 147 { 148 printf("%d\n", CLOCKS_PER_SEC); 149 } 150 151 void test_strftime() 152 { 153 // time_t rawtime; 154 // struct tm* timeinfo; 155 // char buffer[80]; 156 // 157 // time(&rawtime); 158 // timeinfo = localtime(&rawtime); 159 // 160 // strftime(buffer, 80, "Now it's %D.", timeinfo); 161 // puts(buffer); 162 } 163 164 int main() 165 { 166 plan(0);// 23); 167 // TODO: commented for github action 168 169 // sorting in according to : 170 // http://www.cplusplus.com/reference/ctime/ 171 // START_TEST(asctime); 172 // START_TEST(ctime); 173 // START_TEST(gmtime); 174 // START_TEST(mktime); 175 // START_TEST(time); 176 // START_TEST(clock); 177 // START_TEST(difftime); 178 // START_TEST(CLOCKS_PER_SEC); 179 // START_TEST(strftime); 180 // 181 // // sys/time.h 182 // START_TEST(timeval); 183 // START_TEST(timezone); 184 // START_TEST(gettime); 185 186 done_testing(); 187 }