-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDate.go
58 lines (51 loc) · 1.61 KB
/
Date.go
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
package avtoto
import (
"strings"
"time"
)
type Date struct {
timeDateValue time.Time
}
// UnmarshalJSON is used to convert the timestamp from JSON
func (t *Date) UnmarshalJSON(s []byte) error {
var errorTime error
stringTime := strings.ReplaceAll(string(s), "\"", "")
if len(stringTime) > 10 { // 04.01.2023 16:55
t.timeDateValue, errorTime = time.Parse("02.01.2006 15:04", stringTime)
} else if len(stringTime) == 6 { // 05\/01
t.timeDateValue, errorTime = time.Parse("02\\/01", stringTime)
} else {
t.timeDateValue, errorTime = time.Parse("02.01.2006", stringTime)
}
return errorTime
}
// MarshalJSON is used to convert the timestamp to JSON
func (t Date) MarshalJSON() ([]byte, error) {
return []byte(t.timeDateValue.Format("02.01.2006")), nil
}
// Вернуть дату в формате string в одной строке
func (t Date) String() string {
return t.Day() + "." + t.Month() + "." + t.Year() + " " + t.Hour() + ":" + t.Minute() // Время
}
// Вернуть дату в формате string по частям
func (t Date) Strings() (string, string, string) {
return t.Day() + "." + t.Month(), // День и месяц
t.Year(), // Год
t.Hour() + ":" + t.Minute() // Время
}
// Составные части даты и времени
func (t Date) Day() string {
return t.timeDateValue.Format("02")
}
func (t Date) Month() string {
return t.timeDateValue.Format("01")
}
func (t Date) Year() string {
return t.timeDateValue.Format("2006")
}
func (t Date) Hour() string {
return t.timeDateValue.Format("15")
}
func (t Date) Minute() string {
return t.timeDateValue.Format("04")
}