You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
930 B

2 years ago
package formatime
import (
"fmt"
"time"
)
// Date is a nullable time.Time. It supports SQL, JSON serialization AND datetime format(2006-01-02).
// It will marshal to null if null.
type Date struct {
protocol
}
const _DateFormat = "2006-01-02"
// new Date format of time, s is timestamp
func NewDate(s int64) Date {
return Date{protocol{
Time: time.Unix(s, 0),
Valid: true,
}}
}
// new Date format of time right now
func NewDateNow() Date {
return Date{protocol{
Time: time.Now(),
Valid: true,
}}
}
// new Date format of time from time.Time
func NewDateFrom(t time.Time) Date {
return Date{protocol{
Time: t,
Valid: true,
}}
}
// MarshalJSON implements json.Marshaler.
// It will encode null if this time is null.
func (t Date) MarshalJSON() ([]byte, error) {
if !t.Valid {
return []byte("null"), nil
}
var stamp = fmt.Sprintf("\"%s\"", t.Time.Format(_DateFormat))
return []byte(stamp), nil
}