Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
d2a835fb
Commit
d2a835fb
authored
Dec 17, 2009
by
Ben Olive
Committed by
Russ Cox
Dec 17, 2009
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time: add ISO 8601 time format
Fixes #431. R=r, rsc CC=golang-dev
https://golang.org/cl/179079
parent
95c776cc
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
1 deletion
+43
-1
time.go
src/pkg/time/time.go
+22
-1
time_test.go
src/pkg/time/time_test.go
+21
-0
No files found.
src/pkg/time/time.go
View file @
d2a835fb
...
...
@@ -319,6 +319,9 @@ func format(t *Time, fmt string) string {
case
'M'
:
// %M minute 00-59
decimal
(
buf
[
bp
:
bp
+
2
],
t
.
Minute
)
bp
+=
2
case
'm'
:
// %m month 01-12
decimal
(
buf
[
bp
:
bp
+
2
],
t
.
Month
)
bp
+=
2
case
'S'
:
// %S second 00-59
decimal
(
buf
[
bp
:
bp
+
2
],
t
.
Second
)
bp
+=
2
...
...
@@ -328,6 +331,20 @@ func format(t *Time, fmt string) string {
case
'y'
:
// %y year 08
decimal
(
buf
[
bp
:
bp
+
2
],
int
(
t
.
Year
%
100
))
bp
+=
2
case
'z'
:
// %z tz in the form -0500
if
t
.
ZoneOffset
==
0
{
bp
=
addString
(
buf
,
bp
,
"Z"
)
}
else
if
t
.
ZoneOffset
<
0
{
bp
=
addString
(
buf
,
bp
,
"-"
)
decimal
(
buf
[
bp
:
bp
+
2
],
-
t
.
ZoneOffset
/
3600
)
decimal
(
buf
[
bp
+
2
:
bp
+
4
],
(
-
t
.
ZoneOffset
%
3600
)
/
60
)
bp
+=
4
}
else
{
bp
=
addString
(
buf
,
bp
,
"+"
)
decimal
(
buf
[
bp
:
bp
+
2
],
t
.
ZoneOffset
/
3600
)
decimal
(
buf
[
bp
+
2
:
bp
+
4
],
(
t
.
ZoneOffset
%
3600
)
/
60
)
bp
+=
4
}
case
'Z'
:
bp
=
addString
(
buf
,
bp
,
t
.
Zone
)
default
:
...
...
@@ -355,6 +372,10 @@ func (t *Time) RFC850() string { return format(t, "%A, %d-%b-%y %H:%M:%S %Z") }
// RFC 1123: Sun, 06 Nov 1994 08:49:37 UTC
func
(
t
*
Time
)
RFC1123
()
string
{
return
format
(
t
,
"%a, %d %b %Y %H:%M:%S %Z"
)
}
// ISO8601 formats the parsed time value in the style of
// ISO 8601: 1994-11-06T08:49:37Z
func
(
t
*
Time
)
ISO8601
()
string
{
return
format
(
t
,
"%Y-%m-%dT%H:%M:%S%z"
)
}
// String formats the parsed time value in the style of
// date(1)
-
Sun Nov 6 08:49:37 UTC 1994
// date(1)
:
Sun Nov 6 08:49:37 UTC 1994
func
(
t
*
Time
)
String
()
string
{
return
format
(
t
,
"%a %b %e %H:%M:%S %Z %Y"
)
}
src/pkg/time/time_test.go
View file @
d2a835fb
...
...
@@ -101,6 +101,27 @@ func TestSecondsToUTCAndBack(t *testing.T) {
}
}
type
TimeFormatTest
struct
{
time
Time
formattedValue
string
}
var
iso8601Formats
=
[]
TimeFormatTest
{
TimeFormatTest
{
Time
{
2008
,
9
,
17
,
20
,
4
,
26
,
Wednesday
,
0
,
"UTC"
},
"2008-09-17T20:04:26Z"
},
TimeFormatTest
{
Time
{
1994
,
9
,
17
,
20
,
4
,
26
,
Wednesday
,
-
18000
,
"EST"
},
"1994-09-17T20:04:26-0500"
},
TimeFormatTest
{
Time
{
2000
,
12
,
26
,
1
,
15
,
6
,
Wednesday
,
15600
,
"OTO"
},
"2000-12-26T01:15:06+0420"
},
}
func
TestISO8601Conversion
(
t
*
testing
.
T
)
{
for
_
,
f
:=
range
iso8601Formats
{
if
f
.
time
.
ISO8601
()
!=
f
.
formattedValue
{
t
.
Error
(
"ISO8601():"
)
t
.
Errorf
(
" want=%+v"
,
f
.
formattedValue
)
t
.
Errorf
(
" have=%+v"
,
f
.
time
.
ISO8601
())
}
}
}
func
BenchmarkSeconds
(
b
*
testing
.
B
)
{
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
Seconds
()
...
...
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