本文整理汇总了Golang中Time.Time.UnmarshalText方法的典型用法代码示例。如果您正苦于以下问题:Golang Time.UnmarshalText方法的具体用法?Golang Time.UnmarshalText怎么用?Golang Time.UnmarshalText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Time.Time
的用法示例。
在下文中一共展示了Time.UnmarshalText方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handler_logdata_delete
func (ws *Webservice) handler_logdata_delete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
var counter nodedb.Counter
switch vars["id"] {
case "clients":
counter = &nodedb.CountMeshClients{}
case "nodes":
counter = &nodedb.CountMeshNodes{}
default:
counter = &nodedb.CountNodeClients{Node: vars["id"]}
}
var timestamp time.Time
err := timestamp.UnmarshalText([]byte(vars["timestamp"]))
if err != nil {
http.Error(w, "Bad Request", 400)
return
}
counter.SetTimestamp(timestamp)
err = ws.db.Logs.Update(func(tx *bolt.Tx) error {
return ws.db.Logs.Delete(tx, counter)
})
if err != nil {
http.Error(w, "Not Found", 404)
return
}
w.Header().Set("Content-type", "application/json")
ws.db.GenerateLogJSON(w, counter)
}
示例2: UnmarshalTime
func UnmarshalTime(data []byte) (time.Time, error) {
// example unix time: 1046509689.525204000
// example time.Time: 2003-03-01T09:08:09.525204Z
// first try RFC 3339 format
var t time.Time
err := t.UnmarshalText(data)
if err == nil {
return t, err
}
// next try Unix timestamp with and without ns precision
s := strings.Split(string(data[:]), ".")
if len(s) > 2 {
return time.Time{}, fmt.Errorf("not a valid timestamp")
}
sec, err := strconv.ParseInt(s[0], 10, 64)
var nsec int64 = 0
if len(s) == 2 {
nsec, err = strconv.ParseInt(s[1], 10, 64)
}
return time.Unix(sec, nsec), err
}
示例3: populateCache
func (store *fileStore) populateCache() (creationTimePopulated bool, err error) {
if tmpHeaderFile, err := os.Open(store.headerFname); err == nil {
defer tmpHeaderFile.Close()
for {
var seqNum, size int
var offset int64
if cnt, err := fmt.Fscanf(tmpHeaderFile, "%d,%d,%d\n", &seqNum, &offset, &size); err != nil || cnt != 3 {
break
}
store.offsets[seqNum] = msgDef{offset: offset, size: size}
}
}
if timeBytes, err := ioutil.ReadFile(store.sessionFname); err == nil {
var ctime time.Time
if err := ctime.UnmarshalText(timeBytes); err == nil {
store.cache.creationTime = ctime
creationTimePopulated = true
}
}
if senderSeqNumBytes, err := ioutil.ReadFile(store.senderSeqNumsFname); err == nil {
if senderSeqNum, err := strconv.Atoi(string(senderSeqNumBytes)); err == nil {
store.cache.SetNextSenderMsgSeqNum(senderSeqNum)
}
}
if targetSeqNumBytes, err := ioutil.ReadFile(store.targetSeqNumsFname); err == nil {
if targetSeqNum, err := strconv.Atoi(string(targetSeqNumBytes)); err == nil {
store.cache.SetNextTargetMsgSeqNum(targetSeqNum)
}
}
return creationTimePopulated, nil
}
示例4: UnmarshalText
// UnmarshalText unmarshals the data from a text format.
func (v *Time) UnmarshalText(text []byte) error {
var t time.Time
if err := t.UnmarshalText(text); err != nil {
// Try parsing without timezone since that is a valid format
if t, err = time.Parse("2006-01-02T15:04:05", string(text)); err != nil {
return err
}
}
v.Time = t
return nil
}
示例5: getLastUpdated
func (m *baseModel) getLastUpdated(id string, conn redis.Conn) (*time.Time, error) {
timeString, err := redis.String(conn.Do("HGET", m.idType+"s:updated", id))
if err != nil || timeString == "" {
return nil, err
}
t := time.Time{}
err = t.UnmarshalText([]byte(timeString))
return &t, err
}
示例6: getDefaultServer
func getDefaultServer() *Server {
f07, f08 := int16(8), float64(9)
f09, f10 := "ankara", []string{"tr", "en"}
f12 := 6 * time.Second
f12_0, f12_1, f12_2 := time.Millisecond, 2*time.Minute, 3*time.Hour
f15_f01_0, f15_f01_1 := uint16(10), uint16(20)
date := time.Time{}
if err := date.UnmarshalText([]byte("2015-11-05T08:15:30-05:00")); err != nil {
log.Fatalln(err)
}
return &Server{
Name: "koding",
Port: 6060,
Enabled: true,
ID: 1234567890,
Labels: []int{123, 456},
Users: []string{"ankara", "istanbul"},
Interval: 10 * time.Second,
Postgres: Postgres{
Enabled: true,
Port: 5432,
Hosts: []string{"192.168.2.1", "192.168.2.2", "192.168.2.3"},
DBName: "configdb",
AvailabilityRatio: 8.23,
},
F00: 1,
F01: int8(2),
F02: int16(3),
F03: int32(4),
F04: int64(5),
F05: uint32(6),
F06: float32(7.3),
F07: &f07,
F08: &f08,
F09: &f09,
F10: &f10,
F11: &f12,
F12: &[]*time.Duration{&f12_0, &f12_1, &f12_2},
F13: 12,
F14: date,
F15: CustomStruct{
F00: "turkey",
F01: []*uint16{&f15_f01_0, &f15_f01_1},
},
F16: &CustomStruct{
F00: "turkey",
F01: []*uint16{&f15_f01_0, &f15_f01_1},
},
}
}
示例7: Seen
func (a *ActivityLogger) Seen(u string) (time.Time, error) {
t := time.Time{}
s, err := a.store.GetVal("seen", u)
if err != nil {
log.Println("GetVal Error")
return t, err
}
if err := t.UnmarshalText([]byte(s)); err != nil {
return t, err
}
return t, nil
}
示例8: Time
// Time type asserts to `time.Time`
func (j *Json) Time(path ...interface{}) (time.Time, error) {
var t time.Time
tmp, err := j.Get(path...)
if err != nil {
return t, err
}
if t, ok := tmp.data.(time.Time); ok {
return t, nil
} else if tStr, ok := tmp.data.(string); ok {
if t.UnmarshalText([]byte(tStr)) == nil {
return t, nil
}
}
return t, errors.New("type assertion/unmarshalling to time.Time failed")
}
示例9: handleList
func handleList(ctx *cli.Context, client *daemon.Client) error {
path := "/"
if ctx.NArg() > 0 {
path = prefixSlash(ctx.Args().First())
}
depth := ctx.Int("depth")
if ctx.Bool("recursive") {
depth = -1
}
entries, err := client.List(path, depth)
if err != nil {
return ExitCode{
UnknownError,
fmt.Sprintf("ls: %v", err),
}
}
for _, entry := range entries {
modTime := time.Time{}
if err := modTime.UnmarshalText(entry.ModTime); err != nil {
log.Warningf("Could not parse mtime (%s): %v", entry.ModTime, err)
continue
}
fmt.Printf(
"%s\t%s\t%s\n",
colors.Colorize(
humanize.Bytes(uint64(entry.NodeSize)),
colors.Green,
),
colors.Colorize(
humanize.Time(modTime),
colors.Cyan,
),
colors.Colorize(
entry.Path,
colors.Magenta,
),
)
}
return nil
}
示例10: Time
// Time extracts duration expressed as in RFC 3339 format
func Time(name string, out *time.Time, predicates ...Predicate) Param {
return func(r *http.Request) error {
for _, p := range predicates {
if err := p.Pass(name, r); err != nil {
return err
}
}
v := r.Form.Get(name)
if v == "" {
return nil
}
var t time.Time
if err := t.UnmarshalText([]byte(v)); err != nil {
return &BadParameterError{Param: name, Message: err.Error()}
}
*out = t
return nil
}
}
示例11: TimeT
func (q *Queryable) TimeT() (time.Time, error) {
fvv := fv(q.Raw)
switch fvv.Kind() {
case reflect.String:
t := time.Time{}
err := t.UnmarshalText([]byte(fvv.String()))
return t, err
case reflect.Uint:
return time.Unix(int64(fvv.Uint()), 0), nil
case reflect.Int:
return time.Unix(fvv.Int(), 0), nil
case reflect.Struct:
t, ok := q.Raw.(time.Time)
if ok {
return t, nil
}
}
return time.Time{}, ne("not a valid time object " + fvv.Kind().String())
}
示例12: initLastUpdated
func (i *IDB) initLastUpdated(m Model) error {
if !i.lastUpdated.IsZero() {
return nil
}
ch := make(chan error)
db := i.db
t := m.Type()
tr := db.Call("transaction", t.Name(), "readonly")
s := tr.Call("objectStore", t.Name())
idx := s.Call("index", lastUpdatedIndex)
req := idx.Call("openCursor", nil, "prev")
req.Set("onsuccess", func(e js.Object) {
maxLastUpdated := time.Time{}
cursor := e.Get("target").Get("result")
if !cursor.IsNull() {
value := cursor.Get("value")
l := value.Get("Meta").Get("LastUpdated").Str()
if err := maxLastUpdated.UnmarshalText([]byte(l)); err != nil {
go func() {
ch <- err
close(ch)
}()
return
}
if i.lastUpdated.Before(maxLastUpdated) {
i.lastUpdated = maxLastUpdated
}
}
close(ch)
})
req.Set("onerror", func(e js.Object) {
go func() {
ch <- toError(e.Get("target"))
close(ch)
}()
})
return <-ch
}
示例13: getSyncManifest
func (m *baseModel) getSyncManifest(conn redis.Conn) (*SyncManifest, error) {
var manifest SyncManifest = make(map[string]int64)
item, err := redis.Strings(conn.Do("HGETALL", m.idType+"s:updated"))
for i := 0; i < len(item); i += 2 {
t := time.Time{}
err := t.UnmarshalText([]byte(item[i+1]))
if err != nil {
return nil, err
}
manifest[item[i]] = t.UnixNano() / int64(time.Millisecond)
}
if err != nil {
return nil, err
}
return &manifest, nil
}
示例14: attrFromFile
func (sc *serveConn) attrFromFile(file drive_db.File) fuse.Attr {
var atime, mtime, crtime time.Time
if err := atime.UnmarshalText([]byte(file.LastViewedByMeDate)); err != nil {
atime = startup
}
if err := mtime.UnmarshalText([]byte(file.ModifiedDate)); err != nil {
mtime = startup
}
if err := crtime.UnmarshalText([]byte(file.CreatedDate)); err != nil {
crtime = startup
}
blocks := file.FileSize / int64(blockSize)
if r := file.FileSize % int64(blockSize); r > 0 {
blocks += 1
}
attr := fuse.Attr{
Valid: *driveMetadataLatency,
Inode: file.Inode,
Atime: atime,
Mtime: mtime,
Ctime: mtime,
Crtime: crtime,
Uid: sc.uid,
Gid: sc.gid,
Mode: 0755,
Size: uint64(file.FileSize),
Blocks: uint64(blocks),
}
if file.MimeType == driveFolderMimeType {
attr.Mode = os.ModeDir | 0755
}
return attr
}
示例15: decodeRecordData
//.........这里部分代码省略.........
}
fields[key] = OrganizationField(ol)
case FT_GROUP:
gl, err := groupList(v.Value.([]interface{}))
if err != nil {
return nil, err
}
fields[key] = GroupField(gl)
case FT_CATEGORY:
fields[key] = CategoryField(stringList(v.Value.([]interface{})))
case FT_STATUS:
fields[key] = StatusField(v.Value.(string))
case FT_ASSIGNEE:
al, err := userList(v.Value.([]interface{}))
if err != nil {
return nil, err
}
fields[key] = AssigneeField(al)
case FT_RECNUM:
if nid, err := numericId(v.Value.(string)); err != nil {
return nil, err
} else {
rec.id = nid
}
fields[key] = RecordNumberField(v.Value.(string))
case FT_CREATOR:
creator := v.Value.(map[string]interface{})
fields[key] = CreatorField{
creator["code"].(string),
creator["name"].(string),
}
case FT_CTIME:
var ctime time.Time
if ctime.UnmarshalText([]byte(v.Value.(string))) != nil {
return nil, fmt.Errorf("Invalid datetime: %v", v.Value)
}
fields[key] = CreationTimeField(ctime)
case FT_MODIFIER:
modifier := v.Value.(map[string]interface{})
fields[key] = ModifierField{
modifier["code"].(string),
modifier["name"].(string),
}
case FT_MTIME:
var mtime time.Time
if mtime.UnmarshalText([]byte(v.Value.(string))) != nil {
return nil, fmt.Errorf("Invalid datetime: %v", v.Value)
}
fields[key] = CreationTimeField(mtime)
case FT_SUBTABLE:
b2, err := json.Marshal(v.Value)
if err != nil {
return nil, err
}
var stl []SubTableEntry
err = json.Unmarshal(b2, &stl)
if err != nil {
return nil, err
}
ra := make([]*Record, len(stl))
for i, sr := range stl {
b3, err := json.Marshal(sr.Value)
if err != nil {
return nil, err
}
var rd recordData