File: time_format_test.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 1,280 kB
  • sloc: makefile: 16
file content (139 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package toolbox_test

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"github.com/viant/toolbox"
	"strings"
	"testing"
	"time"
)

func TestTimeFormat(t *testing.T) {

	{
		timeLayout := toolbox.DateFormatToLayout("yyyy/MM/dd/hh")
		fmt.Printf("%s\n", timeLayout)
	}

		{
		timeLayout := toolbox.DateFormatToLayout("yyyy-MM-dd HH:mm:ss z")
		timeValue, err := time.Parse(timeLayout, "2018-01-15 08:02:23 UTC")
		assert.Nil(t, err)
		assert.EqualValues(t, 23, timeValue.Second())
	}

	{
		timeLayout := toolbox.DateFormatToLayout("yyyy-MM-dd HH:mm:ss")
		timeValue, err := time.Parse(timeLayout, "2016-03-01 03:10:11")
		assert.Nil(t, err)
		assert.EqualValues(t, 11, timeValue.Second())
	}

	{

		dateLayout := toolbox.DateFormatToLayout("yyyy-MM-dd HH:mm:ss.SSSZ")
		timeValue, err := time.Parse(dateLayout, "2022-11-10 10:32:28.984-08")
		assert.Nil(t, err)

		assert.Equal(t, int64(1668105148), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("yyyy-MM-dd HH:mm:ss.SSSZ")
		timeValue, err := time.Parse(dateLaout, "2022-11-10 10:32:28.984-08")
		assert.Nil(t, err)
		assert.Equal(t, int64(1668105148), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("yyyy-MM-dd HH:mm:ss.SSS")
		timeValue, err := time.Parse(dateLaout, "2017-11-04 22:29:33.363")
		assert.Nil(t, err)

		assert.Equal(t, 2017, timeValue.Year())
		assert.Equal(t, time.Month(11), timeValue.Month())
		assert.Equal(t, 4, timeValue.Day())

		assert.Equal(t, int64(1509834573), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("dd/MM/yyyy hh:mm:ss")
		timeValue, err := time.Parse(dateLaout, "22/02/2016 12:32:01")
		assert.Nil(t, err)
		assert.Equal(t, int64(1456144321), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("yyyyMMdd hh:mm:ss")
		timeValue, err := time.Parse(dateLaout, "20160222 12:32:01")
		assert.Nil(t, err)
		assert.Equal(t, int64(1456144321), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("yyyy-MM-dd hh:mm:ss z")
		timeValue, err := time.Parse(dateLaout, "2016-02-22 12:32:01 UTC")
		assert.Nil(t, err)
		assert.Equal(t, int64(1456144321), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("yyyy-MM-dd hh:mm:ss z")
		timeValue, err := time.Parse(dateLaout, "2016-02-22 12:32:01 UTC")
		assert.Nil(t, err)
		assert.Equal(t, int64(1456144321), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("yyyy-MM-dd hh:mm:ss z")
		timeValue, err := time.Parse(dateLaout, "2016-02-22 12:32:01 UTC")
		assert.Nil(t, err)
		assert.Equal(t, int64(1456144321), timeValue.Unix())
	}

	{
		dateLaout := toolbox.DateFormatToLayout("yyyy-MM-dd HH:mm:ss z")
		timeValue, err := time.Parse(dateLaout, "2016-06-02 21:46:19 UTC")
		assert.Nil(t, err)
		assert.Equal(t, int64(1464903979), timeValue.Unix())
	}

}

func TestGetTimeLayout(t *testing.T) {
	{
		settings := map[string]string{
			toolbox.DateFormatKeyword: "yyyy-MM-dd HH:mm:ss z",
		}
		assert.Equal(t, "2006-01-02 15:04:05 MST", toolbox.GetTimeLayout(settings))
		assert.True(t, toolbox.HasTimeLayout(settings))
	}
	{
		settings := map[string]string{
			toolbox.DateLayoutKeyword: "2006-1-02 15:04:05 MST",
		}
		assert.Equal(t, "2006-1-02 15:04:05 MST", toolbox.GetTimeLayout(settings))
		assert.True(t, toolbox.HasTimeLayout(settings))
	}
	{
		settings := map[string]string{}
		assert.False(t, toolbox.HasTimeLayout(settings))

	}
}

func TestTimestampToString(t *testing.T) {

	{
		date := toolbox.TimestampToString("yyyy-MM-dd HH:mm:ss z", int64(0), 1480435743722684356)
		assert.True(t, strings.Contains(date, "2016-11"))
	}
	{

		date := toolbox.TimestampToString("yyyyMMddhh", int64(0), 1489512277722684356)
		assert.True(t, strings.Contains(date, "201703"))
	}

}