前端之家收集整理的这篇文章主要介绍了
golang timestamp,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package @H_502_4@mmtime
import @H_502_4@(
"fmt"
@H_502_4@ "strconv"
@H_502_4@ "time"
@H_502_4@)
// FMT_TYPE_NOMAL
@H_502_4@const @H_502_4@(
DATE_TIME_FMT @H_502_4@= "2006-01-02 15:04:05"
@H_502_4@
@H_502_4@ @H_502_4@DATE_FMT @H_502_4@= "2006-01-02"
@H_502_4@
@H_502_4@ @H_502_4@TIME_FMT @H_502_4@= "15:04:05"
@H_502_4@
@H_502_4@ @H_502_4@DATE_TIME_FMT_CN @H_502_4@= "2006@H_502_4@年@H_502_4@01@H_502_4@月@H_502_4@02@H_502_4@日@H_502_4@ 15@H_502_4@时@H_502_4@04@H_502_4@分@H_502_4@05@H_502_4@秒@H_502_4@"
@H_502_4@
@H_502_4@ @H_502_4@DATE_FMT_CN @H_502_4@= "2006@H_502_4@年@H_502_4@01@H_502_4@月@H_502_4@02@H_502_4@日@H_502_4@"
@H_502_4@
@H_502_4@ @H_502_4@TIME_FMT_CN @H_502_4@= "15@H_502_4@时@H_502_4@04@H_502_4@分@H_502_4@05@H_502_4@秒@H_502_4@"
@H_502_4@)
const @H_502_4@SecondInNano @H_502_4@= 1000 @H_502_4@* 1000 @H_502_4@* 1000
@H_502_4@
@H_502_4@//return 1441006057 in sec
@H_502_4@func @H_502_4@GetTimestamp@H_502_4@() int64 @H_502_4@{
return @H_502_4@time.Now@H_502_4@().Unix@H_502_4@()
}
//return 1441006057 in sec
@H_502_4@func @H_502_4@GetTimestampString@H_502_4@() string @H_502_4@{
return @H_502_4@strconv.FormatInt@H_502_4@(GetTimestamp@H_502_4@(),@H_502_4@10@H_502_4@)
}
// return 1441007112776 in millisecond
@H_502_4@func @H_502_4@GetTimestampInMilli@H_502_4@() int64 @H_502_4@{
return @H_502_4@int64@H_502_4@(time.Now@H_502_4@().UnixNano@H_502_4@() / (1000 @H_502_4@* 1000@H_502_4@)) // ms
@H_502_4@}
// return 1441007112776 in millisecond
@H_502_4@func @H_502_4@GetTimestampInMilliString@H_502_4@() string @H_502_4@{
return @H_502_4@strconv.FormatInt@H_502_4@(GetTimestampInMilli@H_502_4@(),@H_502_4@10@H_502_4@)
}
//@H_502_4@微秒
@H_502_4@func @H_502_4@GetTimestampInMicro@H_502_4@() int64 @H_502_4@{
return @H_502_4@int64@H_502_4@(time.Now@H_502_4@().UnixNano@H_502_4@() / 1000@H_502_4@) // ms
@H_502_4@}
// @H_502_4@微秒
@H_502_4@func @H_502_4@GetTimestampInMicroString@H_502_4@() {
return @H_502_4@strconv.FormatInt@H_502_4@(GetTimestampInMicro@H_502_4@(),@H_502_4@10@H_502_4@)
}
//format
@H_502_4@func @H_502_4@GetCurrentTimeFormat@H_502_4@(format string@H_502_4@) string @H_502_4@{
return @H_502_4@GetTimeFormat@H_502_4@(GetTimestamp@H_502_4@(),@H_502_4@format)
}
//
@H_502_4@func @H_502_4@GetTimeFormat@H_502_4@(second int64@H_502_4@,@H_502_4@format string@H_502_4@) string @H_502_4@{
return @H_502_4@time.Unix@H_502_4@(second,@H_502_4@0@H_502_4@).Format@H_502_4@(format)
}
// Timing the cost of function call,unix nano was returned
@H_502_4@func @H_502_4@Elapse@H_502_4@(f func@H_502_4@()) int64 @H_502_4@{
now := time.Now@H_502_4@().UnixNano@H_502_4@()
f()
return @H_502_4@time.Now@H_502_4@().UnixNano@H_502_4@() - now
}
// Timing the cost of function call,unix nano was returned
@H_502_4@func @H_502_4@ElapseString@H_502_4@(f func@H_502_4@()) string @H_502_4@{
return @H_502_4@strconv.FormatInt@H_502_4@(Elapse@H_502_4@(f),@H_502_4@10@H_502_4@)
}
// GetMonthDays return days of the month/year
@H_502_4@func @H_502_4@GetMonthDays@H_502_4@(year,@H_502_4@month int@H_502_4@) int @H_502_4@{
switch @H_502_4@month {
case @H_502_4@1@H_502_4@,@H_502_4@3@H_502_4@,@H_502_4@5@H_502_4@,@H_502_4@7@H_502_4@,@H_502_4@8@H_502_4@,@H_502_4@10@H_502_4@,@H_502_4@12@H_502_4@:
return @H_502_4@31
@H_502_4@ @H_502_4@case @H_502_4@4@H_502_4@,@H_502_4@6@H_502_4@,@H_502_4@9@H_502_4@,@H_502_4@11@H_502_4@:
return @H_502_4@30
@H_502_4@ @H_502_4@case @H_502_4@2@H_502_4@:
if @H_502_4@IsLeapYear@H_502_4@(year) {
return @H_502_4@29
@H_502_4@ @H_502_4@}
return @H_502_4@28
@H_502_4@ @H_502_4@default@H_502_4@:
panic@H_502_4@(fmt.Sprintf@H_502_4@("Illegal month:%d"@H_502_4@,@H_502_4@month))
}
}
// IsLeapYear check whether a year is leay
@H_502_4@func @H_502_4@IsLeapYear@H_502_4@(year int@H_502_4@) bool @H_502_4@{
if @H_502_4@year%100 @H_502_4@== 0 @H_502_4@{
return @H_502_4@year%400 @H_502_4@== 0
@H_502_4@ @H_502_4@}
return @H_502_4@year%4 @H_502_4@== 0
@H_502_4@}