github.com/webx-top/com@v1.2.12/encoding/json/encoding.go (about)

     1  package json
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  )
     7  
     8  type UnixTime time.Time
     9  
    10  func (u UnixTime) MarshalJSON() ([]byte, error) {
    11  	seconds := time.Time(u).Unix()
    12  	return []byte(strconv.FormatInt(seconds, 10)), nil
    13  }
    14  
    15  type DateTime time.Time
    16  
    17  func (u DateTime) MarshalJSON() ([]byte, error) {
    18  	t := time.Time(u)
    19  	if t.IsZero() {
    20  		return nil, nil
    21  	}
    22  	return []byte(t.Format(`2006-01-02 15:04:05`)), nil
    23  }