offline-twitter/scraper/timestamp_type.go
2022-03-06 18:17:00 -08:00

41 lines
741 B
Go

package scraper
import (
"time"
"fmt"
"database/sql/driver"
)
type Timestamp struct {
time.Time
}
func (t Timestamp) Value() (driver.Value, error) {
return t.Unix(), nil
}
func (t *Timestamp) Scan(src interface{}) error {
val, is_ok := src.(int64)
if !is_ok {
return fmt.Errorf("Incompatible type for Timestamp: %#v", src)
}
*t = Timestamp{time.Unix(val, 0)}
return nil
}
func TimestampFromString(s string) (Timestamp, error) {
tmp, err := time.Parse(time.RubyDate, s)
if err == nil {
return Timestamp{tmp}, nil
}
tmp, err = time.Parse(time.RFC3339, s)
if err == nil {
return Timestamp{tmp}, nil
}
return Timestamp{}, err
}
func TimestampFromUnix(num int64) Timestamp {
return Timestamp{time.Unix(num, 0)}
}