|
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. |
5 | 21 |
|
6 | 22 | package strftime
|
7 | 23 |
|
8 | 24 | import (
|
| 25 | + "fmt" |
9 | 26 | "testing"
|
10 | 27 | "time"
|
11 | 28 | )
|
12 | 29 |
|
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 | +) |
14 | 98 |
|
15 | 99 | 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 | + } |
24 | 110 | }
|
0 commit comments