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
cebf55dc
Commit
cebf55dc
authored
Dec 15, 2011
by
Roger Peppe
Committed by
Russ Cox
Dec 15, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time: new AddDate method
R=rsc CC=golang-dev
https://golang.org/cl/5465044
parent
fc6df2fd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
time.go
src/pkg/time/time.go
+14
-0
time_test.go
src/pkg/time/time_test.go
+26
-0
No files found.
src/pkg/time/time.go
View file @
cebf55dc
...
@@ -564,6 +564,20 @@ func (t Time) Sub(u Time) Duration {
...
@@ -564,6 +564,20 @@ func (t Time) Sub(u Time) Duration {
return
Duration
(
t
.
sec
-
u
.
sec
)
*
Second
+
Duration
(
t
.
nsec
-
u
.
nsec
)
return
Duration
(
t
.
sec
-
u
.
sec
)
*
Second
+
Duration
(
t
.
nsec
-
u
.
nsec
)
}
}
// AddDate returns the time corresponding to adding the
// given number of years, months, and days to t.
// For example, AddDate(-1, 2, 3) applied to January 1, 2011
// returns March 4, 2010.
//
// AddDate normalizes its result in the same way that Date does,
// so, for example, adding one month to October 31 yields
// December 1, the normalized form for November 31.
func
(
t
Time
)
AddDate
(
years
int
,
months
int
,
days
int
)
Time
{
year
,
month
,
day
:=
t
.
Date
()
hour
,
min
,
sec
:=
t
.
Clock
()
return
Date
(
year
+
years
,
month
+
Month
(
months
),
day
+
days
,
hour
,
min
,
sec
,
int
(
t
.
nsec
),
t
.
loc
)
}
const
(
const
(
secondsPerMinute
=
60
secondsPerMinute
=
60
secondsPerHour
=
60
*
60
secondsPerHour
=
60
*
60
...
...
src/pkg/time/time_test.go
View file @
cebf55dc
...
@@ -634,6 +634,32 @@ func TestDate(t *testing.T) {
...
@@ -634,6 +634,32 @@ func TestDate(t *testing.T) {
}
}
}
}
// Several ways of getting from
// Fri Nov 18 7:56:35 PST 2011
// to
// Thu Mar 19 7:56:35 PST 2016
var
addDateTests
=
[]
struct
{
years
,
months
,
days
int
}{
{
4
,
4
,
1
},
{
3
,
16
,
1
},
{
3
,
15
,
30
},
{
5
,
-
6
,
-
18
-
30
-
12
},
}
func
TestAddDate
(
t
*
testing
.
T
)
{
t0
:=
Date
(
2011
,
11
,
18
,
7
,
56
,
35
,
0
,
UTC
)
t1
:=
Date
(
2016
,
3
,
19
,
7
,
56
,
35
,
0
,
UTC
)
for
_
,
at
:=
range
addDateTests
{
time
:=
t0
.
AddDate
(
at
.
years
,
at
.
months
,
at
.
days
)
if
!
time
.
Equal
(
t1
)
{
t
.
Errorf
(
"AddDate(%d, %d, %d) = %v, want %v"
,
at
.
years
,
at
.
months
,
at
.
days
,
time
,
t1
)
}
}
}
var
daysInTests
=
[]
struct
{
var
daysInTests
=
[]
struct
{
year
,
month
,
di
int
year
,
month
,
di
int
}{
}{
...
...
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