github.com/unirita/cuto@v0.9.8-0.20160830082821-aa6652f877b7/utctime/utctime.go (about) 1 package utctime 2 3 import "time" 4 5 const ( 6 Default = "2006-01-02 15:04:05.000" 7 NoDelimiter = "20060102150405.000" 8 Date8Num = "20060102" 9 ) 10 11 type UTCTime struct { 12 tm time.Time 13 } 14 15 // Now creates UTCTime object with current UTC time. 16 func Now() *UTCTime { 17 u := new(UTCTime) 18 u.tm = time.Now().UTC() 19 return u 20 } 21 22 // Parse parses value as UTC. 23 // The layout defines the format by showing how the reference time. 24 // 25 // If you need more information of layout, look document for time.Time#Parse. 26 func Parse(layout, value string) (UTCTime, error) { 27 t, err := time.ParseInLocation(layout, value, time.UTC) 28 if err != nil { 29 return UTCTime{}, err 30 } 31 return UTCTime{tm: t.UTC()}, nil 32 } 33 34 // ParseLocaltime parses value as localtime. 35 func ParseLocaltime(layout, value string) (UTCTime, error) { 36 t, err := time.ParseInLocation(layout, value, time.Local) 37 if err != nil { 38 return UTCTime{}, err 39 } 40 return UTCTime{tm: t.UTC()}, nil 41 } 42 43 // String returns the time formatted using the Default layout. 44 func (u UTCTime) String() string { 45 return u.Format(Default) 46 } 47 48 // Format returns a textual representation of the utc time value formatted according to layout. 49 func (u UTCTime) Format(layout string) string { 50 return u.tm.Format(layout) 51 } 52 53 // FormatLocaltime returns a textual representation of the local time value formatted according to layout. 54 func (u UTCTime) FormatLocaltime(layout string) string { 55 return u.tm.Local().Format(layout) 56 } 57 58 // AddDays returns UTCTime that has tm after days from u.tm 59 func (u UTCTime) AddDays(days int) UTCTime { 60 return UTCTime{tm: u.tm.Add(time.Duration(days) * 24 * time.Hour)} 61 }