Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
DateTime.h
Go to the documentation of this file.
1// Copyright 2024 libsese
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
22#pragma once
23
24#include "sese/Config.h"
25#include "sese/util/TimeSpan.h"
27
28#ifdef _WIN32
29#pragma warning(disable : 4819)
30#endif
31
32namespace sese {
33
37class DateTime {
38public:
39 using Ptr = std::unique_ptr<DateTime>;
40
44 enum class Policy {
48 FORMAT
49 };
50
51 static DateTime now(int32_t utc = TIME_DEFAULT_ZONE, Policy policy = Policy::FORMAT) noexcept;
52
53 static DateTime::Ptr nowPtr(int32_t utc = TIME_DEFAULT_ZONE, Policy policy = Policy::FORMAT) noexcept;
54
55 explicit DateTime() noexcept = default;
56
57 explicit DateTime(uint64_t timestamp, int32_t utc = TIME_DEFAULT_ZONE, Policy policy = Policy::FORMAT) noexcept;
58
59public:
60 [[nodiscard]] bool isLeapYear() const noexcept { return this->isLeap; }
61 [[nodiscard]] int32_t getYears() const noexcept { return this->years; }
62 [[nodiscard]] int32_t getMonths() const noexcept { return this->months; }
63 [[nodiscard]] int32_t getDays() const noexcept { return this->days; }
64 [[nodiscard]] int32_t getHours() const noexcept { return this->hours; }
65 [[nodiscard]] int32_t getMinutes() const noexcept { return this->minutes; }
66 [[nodiscard]] int32_t getSeconds() const noexcept { return this->seconds; }
67 [[nodiscard]] int32_t getMilliseconds() const noexcept { return this->milliseconds; }
68 [[nodiscard]] int32_t getMicroseconds() const noexcept { return this->microseconds; }
69 [[nodiscard]] int32_t getUTC() const noexcept { return this->utc; }
70 [[nodiscard]] int32_t getDayOfWeek() const noexcept { return this->dayofweek; }
71 [[nodiscard]] int32_t getDayOfYear() const noexcept { return this->dayofyear; }
72 [[nodiscard]] uint64_t getTimestamp() const noexcept { return this->timestamp; }
73
74public:
75 TimeSpan operator-(const DateTime &date_time) const noexcept;
76 DateTime operator-(const TimeSpan &time_span) const noexcept;
77 DateTime operator+(const TimeSpan &time_span) const noexcept;
79 [[nodiscard]] int32_t compareTo(const DateTime &date_time) const noexcept;
81 [[nodiscard]] int32_t unclearCompareTo(const DateTime &date_time) const noexcept;
82
83private:
84 int32_t years = 1970;
85 int32_t months = 1;
86 int32_t days = 1;
87 int32_t hours = 0;
88 int32_t minutes = 0;
89 int32_t seconds = 0;
90 int32_t dayofweek = 0;
91 int32_t dayofyear = 0;
92
93 bool isLeap = false;
94 int32_t milliseconds = 0;
95 int32_t microseconds = 0;
96
97 // Core data
98 int32_t utc = 0;
99 uint64_t timestamp = 0;
100};
101
102template<>
104 std::string datetime_pattern = "yyyy-MM-dd HH:mm:ss";
105
106 bool parse(const std::string &pattern) {
107 datetime_pattern = pattern;
108 return true;
109 }
110
111 void format(FmtCtx &ctx, const DateTime &datetime) const {
112 ctx.builder << DateTimeFormatter::format(datetime, datetime_pattern);
113 }
114};
115
116} // namespace sese