userver: userver/storages/mysql/dates.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
dates.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mysql/dates.hpp
4
5#include <chrono>
6#include <cstdint>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace storages::mysql {
11
12/// @brief a class that represent MySQL DATE type.
13class Date final {
14 public:
15 /// @brief constructs a DATE with year/month/day being zero.
17
18 /// @brief constructs a DATE from provided year, month and day.
19 Date(std::uint32_t year, std::uint32_t month, std::uint32_t day);
20
21 /// @brief constructs a DATE from provided timepoint.
22 explicit Date(std::chrono::system_clock::time_point tp);
23
24 /// @brief converts the DATE to timepoint in UTC time zone.
25 /// throws if stored date is out of timepoint range.
26 std::chrono::system_clock::time_point ToTimePoint() const;
27
28 /// @brief Returns the year part of the date.
29 std::uint32_t GetYear() const noexcept;
30
31 /// @brief Returns the month part of the date.
32 std::uint32_t GetMonth() const noexcept;
33
34 /// @brief Returns the day part of the date.
35 std::uint32_t GetDay() const noexcept;
36
37 bool operator==(const Date& other) const noexcept;
38
39 private:
40 std::uint32_t year_;
41 std::uint32_t month_;
42 std::uint32_t day_;
43};
44
45/// @brief a class that represents MySQL DATETIME type.
46class DateTime final {
47 public:
48 /// @brief constructs a DATETIME with DATE/hour/minute/second/microsecond
49 /// being zero.
51
52 /// @brief constructs a DATETIME from provided DATE, hour, minute, second and
53 /// microsecond.
54 DateTime(Date date, std::uint32_t hour, std::uint32_t minute,
55 std::uint32_t second, std::uint64_t microsecond);
56
57 /// @brief constructs a DATETIME from provided year, month, day, hour, minute,
58 /// second and microsecond.
59 DateTime(std::uint32_t year, std::uint32_t month, std::uint32_t day,
60 std::uint32_t hour, std::uint32_t minute, std::uint32_t second,
61 std::uint64_t microsecond);
62
63 /// @brief constructs a DATETIME from provided timepoint.
64 explicit DateTime(std::chrono::system_clock::time_point tp);
65
66 /// @brief converts the DATETIME to timepoint in UTC time zone.
67 /// throws if stored datetime is out of timepoint range.
68 std::chrono::system_clock::time_point ToTimePoint() const;
69
70 /// @brief Returns the date part of the datetime.
71 const Date& GetDate() const noexcept;
72
73 /// @brief Returns the hour part of the datetime.
74 std::uint32_t GetHour() const noexcept;
75
76 /// @brief Returns the minute part of the datetime.
77 std::uint32_t GetMinute() const noexcept;
78
79 /// @brief Returns the second part of the datetime.
80 std::uint32_t GetSecond() const noexcept;
81
82 /// @brief Returns the microsecond part of the datetime.
83 std::uint64_t GetMicrosecond() const noexcept;
84
85 bool operator==(const DateTime& other) const noexcept;
86
87 private:
88 Date date_;
89 std::uint32_t hour_;
90 std::uint32_t minute_;
91 std::uint32_t second_;
92 std::uint64_t microsecond_;
93};
94
95} // namespace storages::mysql
96
97USERVER_NAMESPACE_END