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