src/os/unix/ngx_time.c - nginx-1.7.10

Functions 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. * FreeBSD does not test /etc/localtime change, however, we can workaround it
  9. * by calling tzset() with TZ and then without TZ to update timezone.
  10. * The trick should work since FreeBSD 2.1.0.
  11. *
  12. * Linux does not test /etc/localtime change in localtime(),
  13. * but may stat("/etc/localtime") several times in every strftime(),
  14. * therefore we use it to update timezone.
  15. *
  16. * Solaris does not test /etc/TIMEZONE change too and no workaround available.
  17. */

  18. void
  19. ngx_timezone_update(void)
  20. {
  21. #if (NGX_FREEBSD)

  22.     if (getenv("TZ")) {
  23.         return;
  24.     }

  25.     putenv("TZ=UTC");

  26.     tzset();

  27.     unsetenv("TZ");

  28.     tzset();

  29. #elif (NGX_LINUX)
  30.     time_t      s;
  31.     struct tm  *t;
  32.     char        buf[4];

  33.     s = time(0);

  34.     t = localtime(&s);

  35.     strftime(buf, 4, "%H", t);

  36. #endif
  37. }


  38. void
  39. ngx_localtime(time_t s, ngx_tm_t *tm)
  40. {
  41. #if (NGX_HAVE_LOCALTIME_R)
  42.     (void) localtime_r(&s, tm);

  43. #else
  44.     ngx_tm_t  *t;

  45.     t = localtime(&s);
  46.     *tm = *t;

  47. #endif

  48.     tm->ngx_tm_mon++;
  49.     tm->ngx_tm_year += 1900;
  50. }


  51. void
  52. ngx_libc_localtime(time_t s, struct tm *tm)
  53. {
  54. #if (NGX_HAVE_LOCALTIME_R)
  55.     (void) localtime_r(&s, tm);

  56. #else
  57.     struct tm  *t;

  58.     t = localtime(&s);
  59.     *tm = *t;

  60. #endif
  61. }


  62. void
  63. ngx_libc_gmtime(time_t s, struct tm *tm)
  64. {
  65. #if (NGX_HAVE_LOCALTIME_R)
  66.     (void) gmtime_r(&s, tm);

  67. #else
  68.     struct tm  *t;

  69.     t = gmtime(&s);
  70.     *tm = *t;

  71. #endif
  72. }