Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
7d8b1860
Commit
7d8b1860
authored
Aug 19, 2019
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time/ISO8601: support time zone offset
parent
b0682582
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
0 deletions
+59
-0
ISO8601.cxx
src/time/ISO8601.cxx
+53
-0
TestISO8601.cxx
test/TestISO8601.cxx
+6
-0
No files found.
src/time/ISO8601.cxx
View file @
7d8b1860
...
...
@@ -58,6 +58,56 @@ FormatISO8601(std::chrono::system_clock::time_point tp)
return
FormatISO8601
(
GmTime
(
tp
));
}
static
std
::
pair
<
unsigned
,
unsigned
>
ParseTimeZoneOffsetRaw
(
const
char
*&
s
)
{
char
*
endptr
;
unsigned
long
value
=
strtoul
(
s
,
&
endptr
,
10
);
if
(
endptr
==
s
+
4
)
{
s
=
endptr
;
return
std
::
make_pair
(
value
/
100
,
value
%
100
);
}
else
if
(
endptr
==
s
+
2
)
{
s
=
endptr
;
unsigned
hours
=
value
,
minutes
=
0
;
if
(
*
s
==
':'
)
{
++
s
;
minutes
=
strtoul
(
s
,
&
endptr
,
10
);
if
(
endptr
!=
s
+
2
)
throw
std
::
runtime_error
(
"Failed to parse time zone offset"
);
s
=
endptr
;
}
return
std
::
make_pair
(
hours
,
minutes
);
}
else
throw
std
::
runtime_error
(
"Failed to parse time zone offset"
);
}
static
std
::
chrono
::
system_clock
::
duration
ParseTimeZoneOffset
(
const
char
*&
s
)
{
assert
(
*
s
==
'+'
||
*
s
==
'-'
);
bool
negative
=
*
s
==
'-'
;
++
s
;
auto
raw
=
ParseTimeZoneOffsetRaw
(
s
);
if
(
raw
.
first
>
13
)
throw
std
::
runtime_error
(
"Time offset hours out of range"
);
if
(
raw
.
second
>=
60
)
throw
std
::
runtime_error
(
"Time offset minutes out of range"
);
std
::
chrono
::
system_clock
::
duration
d
=
std
::
chrono
::
hours
(
raw
.
first
);
d
+=
std
::
chrono
::
minutes
(
raw
.
second
);
if
(
negative
)
d
=
-
d
;
return
d
;
}
std
::
pair
<
std
::
chrono
::
system_clock
::
time_point
,
std
::
chrono
::
system_clock
::
duration
>
ParseISO8601
(
const
char
*
s
)
...
...
@@ -93,8 +143,11 @@ ParseISO8601(const char *s)
auto
tp
=
TimeGm
(
tm
);
/* time zone */
if
(
*
s
==
'Z'
)
++
s
;
else
if
(
*
s
==
'+'
||
*
s
==
'-'
)
tp
-=
ParseTimeZoneOffset
(
s
);
if
(
*
s
!=
0
)
throw
std
::
runtime_error
(
"Garbage at end of time stamp"
);
...
...
test/TestISO8601.cxx
View file @
7d8b1860
...
...
@@ -57,6 +57,12 @@ static constexpr struct {
/* without time zone */
{
"2019-02-04T16:46:41"
,
1549298801
,
std
::
chrono
::
seconds
(
1
)
},
/* with time zone */
{
"2019-02-04T16:46:41+02"
,
1549291601
,
std
::
chrono
::
seconds
(
1
)
},
{
"2019-02-04T16:46:41+0200"
,
1549291601
,
std
::
chrono
::
seconds
(
1
)
},
{
"2019-02-04T16:46:41+02:00"
,
1549291601
,
std
::
chrono
::
seconds
(
1
)
},
{
"2019-02-04T16:46:41-0200"
,
1549306001
,
std
::
chrono
::
seconds
(
1
)
},
};
TEST
(
ISO8601
,
Parse
)
...
...
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