1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #include <nxt_main.h>
8 #include "nxt_tests.h"
9 
10 
11 #if (NXT_TIME_T_SIZE == 4)
12 
13 /* A 86400-fold number below 2^31. */
14 #define NXT_GMTIME_MAX  2147472000
15 
16 #else
17 /*
18  * March 19, 29398 is maximum valid data if nxt_uint_t
19  * is 4 bytes size whilst nxt_time_t is 8 bytes size.
20  */
21 #define NXT_GMTIME_MAX  865550793600
22 #endif
23 
24 
25 nxt_int_t
nxt_gmtime_test(nxt_thread_t * thr)26 nxt_gmtime_test(nxt_thread_t *thr)
27 {
28     struct tm   tm0, *tm1;
29     nxt_time_t  s;
30     nxt_nsec_t  start, end;
31 
32     nxt_thread_time_update(thr);
33     nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime test started");
34 
35     for (s = 0; s < NXT_GMTIME_MAX; s += 86400) {
36 
37         nxt_gmtime(s, &tm0);
38         tm1 = gmtime(&s);
39 
40         if (tm0.tm_mday != tm1->tm_mday
41             || tm0.tm_mon != tm1->tm_mon
42             || tm0.tm_year != tm1->tm_year
43             || tm0.tm_yday != tm1->tm_yday
44             || tm0.tm_wday != tm1->tm_wday)
45         {
46             nxt_log_alert(thr->log,
47                           "gmtime test failed: %T @ %02d.%02d.%d",
48                           s, tm1->tm_mday, tm1->tm_mon + 1,
49                           tm1->tm_year + 1900);
50             return NXT_ERROR;
51         }
52     }
53 
54 
55     nxt_thread_time_update(thr);
56     start = nxt_thread_monotonic_time(thr);
57 
58     for (s = 0; s < 10000000; s++) {
59         nxt_gmtime(s, &tm0);
60     }
61 
62     nxt_thread_time_update(thr);
63     end = nxt_thread_monotonic_time(thr);
64 
65     nxt_log_error(NXT_LOG_NOTICE, thr->log, "nxt_gmtime(): %0.1fns",
66                   (end - start) / 10000000.0);
67 
68 
69     nxt_thread_time_update(thr);
70     start = nxt_thread_monotonic_time(thr);
71 
72     for (s = 0; s < 10000000; s++) {
73         (void) gmtime(&s);
74     }
75 
76     nxt_thread_time_update(thr);
77     end = nxt_thread_monotonic_time(thr);
78 
79     nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime(): %0.1fns",
80                   (end - start) / 10000000.0);
81 
82     nxt_log_error(NXT_LOG_NOTICE, thr->log, "gmtime test passed");
83     return NXT_OK;
84 }
85