timeutils: github.com/simplereach/timeutils Index | Examples | Files

package timeutils

import "github.com/simplereach/timeutils"

Index

Examples

Package Files

parse.go time.go

Variables

var (
    ErrInvalidFormat = errors.New("invalid format")
)

Common errors.

func ParseDateString Uses

func ParseDateString(dt string) (time.Time, error)

ParseDateString takes a string and passes it through Approxidate Parses into a time.Time

func ParseMillis Uses

func ParseMillis(dt int64) (time.Time, error)

ParseMillis parses a milliseconds-since-epoch time stamp to a time.Time

type Format Uses

type Format int32

Format enum type.

const (
    Timestamp Format = iota
    TimestampNano
    ANSIC
    UnixDate
    RubyDate
    RFC822
    RFC822Z
    RFC850
    RFC1123
    RFC1123Z
    RFC3339
    RFC3339Nano
    Kitchen
)

Format enum values.

type Time Uses

type Time struct {
    time.Time `bson:",inline"`
    // contains filtered or unexported fields
}

Time wraps time.Time overriddin the json marshal/unmarshal to pass timestamp as integer

Code:

defer os.Setenv("TZ", os.Getenv("TZ"))
os.Setenv("TZ", "UTC")

var t timeutils.Time
_ = json.Unmarshal([]byte(`1438883568`), &t)
fmt.Println(t.UTC())
t = timeutils.Time{}
_ = json.Unmarshal([]byte(`1438883568790859087`), &t)
fmt.Println(t.UTC())
t = timeutils.Time{}
_ = json.Unmarshal([]byte(`"2015-08-06T17:52:48Z"`), &t)
fmt.Println(t.UTC())
t = timeutils.Time{}
_ = json.Unmarshal([]byte(`"09:51:20.939152pm 2014-31-12"`), &t)
fmt.Println(t.UTC())

Output:

2015-08-06 17:52:48 +0000 UTC
2015-08-06 17:52:48.790859087 +0000 UTC
2015-08-06 17:52:48 +0000 UTC
2014-12-31 21:51:20.939152 +0000 UTC

func NewTime Uses

func NewTime(t time.Time, format Format) Time

NewTime create a new Time object with the given format.

Code:

defer os.Setenv("TZ", os.Getenv("TZ"))
os.Setenv("TZ", "UTC")

std := time.Unix(1438883568, 790859087)
enc := json.NewEncoder(os.Stdout)
_ = enc.Encode(timeutils.NewTime(std, timeutils.TimestampNano))
_ = enc.Encode(timeutils.NewTime(std, timeutils.Timestamp))
_ = enc.Encode(timeutils.NewTime(std, timeutils.RFC3339))
_ = enc.Encode(timeutils.NewTime(std, timeutils.RFC3339Nano))

Output:

1438883568790859087
1438883568
"2015-08-06T17:52:48Z"
"2015-08-06T17:52:48.790859087Z"

func (Time) FormatMode Uses

func (t Time) FormatMode(format Format) Time

FormatMode create a copy of the time object and sets the format method to be used by the marhsal functions.

func (Time) GetBSON Uses

func (t Time) GetBSON() (interface{}, error)

GetBSON implements mgo/bson.Getter interface.

func (Time) MarshalJSON Uses

func (t Time) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler interface.

func (*Time) SetBSON Uses

func (t *Time) SetBSON(raw bson.Raw) error

SetBSON implements mgo/bson.Setter interface.

func (*Time) UnmarshalJSON Uses

func (t *Time) UnmarshalJSON(buf []byte) error

UnmarshalJSON implements json.Unmarshaler inferface.

Package timeutils imports 9 packages (graph) and is imported by 1 packages. Updated 2017-04-28. Refresh now. Tools for package owners.