Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
nghttp2
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
nghttp2
Commits
de2a8555
Commit
de2a8555
authored
Nov 23, 2014
by
Tatsuhiro Tsujikawa
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement faster formatting for format_iso8601 and format_common_log
parent
df401f57
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
107 additions
and
28 deletions
+107
-28
src/util.cc
src/util.cc
+95
-1
src/util.h
src/util.h
+12
-27
No files found.
src/util.cc
View file @
de2a8555
...
...
@@ -167,7 +167,7 @@ std::string quote_string(const std::string& target)
namespace
{
template
<
typename
Iterator
>
Iterator
cpydig
(
Iterator
d
,
in
t
n
,
size_t
len
)
Iterator
cpydig
(
Iterator
d
,
uint32_
t
n
,
size_t
len
)
{
auto
p
=
d
+
len
-
1
;
...
...
@@ -223,6 +223,100 @@ std::string http_date(time_t t)
return
res
;
}
std
::
string
common_log_date
(
time_t
t
)
{
struct
tm
tms
;
if
(
localtime_r
(
&
t
,
&
tms
)
==
nullptr
)
{
return
""
;
}
// Format data like this:
// 03/Jul/2014:00:19:38 +0900
std
::
string
res
;
res
.
resize
(
26
);
auto
p
=
std
::
begin
(
res
);
p
=
cpydig
(
p
,
tms
.
tm_mday
,
2
);
*
p
++
=
'/'
;
auto
s
=
MONTH
[
tms
.
tm_mon
];
p
=
std
::
copy
(
s
,
s
+
3
,
p
);
*
p
++
=
'/'
;
p
=
cpydig
(
p
,
tms
.
tm_year
+
1900
,
4
);
*
p
++
=
':'
;
p
=
cpydig
(
p
,
tms
.
tm_hour
,
2
);
*
p
++
=
':'
;
p
=
cpydig
(
p
,
tms
.
tm_min
,
2
);
*
p
++
=
':'
;
p
=
cpydig
(
p
,
tms
.
tm_sec
,
2
);
*
p
++
=
' '
;
auto
gmtoff
=
tms
.
tm_gmtoff
;
if
(
gmtoff
>=
0
)
{
*
p
++
=
'+'
;
}
else
{
*
p
++
=
'-'
;
gmtoff
=
-
gmtoff
;
}
p
=
cpydig
(
p
,
gmtoff
/
3600
,
2
);
p
=
cpydig
(
p
,
(
gmtoff
%
3600
)
/
60
,
2
);
return
res
;
}
std
::
string
iso8601_date
(
int64_t
ms
)
{
time_t
sec
=
ms
/
1000
;
tm
tms
;
if
(
localtime_r
(
&
sec
,
&
tms
)
==
nullptr
)
{
return
""
;
}
// Format data like this:
// 2014-11-15T12:58:24.741Z
// 2014-11-15T12:58:24.741+09:00
std
::
string
res
;
res
.
resize
(
29
);
auto
p
=
std
::
begin
(
res
);
p
=
cpydig
(
p
,
tms
.
tm_year
+
1900
,
4
);
*
p
++
=
'-'
;
p
=
cpydig
(
p
,
tms
.
tm_mon
+
1
,
2
);
*
p
++
=
'-'
;
p
=
cpydig
(
p
,
tms
.
tm_mday
,
2
);
*
p
++
=
'T'
;
p
=
cpydig
(
p
,
tms
.
tm_hour
,
2
);
*
p
++
=
':'
;
p
=
cpydig
(
p
,
tms
.
tm_min
,
2
);
*
p
++
=
':'
;
p
=
cpydig
(
p
,
tms
.
tm_sec
,
2
);
*
p
++
=
'.'
;
p
=
cpydig
(
p
,
ms
%
1000
,
3
);
auto
gmtoff
=
tms
.
tm_gmtoff
;
if
(
gmtoff
==
0
)
{
*
p
++
=
'Z'
;
}
else
{
if
(
gmtoff
>
0
)
{
*
p
++
=
'+'
;
}
else
{
*
p
++
=
'-'
;
gmtoff
=
-
gmtoff
;
}
p
=
cpydig
(
p
,
gmtoff
/
3600
,
2
);
*
p
++
=
':'
;
p
=
cpydig
(
p
,
(
gmtoff
%
3600
)
/
60
,
2
);
}
res
.
resize
(
p
-
std
::
begin
(
res
));
return
res
;
}
time_t
parse_http_date
(
const
std
::
string
&
s
)
{
tm
tm
;
...
...
src/util.h
View file @
de2a8555
...
...
@@ -212,6 +212,14 @@ std::string format_hex(const unsigned char *s, size_t len);
std
::
string
http_date
(
time_t
t
);
// Returns given time |t| from epoch in Common Log format (e.g.,
// 03/Jul/2014:00:19:38 +0900)
std
::
string
common_log_date
(
time_t
t
);
// Returns given millisecond |ms| from epoch in ISO 8601 format (e.g.,
// 2014-11-15T12:58:24.741Z)
std
::
string
iso8601_date
(
int64_t
ms
);
time_t
parse_http_date
(
const
std
::
string
&
s
);
template
<
typename
InputIterator1
,
typename
InputIterator2
>
...
...
@@ -489,21 +497,11 @@ std::vector<unsigned char> get_default_alpn();
template
<
typename
T
>
std
::
string
format_common_log
(
const
T
&
tp
)
{
auto
t
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milli
seconds
>
auto
t
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
seconds
>
(
tp
.
time_since_epoch
());
time_t
sec
=
t
.
count
()
/
1000
;
tm
tms
;
if
(
localtime_r
(
&
sec
,
&
tms
)
==
nullptr
)
{
return
""
;
}
char
buf
[
32
];
strftime
(
buf
,
sizeof
(
buf
),
"%d/%b/%Y:%T %z"
,
&
tms
);
return
buf
;
return
common_log_date
(
t
.
count
());
}
// Returns given time |tp| in ISO 8601 format (e.g.,
// 2014-11-15T12:58:24.741Z)
// Expected type of |tp| is std::chrono::timepoint
...
...
@@ -512,20 +510,7 @@ std::string format_iso8601(const T& tp)
{
auto
t
=
std
::
chrono
::
duration_cast
<
std
::
chrono
::
milliseconds
>
(
tp
.
time_since_epoch
());
time_t
sec
=
t
.
count
()
/
1000
;
tm
tms
;
if
(
gmtime_r
(
&
sec
,
&
tms
)
==
nullptr
)
{
return
""
;
}
char
buf
[
128
];
auto
nwrite
=
strftime
(
buf
,
sizeof
(
buf
),
"%Y-%m-%dT%H:%M:%S"
,
&
tms
);
snprintf
(
&
buf
[
nwrite
],
sizeof
(
buf
)
-
nwrite
,
".%03ldZ"
,
static_cast
<
int
>
(
t
.
count
()
%
1000
));
return
buf
;
return
iso8601_date
(
t
.
count
());
}
// Return the system precision of the template parameter |Clock| as
...
...
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