Skip to content

Commit 8699548

Browse files
authored
Merge pull request #8 from timonwong/add-tests
Add tests and coverage reporting
2 parents cfc3a5b + 40ca9ff commit 8699548

File tree

3 files changed

+122
-23
lines changed

3 files changed

+122
-23
lines changed

.travis.yml

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ sudo: false
22

33
language: go
44

5+
go_import_path: github.com/imperfectgo/go-strftime
6+
57
go:
6-
- '1.7'
7-
- '1.8'
8-
- '1.9'
9-
- '1.10'
8+
- '1.7.x'
9+
- '1.8.x'
10+
- '1.9.x'
11+
- '1.10.x'
1012
- master
1113

1214
matrix:
@@ -17,7 +19,16 @@ matrix:
1719
# tests pass on the stable versions of Go.
1820
fast_finish: true
1921

20-
go_import_path: github.com/imperfectgo/go-strftime
21-
2222
notifications:
2323
email: false
24+
25+
install:
26+
- go get github.com/go-playground/overalls
27+
- go get github.com/mattn/goveralls
28+
29+
script:
30+
- overalls -project=github.com/imperfectgo/go-strftime -covermode=atomic -- -race
31+
32+
after_script:
33+
- mv overalls.coverprofile coverage.txt
34+
- bash <(curl -s https://codecov.io/bash)

format.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
107107
case stdYield:
108108
continue
109109
case stdISO8601WeekYear:
110-
b = appendInt(b, iso8601WeekYear/100, 2)
110+
b = appendInt(b, iso8601WeekYear%100, 2)
111111
case stdISO8601LongWeekYear:
112112
b = appendInt(b, iso8601WeekYear, 4)
113113
case stdISO8601Week:
@@ -123,7 +123,7 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
123123
case stdFirstTwoDigitYear:
124124
b = appendInt(b, year/100, 2)
125125
case stdYearDay:
126-
b = appendInt(b, yday, 3)
126+
b = appendInt(b, yday+1, 3)
127127
case stdMonth:
128128
b = append(b, month.String()[:3]...)
129129
case stdLongMonth:
@@ -209,7 +209,7 @@ func AppendFormat(b []byte, t time.Time, layout string) []byte {
209209
break
210210
}
211211
// No time zone known for this time, but we must print one.
212-
// Use the -0700 format.
212+
// Use the -0700 laylout.
213213
zone := offset / 60 // convert to minutes
214214
if zone < 0 {
215215
b = append(b, '-')
@@ -286,6 +286,7 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
286286
case 'P':
287287
return layout[0:specPos], stdpm, layout[i+1:]
288288
case 'r':
289+
return layout[0:specPos], stdYield, "%I:%M:%S %p" + layout[i+1:]
289290
case 'R': // %H:%M"
290291
return layout[0:specPos], stdYield, "%H:%M" + layout[i+1:]
291292
case 'S':
@@ -305,7 +306,7 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
305306
case 'W':
306307
// TODO: week of the year as a decimal number (Monday is the first day of the week)
307308
case 'x': // locale depended date representation (assumes "C" locale)
308-
return layout[0:specPos], stdYield, "%m/%d/%y" + layout[i+1:]
309+
return layout[0:specPos], stdYield, "%m/%d/%Y" + layout[i+1:]
309310
case 'X': // locale depended time representation (assumes "C" locale)
310311
return layout[0:specPos], stdYield, "%H:%M:%S" + layout[i+1:]
311312
case 'y':
@@ -317,6 +318,7 @@ func nextStdChunk(layout string) (prefix string, std int, suffix string) {
317318
case 'Z':
318319
return layout[0:specPos], stdTZ, layout[i+1:]
319320
case '%':
321+
return layout[0:specPos] + "%", stdYield, layout[i+1:]
320322
}
321323

322324
specPos = -1

format_test.go

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,110 @@
1-
// Copyright 2018 Timon Wong. All rights reserved.
2-
// Copyright 2009 The Go Authors. All rights reserved.
3-
// Use of this source code is governed by a BSD-style
4-
// license that can be found in the LICENSE file.
1+
// Copied from https://github.com/awoodbeck/strftime
2+
// Copyright (c) 2018 Adam Woodbeck
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
521

622
package strftime
723

824
import (
25+
"fmt"
926
"testing"
1027
"time"
1128
)
1229

13-
const layout = "%Y-%m-%dT%H:%M:%S.%f%z"
30+
var (
31+
t1 = time.Date(2018, time.July, 9, 13, 14, 15, 0, time.UTC)
32+
t2 = time.Date(1950, time.December, 10, 4, 45, 59, 0, time.UTC)
33+
t3 = time.Date(2016, time.January, 1, 13, 14, 15, 0, time.UTC)
34+
t4 = time.Date(2015, time.January, 1, 13, 14, 15, 0, time.UTC)
35+
36+
tc = []struct {
37+
time time.Time
38+
laylout string
39+
expected string
40+
}{
41+
{time: t1, laylout: "%", expected: "%"},
42+
{time: t1, laylout: "%%", expected: "%"},
43+
{time: t1, laylout: "%Q", expected: "%Q"},
44+
{time: t1, laylout: "%%n", expected: "%n"},
45+
{time: t1, laylout: "%%t", expected: "%t"},
46+
{time: t1, laylout: "%n%t", expected: "\n\t"},
47+
{time: t1, laylout: "%a", expected: "Mon"},
48+
{time: t1, laylout: "%A", expected: "Monday"},
49+
{time: t1, laylout: "%b", expected: "Jul"},
50+
{time: t1, laylout: "%h", expected: "Jul"},
51+
{time: t1, laylout: "%B", expected: "July"},
52+
{time: t1, laylout: "%c", expected: "Mon Jul 9 13:14:15 2018"},
53+
{time: t1, laylout: "%C", expected: "20"},
54+
{time: t1, laylout: "%d", expected: "09"},
55+
{time: t1, laylout: "%D", expected: "07/09/18"},
56+
{time: t1, laylout: "%e", expected: " 9"},
57+
{time: t1, laylout: "%F", expected: "2018-07-09"},
58+
{time: t1, laylout: "%g", expected: "18"},
59+
{time: t1, laylout: "%G", expected: "2018"},
60+
{time: t1, laylout: "%H", expected: "13"},
61+
{time: t1, laylout: "%I", expected: "01"},
62+
{time: t1, laylout: "%j", expected: "190"},
63+
{time: t1, laylout: "%m", expected: "07"},
64+
{time: t1, laylout: "%M", expected: "14"},
65+
{time: t1, laylout: "%n", expected: "\n"},
66+
{time: t1, laylout: "%p", expected: "PM"},
67+
{time: t2, laylout: "%p", expected: "AM"},
68+
{time: t1, laylout: "%P", expected: "pm"},
69+
{time: t2, laylout: "%P", expected: "am"},
70+
{time: t1, laylout: "%r", expected: "01:14:15 PM"},
71+
{time: t2, laylout: "%r", expected: "04:45:59 AM"},
72+
{time: t1, laylout: "%R", expected: "13:14"},
73+
{time: t2, laylout: "%R", expected: "04:45"},
74+
{time: t1, laylout: "%S", expected: "15"},
75+
{time: t1, laylout: "%t", expected: "\t"},
76+
{time: t1, laylout: "%T", expected: "13:14:15"},
77+
{time: t2, laylout: "%T", expected: "04:45:59"},
78+
{time: t1, laylout: "%u", expected: "1"},
79+
{time: t2, laylout: "%u", expected: "7"},
80+
{time: t1, laylout: "%V", expected: "28"},
81+
{time: t3, laylout: "%V", expected: "53"}, // 3 January days in this week.
82+
{time: t4, laylout: "%V", expected: "01"}, // 4 January days in this week.
83+
{time: t1, laylout: "%w", expected: "1"},
84+
{time: t2, laylout: "%w", expected: "0"},
85+
{time: t1, laylout: "%x", expected: "07/09/2018"},
86+
{time: t1, laylout: "%X", expected: "13:14:15"},
87+
{time: t2, laylout: "%X", expected: "04:45:59"},
88+
{time: t1, laylout: "%y", expected: "18"},
89+
{time: t1, laylout: "%Y", expected: "2018"},
90+
{time: t1, laylout: "%z", expected: "+0000"},
91+
{time: t1, laylout: "%Z", expected: "UTC"},
92+
{time: t1, laylout: "foo", expected: "foo"},
93+
{time: t1, laylout: "bar%", expected: "bar%"},
94+
{time: t1, laylout: "%1", expected: "%1"},
95+
{time: t1, laylout: "%Y-%m-%dtest\n\t%Z", expected: "2018-07-09test\n\tUTC"},
96+
}
97+
)
1498

1599
func TestFormat(t *testing.T) {
16-
now := time.Now()
17-
t.Log(now.Format(time.RFC3339Nano))
18-
t.Logf("Format %s -> %s", layout, Format(now, layout))
19-
t.Logf("Format %s -> %s", "%f", Format(now, "%f"))
20-
t.Logf("Format %s -> %s", "%c", Format(now, "%c"))
21-
t.Logf("Format %s -> %s", "%R", Format(now, "%R"))
22-
t.Logf("Format %s -> %s", "%T", Format(now, "%T"))
23-
t.Logf("Format %s -> %s", "%j", Format(now, "%j"))
100+
101+
for i := range tc {
102+
c := tc[i]
103+
t.Run(fmt.Sprintf("layout: %s", c.laylout), func(t *testing.T) {
104+
actual := Format(c.time, c.laylout)
105+
if actual != c.expected {
106+
t.Errorf("expected: %q; actual: %q", c.expected, actual)
107+
}
108+
})
109+
}
24110
}

0 commit comments

Comments
 (0)