src/core/ngx_times.c - nginx-1.7.10

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

  15. #define NGX_TIME_SLOTS   64

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

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

  25. #if !(NGX_WIN32)

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

  31. static ngx_int_t         cached_gmtoff;
  32. #endif

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


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

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

  55.     ngx_cached_time = &cached_time[0];

  56.     ngx_time_update();
  57. }


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

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

  70.     ngx_gettimeofday(&tv);

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

  73.     ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;

  74.     tp = &cached_time[slot];

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

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

  85.     tp = &cached_time[slot];

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

  88.     ngx_gmtime(sec, &gmt);


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

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

  94. #if (NGX_HAVE_GETTIMEZONE)

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

  97. #elif (NGX_HAVE_GMTOFF)

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

  101. #else

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

  105. #endif


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

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


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

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

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

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

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

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

  129.     ngx_memory_barrier();

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

  136.     ngx_unlock(&ngx_time_lock);
  137. }


  138. #if !(NGX_WIN32)

  139. void
  140. ngx_time_sigsafe_update(void)
  141. {
  142.     u_char          *p, *p2;
  143.     ngx_tm_t         tm;
  144.     time_t           sec;
  145.     ngx_time_t      *tp;
  146.     struct timeval   tv;

  147.     if (!ngx_trylock(&ngx_time_lock)) {
  148.         return;
  149.     }

  150.     ngx_gettimeofday(&tv);

  151.     sec = tv.tv_sec;

  152.     tp = &cached_time[slot];

  153.     if (tp->sec == sec) {
  154.         ngx_unlock(&ngx_time_lock);
  155.         return;
  156.     }

  157.     if (slot == NGX_TIME_SLOTS - 1) {
  158.         slot = 0;
  159.     } else {
  160.         slot++;
  161.     }

  162.     tp = &cached_time[slot];

  163.     tp->sec = 0;

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

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

  166.     (void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d",
  167.                        tm.ngx_tm_year, tm.ngx_tm_mon,
  168.                        tm.ngx_tm_mday, tm.ngx_tm_hour,
  169.                        tm.ngx_tm_min, tm.ngx_tm_sec);

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

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

  174.     ngx_memory_barrier();

  175.     ngx_cached_err_log_time.data = p;
  176.     ngx_cached_syslog_time.data = p2;

  177.     ngx_unlock(&ngx_time_lock);
  178. }

  179. #endif


  180. u_char *
  181. ngx_http_time(u_char *buf, time_t t)
  182. {
  183.     ngx_tm_t  tm;

  184.     ngx_gmtime(t, &tm);

  185.     return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
  186.                        week[tm.ngx_tm_wday],
  187.                        tm.ngx_tm_mday,
  188.                        months[tm.ngx_tm_mon - 1],
  189.                        tm.ngx_tm_year,
  190.                        tm.ngx_tm_hour,
  191.                        tm.ngx_tm_min,
  192.                        tm.ngx_tm_sec);
  193. }


  194. u_char *
  195. ngx_http_cookie_time(u_char *buf, time_t t)
  196. {
  197.     ngx_tm_t  tm;

  198.     ngx_gmtime(t, &tm);

  199.     /*
  200.      * Netscape 3.x does not understand 4-digit years at all and
  201.      * 2-digit years more than "37"
  202.      */

  203.     return ngx_sprintf(buf,
  204.                        (tm.ngx_tm_year > 2037) ?
  205.                                          "%s, %02d-%s-%d %02d:%02d:%02d GMT":
  206.                                          "%s, %02d-%s-%02d %02d:%02d:%02d GMT",
  207.                        week[tm.ngx_tm_wday],
  208.                        tm.ngx_tm_mday,
  209.                        months[tm.ngx_tm_mon - 1],
  210.                        (tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
  211.                                                  tm.ngx_tm_year % 100,
  212.                        tm.ngx_tm_hour,
  213.                        tm.ngx_tm_min,
  214.                        tm.ngx_tm_sec);
  215. }


  216. void
  217. ngx_gmtime(time_t t, ngx_tm_t *tp)
  218. {
  219.     ngx_int_t   yday;
  220.     ngx_uint_t  n, sec, min, hour, mday, mon, year, wday, days, leap;

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

  222.     n = (ngx_uint_t) t;

  223.     days = n / 86400;

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

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

  226.     n %= 86400;
  227.     hour = n / 3600;
  228.     n %= 3600;
  229.     min = n / 60;
  230.     sec = n % 60;

  231.     /*
  232.      * the algorithm based on Gauss' formula,
  233.      * see src/http/ngx_http_parse_time.c
  234.      */

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

  237.     /*
  238.      * The "days" should be adjusted to 1 only, however, some March 1st's go
  239.      * to previous year, so we adjust them to 2.  This causes also shift of the
  240.      * last February days to next year, but we catch the case when "yday"
  241.      * becomes negative.
  242.      */

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

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

  245.     if (yday < 0) {
  246.         leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
  247.         yday = 365 + leap + yday;
  248.         year--;
  249.     }

  250.     /*
  251.      * The empirical formula that maps "yday" to month.
  252.      * There are at least 10 variants, some of them are:
  253.      *     mon = (yday + 31) * 15 / 459
  254.      *     mon = (yday + 31) * 17 / 520
  255.      *     mon = (yday + 31) * 20 / 612
  256.      */

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

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

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

  260.     if (yday >= 306) {

  261.         year++;
  262.         mon -= 10;

  263.         /*
  264.          * there is no "yday" in Win32 SYSTEMTIME
  265.          *
  266.          * yday -= 306;
  267.          */

  268.     } else {

  269.         mon += 2;

  270.         /*
  271.          * there is no "yday" in Win32 SYSTEMTIME
  272.          *
  273.          * yday += 31 + 28 + leap;
  274.          */
  275.     }

  276.     tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
  277.     tp->ngx_tm_min = (ngx_tm_min_t) min;
  278.     tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
  279.     tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
  280.     tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
  281.     tp->ngx_tm_year = (ngx_tm_year_t) year;
  282.     tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
  283. }


  284. time_t
  285. ngx_next_time(time_t when)
  286. {
  287.     time_t     now, next;
  288.     struct tm  tm;

  289.     now = ngx_time();

  290.     ngx_libc_localtime(now, &tm);

  291.     tm.tm_hour = (int) (when / 3600);
  292.     when %= 3600;
  293.     tm.tm_min = (int) (when / 60);
  294.     tm.tm_sec = (int) (when % 60);

  295.     next = mktime(&tm);

  296.     if (next == -1) {
  297.         return -1;
  298.     }

  299.     if (next - now > 0) {
  300.         return next;
  301.     }

  302.     tm.tm_mday++;

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

  304.     next = mktime(&tm);

  305.     if (next != -1) {
  306.         return next;
  307.     }

  308.     return -1;
  309. }