-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdate.go
More file actions
69 lines (59 loc) · 1.36 KB
/
date.go
File metadata and controls
69 lines (59 loc) · 1.36 KB
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
package edgar
import (
"encoding/json"
"strconv"
"strings"
"time"
)
// Timestamp is the edgar package representation of time.Time
type Timestamp time.Time
func (t Timestamp) String() string {
return time.Time(t).Format("2006-01-02")
}
// MarshalJSON marshals Timestamp in a specific format for JSON marsahlling
func (t Timestamp) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}
// UnmarshalJSON unmarshals Timestamp in a specific format for JSON unmarshal
func (t *Timestamp) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
*t = getDate(s)
return nil
}
func getYear(date string) int {
strs := strings.Split(date, "-")
if len(strs) != 3 {
return 0
}
year, _ := strconv.Atoi(strs[0])
return year
}
func getMonth(date string) int {
strs := strings.Split(date, "-")
if len(strs) != 3 {
return 0
}
year, _ := strconv.Atoi(strs[1])
return year
}
func getDay(date string) int {
strs := strings.Split(date, "-")
if len(strs) != 3 {
return 0
}
year, _ := strconv.Atoi(strs[2])
return year
}
func getDate(dateStr string) Timestamp {
year := getYear(dateStr)
month := getMonth(dateStr)
day := getDay(dateStr)
ts := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
return Timestamp(ts)
}
func getDateString(ts time.Time) string {
return ts.Format("2006-01-02")
}