package connect import ( "bytes" "fmt" "io" "io/ioutil" "net/http" "time" ) // date formats a time.Time as a date usable in the Garmin Connect API. func formatDate(t time.Time) string { return fmt.Sprintf("%04d-%02d-%02d", t.Year(), t.Month(), t.Day()) } // drainBody reads all of b to memory and then returns two equivalent // ReadClosers yielding the same bytes. // // It returns an error if the initial slurp of all bytes fails. It does not attempt // to make the returned ReadClosers have identical error-matching behavior. // // Liberated from net/http/httputil/dump.go. func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) { if b == http.NoBody { // No copying needed. Preserve the magic sentinel meaning of NoBody. return http.NoBody, http.NoBody, nil } var buf bytes.Buffer if _, err = buf.ReadFrom(b); err != nil { return nil, b, err } if err = b.Close(); err != nil { return nil, b, err } return ioutil.NopCloser(&buf), ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil }