time.go raw

   1  /*
   2   * Copyright 2017 Baidu, Inc.
   3   *
   4   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
   5   * except in compliance with the License. You may obtain a copy of the License at
   6   *
   7   * http://www.apache.org/licenses/LICENSE-2.0
   8   *
   9   * Unless required by applicable law or agreed to in writing, software distributed under the
  10   * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  11   * either express or implied. See the License for the specific language governing permissions
  12   * and limitations under the License.
  13   */
  14  
  15  // time.go - define the time utility functions for BCE
  16  
  17  package util
  18  
  19  import "time"
  20  
  21  const (
  22  	RFC822Format  = "Mon, 02 Jan 2006 15:04:05 MST"
  23  	ISO8601Format = "2006-01-02T15:04:05Z"
  24  )
  25  
  26  func NowUTCSeconds() int64 { return time.Now().UTC().Unix() }
  27  
  28  func NowUTCNanoSeconds() int64 { return time.Now().UTC().UnixNano() }
  29  
  30  func FormatRFC822Date(timestamp_second int64) string {
  31  	tm := time.Unix(timestamp_second, 0).UTC()
  32  	return tm.Format(RFC822Format)
  33  }
  34  
  35  func ParseRFC822Date(time_string string) (time.Time, error) {
  36  	return time.Parse(RFC822Format, time_string)
  37  }
  38  
  39  func FormatISO8601Date(timestamp_second int64) string {
  40  	tm := time.Unix(timestamp_second, 0).UTC()
  41  	return tm.Format(ISO8601Format)
  42  }
  43  
  44  func ParseISO8601Date(time_string string) (time.Time, error) {
  45  	return time.Parse(ISO8601Format, time_string)
  46  }
  47