src/core/ngx_times.c - nginx source code

Global variables defined

Functions defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>


  7. static ngx_msec_t ngx_monotonic_time(time_t sec, ngx_uint_t msec);


  8. /*
  9. * The time may be updated by signal handler or by several threads.
  10. * The time update operations are rare and require to hold the ngx_time_lock.
  11. * The time read operations are frequent, so they are lock-free and get time
  12. * values and strings from the current slot.  Thus thread may get the corrupted
  13. * values only if it is preempted while copying and then it is not scheduled
  14. * to run more than NGX_TIME_SLOTS seconds.
  15. */

  16. #define NGX_TIME_SLOTS   64

  17. static ngx_uint_t        slot;
  18. static ngx_atomic_t      ngx_time_lock;

  19. volatile ngx_msec_t      ngx_current_msec;
  20. volatile ngx_time_t     *ngx_cached_time;
  21. volatile ngx_str_t       ngx_cached_err_log_time;
  22. volatile ngx_str_t       ngx_cached_http_time;
  23. volatile ngx_str_t       ngx_cached_http_log_time;
  24. volatile ngx_str_t       ngx_cached_http_log_iso8601;
  25. volatile ngx_str_t       ngx_cached_syslog_time;

  26. #if !(NGX_WIN32)

  27. /*
  28. * localtime() and localtime_r() are not Async-Signal-Safe functions, therefore,
  29. * they must not be called by a signal handler, so we use the cached
  30. * GMT offset value. Fortunately the value is changed only two times a year.
  31. */

  32. static ngx_int_t         cached_gmtoff;
  33. #endif

  34. static ngx_time_t        cached_time[NGX_TIME_SLOTS];
  35. static u_char            cached_err_log_time[NGX_TIME_SLOTS]
  36.                                     [sizeof("1970/09/28 12:00:00")];
  37. static u_char            cached_http_time[NGX_TIME_SLOTS]
  38.                                     [sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
  39. static u_char            cached_http_log_time[NGX_TIME_SLOTS]
  40.                                     [sizeof("28/Sep/1970:12:00:00 +0600")];
  41. static u_char            cached_http_log_iso8601[NGX_TIME_SLOTS]
  42.                                     [sizeof("1970-09-28T12:00:00+06:00")];
  43. static u_char            cached_syslog_time[NGX_TIME_SLOTS]
  44.                                     [sizeof("Sep 28 12:00:00")];


  45. static char  *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
  46. static char  *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  47.                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

  48. void
  49. ngx_time_init(void)
  50. {
  51.     ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
  52.     ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
  53.     ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
  54.     ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;
  55.     ngx_cached_syslog_time.len = sizeof("Sep 28 12:00:00") - 1;

  56.     ngx_cached_time = &cached_time[0];

  57.     ngx_time_update();
  58. }


  59. void
  60. ngx_time_update(void)
  61. {
  62.     u_char          *p0, *p1, *p2, *p3, *p4;
  63.     ngx_tm_t         tm, gmt;
  64.     time_t           sec;
  65.     ngx_uint_t       msec;
  66.     ngx_time_t      *tp;
  67.     struct timeval   tv;

  68.     if (!ngx_trylock(&ngx_time_lock)) {
  69.         return;
  70.     }

  71.     ngx_gettimeofday(&tv);

  72.     sec = tv.tv_sec;
  73.     msec = tv.tv_usec / 1000;

  74.     ngx_current_msec = ngx_monotonic_time(sec, msec);

  75.     tp = &cached_time[slot];

  76.     if (tp->sec == sec) {
  77.         tp->msec = msec;
  78.         ngx_unlock(&ngx_time_lock);
  79.         return;
  80.     }

  81.     if (slot == NGX_TIME_SLOTS - 1) {
  82.         slot = 0;
  83.     } else {
  84.         slot++;
  85.     }

  86.     tp = &cached_time[slot];

  87.     tp->sec = sec;
  88.     tp->msec = msec;

  89.     ngx_gmtime(sec, &gmt);


  90.     p0 = &cached_http_time[slot][0];

  91.     (void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT",
  92.                        week[gmt.ngx_tm_wday], gmt.ngx_tm_mday,
  93.                        months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year,
  94.                        gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec);

  95. #if (NGX_HAVE_GETTIMEZONE)

  96.     tp->gmtoff = ngx_gettimezone();
  97.     ngx_gmtime(sec + tp->gmtoff * 60, &tm);

  98. #elif (NGX_HAVE_GMTOFF)

  99.     ngx_localtime(sec, &tm);
  100.     cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
  101.     tp->gmtoff = cached_gmtoff;

  102. #else

  103.     ngx_localtime(sec, &tm);
  104.     cached_gmtoff = ngx_timezone(tm.ngx_tm_isdst);
  105.     tp->gmtoff = cached_gmtoff;

  106. #endif


  107.     p1 = &cached_err_log_time[slot][0];

  108.     (void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
  109.                        tm.ngx_tm_year, tm.ngx_tm_mon,
  110.                        tm.ngx_tm_mday, tm.ngx_tm_hour,
  111.                        tm.ngx_tm_min, tm.ngx_tm_sec);


  112.     p2 = &cached_http_log_time[slot][0];

  113.     (void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02i%02i",
  114.                        tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
  115.                        tm.ngx_tm_year, tm.ngx_tm_hour,
  116.                        tm.ngx_tm_min, tm.ngx_tm_sec,
  117.                        tp->gmtoff < 0 ? '-' : '+',
  118.                        ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));

  119.     p3 = &cached_http_log_iso8601[slot][0];

  120.     (void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%02i:%02i",
  121.                        tm.ngx_tm_year, tm.ngx_tm_mon,
  122.                        tm.ngx_tm_mday, tm.ngx_tm_hour,
  123.                        tm.ngx_tm_min, tm.ngx_tm_sec,
  124.                        tp->gmtoff < 0 ? '-' : '+',
  125.                        ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));

  126.     p4 = &cached_syslog_time[slot][0];

  127.     (void) ngx_sprintf(p4, "%s %2d %02d:%02d:%02d",
  128.                        months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
  129.                        tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);

  130.     ngx_memory_barrier();

  131.     ngx_cached_time = tp;
  132.     ngx_cached_http_time.data = p0;
  133.     ngx_cached_err_log_time.data = p1;
  134.     ngx_cached_http_log_time.data = p2;
  135.     ngx_cached_http_log_iso8601.data = p3;
  136.     ngx_cached_syslog_time.data = p4;

  137.     ngx_unlock(&ngx_time_lock);
  138. }


  139. static ngx_msec_t
  140. ngx_monotonic_time(time_t sec, ngx_uint_t msec)
  141. {
  142. #if (NGX_HAVE_CLOCK_MONOTONIC)
  143.     struct timespec  ts;

  144. #if defined(CLOCK_MONOTONIC_FAST)
  145.     clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
  146. #else
  147.     clock_gettime(CLOCK_MONOTONIC, &ts);
  148. #endif

  149.     sec = ts.tv_sec;
  150.     msec = ts.tv_nsec / 1000000;

  151. #endif

  152.     return (ngx_msec_t) sec * 1000 + msec;
  153. }


  154. #if !(NGX_WIN32)

  155. void
  156. ngx_time_sigsafe_update(void)
  157. {
  158.     u_char          *p, *p2;
  159.     ngx_tm_t         tm;
  160.     time_t           sec;
  161.     ngx_time_t      *tp;
  162.     struct timeval   tv;

  163.     if (!ngx_trylock(&ngx_time_lock)) {
  164.         return;
  165.     }

  166.     ngx_gettimeofday(&tv);

  167.     sec = tv.tv_sec;

  168.     tp = &cached_time[slot];

  169.     if (tp->sec == sec) {
  170.         ngx_unlock(&ngx_time_lock);
  171.         return;
  172.     }

  173.     if (slot == NGX_TIME_SLOTS - 1) {
  174.         slot = 0;
  175.     } else {
  176.         slot++;
  177.     }

  178.     tp = &cached_time[slot];

  179.     tp->sec = 0;

  180.     ngx_gmtime(sec + cached_gmtoff * 60, &tm);

  181.     p = &cached_err_log_time[slot][0];

  182.     (void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d",
  183.                        tm.ngx_tm_year, tm.ngx_tm_mon,
  184.                        tm.ngx_tm_mday, tm.ngx_tm_hour,
  185.                        tm.ngx_tm_min, tm.ngx_tm_sec);

  186.     p2 = &cached_syslog_time[slot][0];

  187.     (void) ngx_sprintf(p2, "%s %2d %02d:%02d:%02d",
  188.                        months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
  189.                        tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);

  190.     ngx_memory_barrier();

  191.     ngx_cached_err_log_time.data = p;
  192.     ngx_cached_syslog_time.data = p2;

  193.     ngx_unlock(&ngx_time_lock);
  194. }

  195. #endif


  196. u_char *
  197. ngx_http_time(u_char *buf, time_t t)
  198. {
  199.     ngx_tm_t  tm;

  200.     ngx_gmtime(t, &tm);

  201.     return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
  202.                        week[tm.ngx_tm_wday],
  203.                        tm.ngx_tm_mday,
  204.                        months[tm.ngx_tm_mon - 1],
  205.                        tm.ngx_tm_year,
  206.                        tm.ngx_tm_hour,
  207.                        tm.ngx_tm_min,
  208.                        tm.ngx_tm_sec);
  209. }


  210. u_char *
  211. ngx_http_cookie_time(u_char *buf, time_t t)
  212. {
  213.     ngx_tm_t  tm;

  214.     ngx_gmtime(t, &tm);

  215.     /*
  216.      * Netscape 3.x does not understand 4-digit years at all and
  217.      * 2-digit years more than "37"
  218.      */

  219.     return ngx_sprintf(buf,
  220.                        (tm.ngx_tm_year > 2037) ?
  221.                                          "%s, %02d-%s-%d %02d:%02d:%02d GMT":
  222.                                          "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
  223.                        week[tm.ngx_tm_wday],
  224.                        tm.ngx_tm_mday,
  225.                        months[tm.ngx_tm_mon - 1],
  226.                        (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
  227.                                                  tm.ngx_tm_year % 100,
  228.                        tm.ngx_tm_hour,
  229.                        tm.ngx_tm_min,
  230.                        tm.ngx_tm_sec);
  231. }


  232. void
  233. ngx_gmtime(time_t t, ngx_tm_t *tp)
  234. {
  235.     ngx_int_t   yday;
  236.     ngx_uint_t  sec, min, hour, mday, mon, year, wday, days, leap;

  237.     /* the calculation is valid for positive time_t only */

  238.     if (t < 0) {
  239.         t = 0;
  240.     }

  241.     days = t / 86400;
  242.     sec = t % 86400;

  243.     /*
  244.      * no more than 4 year digits supported,
  245.      * truncate to December 31, 9999, 23:59:59
  246.      */

  247.     if (days > 2932896) {
  248.         days = 2932896;
  249.         sec = 86399;
  250.     }

  251.     /* January 1, 1970 was Thursday */

  252.     wday = (4 + days) % 7;

  253.     hour = sec / 3600;
  254.     sec %= 3600;
  255.     min = sec / 60;
  256.     sec %= 60;

  257.     /*
  258.      * the algorithm based on Gauss' formula,
  259.      * see src/core/ngx_parse_time.c
  260.      */

  261.     /* days since March 1, 1 BC */
  262.     days = days - (31 + 28) + 719527;

  263.     /*
  264.      * The "days" should be adjusted to 1 only, however, some March 1st's go
  265.      * to previous year, so we adjust them to 2.  This causes also shift of the
  266.      * last February days to next year, but we catch the case when "yday"
  267.      * becomes negative.
  268.      */

  269.     year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);

  270.     yday = days - (365 * year + year / 4 - year / 100 + year / 400);

  271.     if (yday < 0) {
  272.         leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
  273.         yday = 365 + leap + yday;
  274.         year--;
  275.     }

  276.     /*
  277.      * The empirical formula that maps "yday" to month.
  278.      * There are at least 10 variants, some of them are:
  279.      *     mon = (yday + 31) * 15 / 459
  280.      *     mon = (yday + 31) * 17 / 520
  281.      *     mon = (yday + 31) * 20 / 612
  282.      */

  283.     mon = (yday + 31) * 10 / 306;

  284.     /* the Gauss' formula that evaluates days before the month */

  285.     mday = yday - (367 * mon / 12 - 30) + 1;

  286.     if (yday >= 306) {

  287.         year++;
  288.         mon -= 10;

  289.         /*
  290.          * there is no "yday" in Win32 SYSTEMTIME
  291.          *
  292.          * yday -= 306;
  293.          */

  294.     } else {

  295.         mon += 2;

  296.         /*
  297.          * there is no "yday" in Win32 SYSTEMTIME
  298.          *
  299.          * yday += 31 + 28 + leap;
  300.          */
  301.     }

  302.     tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
  303.     tp->ngx_tm_min = (ngx_tm_min_t) min;
  304.     tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
  305.     tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
  306.     tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
  307.     tp->ngx_tm_year = (ngx_tm_year_t) year;
  308.     tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
  309. }


  310. time_t
  311. ngx_next_time(time_t when)
  312. {
  313.     time_t     now, next;
  314.     struct tm  tm;

  315.     now = ngx_time();

  316.     ngx_libc_localtime(now, &tm);

  317.     tm.tm_hour = (int) (when / 3600);
  318.     when %= 3600;
  319.     tm.tm_min = (int) (when / 60);
  320.     tm.tm_sec = (int) (when % 60);

  321.     next = mktime(&tm);

  322.     if (next == -1) {
  323.         return -1;
  324.     }

  325.     if (next - now > 0) {
  326.         return next;
  327.     }

  328.     tm.tm_mday++;

  329.     /* mktime() should normalize a date (Jan 32, etc) */

  330.     next = mktime(&tm);

  331.     if (next != -1) {
  332.         return next;
  333.     }

  334.     return -1;
  335. }