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