my_systime.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /**
  23. @file mysys/my_systime.cc Functions for manipulating timespec
  24. structs and get_date for converting a time_t to string using various
  25. date formats.
  26. */
  27. #include "my_systime.h"
  28. #include "my_config.h"
  29. #include <assert.h>
  30. #include <algorithm> // std::min
  31. #include <chrono>
  32. #include <cstdio> // std::sprintf()
  33. #include <limits> // std::numeric_limits
  34. // Note that timespec is in time.h in C99, but std::timespec will not
  35. // be in ctime until C++17
  36. #include <time.h> // time_t, timespec
  37. /**
  38. Set the value of a timespec object to the current time plus a
  39. number of nanosconds.
  40. @note the sec value is capped at std::chrono::nanoseconds::max()
  41. @param[out] abstime time value being modified
  42. @param nsec number of nanoseconds to add to current time
  43. */
  44. void set_timespec_nsec(struct timespec *abstime, Timeout_type nsec) {
  45. assert(nsec != std::numeric_limits<Timeout_type>::max());
  46. if (nsec == TIMEOUT_INF) {
  47. *abstime = TIMESPEC_POSINF;
  48. return;
  49. }
  50. unsigned long long int now = my_getsystime() + (nsec / 100);
  51. unsigned long long int tv_sec = now / 10000000ULL;
  52. #if SIZEOF_TIME_T < SIZEOF_LONG_LONG
  53. /* Ensure that the number of seconds don't overflow. */
  54. tv_sec = std::min(tv_sec, static_cast<unsigned long long int>(
  55. std::numeric_limits<time_t>::max()));
  56. #endif
  57. abstime->tv_sec = static_cast<time_t>(tv_sec);
  58. abstime->tv_nsec = (now % 10000000ULL) * 100 + (nsec % 100);
  59. }
  60. /**
  61. Set the value of a timespec object to the current time plus a
  62. number of seconds using seconds.
  63. @note the sec value is capped at std::chrono::seconds::max()
  64. @param[out] abstime time value being modified
  65. @param sec number of seconds to add to current time
  66. */
  67. void set_timespec(struct timespec *abstime, Timeout_type sec) {
  68. assert(sec != std::numeric_limits<Timeout_type>::max());
  69. if (sec == TIMEOUT_INF) {
  70. *abstime = TIMESPEC_POSINF;
  71. return;
  72. }
  73. set_timespec_nsec(abstime, sec * 1000000000ULL);
  74. }
  75. /**
  76. Store textual representation of date in a character array.
  77. @param[out] to character array where date will be written
  78. @param flag format of date:
  79. If flag & GETDATE_TIME Return date and time
  80. If flag & GETDATE_SHORT_DATE Return short date format YYMMDD
  81. If flag & GETDATE_HHMMSSTIME Return time in HHMMDD format.
  82. If flag & GETDATE_GMT Date/time in GMT
  83. If flag & GETDATE_FIXEDLENGTH Return fixed length date/time
  84. @param date time_t value for conversion.
  85. @note If flag & GETDATE_T_DELIMITER Append 'T' between date and time.
  86. If flag & GETDATE_SHORT_DATE_FULL_YEAR Return compact date format YYYYMMDD
  87. */
  88. void get_date(char *to, int flag, time_t date) {
  89. struct tm *start_time;
  90. time_t skr;
  91. struct tm tm_tmp;
  92. skr = date ? (time_t)date : my_time(0);
  93. if (flag & GETDATE_GMT)
  94. gmtime_r(&skr, &tm_tmp);
  95. else
  96. localtime_r(&skr, &tm_tmp);
  97. start_time = &tm_tmp;
  98. if (flag & GETDATE_SHORT_DATE) {
  99. to += std::sprintf(to, "%02d%02d%02d", start_time->tm_year % 100,
  100. start_time->tm_mon + 1, start_time->tm_mday);
  101. } else if (flag & GETDATE_SHORT_DATE_FULL_YEAR) {
  102. to += std::sprintf(to, "%4d%02d%02d", start_time->tm_year + 1900,
  103. start_time->tm_mon + 1, start_time->tm_mday);
  104. } else {
  105. to += std::sprintf(
  106. to, ((flag & GETDATE_FIXEDLENGTH) ? "%4d-%02d-%02d" : "%d-%02d-%02d"),
  107. start_time->tm_year + 1900, start_time->tm_mon + 1,
  108. start_time->tm_mday);
  109. }
  110. if (flag & GETDATE_DATE_TIME) {
  111. if (flag & GETDATE_T_DELIMITER) {
  112. *to = 'T';
  113. ++to;
  114. }
  115. to += std::sprintf(
  116. to,
  117. ((flag & GETDATE_FIXEDLENGTH) ? " %02d:%02d:%02d" : " %2d:%02d:%02d"),
  118. start_time->tm_hour, start_time->tm_min, start_time->tm_sec);
  119. } else if (flag & GETDATE_HHMMSSTIME) {
  120. if (flag & GETDATE_T_DELIMITER) {
  121. *to = 'T';
  122. ++to;
  123. }
  124. to += std::sprintf(to, "%02d%02d%02d", start_time->tm_hour,
  125. start_time->tm_min, start_time->tm_sec);
  126. }
  127. }