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
f5fe8492
Commit
f5fe8492
authored
Nov 28, 2018
by
Victor Zverovich
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Specialize formatter for chrono durations
parent
a5a9805a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
99 additions
and
6 deletions
+99
-6
include/fmt/time.h
include/fmt/time.h
+92
-6
test/time-test.cc
test/time-test.cc
+7
-0
No files found.
include/fmt/time.h
View file @
f5fe8492
...
...
@@ -11,6 +11,13 @@
#include "format.h"
#include <ctime>
#ifndef FMT_USE_CHRONO
# define FMT_USE_CHRONO 0
#endif
#if FMT_USE_CHRONO
# include <chrono>
#endif
FMT_BEGIN_NAMESPACE
// Prevents expansion of a preceding token as a function-style macro.
...
...
@@ -24,11 +31,9 @@ inline null<> gmtime_r(...) { return null<>(); }
inline
null
<>
gmtime_s
(...)
{
return
null
<>
();
}
// Parses a put_time-like format string and invokes handler actions.
template
<
bool
IS_CONSTEXPR
,
typename
Char
,
typename
Handler
>
FMT_CONSTEXPR
void
parse_datetime_format
(
basic_string_view
<
Char
>
format_str
,
Handler
&&
handler
)
{
auto
begin
=
format_str
.
data
();
auto
end
=
begin
+
format_str
.
size
();
template
<
typename
Char
,
typename
Handler
>
FMT_CONSTEXPR
const
Char
*
parse_chrono_format
(
const
Char
*
begin
,
const
Char
*
end
,
Handler
&&
handler
)
{
auto
ptr
=
begin
;
while
(
ptr
!=
end
)
{
auto
c
=
*
ptr
;
...
...
@@ -65,13 +70,94 @@ FMT_CONSTEXPR void parse_datetime_format(
case
'B'
:
handler
.
on_full_month
();
break
;
// Hour, minute, second:
case
'S'
:
handler
.
on_second
();
break
;
// TODO: parse more format specifiers
}
}
if
(
begin
!=
ptr
)
handler
.
on_text
(
begin
,
ptr
);
return
ptr
;
}
}
// namespace internal
struct
chrono_format_checker
{
template
<
typename
Char
>
void
on_text
(
const
Char
*
,
const
Char
*
)
{}
void
on_abbr_weekday
()
{}
void
on_full_weekday
()
{}
void
on_dec0_weekday
()
{}
void
on_dec1_weekday
()
{}
void
on_abbr_month
()
{}
void
on_full_month
()
{}
void
on_second
()
{}
};
}
// namespace internal
#if FMT_USE_CHRONO
namespace
internal
{
template
<
typename
OutputIt
,
typename
Char
>
struct
chrono_formatter
{
OutputIt
out
;
std
::
chrono
::
seconds
s
;
std
::
chrono
::
milliseconds
ms
;
explicit
chrono_formatter
(
OutputIt
o
)
:
out
(
o
)
{}
template
<
typename
Int
>
void
write
(
Int
value
,
int
width
)
{
typedef
typename
int_traits
<
Int
>::
main_type
main_type
;
main_type
n
=
value
;
auto
num_digits
=
internal
::
count_digits
(
n
);
if
(
width
>
num_digits
)
out
=
std
::
fill_n
(
out
,
width
-
num_digits
,
'0'
);
out
=
format_decimal
<
Char
>
(
out
,
n
,
num_digits
);
}
void
on_text
(
const
Char
*
,
const
Char
*
)
{}
void
on_abbr_weekday
()
{}
void
on_full_weekday
()
{}
void
on_dec0_weekday
()
{}
void
on_dec1_weekday
()
{}
void
on_abbr_month
()
{}
void
on_full_month
()
{}
void
on_second
()
{
write
(
s
.
count
(),
2
);
if
(
ms
!=
std
::
chrono
::
milliseconds
())
{
*
out
++
=
'.'
;
write
(
ms
.
count
(),
3
);
}
}
};
}
// namespace internal
template
<
typename
Rep
,
typename
Period
,
typename
Char
>
struct
formatter
<
std
::
chrono
::
duration
<
Rep
,
Period
>
,
Char
>
{
mutable
basic_string_view
<
Char
>
format_str
;
using
Duration
=
std
::
chrono
::
duration
<
Rep
,
Period
>
;
FMT_CONSTEXPR
auto
parse
(
basic_parse_context
<
Char
>
&
ctx
)
->
decltype
(
ctx
.
begin
())
{
auto
begin
=
ctx
.
begin
(),
end
=
ctx
.
end
();
end
=
parse_chrono_format
(
begin
,
end
,
internal
::
chrono_format_checker
());
format_str
=
basic_string_view
<
Char
>
(
&*
begin
,
end
-
begin
);
return
end
;
}
template
<
typename
FormatContext
>
auto
format
(
const
Duration
&
d
,
FormatContext
&
ctx
)
->
decltype
(
ctx
.
out
())
{
internal
::
chrono_formatter
<
decltype
(
ctx
.
out
()),
Char
>
f
(
ctx
.
out
());
f
.
s
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
seconds
>
(
d
);
f
.
ms
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
d
-
f
.
s
);
parse_chrono_format
(
format_str
.
begin
(),
format_str
.
end
(),
f
);
return
f
.
out
;
}
};
#endif // FMT_USE_CHRONO
// Thread-safe replacement for std::localtime
inline
std
::
tm
localtime
(
std
::
time_t
time
)
{
...
...
test/time-test.cc
View file @
f5fe8492
...
...
@@ -57,3 +57,10 @@ TEST(TimeTest, GMTime) {
std
::
tm
tm
=
*
std
::
gmtime
(
&
t
);
EXPECT_TRUE
(
EqualTime
(
tm
,
fmt
::
gmtime
(
t
)));
}
#if FMT_USE_CHRONO
TEST
(
TimeTest
,
Chrono
)
{
EXPECT_EQ
(
"42"
,
fmt
::
format
(
"{:%S}"
,
std
::
chrono
::
seconds
(
42
)));
EXPECT_EQ
(
"01.234"
,
fmt
::
format
(
"{:%S}"
,
std
::
chrono
::
milliseconds
(
1234
)));
}
#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