|
| 1 | +// Copyright (c) 2018 Palantir Technologies. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package datetime_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + |
| 15 | + "github.com/palantir/pkg/datetime" |
| 16 | +) |
| 17 | + |
| 18 | +var dateTimeJSONs = []struct { |
| 19 | + sec int64 |
| 20 | + zoneOffset int |
| 21 | + str string |
| 22 | + json string |
| 23 | +}{ |
| 24 | + { |
| 25 | + sec: 1483326245, |
| 26 | + str: `2017-01-02T03:04:05Z`, |
| 27 | + json: `"2017-01-02T03:04:05Z"`, |
| 28 | + }, |
| 29 | + { |
| 30 | + sec: 1483326245, |
| 31 | + str: `2017-01-02T03:04:05Z`, |
| 32 | + json: `"2017-01-02T03:04:05.000Z"`, |
| 33 | + }, |
| 34 | + { |
| 35 | + sec: 1483326245, |
| 36 | + str: `2017-01-02T03:04:05Z`, |
| 37 | + json: `"2017-01-02T03:04:05.000000000Z"`, |
| 38 | + }, |
| 39 | + { |
| 40 | + sec: 1483326245, |
| 41 | + zoneOffset: 3600, |
| 42 | + str: `2017-01-02T04:04:05+01:00`, |
| 43 | + json: `"2017-01-02T04:04:05.000000000+01:00"`, |
| 44 | + }, |
| 45 | + { |
| 46 | + sec: 1483326245, |
| 47 | + zoneOffset: 7200, |
| 48 | + str: `2017-01-02T05:04:05+02:00`, |
| 49 | + json: `"2017-01-02T05:04:05.000000000+02:00"`, |
| 50 | + }, |
| 51 | + { |
| 52 | + sec: 1483326245, |
| 53 | + zoneOffset: 3600, |
| 54 | + str: `2017-01-02T04:04:05+01:00`, |
| 55 | + json: `"2017-01-02T04:04:05.000000000+01:00[Europe/Berlin]"`, |
| 56 | + }, |
| 57 | +} |
| 58 | + |
| 59 | +func TestDateTimeString(t *testing.T) { |
| 60 | + for i, currCase := range dateTimeJSONs { |
| 61 | + currDateTime := datetime.DateTime(time.Unix(currCase.sec, 0).In(time.FixedZone("", currCase.zoneOffset))) |
| 62 | + assert.Equal(t, currCase.str, currDateTime.String(), "Case %d", i) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestDateTimeMarshal(t *testing.T) { |
| 67 | + for i, currCase := range dateTimeJSONs { |
| 68 | + currDateTime := datetime.DateTime(time.Unix(currCase.sec, 0).In(time.FixedZone("", currCase.zoneOffset))) |
| 69 | + bytes, err := json.Marshal(currDateTime) |
| 70 | + require.NoError(t, err, "Case %d: marshal %q", i, currDateTime.String()) |
| 71 | + |
| 72 | + var unmarshaledFromMarshal datetime.DateTime |
| 73 | + err = json.Unmarshal(bytes, &unmarshaledFromMarshal) |
| 74 | + require.NoError(t, err, "Case %d: unmarshal %q", i, string(bytes)) |
| 75 | + |
| 76 | + var unmarshaledFromCase datetime.DateTime |
| 77 | + err = json.Unmarshal([]byte(currCase.json), &unmarshaledFromCase) |
| 78 | + require.NoError(t, err, "Case %d: unmarshal %q", i, currCase.json) |
| 79 | + |
| 80 | + assert.Equal(t, unmarshaledFromCase, unmarshaledFromMarshal, "Case %d", i) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func TestDateTimeUnmarshal(t *testing.T) { |
| 85 | + for i, currCase := range dateTimeJSONs { |
| 86 | + wantDateTime := time.Unix(currCase.sec, 0).UTC() |
| 87 | + if currCase.zoneOffset != 0 { |
| 88 | + wantDateTime = wantDateTime.In(time.FixedZone("", currCase.zoneOffset)) |
| 89 | + } |
| 90 | + |
| 91 | + var gotDateTime datetime.DateTime |
| 92 | + err := json.Unmarshal([]byte(currCase.json), &gotDateTime) |
| 93 | + require.NoError(t, err, "Case %d", i) |
| 94 | + |
| 95 | + assert.Equal(t, wantDateTime, time.Time(gotDateTime), "Case %d", i) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestDateTimeUnmarshalInvalid(t *testing.T) { |
| 100 | + for i, currCase := range []struct { |
| 101 | + input string |
| 102 | + wantErr string |
| 103 | + }{ |
| 104 | + { |
| 105 | + input: `"foo"`, |
| 106 | + wantErr: "parsing time \"foo\" as \"2006-01-02T15:04:05.999999999Z07:00\": cannot parse \"foo\" as \"2006\"", |
| 107 | + }, |
| 108 | + { |
| 109 | + input: `"2017-01-02T04:04:05.000000000+01:00[Europe/Berlin"`, |
| 110 | + wantErr: "parsing time \"2017-01-02T04:04:05.000000000+01:00[Europe/Berlin\": extra text: [Europe/Berlin", |
| 111 | + }, |
| 112 | + { |
| 113 | + input: `"2017-01-02T04:04:05.000000000+01:00[[Europe/Berlin]]"`, |
| 114 | + wantErr: "parsing time \"2017-01-02T04:04:05.000000000+01:00[\": extra text: [", |
| 115 | + }, |
| 116 | + } { |
| 117 | + var gotDateTime *datetime.DateTime |
| 118 | + err := json.Unmarshal([]byte(currCase.input), &gotDateTime) |
| 119 | + assert.EqualError(t, err, currCase.wantErr, "Case %d", i) |
| 120 | + } |
| 121 | +} |
0 commit comments