加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

golang timestamp

发布时间:2020-12-16 18:39:13 所属栏目:大数据 来源:网络整理
导读:package mmtime import ( "fmt" "strconv" "time" ) // FMT_TYPE_NOMAL const ( DATE_TIME_FMT = "2006-01-02 15:04:05" DATE_FMT = "2006-01-02" TIME_FMT = "15:04:05" DATE_TIME_FMT_CN = "2006 年 01 月 02 日 15 时 04 分 05 秒 " DATE_FMT_CN = "2006
package mmtime

import (
   "fmt"
   "strconv"
   "time"
)

// FMT_TYPE_NOMAL
const (
   DATE_TIME_FMT = "2006-01-02 15:04:05"

   DATE_FMT = "2006-01-02"

   TIME_FMT = "15:04:05"

   DATE_TIME_FMT_CN = "20060102 150405"

   DATE_FMT_CN = "20060102"

   TIME_FMT_CN = "150405"
)

const SecondInNano = 1000 * 1000 * 1000

//return 1441006057 in sec
func GetTimestamp() int64 {
   return time.Now().Unix()
}

//return 1441006057 in sec
func GetTimestampString() string {
   return strconv.FormatInt(GetTimestamp(),10)
}

// return 1441007112776 in millisecond
func GetTimestampInMilli() int64 {
   return int64(time.Now().UnixNano() / (1000 * 1000)) // ms
}

// return 1441007112776 in millisecond
func GetTimestampInMilliString() string {
   return strconv.FormatInt(GetTimestampInMilli(),10)
}

//微秒
func GetTimestampInMicro() int64 {
   return int64(time.Now().UnixNano() / 1000) // ms
}
// 微秒
func GetTimestampInMicroString() {
   return strconv.FormatInt(GetTimestampInMicro(),10)
}


//format
func GetCurrentTimeFormat(format string) string {
   return GetTimeFormat(GetTimestamp(),format)
}

//
func GetTimeFormat(second int64,format string) string {
   return time.Unix(second,0).Format(format)
}

// Timing the cost of function call,unix nano was returned
func Elapse(f func()) int64 {
   now := time.Now().UnixNano()
   f()
   return time.Now().UnixNano() - now
}

// Timing the cost of function call,unix nano was returned
func ElapseString(f func()) string {
   return strconv.FormatInt(Elapse(f),10)
}

// GetMonthDays return days of the month/year
func GetMonthDays(year,month int) int {
   switch month {
   case 1,3,5,7,8,10,12:
      return 31
   case 4,6,9,11:
      return 30
   case 2:
      if IsLeapYear(year) {
         return 29
      }
      return 28
   default:
      panic(fmt.Sprintf("Illegal month:%d",month))
   }
}

// IsLeapYear check whether a year is leay
func IsLeapYear(year int) bool {
   if year%100 == 0 {
      return year%400 == 0
   }

   return year%4 == 0
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读