MySQL 9.6.0
Source Code Documentation
mysql_time.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2021, 2025, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTER_SRC_REST_MRS_SRC_HELPER_MYSQL_TIME_H_
27#define ROUTER_SRC_REST_MRS_SRC_HELPER_MYSQL_TIME_H_
28
29#include <time.h>
30#include <iomanip>
31#include <sstream>
32#include <string>
33
34#include "my_macros.h"
35
36namespace helper {
37
38class DateTime {
39 public:
40 DateTime() : time_{0} {}
41
42 DateTime(const std::string &text_time) { from_string(text_time); }
43
44 void from_string(const std::string &text_time) {
45 tm out_time;
46#ifdef HAVE_STRPTIME
47 strptime(text_time.c_str(), "%Y-%m-%d %T", &out_time);
48#else
49 std::stringstream timestamp_ss(text_time);
50
51 timestamp_ss >> std::get_time(&out_time, "%Y-%m-%d %T");
52#endif
53 time_ = IF_WIN(_mkgmtime, timegm)(&out_time);
54 }
55
56 std::string to_string() const {
57 std::string result(70, '\0');
58
59 tm out_time;
60
61#ifdef _WIN32
62 gmtime_s(&out_time, &time_);
63#else
64 gmtime_r(&time_, &out_time);
65#endif
66 auto size =
67 strftime(&result[0], result.length(), "'%Y-%m-%d %T'", &out_time);
68
69 if (0 == size) {
70 result[0] = '0';
71 size = 1;
72 }
73
74 result.resize(size);
75
76 return result;
77 }
78
79 friend bool operator<=(const DateTime &l, const DateTime &r);
80
81 time_t time_;
82
83 // TODO(lkotula): move private up (Shouldn't be in review)
84 private:
85};
86
87inline bool operator<=(const DateTime &l, const DateTime &r) {
88 return l.time_ <= r.time_;
89}
90
91} // namespace helper
92
93#endif // ROUTER_SRC_REST_MRS_SRC_HELPER_MYSQL_TIME_H_
Definition: mysql_time.h:38
void from_string(const std::string &text_time)
Definition: mysql_time.h:44
DateTime(const std::string &text_time)
Definition: mysql_time.h:42
DateTime()
Definition: mysql_time.h:40
time_t time_
Definition: mysql_time.h:81
std::string to_string() const
Definition: mysql_time.h:56
friend bool operator<=(const DateTime &l, const DateTime &r)
Definition: mysql_time.h:87
Some common macros.
#define IF_WIN(A, B)
Definition: my_macros.h:38
Definition: cache.h:33
bool operator<=(const DateTime &l, const DateTime &r)
Definition: mysql_time.h:87
size_t size(const char *const c)
Definition: base64.h:46
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
struct result result
Definition: result.h:34
Definition: result.h:30
Include file for Sun RPC to compile out of the box.