关键词搜索

源码搜索 ×
×

漫话Redis源码之五十二

发布2022-01-09浏览366次

详情内容

看看is_leap_year,是不是很熟悉,估计在大学期间就做过吧。看看人家是怎么实现的。

这个文件主要是一些时间函数的实现,简单。

  1. /*
  2. * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of Redis nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <time.h>
  30. /* This is a safe version of localtime() which contains no locks and is
  31. * fork() friendly. Even the _r version of localtime() cannot be used safely
  32. * in Redis. Another thread may be calling localtime() while the main thread
  33. * forks(). Later when the child process calls localtime() again, for instance
  34. * in order to log something to the Redis log, it may deadlock: in the copy
  35. * of the address space of the forked process the lock will never be released.
  36. *
  37. * This function takes the timezone 'tz' as argument, and the 'dst' flag is
  38. * used to check if daylight saving time is currently in effect. The caller
  39. * of this function should obtain such information calling tzset() ASAP in the
  40. * main() function to obtain the timezone offset from the 'timezone' global
  41. * variable. To obtain the daylight information, if it is currently active or not,
  42. * one trick is to call localtime() in main() ASAP as well, and get the
  43. * information from the tm_isdst field of the tm structure. However the daylight
  44. * time may switch in the future for long running processes, so this information
  45. * should be refreshed at safe times.
  46. *
  47. * Note that this function does not work for dates < 1/1/1970, it is solely
  48. * designed to work with what time(NULL) may return, and to support Redis
  49. * logging of the dates, it's not really a complete implementation. */
  50. static int is_leap_year(time_t year) {
  51. if (year % 4) return 0; /* A year not divisible by 4 is not leap. */
  52. else if (year % 100) return 1; /* If div by 4 and not 100 is surely leap. */
  53. else if (year % 400) return 0; /* If div by 100 *and* not by 400 is not leap. */
  54. else return 1; /* If div by 100 and 400 is leap. */
  55. }
  56. void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst) {
  57. const time_t secs_min = 60;
  58. const time_t secs_hour = 3600;
  59. const time_t secs_day = 3600*24;
  60. t -= tz; /* Adjust for timezone. */
  61. t += 3600*dst; /* Adjust for daylight time. */
  62. time_t days = t / secs_day; /* Days passed since epoch. */
  63. time_t seconds = t % secs_day; /* Remaining seconds. */
  64. tmp->tm_isdst = dst;
  65. tmp->tm_hour = seconds / secs_hour;
  66. tmp->tm_min = (seconds % secs_hour) / secs_min;
  67. tmp->tm_sec = (seconds % secs_hour) % secs_min;
  68. /* 1/1/1970 was a Thursday, that is, day 4 from the POV of the tm structure
  69. * where sunday = 0, so to calculate the day of the week we have to add 4
  70. * and take the modulo by 7. */
  71. tmp->tm_wday = (days+4)%7;
  72. /* Calculate the current year. */
  73. tmp->tm_year = 1970;
  74. while(1) {
  75. /* Leap years have one day more. */
  76. time_t days_this_year = 365 + is_leap_year(tmp->tm_year);
  77. if (days_this_year > days) break;
  78. days -= days_this_year;
  79. tmp->tm_year++;
  80. }
  81. tmp->tm_yday = days; /* Number of day of the current year. */
  82. /* We need to calculate in which month and day of the month we are. To do
  83. * so we need to skip days according to how many days there are in each
  84. * month, and adjust for the leap year that has one more day in February. */
  85. int mdays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  86. mdays[1] += is_leap_year(tmp->tm_year);
  87. tmp->tm_mon = 0;
  88. while(days >= mdays[tmp->tm_mon]) {
  89. days -= mdays[tmp->tm_mon];
  90. tmp->tm_mon++;
  91. }
  92. tmp->tm_mday = days+1; /* Add 1 since our 'days' is zero-based. */
  93. tmp->tm_year -= 1900; /* Surprisingly tm_year is year-1900. */
  94. }
  95. #ifdef LOCALTIME_TEST_MAIN
  96. #include <stdio.h>
  97. int main(void) {
  98. /* Obtain timezone and daylight info. */
  99. tzset(); /* Now 'timezome' global is populated. */
  100. time_t t = time(NULL);
  101. struct tm *aux = localtime(&t);
  102. int daylight_active = aux->tm_isdst;
  103. struct tm tm;
  104. char buf[1024];
  105. nolocks_localtime(&tm,t,timezone,daylight_active);
  106. strftime(buf,sizeof(buf),"%d %b %H:%M:%S",&tm);
  107. printf("[timezone: %d, dl: %d] %s\n", (int)timezone, (int)daylight_active, buf);
  108. }
  109. #endif

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载