Skip to content

Commit

Permalink
date fmt pass test.
Browse files Browse the repository at this point in the history
  • Loading branch information
wedojava committed Sep 29, 2020
1 parent bb45c07 commit 4178c4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions internal/fetcher/sites/cna/cna.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -53,8 +54,25 @@ func setDate(p *Post) error {
if len(cs) <= 0 {
return fmt.Errorf("SetData got nothing.")
}
// TODO: date format's needed.
p.Date = cs[0]
tY := cs[0][:4]
tM := cs[0][5:7]
tD := cs[0][7:9]
tH := cs[0][11:13]
tm := cs[0][14:16]
yy, err := strconv.Atoi(tY)
mm, err := strconv.Atoi(tM)
dd, err := strconv.Atoi(tD)
h, err := strconv.Atoi(tH)
m, err := strconv.Atoi(tm)
if err != nil {
return err
}
// China doesn't have daylight saving. It uses a fixed 8 hour offset from UTC.
secondsEastOfUTC := int((8 * time.Hour).Seconds())
beijing := time.FixedZone("Beijing Time", secondsEastOfUTC)
t := time.Date(yy, time.Month(mm), dd, h, m, 0, 0, beijing)
p.Date = t.Format(time.RFC3339)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/fetcher/sites/cna/cna_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSetDate(t *testing.T) {
if err := setDate(p); err != nil {
t.Errorf("test SetPost err: %v", doc)
}
want := "2020-09-01T12:14:01+08:00"
want := "2020-08-31T11:49:00+08:00"
if p.Date != want {
t.Errorf("\ngot: %v\nwant: %v", p.Date, want)
}
Expand Down

0 comments on commit 4178c4b

Please # to comment.