Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
fmt
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
fmt
Commits
f28cf330
Unverified
Commit
f28cf330
authored
Jun 12, 2021
by
sunmy2019
Committed by
GitHub
Jun 11, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adding a default format for std::chrono::time_point<std::chrono::syst… (#2345)
parent
55010a9d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
0 deletions
+26
-0
include/fmt/chrono.h
include/fmt/chrono.h
+23
-0
test/chrono-test.cc
test/chrono-test.cc
+1
-0
test/compile-test.cc
test/compile-test.cc
+2
-0
No files found.
include/fmt/chrono.h
View file @
f28cf330
...
...
@@ -451,14 +451,37 @@ FMT_END_DETAIL_NAMESPACE
template
<
typename
Char
,
typename
Duration
>
struct
formatter
<
std
::
chrono
::
time_point
<
std
::
chrono
::
system_clock
,
Duration
>
,
Char
>
:
formatter
<
std
::
tm
,
Char
>
{
FMT_CONSTEXPR
formatter
()
{
this
->
specs
=
{
default_specs
,
sizeof
(
default_specs
)
/
sizeof
(
Char
)};
}
template
<
typename
ParseContext
>
FMT_CONSTEXPR
auto
parse
(
ParseContext
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
auto
it
=
ctx
.
begin
();
if
(
it
!=
ctx
.
end
()
&&
*
it
==
':'
)
++
it
;
auto
end
=
it
;
while
(
end
!=
ctx
.
end
()
&&
*
end
!=
'}'
)
++
end
;
if
(
end
!=
it
)
this
->
specs
=
{
it
,
detail
::
to_unsigned
(
end
-
it
)};
return
end
;
}
template
<
typename
FormatContext
>
auto
format
(
std
::
chrono
::
time_point
<
std
::
chrono
::
system_clock
>
val
,
FormatContext
&
ctx
)
->
decltype
(
ctx
.
out
())
{
std
::
tm
time
=
localtime
(
val
);
return
formatter
<
std
::
tm
,
Char
>::
format
(
time
,
ctx
);
}
static
constexpr
Char
default_specs
[]
=
{
'%'
,
'Y'
,
'-'
,
'%'
,
'm'
,
'-'
,
'%'
,
'd'
,
' '
,
'%'
,
'H'
,
':'
,
'%'
,
'M'
,
':'
,
'%'
,
'S'
};
};
template
<
typename
Char
,
typename
Duration
>
constexpr
Char
formatter
<
std
::
chrono
::
time_point
<
std
::
chrono
::
system_clock
,
Duration
>
,
Char
>::
default_specs
[];
template
<
typename
Char
>
struct
formatter
<
std
::
tm
,
Char
>
{
template
<
typename
ParseContext
>
FMT_CONSTEXPR
auto
parse
(
ParseContext
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
...
...
test/chrono-test.cc
View file @
f28cf330
...
...
@@ -99,6 +99,7 @@ template <typename TimePoint> auto strftime(TimePoint tp) -> std::string {
TEST
(
chrono_test
,
time_point
)
{
auto
t1
=
std
::
chrono
::
system_clock
::
now
();
EXPECT_EQ
(
strftime
(
t1
),
fmt
::
format
(
"{:%Y-%m-%d %H:%M:%S}"
,
t1
));
EXPECT_EQ
(
strftime
(
t1
),
fmt
::
format
(
"{}"
,
t1
));
using
time_point
=
std
::
chrono
::
time_point
<
std
::
chrono
::
system_clock
,
std
::
chrono
::
seconds
>
;
auto
t2
=
time_point
(
std
::
chrono
::
seconds
(
42
));
...
...
test/compile-test.cc
View file @
f28cf330
...
...
@@ -92,6 +92,8 @@ TEST(compile_test, format_default) {
EXPECT_EQ
(
"foo"
,
fmt
::
format
(
FMT_COMPILE
(
"{}"
),
"foo"
));
EXPECT_EQ
(
"foo"
,
fmt
::
format
(
FMT_COMPILE
(
"{}"
),
std
::
string
(
"foo"
)));
EXPECT_EQ
(
"foo"
,
fmt
::
format
(
FMT_COMPILE
(
"{}"
),
test_formattable
()));
auto
t
=
std
::
chrono
::
system_clock
::
now
();
EXPECT_EQ
(
fmt
::
format
(
"{}"
,
t
),
fmt
::
format
(
FMT_COMPILE
(
"{}"
),
t
));
# ifdef __cpp_lib_byte
EXPECT_EQ
(
"42"
,
fmt
::
format
(
FMT_COMPILE
(
"{}"
),
std
::
byte
{
42
}));
# endif
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment