From 6fec55a384b1687ac630122c5f89df65c55d0eb0 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 13 May 2012 13:27:11 -0700 Subject: [PATCH] simplify legacy time code Change-Id: I643b722adddaa3bfecab6044de2c5a373e5d7dd1 --- pkg/index/index.go | 12 +++++------ pkg/schema/schema.go | 43 ++++++++++----------------------------- pkg/schema/schema_test.go | 19 +++++++++++++++++ 3 files changed, 36 insertions(+), 38 deletions(-) diff --git a/pkg/index/index.go b/pkg/index/index.go index 586a9e43e..34bf10504 100644 --- a/pkg/index/index.go +++ b/pkg/index/index.go @@ -26,7 +26,6 @@ import ( "camlistore.org/pkg/blobref" "camlistore.org/pkg/blobserver" - "camlistore.org/pkg/schema" "camlistore.org/pkg/search" ) @@ -208,9 +207,10 @@ func (x *Index) GetRecentPermanodes(dest chan *search.Result, owner *blobref.Blo if len(parts) != 4 { continue } - mTime := unreverseTimeString(parts[2]) - mTimeNs := schema.NanosFromRFC3339(mTime) - mTimeSec := mTimeNs / 1e9 + var mTimeSec int64 + if mTime, err := time.Parse(time.RFC3339, unreverseTimeString(parts[2])); err == nil { + mTimeSec = mTime.Unix() + } permaRef := blobref.Parse(permaStr) if permaRef == nil { continue @@ -253,12 +253,12 @@ func (x *Index) GetOwnerClaims(permaNode, owner *blobref.BlobRef) (cl search.Cla if claimRef == nil { continue } - nanos := schema.NanosFromRFC3339(keyPart[3]) + date, _ := time.Parse(time.RFC3339, keyPart[3]) cl = append(cl, &search.Claim{ BlobRef: claimRef, Signer: owner, Permanode: permaNode, - Date: time.Unix(0, nanos).UTC(), + Date: date, Type: urld(valPart[0]), Attr: urld(valPart[1]), Value: urld(valPart[2]), diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 077b54428..738579316 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -29,7 +29,6 @@ import ( "os" "path/filepath" "strconv" - "strings" "sync" "time" "unicode/utf8" @@ -320,11 +319,11 @@ func (ss *Superset) ModTime() time.Time { if ss.UnixMtime == "" { return time.Time{} } - nanos := NanosFromRFC3339(ss.UnixMtime) - if nanos == -1 { + t, err := time.Parse(time.RFC3339, ss.UnixMtime) + if err != nil { return time.Time{} } - return time.Unix(0, nanos) + return t } var DefaultStatHasher = &defaultStatHasher{} @@ -540,40 +539,20 @@ func NewDelAttributeClaim(permaNode *blobref.BlobRef, attr string) map[string]in // Types of ShareRefs const ShareHaveRef = "haveref" +// RFC3339FromTime returns an RFC3339-formatted time in UTC. +// Fractional seconds are only included if the time has fractional +// seconds. func RFC3339FromTime(t time.Time) string { - // TODO-GO1: this is now needlessly complex after the gofix - // and signature change. - epochnanos := t.UnixNano() - nanos := epochnanos % 1e9 - esec := epochnanos / 1e9 - t = time.Unix(esec, 0).UTC() - timeStr := t.Format(time.RFC3339) - if nanos == 0 { - return timeStr + if t.UnixNano() % 1e9 == 0 { + return t.UTC().Format(time.RFC3339) } - nanoStr := fmt.Sprintf("%09d", nanos) - nanoStr = strings.TrimRight(nanoStr, "0") - return timeStr[:len(timeStr)-1] + "." + nanoStr + "Z" + return t.UTC().Format(time.RFC3339Nano) } func NanosFromRFC3339(timestr string) int64 { - dotpos := strings.Index(timestr, ".") - simple3339 := timestr - nanostr := "" - if dotpos != -1 { - if !strings.HasSuffix(timestr, "Z") { - return -1 - } - simple3339 = timestr[:dotpos] + "Z" - nanostr = timestr[dotpos+1 : len(timestr)-1] - if needDigits := 9 - len(nanostr); needDigits > 0 { - nanostr = nanostr + "000000000"[:needDigits] - } - } - t, err := time.Parse(time.RFC3339, simple3339) + t, err := time.Parse(time.RFC3339, timestr) if err != nil { return -1 } - nanos, _ := strconv.ParseInt(nanostr, 10, 64) - return t.Unix()*1e9 + nanos + return t.UnixNano() } diff --git a/pkg/schema/schema_test.go b/pkg/schema/schema_test.go index 320b2654a..37120ad00 100644 --- a/pkg/schema/schema_test.go +++ b/pkg/schema/schema_test.go @@ -21,6 +21,7 @@ import ( "os" "strings" "testing" + "time" . "camlistore.org/pkg/test/asserts" ) @@ -90,3 +91,21 @@ func TestStringFromMixedArray(t *testing.T) { } } } + +func TestRFC3339(t *testing.T) { + tests := []string{ + "2012-05-13T15:02:47Z", + "2012-05-13T15:02:47.1234Z", + "2012-05-13T15:02:47.123456789Z", + } + for _, in := range tests { + tm, err := time.Parse(time.RFC3339, in) + if err != nil { + t.Errorf("error parsing %q", in) + continue + } + if out := RFC3339FromTime(tm); in != out { + t.Errorf("RFC3339FromTime(%q) = %q; want %q", in, out, in) + } + } +} \ No newline at end of file