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
f88c020f
Commit
f88c020f
authored
Oct 14, 2021
by
Vladislav Shchapov
Committed by
Victor Zverovich
Oct 16, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Generalization of strftime/wcsftime function calls in tests
parent
2eeddba7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
21 deletions
+33
-21
test/chrono-test.cc
test/chrono-test.cc
+21
-17
test/xchar-test.cc
test/xchar-test.cc
+12
-4
No files found.
test/chrono-test.cc
View file @
f88c020f
...
...
@@ -42,6 +42,14 @@ auto make_second(int s) -> std::tm {
return
time
;
}
std
::
string
system_strftime
(
const
std
::
string
&
format
,
const
std
::
tm
*
timeptr
,
size_t
maxsize
=
1024
)
{
std
::
vector
<
char
>
output
(
maxsize
);
auto
size
=
std
::
strftime
(
output
.
data
(),
output
.
size
(),
format
.
c_str
(),
timeptr
);
return
std
::
string
(
output
.
data
(),
size
);
}
TEST
(
chrono_test
,
format_tm
)
{
auto
tm
=
std
::
tm
();
tm
.
tm_year
=
116
;
...
...
@@ -102,10 +110,9 @@ TEST(chrono_test, format_tm) {
std
::
time_t
t
=
std
::
mktime
(
&
tm
);
tm
=
*
std
::
localtime
(
&
t
);
char
output
[
256
]
=
{};
std
::
strftime
(
output
,
sizeof
(
output
),
iso_week_spec
.
c_str
(),
&
tm
);
auto
fmt_spec
=
std
::
string
(
"{:"
).
append
(
iso_week_spec
).
append
(
"}"
);
EXPECT_EQ
(
output
,
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
tm
));
EXPECT_EQ
(
system_strftime
(
iso_week_spec
,
&
tm
),
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
tm
));
}
// Every day from 1970-01-01
...
...
@@ -113,10 +120,9 @@ TEST(chrono_test, format_tm) {
for
(
std
::
time_t
t
=
6
*
3600
;
t
<
time_now
;
t
+=
86400
)
{
tm
=
*
std
::
localtime
(
&
t
);
char
output
[
256
]
=
{};
std
::
strftime
(
output
,
sizeof
(
output
),
iso_week_spec
.
c_str
(),
&
tm
);
auto
fmt_spec
=
std
::
string
(
"{:"
).
append
(
iso_week_spec
).
append
(
"}"
);
EXPECT_EQ
(
output
,
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
tm
));
EXPECT_EQ
(
system_strftime
(
iso_week_spec
,
&
tm
),
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
tm
));
}
}
...
...
@@ -208,22 +214,20 @@ TEST(chrono_test, gmtime) {
EXPECT_TRUE
(
equal
(
tm
,
fmt
::
gmtime
(
t
)));
}
template
<
typename
TimePoint
>
auto
strftime
(
TimePoint
tp
)
->
std
::
string
{
template
<
typename
TimePoint
>
auto
strftime
_full
(
TimePoint
tp
)
->
std
::
string
{
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
tp
);
auto
tm
=
*
std
::
localtime
(
&
t
);
char
output
[
256
]
=
{};
std
::
strftime
(
output
,
sizeof
(
output
),
"%Y-%m-%d %H:%M:%S"
,
&
tm
);
return
output
;
return
system_strftime
(
"%Y-%m-%d %H:%M:%S"
,
&
tm
);
}
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
));
EXPECT_EQ
(
strftime
_full
(
t1
),
fmt
::
format
(
"{:%Y-%m-%d %H:%M:%S}"
,
t1
));
EXPECT_EQ
(
strftime
_full
(
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
));
EXPECT_EQ
(
strftime
(
t2
),
fmt
::
format
(
"{:%Y-%m-%d %H:%M:%S}"
,
t2
));
EXPECT_EQ
(
strftime
_full
(
t2
),
fmt
::
format
(
"{:%Y-%m-%d %H:%M:%S}"
,
t2
));
std
::
vector
<
std
::
string
>
spec_list
=
{
"%%"
,
"%n"
,
"%t"
,
"%Y"
,
"%EY"
,
"%y"
,
"%Oy"
,
"%Ey"
,
"%C"
,
"%EC"
,
...
...
@@ -236,12 +240,12 @@ TEST(chrono_test, time_point) {
for
(
const
auto
&
spec
:
spec_list
)
{
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
t1
);
auto
tm
=
*
std
::
localtime
(
&
t
);
char
output
[
256
]
=
{};
std
::
strftime
(
output
,
sizeof
(
output
),
spec
.
c_str
()
,
&
tm
);
auto
sys_output
=
system_strftime
(
spec
,
&
tm
);
auto
fmt_spec
=
std
::
string
(
"{:"
).
append
(
spec
).
append
(
"}"
);
EXPECT_EQ
(
output
,
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
t1
));
EXPECT_EQ
(
output
,
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
tm
));
EXPECT_EQ
(
sys_
output
,
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
t1
));
EXPECT_EQ
(
sys_
output
,
fmt
::
format
(
fmt
::
runtime
(
fmt_spec
),
tm
));
}
}
...
...
test/xchar-test.cc
View file @
f88c020f
...
...
@@ -267,6 +267,14 @@ TEST(xchar_test, chrono) {
EXPECT_EQ
(
fmt
::
format
(
L"{:%T}"
,
tm
),
L"11:22:33"
);
}
std
::
wstring
system_wcsftime
(
const
std
::
wstring
&
format
,
const
std
::
tm
*
timeptr
,
size_t
maxsize
=
1024
)
{
std
::
vector
<
wchar_t
>
output
(
maxsize
);
auto
size
=
std
::
wcsftime
(
output
.
data
(),
output
.
size
(),
format
.
c_str
(),
timeptr
);
return
std
::
wstring
(
output
.
data
(),
size
);
}
TEST
(
chrono_test
,
time_point
)
{
auto
t1
=
std
::
chrono
::
system_clock
::
now
();
...
...
@@ -282,12 +290,12 @@ TEST(chrono_test, time_point) {
for
(
const
auto
&
spec
:
spec_list
)
{
auto
t
=
std
::
chrono
::
system_clock
::
to_time_t
(
t1
);
auto
tm
=
*
std
::
localtime
(
&
t
);
wchar_t
output
[
1024
]
=
{};
std
::
wcsftime
(
output
,
sizeof
(
output
)
/
sizeof
(
wchar_t
),
spec
.
c_str
()
,
&
tm
);
auto
sys_output
=
system_wcsftime
(
spec
,
&
tm
);
auto
fmt_spec
=
std
::
wstring
(
L"{:"
).
append
(
spec
).
append
(
L"}"
);
EXPECT_EQ
(
output
,
fmt
::
format
(
fmt_spec
,
t1
));
EXPECT_EQ
(
output
,
fmt
::
format
(
fmt_spec
,
tm
));
EXPECT_EQ
(
sys_
output
,
fmt
::
format
(
fmt_spec
,
t1
));
EXPECT_EQ
(
sys_
output
,
fmt
::
format
(
fmt_spec
,
tm
));
}
}
...
...
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