Sese Framework
x.y.z
A cross-platform framework
载入中...
搜索中...
未找到
DateTime.h
浏览该文件的文档.
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
21
#pragma once
22
23
#include "
sese/Config.h
"
24
#include "
sese/util/TimeSpan.h
"
25
#include "
sese/text/DateTimeFormatter.h
"
26
27
#ifdef _WIN32
28
#pragma warning(disable : 4819)
29
#endif
30
31
// /**
32
// * 通用的时间 API
33
// * @param tp timeval 结构体
34
// * @param tzp timezone 结构体
35
// * @return 成功(0), 失败(-1)
36
// */
37
// inline int32_t getTimeOfDate(struct timeval *tp, struct timezone *tzp = nullptr);
38
39
namespace
sese
{
40
44
class
DateTime
{
45
public
:
46
using
Ptr
= std::unique_ptr<DateTime>;
47
51
enum class
Policy
{
53
ONLY_CREATE
,
55
FORMAT
56
};
57
58
static
DateTime
now
(int32_t
utc
=
TIME_DEFAULT_ZONE
,
Policy
policy =
Policy::FORMAT
)
noexcept
;
59
60
static
DateTime::Ptr
nowPtr
(int32_t
utc
=
TIME_DEFAULT_ZONE
,
Policy
policy =
Policy::FORMAT
)
noexcept
;
61
62
explicit
DateTime
() noexcept = default;
63
64
explicit
DateTime
(uint64_t
timestamp
, int32_t
utc
=
TIME_DEFAULT_ZONE
,
Policy
policy =
Policy
::
FORMAT
) noexcept;
65
66
public:
67
[[nodiscard]]
bool
isLeapYear
() const noexcept {
return
this->
isLeap
; }
68
[[nodiscard]] int32_t
getYears
() const noexcept {
return
this->
years
; }
69
[[nodiscard]] int32_t
getMonths
() const noexcept {
return
this->
months
; }
70
[[nodiscard]] int32_t
getDays
() const noexcept {
return
this->
days
; }
71
[[nodiscard]] int32_t
getHours
() const noexcept {
return
this->
hours
; }
72
[[nodiscard]] int32_t
getMinutes
() const noexcept {
return
this->
minutes
; }
73
[[nodiscard]] int32_t
getSeconds
() const noexcept {
return
this->
seconds
; }
74
[[nodiscard]] int32_t
getMilliseconds
() const noexcept {
return
this->
milliseconds
; }
75
[[nodiscard]] int32_t
getMicroseconds
() const noexcept {
return
this->
microseconds
; }
76
[[nodiscard]] int32_t
getUTC
() const noexcept {
return
this->
utc
; }
77
[[nodiscard]] int32_t
getDayOfWeek
() const noexcept {
return
this->
dayofweek
; }
78
[[nodiscard]] int32_t
getDayOfYear
() const noexcept {
return
this->
dayofyear
; }
79
[[nodiscard]] uint64_t
getTimestamp
() const noexcept {
return
this->
timestamp
; }
80
81
public
:
82
TimeSpan
operator-
(
const
DateTime
&date_time)
const
noexcept
;
83
DateTime
operator-
(
const
TimeSpan
&time_span)
const
noexcept
;
84
DateTime
operator+
(
const
TimeSpan
&time_span)
const
noexcept
;
86
[[nodiscard]] int32_t
compareTo
(
const
DateTime
&date_time)
const
noexcept
;
88
[[nodiscard]] int32_t
unclearCompareTo
(
const
DateTime
&date_time)
const
noexcept
;
89
90
private
:
91
// 初次计算的数据
92
int32_t
years
= 1970;
93
int32_t
months
= 1;
94
int32_t
days
= 1;
95
int32_t
hours
= 0;
96
int32_t
minutes
= 0;
97
int32_t
seconds
= 0;
98
int32_t
dayofweek
= 0;
99
int32_t
dayofyear
= 0;
100
101
bool
isLeap
=
false
;
102
int32_t
milliseconds
= 0;
103
int32_t
microseconds
= 0;
104
105
// 核心数据
106
int32_t
utc
= 0;
107
uint64_t
timestamp
= 0;
108
};
109
110
template
<>
111
struct
text::overload::Formatter
<
DateTime
> {
112
std::string datetime_pattern =
"yyyy-MM-dd HH:mm:ss"
;
113
114
bool
parse
(
const
std::string &pattern) {
115
datetime_pattern = pattern;
116
return
true
;
117
}
118
119
void
format
(
FmtCtx
&ctx,
const
DateTime
&datetime)
const
{
120
ctx.
builder
<<
DateTimeFormatter::format
(datetime, datetime_pattern);
121
}
122
};
123
124
}
// namespace sese
sese
util
DateTime.h
生成于 2024年 十一月 4日 星期一 09:58:03 , 为 Sese Framework使用
1.11.0