本文整理汇总了Golang中net/http.ParseTime函数的典型用法代码示例。如果您正苦于以下问题:Golang ParseTime函数的具体用法?Golang ParseTime怎么用?Golang ParseTime使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ParseTime函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: open
func (h *httpFSHandler) open(w http.ResponseWriter, r *http.Request) error {
fi, err := h.fs.Stat(r.URL.Path)
if err != nil {
return err
}
notMod := false
if ifModSince, err := http.ParseTime(r.Header.Get("if-modified-since")); err == nil {
if !fi.ModTime().IsZero() && fi.ModTime().Before(ifModSince) {
w.WriteHeader(http.StatusNotModified)
notMod = true
}
}
writeFileInfoHeaders(w, fi, true)
if notMod {
return nil
}
f, err := h.fs.Open(r.URL.Path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(w, f)
return err
}
示例2: Expires
func (r *Resource) Expires() (time.Time, error) {
if expires := r.header.Get("Expires"); expires != "" {
return http.ParseTime(expires)
}
return time.Time{}, nil
}
示例3: MaxAge
func (r *Resource) MaxAge(shared bool) (time.Duration, error) {
cc, err := r.cacheControl()
if err != nil {
return time.Duration(0), err
}
if cc.Has("s-maxage") && shared {
if maxAge, err := cc.Duration("s-maxage"); err != nil {
return time.Duration(0), err
} else if maxAge > 0 {
return maxAge, nil
}
}
if cc.Has("max-age") {
if maxAge, err := cc.Duration("max-age"); err != nil {
return time.Duration(0), err
} else if maxAge > 0 {
return maxAge, nil
}
}
if expiresVal := r.header.Get("Expires"); expiresVal != "" {
expires, err := http.ParseTime(expiresVal)
if err != nil {
return time.Duration(0), err
}
return expires.Sub(Clock()), nil
}
return time.Duration(0), nil
}
示例4: storeRateLimit
func (h *Host) storeRateLimit(call string, headers http.Header) {
// check for Bucket header
var err error
l, err := strconv.ParseInt(headers.Get("x-ratelimit-limit"), 10, 32)
if err != nil {
debugLogger.Println("Error parsing rate limit:", err)
}
u, err := strconv.ParseInt(headers.Get("x-ratelimit-used"), 10, 32)
if err != nil {
debugLogger.Println("Error parsing rate limit used:", err)
}
r, err := strconv.ParseInt(headers.Get("x-ratelimit-remaining"), 10, 32)
if err != nil {
debugLogger.Println("Error parsing rate limit remaining:", err)
}
debugLogger.Println(headers.Get("Date"))
lc, err := http.ParseTime(headers.Get("Date"))
if err != nil {
debugLogger.Println("Error parsing date header:", err)
}
info := RateLimitInfo{Bucket: headers.Get("x-ratelimit-bucket"),
Used: int(u), Limit: int(l), Remaining: int(r), LastCall: lc, Drift: time.Now().Sub(lc)}
debugLogger.Println("storing rate limit info for", call, "info", info)
h.rateLimitLock.Lock()
defer h.rateLimitLock.Unlock()
if info.LastCall.After(h.rateLimits[info.Bucket].LastCall) || info.LastCall == h.rateLimits[info.Bucket].LastCall {
h.callToBucket[call] = info.Bucket
h.rateLimits[info.Bucket] = info
}
}
示例5: checkServerTime
// Check the server time is the same as ours, once for each server
func checkServerTime(req *http.Request, resp *http.Response) {
host := req.URL.Host
if req.Host != "" {
host = req.Host
}
checkedHostMu.RLock()
_, ok := checkedHost[host]
checkedHostMu.RUnlock()
if ok {
return
}
dateString := resp.Header.Get("Date")
if dateString == "" {
return
}
date, err := http.ParseTime(dateString)
if err != nil {
Debug(nil, "Couldn't parse Date: from server %s: %q: %v", host, dateString, err)
return
}
dt := time.Since(date)
const window = 5 * 60 * time.Second
if dt > window || dt < -window {
Log(nil, "Time may be set wrong - time from %q is %v different from this computer", host, dt)
}
checkedHostMu.Lock()
checkedHost[host] = struct{}{}
checkedHostMu.Unlock()
}
示例6: parseSearchResponse
func parseSearchResponse(httpResponse io.Reader, responseAddr *net.UDPAddr) (*SearchResponse, error) {
reader := bufio.NewReader(httpResponse)
request := &http.Request{} // Needed for ReadResponse but doesn't have to be real
response, err := http.ReadResponse(reader, request)
if err != nil {
return nil, err
}
headers := response.Header
res := &SearchResponse{}
res.Control = headers.Get("cache-control")
res.Server = headers.Get("server")
res.ST = headers.Get("st")
res.Ext = headers.Get("ext")
res.USN = headers.Get("usn")
res.ResponseAddr = responseAddr
if headers.Get("location") != "" {
res.Location, err = response.Location()
if err != nil {
return nil, err
}
}
date := headers.Get("date")
if date != "" {
res.Date, err = http.ParseTime(date)
if err != nil {
return nil, err
}
}
return res, nil
}
示例7: parse_time2
func parse_time2(txt string) time.Time {
t, err := http.ParseTime(txt)
if err == nil {
t = time.Now()
}
return t
}
示例8: timeHeader
func timeHeader(key string, h http.Header) (time.Time, error) {
if header := h.Get(key); header != "" {
return http.ParseTime(header)
} else {
return time.Time{}, errNoHeader
}
}
示例9: retryAfter
func retryAfter(v string) time.Duration {
if i, err := strconv.Atoi(v); err == nil {
return time.Duration(i) * time.Second
}
if t, err := http.ParseTime(v); err == nil {
return t.Sub(time.Now())
}
return time.Second
}
示例10: downloadJar
func (self *JenkinsClientDownloader) downloadJar(config *util.Config) error {
util.GOut("DOWNLOAD", "Getting latest Jenkins client %v", (config.CIHostURI + "/" + ClientJarURL))
// Create the HTTP request.
request, err := config.CIRequest("GET", ClientJarURL, nil)
if err != nil {
return err
}
if fi, err := os.Stat(ClientJarName); err == nil {
request.Header.Add("If-Modified-Since", fi.ModTime().Format(http.TimeFormat))
}
// Perform the HTTP request.
var source io.ReadCloser
sourceTime := time.Now()
if response, err := config.CIClient().Do(request); err == nil {
defer response.Body.Close()
source = response.Body
if response.StatusCode == 304 {
util.GOut("DOWNLOAD", "Jenkins client is up-to-date, no need to download.")
return nil
} else if response.StatusCode != 200 {
return fmt.Errorf("Failed downloading jenkins client. Cause: HTTP-%v %v", response.StatusCode, response.Status)
}
if value := response.Header.Get("Last-Modified"); value != "" {
if time, err := http.ParseTime(value); err == nil {
sourceTime = time
}
}
} else {
return fmt.Errorf("Failed downloading jenkins client. Connect failed. Cause: %v", err)
}
target, err := os.Create(ClientJarDownloadName)
defer target.Close()
if err != nil {
return fmt.Errorf("Failed downloading jenkins client. Cannot create local file. Cause: %v", err)
}
if _, err = io.Copy(target, source); err == nil {
target.Close()
if err = os.Remove(ClientJarName); err == nil || os.IsNotExist(err) {
if err = os.Rename(ClientJarDownloadName, ClientJarName); err == nil {
os.Chtimes(ClientJarName, sourceTime, sourceTime)
}
}
return err
} else {
return fmt.Errorf("Failed downloading jenkins client. Transfer failed. Cause: %v", err)
}
}
示例11: DateAfter
func (r *Resource) DateAfter(d time.Time) bool {
if dateHeader := r.header.Get("Date"); dateHeader != "" {
if t, err := http.ParseTime(dateHeader); err != nil {
return false
} else {
return t.After(d)
}
}
return false
}
示例12: retryAfter
func retryAfter(v string) (time.Duration, error) {
if i, err := strconv.Atoi(v); err == nil {
return time.Duration(i) * time.Second, nil
}
t, err := http.ParseTime(v)
if err != nil {
return 0, err
}
return t.Sub(timeNow()), nil
}
示例13: LastModified
func (r *Resource) LastModified() time.Time {
var modTime time.Time
if lastModHeader := r.header.Get("Last-Modified"); lastModHeader != "" {
if t, err := http.ParseTime(lastModHeader); err == nil {
modTime = t
}
}
return modTime
}
示例14: GetLaterTimeStr
// return later time
func GetLaterTimeStr(a, b string) (result string, err error) {
timeA, err := http.ParseTime(a)
if nil != err {
log.Printf("[ERROR] failed to parse string %s as http time", a)
return
}
timeB, err := http.ParseTime(b)
if nil != err {
log.Printf("[ERROR] failed to parse string %s as http time", b)
return
}
if timeA.After(timeB) {
result = a
} else {
result = b
}
return
}
示例15: checkETag
// checkETag implements If-None-Match and If-Range checks.
//
// The ETag or modtime must have been previously set in the
// ResponseWriter's headers. The modtime is only compared at second
// granularity and may be the zero value to mean unknown.
//
// The return value is the effective request "Range" header to use and
// whether this request is now considered done.
func checkETag(w http.ResponseWriter, r *http.Request, modtime time.Time) (rangeReq string, done bool) {
etag := w.Header().Get("Etag")
rangeReq = r.Header.Get("Range")
// Invalidate the range request if the entity doesn't match the one
// the client was expecting.
// "If-Range: version" means "ignore the Range: header unless version matches the
// current file."
// We only support ETag versions.
// The caller must have set the ETag on the response already.
if ir := r.Header.Get("If-Range"); ir != "" && ir != etag {
// The If-Range value is typically the ETag value, but it may also be
// the modtime date. See golang.org/issue/8367.
timeMatches := false
if !modtime.IsZero() {
if t, err := http.ParseTime(ir); err == nil && t.Unix() == modtime.Unix() {
timeMatches = true
}
}
if !timeMatches {
rangeReq = ""
}
}
if inm := r.Header.Get("If-None-Match"); inm != "" {
// Must know ETag.
if etag == "" {
return rangeReq, false
}
// TODO(bradfitz): non-GET/HEAD requests require more work:
// sending a different status code on matches, and
// also can't use weak cache validators (those with a "W/
// prefix). But most users of ServeContent will be using
// it on GET or HEAD, so only support those for now.
if r.Method != "GET" && r.Method != "HEAD" {
return rangeReq, false
}
// TODO(bradfitz): deal with comma-separated or multiple-valued
// list of If-None-match values. For now just handle the common
// case of a single item.
if inm == etag || inm == "*" {
h := w.Header()
delete(h, "Content-Type")
delete(h, "Content-Length")
w.WriteHeader(http.StatusNotModified)
return "", true
}
}
return rangeReq, false
}