github.com/hungdoo/bot@v0.0.0-20240325145135-dd1f386f7b81/src/packages/bybitapi/time.go (about)

     1  package bybitapi
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  )
     7  
     8  // UnixTime is a custom type that embeds time.Time
     9  type UnixTime struct {
    10  	time.Time
    11  }
    12  
    13  // UnmarshalJSON satisfies the Unmarshaler interface for UnixTime
    14  func (ut *UnixTime) UnmarshalJSON(b []byte) error {
    15  	var timestamp int64
    16  	err := json.Unmarshal(b, &timestamp)
    17  	if err != nil {
    18  		return err
    19  	}
    20  	ut.Time = time.UnixMilli(timestamp)
    21  	return nil
    22  }
    23  
    24  // MarshalJSON satisfies the Marshaler interface for UnixTime
    25  func (ut UnixTime) MarshalJSON() ([]byte, error) {
    26  	return json.Marshal(ut.Time.Format(time.RFC1123Z))
    27  }