2020-11-02 10:47:59 +08:00
<!DOCTYPE html>
2021-02-01 21:40:58 +08:00
< html >
< head >
< title > go-carbon代码测试覆盖率报告< / title >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" / >
< style >
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
< / style >
< / head >
< body >
< div id = "topbar" >
< div id = "nav" >
< select id = "files" > < option value = "file0" > github.com/golang-module/carbon/calendar.go (100.0%)< / option > < option value = "file1" > github.com/golang-module/carbon/carbon.go (100.0%)< / option > < option value = "file2" > github.com/golang-module/carbon/database.go (44.4%)< / option > < option value = "file3" > github.com/golang-module/carbon/diffForHumans.go (0.0%)< / option > < option value = "file4" > github.com/golang-module/carbon/final.go (100.0%)< / option > < option value = "file5" > github.com/golang-module/carbon/private.go (100.0%)< / option > < / select >
< / div >
< div id = "legend" >
< span > not tracked< / span >
< span class = "cov0" > not covered< / span >
< span class = "cov8" > covered< / span >
< / div >
< / div >
< div id = "content" >
< pre class = "file" id = "file0" style = "display: block;" > package carbon
2020-11-02 10:47:59 +08:00
var (
// 十二生肖
2021-02-01 21:40:58 +08:00
SymbolicAnimals = [12]string{" 猴" , " 鸡" , " 狗" , " 猪" , " 鼠" , " 牛" , " 虎" , " 兔" , " 龙" , " 蛇" , " 马" , " 羊" }
2020-11-02 10:47:59 +08:00
// 天干
2021-02-01 21:40:58 +08:00
HeavenlyStems = [10]string{" 庚" , " 辛" , " 壬" , " 癸" , " 甲" , " 乙" , " 丙" , " 丙" , " 戊" , " 己" }
2020-11-02 10:47:59 +08:00
// 地支
2021-02-01 21:40:58 +08:00
EarthlyBranches = [12]string{" 申" , " 酉" , " 戌" , " 亥" , " 子" , " 丑" , " 寅" , " 卯" , " 辰" , " 巳" , " 午" , " 未" }
2020-11-02 10:47:59 +08:00
)
// ToAnimalYear 获取生肖年
func (c Carbon) ToAnimalYear() string < span class = "cov8" title = "1" > {
if c.Time.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
< span class = "cov8" title = "1" > year := c.Time.Year()
return SymbolicAnimals[year%12]< / span >
}
// ToLunarYear 获取农历年
func (c Carbon) ToLunarYear() string < span class = "cov8" title = "1" > {
if c.Time.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
< span class = "cov8" title = "1" > year := c.Time.Year()
return HeavenlyStems[year%10] + EarthlyBranches[year%12]< / span >
}
// IsYearOfRat 是否是鼠年
func (c Carbon) IsYearOfRat() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 4 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfOx 是否是牛年
func (c Carbon) IsYearOfOx() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 5 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfTiger 是否是虎年
func (c Carbon) IsYearOfTiger() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 6 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfRabbit 是否是兔年
func (c Carbon) IsYearOfRabbit() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 7 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfDragon 是否是龙年
func (c Carbon) IsYearOfDragon() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 8 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfSnake 是否是蛇年
func (c Carbon) IsYearOfSnake() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 9 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfHorse 是否是马年
func (c Carbon) IsYearOfHorse() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 10 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfGoat 是否是羊年
func (c Carbon) IsYearOfGoat() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 11 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfMonkey 是否是猴年
func (c Carbon) IsYearOfMonkey() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 0 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfRooster 是否是鸡年
func (c Carbon) IsYearOfRooster() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 1 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfDog 是否是狗年
func (c Carbon) IsYearOfDog() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 2 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsYearOfPig 是否是猪年
func (c Carbon) IsYearOfPig() bool < span class = "cov8" title = "1" > {
year := c.Time.Year()
if year%12 == 3 < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
< / pre >
2021-02-01 21:40:58 +08:00
< pre class = "file" id = "file1" style = "display: none" > package carbon
2020-11-02 10:47:59 +08:00
import (
2021-02-01 21:40:58 +08:00
" strconv"
" strings"
" time"
2020-11-02 10:47:59 +08:00
)
type Carbon struct {
2021-01-26 17:08:27 +08:00
Time time.Time
Loc *time.Location
Error error
2020-11-02 10:47:59 +08:00
}
2021-01-29 09:42:51 +08:00
// Now 当前
2020-11-02 10:47:59 +08:00
func (c Carbon) Now() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Now()
return c
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
// Now 当前(默认时区)
func Now() Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).Now()
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
// Tomorrow 明天
2020-11-02 10:47:59 +08:00
func (c Carbon) Tomorrow() Carbon < span class = "cov8" title = "1" > {
2021-01-29 09:42:51 +08:00
if c.Time.IsZero() < / span > < span class = "cov8" title = "1" > {
c.Time = time.Now().AddDate(0, 0, 1)
}< / span > else< span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(0, 0, 1)
}< / span >
< span class = "cov8" title = "1" > return c< / span >
}
2020-11-02 10:47:59 +08:00
2021-01-26 17:08:27 +08:00
// Tomorrow 明天(默认时区)
func Tomorrow() Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).Tomorrow()
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
// Yesterday 昨天
2020-11-02 10:47:59 +08:00
func (c Carbon) Yesterday() Carbon < span class = "cov8" title = "1" > {
2021-01-29 09:42:51 +08:00
if c.Time.IsZero() < / span > < span class = "cov8" title = "1" > {
c.Time = time.Now().AddDate(0, 0, -1)
}< / span > else< span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(0, 0, -1)
}< / span >
< span class = "cov8" title = "1" > return c< / span >
}
2020-11-02 10:47:59 +08:00
2021-01-26 17:08:27 +08:00
// Yesterday 昨天(默认时区)
func Yesterday() Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).Yesterday()
}< / span >
2021-01-29 09:42:51 +08:00
// CreateFromTimestamp 从时间戳创建Carbon实例
2021-01-26 17:08:27 +08:00
func (c Carbon) CreateFromTimestamp(timestamp int64) Carbon < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
ts := timestamp
switch len(strconv.FormatInt(timestamp, 10)) < / span > {
case 10:< span class = "cov8" title = "1" >
ts = timestamp< / span >
case 13:< span class = "cov8" title = "1" >
ts = timestamp / 1e3< / span >
case 16:< span class = "cov8" title = "1" >
ts = timestamp / 1e6< / span >
case 19:< span class = "cov8" title = "1" >
ts = timestamp / 1e9< / span >
default:< span class = "cov8" title = "1" >
ts = 0< / span >
}
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > c.Time = time.Unix(ts, 0)
return c< / span >
2020-11-02 10:47:59 +08:00
}
2021-01-26 17:08:27 +08:00
// CreateFromTimestamp 从时间戳创建Carbon实例(默认时区)
func CreateFromTimestamp(timestamp int64) Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).CreateFromTimestamp(timestamp)
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
// CreateFromDateTime 从年月日时分秒创建Carbon实例
2020-11-02 10:47:59 +08:00
func (c Carbon) CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, 0, c.Loc)
return c
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
// CreateFromDateTime 从年月日时分秒创建Carbon实例(默认时区)
func CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).CreateFromDateTime(year, month, day, hour, minute, second)
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
// CreateFromDate 从年月日创建Carbon实例
2020-11-02 10:47:59 +08:00
func (c Carbon) CreateFromDate(year int, month int, day int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
hour, minute, second := time.Now().Clock()
c.Time = time.Date(year, time.Month(month), day, hour, minute, second, 0, c.Loc)
return c
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
// CreateFromDate 从年月日创建Carbon实例(默认时区)
func CreateFromDate(year int, month int, day int) Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).CreateFromDate(year, month, day)
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
// CreateFromTime 从时分秒创建Carbon实例
2020-11-02 10:47:59 +08:00
func (c Carbon) CreateFromTime(hour int, minute int, second int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
year, month, day := time.Now().Date()
c.Time = time.Date(year, month, day, hour, minute, second, 0, c.Loc)
return c
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
// CreateFromTime 从时分秒创建Carbon实例(默认时区)
func CreateFromTime(hour int, minute int, second int) Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).CreateFromTime(hour, minute, second)
2020-11-02 10:47:59 +08:00
}< / span >
2021-02-01 21:29:21 +08:00
// Time2Carbon 原生time.Time 转换成 Carbon实例
func Time2Carbon(tt time.Time) Carbon < span class = "cov8" title = "1" > {
loc, _ := time.LoadLocation(Local)
return Carbon{Time: tt, Loc: loc}
2020-11-02 10:47:59 +08:00
}< / span >
2021-02-01 21:29:21 +08:00
// Carbon2Time Carbon实例 转换成 原生time.Time
func (c Carbon) Carbon2Time() time.Time < span class = "cov8" title = "1" > {
return c.Time
2021-01-26 17:08:27 +08:00
}< / span >
2020-11-02 10:47:59 +08:00
2021-01-29 09:42:51 +08:00
// Parse 解析标准格式时间字符串
2021-01-26 17:08:27 +08:00
func (c Carbon) Parse(value string) Carbon < span class = "cov8" title = "1" > {
if c.Error != nil < / span > < span class = "cov8" title = "1" > {
return c
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > layout := DateTimeFormat
2021-02-01 21:40:58 +08:00
if value == " " || value == " 0" || value == " 0000-00-00 00:00:00" || value == " 0000-00-00" || value == " 00:00:00" < / span > < span class = "cov8" title = "1" > {
2021-01-29 09:42:51 +08:00
return c
2020-11-02 10:47:59 +08:00
}< / span >
2021-02-01 21:40:58 +08:00
< span class = "cov8" title = "1" > if len(value) == 10 & & strings.Count(value, " -" ) == 2 < / span > < span class = "cov8" title = "1" > {
2021-01-29 09:42:51 +08:00
layout = DateFormat
2020-11-02 10:47:59 +08:00
}< / span >
2021-02-01 21:40:58 +08:00
< span class = "cov8" title = "1" > if strings.Index(value, " T" ) == 10 < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
layout = RFC3339Format
}< / span >
2021-01-29 09:42:51 +08:00
< span class = "cov8" title = "1" > if _, err := strconv.ParseInt(value, 10, 64); err == nil < / span > < span class = "cov8" title = "1" > {
switch len(value) < / span > {
case 8:< span class = "cov8" title = "1" >
layout = ShortDateFormat< / span >
case 14:< span class = "cov8" title = "1" >
layout = ShortDateTimeFormat< / span >
}
}
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > tt, err := parseByLayout(value, layout)
c.Time = tt
c.Error = err
return c< / span >
2020-11-02 10:47:59 +08:00
}
2021-01-26 17:08:27 +08:00
// Parse 解析标准格式时间字符串(默认时区)
func Parse(value string) Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).Parse(value)
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
// ParseByFormat 解析指定格式时间字符串
2020-11-02 10:47:59 +08:00
func (c Carbon) ParseByFormat(value string, format string) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
if c.Error != nil < / span > < span class = "cov8" title = "1" > {
return c
}< / span >
< span class = "cov8" title = "1" > layout := format2layout(format)
2021-02-01 21:29:21 +08:00
return c.ParseByLayout(value, layout)< / span >
2021-01-26 17:08:27 +08:00
}
2020-11-02 10:47:59 +08:00
2021-01-26 17:08:27 +08:00
// ParseByFormat 解析指定格式时间字符串(默认时区)
func ParseByFormat(value string, format string) Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).ParseByFormat(value, format)
2020-11-02 10:47:59 +08:00
}< / span >
2021-02-01 21:29:21 +08:00
// ParseByLayout 解析布局字符串
func (c Carbon) ParseByLayout(value string, layout string) Carbon < span class = "cov8" title = "1" > {
if c.Error != nil < / span > < span class = "cov8" title = "1" > {
return c
}< / span >
< span class = "cov8" title = "1" > tt, err := parseByLayout(value, layout)
c.Time = tt
c.Error = err
return c< / span >
}
// ParseByLayout 解析布局字符串(默认时区)
func ParseByLayout(value string, layout string) Carbon < span class = "cov8" title = "1" > {
return SetTimezone(Local).ParseByLayout(value, layout)
}< / span >
2020-11-03 07:41:50 +08:00
// AddDurations 按照持续时间字符串增加时间
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) AddDuration(duration string) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
if c.Error != nil < / span > < span class = "cov8" title = "1" > {
return c
}< / span >
< span class = "cov8" title = "1" > td, err := parseByDuration(duration)
c.Time = c.Time.Add(td)
c.Error = err
return c< / span >
}
2020-11-02 10:47:59 +08:00
2020-11-03 07:41:50 +08:00
// SubDurations 按照持续时间字符串减少时间
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) SubDuration(duration string) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
if c.Error != nil < / span > < span class = "cov8" title = "1" > {
return c
}< / span >
2021-02-01 21:40:58 +08:00
< span class = "cov8" title = "1" > td, err := parseByDuration(" -" + duration)
2021-01-26 17:08:27 +08:00
c.Time = c.Time.Add(td)
c.Error = err
return c< / span >
}
// AddCenturies N世纪后
func (c Carbon) AddCenturies(centuries int) Carbon < span class = "cov8" title = "1" > {
return c.AddYears(YearsPerCentury * centuries)
}< / span >
2021-02-01 21:29:21 +08:00
// AddCenturiesNoOverflow N世纪后(月份不溢出)
func (c Carbon) AddCenturiesNoOverflow(centuries int) Carbon < span class = "cov8" title = "1" > {
return c.AddYearsNoOverflow(centuries * YearsPerCentury)
}< / span >
2021-01-26 17:08:27 +08:00
// AddCentury 1世纪后
func (c Carbon) AddCentury() Carbon < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
return c.AddCenturies(1)
}< / span >
// AddCenturyNoOverflow 1世纪后(月份不溢出)
func (c Carbon) AddCenturyNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.AddCenturiesNoOverflow(1)
2021-01-26 17:08:27 +08:00
}< / span >
// SubCenturies N世纪前
func (c Carbon) SubCenturies(centuries int) Carbon < span class = "cov8" title = "1" > {
return c.SubYears(YearsPerCentury * centuries)
}< / span >
2021-02-01 21:29:21 +08:00
// SubCenturiesNoOverflow N世纪前(月份不溢出)
func (c Carbon) SubCenturiesNoOverflow(centuries int) Carbon < span class = "cov8" title = "1" > {
return c.SubYearsNoOverflow(centuries * YearsPerCentury)
}< / span >
2021-01-26 17:08:27 +08:00
// SubCentury 1世纪前
func (c Carbon) SubCentury() Carbon < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
return c.SubCenturies(1)
}< / span >
// SubCenturyNoOverflow 1世纪前(月份不溢出)
func (c Carbon) SubCenturyNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.SubCenturiesNoOverflow(1)
2020-11-03 07:41:50 +08:00
}< / span >
2020-11-02 10:47:59 +08:00
// AddYears N年后
func (c Carbon) AddYears(years int) Carbon < span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(years, 0, 0)
return c
}< / span >
2021-02-01 21:29:21 +08:00
// AddYearsNoOverflow N年后(月份不溢出)
func (c Carbon) AddYearsNoOverflow(years int) Carbon < span class = "cov8" title = "1" > {
year := c.Time.Year() + years
month := c.Time.Month()
day := c.Time.Day()
// 获取N年后本月的最后一天
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc).AddDate(0, 1, -1)
if day > last.Day() < / span > < span class = "cov8" title = "1" > {
day = last.Day()
}< / span >
< span class = "cov8" title = "1" > c.Time = time.Date(last.Year(), last.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
return c< / span >
}
2020-11-02 10:47:59 +08:00
// AddYear 1年后
func (c Carbon) AddYear() Carbon < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
return c.AddYears(1)
}< / span >
// AddYearNoOverflow 1年后(月份不溢出)
func (c Carbon) AddYearNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.AddYearsNoOverflow(1)
2020-11-02 10:47:59 +08:00
}< / span >
// SubYears N年前
func (c Carbon) SubYears(years int) Carbon < span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(-years, 0, 0)
return c
}< / span >
2021-02-01 21:29:21 +08:00
// SubYearsWithOverflow N年前(月份不溢出)
func (c Carbon) SubYearsNoOverflow(years int) Carbon < span class = "cov8" title = "1" > {
return c.AddYearsNoOverflow(-years)
}< / span >
2020-11-02 10:47:59 +08:00
// SubYear 1年前
func (c Carbon) SubYear() Carbon < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
return c.SubYears(1)
}< / span >
// SubYearWithOverflow 1年前(月份不溢出)
func (c Carbon) SubYearNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.SubYearsNoOverflow(1)
2020-11-02 10:47:59 +08:00
}< / span >
2021-02-01 21:29:21 +08:00
// AddQuarters N季度后
2020-11-02 10:47:59 +08:00
func (c Carbon) AddQuarters(quarters int) Carbon < span class = "cov8" title = "1" > {
return c.AddMonths(quarters * MonthsPerQuarter)
}< / span >
2021-02-01 21:29:21 +08:00
// AddQuartersNoOverflow N季度后(月份不溢出)
func (c Carbon) AddQuartersNoOverflow(quarters int) Carbon < span class = "cov8" title = "1" > {
return c.AddMonthsNoOverflow(quarters * MonthsPerQuarter)
}< / span >
// AddQuarter 1季度后
2020-11-02 10:47:59 +08:00
func (c Carbon) AddQuarter() Carbon < span class = "cov8" title = "1" > {
return c.AddQuarters(1)
}< / span >
2021-02-01 21:29:21 +08:00
// NextQuarters 1季度后(月份不溢出)
func (c Carbon) AddQuarterNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.AddQuartersNoOverflow(1)
}< / span >
// SubQuarters N季度前
2020-11-02 10:47:59 +08:00
func (c Carbon) SubQuarters(quarters int) Carbon < span class = "cov8" title = "1" > {
return c.SubMonths(quarters * MonthsPerQuarter)
}< / span >
2021-02-01 21:29:21 +08:00
// SubQuartersNoOverflow N季度前(月份不溢出)
func (c Carbon) SubQuartersNoOverflow(quarters int) Carbon < span class = "cov8" title = "1" > {
return c.AddMonthsNoOverflow(-quarters * MonthsPerQuarter)
}< / span >
// SubQuarter 1季度前
2020-11-02 10:47:59 +08:00
func (c Carbon) SubQuarter() Carbon < span class = "cov8" title = "1" > {
return c.SubQuarters(1)
}< / span >
2021-02-01 21:29:21 +08:00
// SubQuarterNoOverflow 1季度前(月份不溢出)
func (c Carbon) SubQuarterNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.SubQuartersNoOverflow(1)
}< / span >
2020-11-02 10:47:59 +08:00
// AddMonths N月后
func (c Carbon) AddMonths(months int) Carbon < span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(0, months, 0)
return c
}< / span >
2021-02-01 21:29:21 +08:00
// AddMonthsNoOverflow N月后(月份不溢出)
func (c Carbon) AddMonthsNoOverflow(months int) Carbon < span class = "cov8" title = "1" > {
year := c.Time.Year()
month := c.Time.Month() + time.Month(months)
day := c.Time.Day()
// 获取N月后的最后一天
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc).AddDate(0, 1, -1)
if day > last.Day() < / span > < span class = "cov8" title = "1" > {
day = last.Day()
}< / span >
< span class = "cov8" title = "1" > c.Time = time.Date(last.Year(), last.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
return c< / span >
}
2020-11-02 10:47:59 +08:00
// AddMonth 1月后
func (c Carbon) AddMonth() Carbon < span class = "cov8" title = "1" > {
return c.AddMonths(1)
}< / span >
2021-02-01 21:29:21 +08:00
// AddMonthNoOverflow 1月后(月份不溢出)
func (c Carbon) AddMonthNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.AddMonthsNoOverflow(1)
}< / span >
2020-11-02 10:47:59 +08:00
// SubMonths N月前
func (c Carbon) SubMonths(months int) Carbon < span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(0, -months, 0)
return c
}< / span >
2021-02-01 21:29:21 +08:00
// SubMonthsNoOverflow N月前(月份不溢出)
func (c Carbon) SubMonthsNoOverflow(months int) Carbon < span class = "cov8" title = "1" > {
return c.AddMonthsNoOverflow(-months)
}< / span >
2020-11-02 10:47:59 +08:00
// SubMonth 1月前
func (c Carbon) SubMonth() Carbon < span class = "cov8" title = "1" > {
return c.SubMonths(1)
}< / span >
2021-02-01 21:29:21 +08:00
// SubMonthNoOverflow 1月前(月份不溢出)
func (c Carbon) SubMonthNoOverflow() Carbon < span class = "cov8" title = "1" > {
return c.SubMonthsNoOverflow(1)
}< / span >
2020-11-02 10:47:59 +08:00
// AddWeeks N周后
func (c Carbon) AddWeeks(weeks int) Carbon < span class = "cov8" title = "1" > {
return c.AddDays(weeks * DaysPerWeek)
}< / span >
// AddWeek 1天后
func (c Carbon) AddWeek() Carbon < span class = "cov8" title = "1" > {
return c.AddWeeks(1)
}< / span >
// SubWeeks N周后
func (c Carbon) SubWeeks(weeks int) Carbon < span class = "cov8" title = "1" > {
return c.SubDays(weeks * DaysPerWeek)
}< / span >
// SubWeek 1天后
func (c Carbon) SubWeek() Carbon < span class = "cov8" title = "1" > {
return c.SubWeeks(1)
}< / span >
// AddDays N天后
func (c Carbon) AddDays(days int) Carbon < span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(0, 0, days)
return c
}< / span >
// AddDay 1天后
func (c Carbon) AddDay() Carbon < span class = "cov8" title = "1" > {
return c.AddDays(1)
}< / span >
// SubDays N天前
func (c Carbon) SubDays(days int) Carbon < span class = "cov8" title = "1" > {
c.Time = c.Time.AddDate(0, 0, -days)
return c
}< / span >
// SubDay 1天前
func (c Carbon) SubDay() Carbon < span class = "cov8" title = "1" > {
return c.SubDays(1)
}< / span >
// AddHours N小时后
func (c Carbon) AddHours(hours int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
td := time.Duration(hours) * time.Hour
c.Time = c.Time.Add(td)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// AddHour 1小时后
func (c Carbon) AddHour() Carbon < span class = "cov8" title = "1" > {
return c.AddHours(1)
}< / span >
// SubHours N小时前
func (c Carbon) SubHours(hours int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
td := time.Duration(hours) * -time.Hour
c.Time = c.Time.Add(td)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SubHour 1小时前
func (c Carbon) SubHour() Carbon < span class = "cov8" title = "1" > {
return c.SubHours(1)
}< / span >
// AddMinutes N分钟后
func (c Carbon) AddMinutes(minutes int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
td := time.Duration(minutes) * time.Minute
c.Time = c.Time.Add(td)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// AddMinute 1分钟后
func (c Carbon) AddMinute() Carbon < span class = "cov8" title = "1" > {
return c.AddMinutes(1)
}< / span >
// SubMinutes N分钟前
func (c Carbon) SubMinutes(minutes int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
td := time.Duration(minutes) * -time.Minute
c.Time = c.Time.Add(td)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SubMinute 1分钟前
func (c Carbon) SubMinute() Carbon < span class = "cov8" title = "1" > {
return c.SubMinutes(1)
}< / span >
// AddSeconds N秒钟后
func (c Carbon) AddSeconds(seconds int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
td := time.Duration(seconds) * time.Second
c.Time = c.Time.Add(td)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// AddSecond 1秒钟后
func (c Carbon) AddSecond() Carbon < span class = "cov8" title = "1" > {
return c.AddSeconds(1)
}< / span >
// SubSeconds N秒钟前
func (c Carbon) SubSeconds(seconds int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
td := time.Duration(seconds) * -time.Second
c.Time = c.Time.Add(td)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SubSecond 1秒钟前
func (c Carbon) SubSecond() Carbon < span class = "cov8" title = "1" > {
return c.SubSeconds(1)
}< / span >
// StartOfYear 本年开始时间
func (c Carbon) StartOfYear() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), 1, 1, 0, 0, 0, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// EndOfYear 本年结束时间
func (c Carbon) EndOfYear() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), 12, 31, 23, 59, 59, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// StartOfMonth 本月开始时间
func (c Carbon) StartOfMonth() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), 1, 0, 0, 0, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// EndOfMonth 本月结束时间
func (c Carbon) EndOfMonth() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
t := time.Date(c.Time.Year(), c.Time.Month(), 1, 23, 59, 59, 0, c.Loc)
2020-11-02 10:47:59 +08:00
c.Time = t.AddDate(0, 1, -1)
return c
}< / span >
// StartOfWeek 本周开始时间
func (c Carbon) StartOfWeek() Carbon < span class = "cov8" title = "1" > {
days := c.Time.Weekday()
if days == 0 < / span > < span class = "cov8" title = "1" > {
days = DaysPerWeek
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Loc)
2020-11-02 10:47:59 +08:00
c.Time = t.AddDate(0, 0, int(1-days))
return c< / span >
}
// EndOfWeek 本周结束时间
func (c Carbon) EndOfWeek() Carbon < span class = "cov8" title = "1" > {
days := c.Time.Weekday()
if days == 0 < / span > < span class = "cov8" title = "1" > {
days = DaysPerWeek
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Loc)
2020-11-02 10:47:59 +08:00
c.Time = t.AddDate(0, 0, int(DaysPerWeek-days))
return c< / span >
}
// StartOfDay 本日开始时间
func (c Carbon) StartOfDay() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// EndOfDay 本日结束时间
func (c Carbon) EndOfDay() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// StartOfHour 小时开始时间
func (c Carbon) StartOfHour() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 0, 0, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// EndOfHour 小时结束时间
func (c Carbon) EndOfHour() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 59, 59, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// StartOfMinute 分钟开始时间
func (c Carbon) StartOfMinute() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 0, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// EndOfMinute 分钟结束时间
func (c Carbon) EndOfMinute() Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 59, 0, c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// Timezone 设置时区
func SetTimezone(name string) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
loc, err := getLocationByTimezone(name)
return Carbon{Loc: loc, Error: err}
2020-11-02 10:47:59 +08:00
}< / span >
// Timezone 设置时区
func (c Carbon) SetTimezone(name string) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
loc, err := getLocationByTimezone(name)
c.Loc = loc
c.Error = err
return c
2020-11-02 10:47:59 +08:00
}< / span >
// SetYear 设置年
func (c Carbon) SetYear(year int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(year, c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SetMonth 设置月
func (c Carbon) SetMonth(month int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), time.Month(month), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SetDay 设置日
func (c Carbon) SetDay(day int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SetHour 设置时
func (c Carbon) SetHour(hour int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), hour, c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SetMinute 设置分
func (c Carbon) SetMinute(minute int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), minute, c.Time.Second(), c.Time.Nanosecond(), c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
// SetSecond 设置秒
func (c Carbon) SetSecond(second int) Carbon < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), second, c.Time.Nanosecond(), c.Loc)
2020-11-02 10:47:59 +08:00
return c
}< / span >
< / pre >
2021-02-01 21:40:58 +08:00
< pre class = "file" id = "file2" style = "display: none" > package carbon
2020-11-02 10:47:59 +08:00
import (
2021-02-01 21:40:58 +08:00
" database/sql/driver"
" fmt"
" time"
2020-11-02 10:47:59 +08:00
)
type ToDateTimeString struct {
Carbon
}
type ToDateString struct {
Carbon
}
type ToTimeString struct {
Carbon
}
type ToTimestamp struct {
Carbon
}
2020-11-03 07:41:50 +08:00
type ToTimestampWithSecond struct {
Carbon
}
type ToTimestampWithMillisecond struct {
Carbon
}
type ToTimestampWithMicrosecond struct {
Carbon
}
type ToTimestampWithNanosecond struct {
Carbon
}
2020-11-02 10:47:59 +08:00
func (c *Carbon) Scan(v interface{}) error < span class = "cov0" title = "0" > {
value, ok := v.(time.Time)
if ok < / span > < span class = "cov0" title = "0" > {
*c = Carbon{Time: value}
return nil
}< / span >
2021-02-01 21:40:58 +08:00
< span class = "cov0" title = "0" > return fmt.Errorf(" can not convert %v to timestamp" , v)< / span >
2020-11-02 10:47:59 +08:00
}
func (c Carbon) Value() (driver.Value, error) < span class = "cov0" title = "0" > {
var zeroTime time.Time
var timeTime = c.Time
if timeTime.UnixNano() == zeroTime.UnixNano() < / span > < span class = "cov0" title = "0" > {
return nil, nil
}< / span >
< span class = "cov0" title = "0" > return timeTime, nil< / span >
}
func (c ToDateTimeString) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return []byte(fmt.Sprintf(`" %s" `, c.ToDateTimeString())), nil
2020-11-02 10:47:59 +08:00
}< / span >
func (c ToDateString) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return []byte(fmt.Sprintf(`" %s" `, c.ToDateString())), nil
2020-11-02 10:47:59 +08:00
}< / span >
func (c ToTimeString) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return []byte(fmt.Sprintf(`" %s" `, c.ToTimeString())), nil
2020-11-02 10:47:59 +08:00
}< / span >
func (c ToTimestamp) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
}< / span >
2020-11-03 07:41:50 +08:00
2020-11-06 09:28:10 +08:00
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
2020-11-03 07:41:50 +08:00
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil
}< / span >
2020-11-06 09:28:10 +08:00
func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
2020-11-03 07:41:50 +08:00
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil
}< / span >
2020-11-06 09:28:10 +08:00
func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
2020-11-03 07:41:50 +08:00
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil
}< / span >
2020-11-06 09:28:10 +08:00
func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) < span class = "cov8" title = "1" > {
2020-11-03 07:41:50 +08:00
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
}< / span >
2020-11-02 10:47:59 +08:00
< / pre >
2021-02-01 21:40:58 +08:00
< pre class = "file" id = "file3" style = "display: none" > package carbon
2020-11-02 10:47:59 +08:00
import (
2021-02-01 21:40:58 +08:00
" fmt"
" math"
" strconv"
2020-11-02 10:47:59 +08:00
)
2021-02-01 21:29:21 +08:00
func (c Carbon) DiffForHumans() string < span class = "cov0" title = "0" > {
fmt.Println(c.DiffInDays(Now()))
2021-02-01 21:40:58 +08:00
flag := " "
2021-02-01 21:29:21 +08:00
if c.DiffInSeconds(Now()) > 0 < / span > < span class = "cov0" title = "0" > {
2021-02-01 21:40:58 +08:00
flag = " 前"
2021-02-01 21:29:21 +08:00
}< / span > else< span class = "cov0" title = "0" > {
2021-02-01 21:40:58 +08:00
flag = " 后"
2021-02-01 21:29:21 +08:00
}< / span >
< span class = "cov0" title = "0" > dc := (c.ToTimestamp() - Now().ToTimestamp()) / 31536000
if math.Abs(float64(dc)) > = 1 < / span > < span class = "cov0" title = "0" > {
2021-02-01 21:40:58 +08:00
return strconv.FormatInt(dc, 10) + " 年" + flag
2021-02-01 21:29:21 +08:00
}< / span >
< span class = "cov0" title = "0" > mc := (c.ToTimestamp() - Now().ToTimestamp()) / 604800
if math.Abs(float64(mc)) > = 1 < / span > < span class = "cov0" title = "0" > {
2021-02-01 21:40:58 +08:00
return strconv.FormatInt(mc, 10) + " 周" + flag
2021-02-01 21:29:21 +08:00
}< / span >
2021-02-01 21:40:58 +08:00
< span class = "cov0" title = "0" > return " " < / span >
2021-02-01 21:29:21 +08:00
}
func (c Carbon) DiffForHumansFrom(end Carbon) string < span class = "cov0" title = "0" > {
return c.Time.In(c.Loc).String()
2021-01-18 10:34:51 +08:00
}< / span >
2021-02-01 21:29:21 +08:00
< / pre >
2021-02-01 21:40:58 +08:00
< pre class = "file" id = "file4" style = "display: none" > package carbon
2021-02-01 21:29:21 +08:00
import (
2021-02-01 21:40:58 +08:00
" bytes"
" strconv"
" strings"
" time"
2021-02-01 21:29:21 +08:00
)
2021-01-18 10:34:51 +08:00
2021-02-01 21:40:58 +08:00
// ToString 输出" 2006-01-02 15:04:05.999999999 -0700 MST" 格式字符串
2020-11-02 10:47:59 +08:00
func (c Carbon) ToString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
return c.Time.In(c.Loc).String()
2020-11-02 10:47:59 +08:00
}< / span >
// ToTimestamp ToTimestampWithSecond的简称
func (c Carbon) ToTimestamp() int64 < span class = "cov8" title = "1" > {
return c.ToTimestampWithSecond()
}< / span >
// ToTimestampWithSecond 输出秒级时间戳
func (c Carbon) ToTimestampWithSecond() int64 < span class = "cov8" title = "1" > {
return c.Time.Unix()
}< / span >
// ToTimestampWithMillisecond 输出毫秒级时间戳
func (c Carbon) ToTimestampWithMillisecond() int64 < span class = "cov8" title = "1" > {
return c.Time.UnixNano() / int64(time.Millisecond)
}< / span >
// ToTimestampWithMicrosecond 输出微秒级时间戳
func (c Carbon) ToTimestampWithMicrosecond() int64 < span class = "cov8" title = "1" > {
return c.Time.UnixNano() / int64(time.Microsecond)
}< / span >
// ToTimestampWithNanosecond 输出纳秒级时间戳
func (c Carbon) ToTimestampWithNanosecond() int64 < span class = "cov8" title = "1" > {
return c.Time.UnixNano()
}< / span >
// Format ToFormatString的简称
func (c Carbon) Format(format string) string < span class = "cov8" title = "1" > {
return c.ToFormatString(format)
}< / span >
// ToFormatString 输出指定格式时间
func (c Carbon) ToFormatString(format string) string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
2021-02-01 21:29:21 +08:00
< span class = "cov8" title = "1" > runes := []rune(format)
buffer := bytes.NewBuffer(nil)
for i := 0; i < len(runes); i++ < / span > < span class = "cov8" title = "1" > {
if layout, ok := formats[byte(runes[i])]; ok < / span > < span class = "cov8" title = "1" > {
buffer.WriteString(c.Time.In(c.Loc).Format(layout))
}< / span > else< span class = "cov8" title = "1" > {
switch runes[i] < / span > {
case '\\':< span class = "cov8" title = "1" >
buffer.WriteRune(runes[i+1])
i += 2
continue< / span >
case 'W':< span class = "cov8" title = "1" > // ISO-8601 格式数字表示的年份中的第几周,取值范围 1-52
buffer.WriteString(strconv.Itoa(c.WeekOfYear()))< / span >
case 'N':< span class = "cov8" title = "1" > // ISO-8601 格式数字表示的星期中的第几天,取值范围 1-7
buffer.WriteString(strconv.Itoa(c.DayOfWeek()))< / span >
case 'S':< span class = "cov8" title = "1" > // 第几天的英文缩写后缀, 如st, nd, rd, th
2021-02-01 21:40:58 +08:00
day := c.Time.In(c.Loc).Format(" 02" )
suffix := " "
2021-02-01 21:29:21 +08:00
switch day < / span > {
2021-02-01 21:40:58 +08:00
case " 01" , " 21" , " 31" :< span class = "cov8" title = "1" >
suffix = " st" < / span >
case " 02" , " 22" :< span class = "cov8" title = "1" >
suffix = " nd" < / span >
case " 03" , " 23" :< span class = "cov8" title = "1" >
suffix = " rd" < / span >
2021-02-01 21:29:21 +08:00
default:< span class = "cov8" title = "1" >
2021-02-01 21:40:58 +08:00
suffix = " th" < / span >
2021-02-01 21:29:21 +08:00
}
< span class = "cov8" title = "1" > buffer.WriteString(suffix)< / span >
case 'L':< span class = "cov8" title = "1" > // 是否为闰年,如果是闰年为 1, 否则为 0
if c.IsLeapYear() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
buffer.WriteString(" 1" )
2021-02-01 21:29:21 +08:00
}< / span > else< span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
buffer.WriteString(" 0" )
2021-02-01 21:29:21 +08:00
}< / span >
case 'G':< span class = "cov8" title = "1" > // 数字表示的小时, 24 小时格式,没有前导零,取值范围 0-23
2021-02-01 21:40:58 +08:00
s := strings.Replace(c.Time.In(c.Loc).Format(" 15" ), " 0" , " " , 1)
2021-02-01 21:29:21 +08:00
buffer.WriteString(s)< / span >
case 'U':< span class = "cov8" title = "1" > // 秒级时间戳,如 1611818268
buffer.WriteString(strconv.FormatInt(c.ToTimestamp(), 10))< / span >
case 'u':< span class = "cov8" title = "1" > // 数字表示的毫秒,如 999
buffer.WriteString(strconv.Itoa(c.Millisecond()))< / span >
case 'w':< span class = "cov8" title = "1" > // 数字表示的星期中的第几天,取值范围 0-6
buffer.WriteString(strconv.Itoa(c.DayOfWeek() - 1))< / span >
case 't':< span class = "cov8" title = "1" > // 指定的月份有几天,取值范围 28-31
buffer.WriteString(strconv.Itoa(c.DaysInMonth()))< / span >
case 'z':< span class = "cov8" title = "1" > // 年份中的第几天,取值范围 0-365
buffer.WriteString(strconv.Itoa(c.DayOfYear() - 1))< / span >
case 'e':< span class = "cov8" title = "1" > // 当前时区,如 UTC, GMT, Atlantic/Azores
buffer.WriteString(c.Timezone())< / span >
default:< span class = "cov8" title = "1" >
buffer.WriteRune(runes[i])< / span >
}
}
2021-01-29 09:42:51 +08:00
}
2021-02-01 21:29:21 +08:00
< span class = "cov8" title = "1" > return buffer.String()< / span >
2020-11-02 10:47:59 +08:00
}
// ToDayDateTimeString 输出天数日期时间字符串
func (c Carbon) ToDayDateTimeString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-29 09:42:51 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(DayDateTimeFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToDateTimeString 输出日期时间字符串
func (c Carbon) ToDateTimeString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(DateTimeFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToDateString 输出日期字符串
func (c Carbon) ToDateString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(DateFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToTimeString 输出时间字符串
func (c Carbon) ToTimeString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(TimeFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToAtomString 输出Atom格式字符串
func (c Carbon) ToAtomString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC3339Format)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToAnsicString 输出Ansic格式字符串
2020-11-02 10:47:59 +08:00
func (c Carbon) ToAnsicString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(AnsicFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToCookieString 输出Cookie格式字符串
func (c Carbon) ToCookieString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(CookieFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToRssString 输出RSS格式字符串
func (c Carbon) ToRssString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RssFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToW3cString 输出W3C格式字符串
func (c Carbon) ToW3cString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC3339Format)< / span >
2020-11-02 10:47:59 +08:00
}
// ToUnixDateString 输出UnixDate格式字符串
func (c Carbon) ToUnixDateString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(UnixDateFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToUnixDateString 输出RubyDate格式字符串
func (c Carbon) ToRubyDateString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RubyDateFormat)< / span >
2020-11-02 10:47:59 +08:00
}
// ToKitchenString 输出Kitchen格式字符串
func (c Carbon) ToKitchenString() string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(KitchenFormat)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc822String 输出RFC822格式字符串
func (c Carbon) ToRfc822String() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC822Format)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc822zString 输出RFC822Z格式字符串
func (c Carbon) ToRfc822zString() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC822ZFormat)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc850String 输出RFC850格式字符串
func (c Carbon) ToRfc850String() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC850Format)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc1036String 输出RFC1036格式字符串
func (c Carbon) ToRfc1036String() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC1036Format)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc1123String 输出RFC1123格式字符串
func (c Carbon) ToRfc1123String() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC1123Format)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc1123ZString 输出RFC1123Z格式字符串
func (c Carbon) ToRfc1123ZString() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC1123ZFormat)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc2822String 输出RFC2822格式字符串
func (c Carbon) ToRfc2822String() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC2822Format)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc3339String 输出RFC3339格式字符串
func (c Carbon) ToRfc3339String() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC3339Format)< / span >
2020-11-02 10:47:59 +08:00
}
2021-02-01 21:29:21 +08:00
// ToRfc7231String 输出RFC7231格式字符串
func (c Carbon) ToRfc7231String() string < span class = "cov8" title = "1" > {
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
return " "
2020-11-02 10:47:59 +08:00
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Format(RFC7231Format)< / span >
2020-11-02 10:47:59 +08:00
}
// DiffInWeeks 相差多少周
func (start Carbon) DiffInWeeks(end Carbon) int64 < span class = "cov8" title = "1" > {
return start.DiffInSeconds(end) / SecondsPerWeek
}< / span >
2021-01-18 10:34:51 +08:00
// DiffInWeeksWithAbs 相差多少周(绝对值)
2020-11-06 09:28:10 +08:00
func (start Carbon) DiffInWeeksWithAbs(end Carbon) int64 < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
diff := start.DiffInWeeks(end)
if diff < 0 < / span > < span class = "cov8" title = "1" > {
diff = -diff
}< / span >
< span class = "cov8" title = "1" > return diff< / span >
}
// DiffInDays 相差多少天
func (start Carbon) DiffInDays(end Carbon) int64 < span class = "cov8" title = "1" > {
return start.DiffInSeconds(end) / SecondsPerDay
}< / span >
2021-01-18 10:34:51 +08:00
// DiffInDaysWithAbs 相差多少天(绝对值)
2020-11-06 09:28:10 +08:00
func (start Carbon) DiffInDaysWithAbs(end Carbon) int64 < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
diff := start.DiffInDays(end)
if diff < 0 < / span > < span class = "cov8" title = "1" > {
diff = -diff
}< / span >
< span class = "cov8" title = "1" > return diff< / span >
}
// DiffInHours 相差多少小时
func (start Carbon) DiffInHours(end Carbon) int64 < span class = "cov8" title = "1" > {
return start.DiffInSeconds(end) / SecondsPerHour
}< / span >
2021-01-18 10:34:51 +08:00
// DiffInHoursWithAbs 相差多少小时(绝对值)
2020-11-06 09:28:10 +08:00
func (start Carbon) DiffInHoursWithAbs(end Carbon) int64 < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
diff := start.DiffInHours(end)
if diff < 0 < / span > < span class = "cov8" title = "1" > {
diff = -diff
}< / span >
< span class = "cov8" title = "1" > return diff< / span >
}
// DiffInMinutes 相差多少分钟
func (start Carbon) DiffInMinutes(end Carbon) int64 < span class = "cov8" title = "1" > {
return start.DiffInSeconds(end) / SecondsPerMinute
}< / span >
2021-01-18 10:34:51 +08:00
// DiffInMinutesWithAbs 相差多少分钟(绝对值)
2020-11-06 09:28:10 +08:00
func (start Carbon) DiffInMinutesWithAbs(end Carbon) int64 < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
diff := start.DiffInMinutes(end)
if diff < 0 < / span > < span class = "cov8" title = "1" > {
diff = -diff
}< / span >
< span class = "cov8" title = "1" > return diff< / span >
}
// DiffInSeconds 相差多少秒
func (start Carbon) DiffInSeconds(end Carbon) int64 < span class = "cov8" title = "1" > {
if start.Time.IsZero() & & end.Time.IsZero() < / span > < span class = "cov8" title = "1" > {
return 0
}< / span >
< span class = "cov8" title = "1" > if end.Time.IsZero() < / span > < span class = "cov8" title = "1" > {
return -start.ToTimestamp()
}< / span >
< span class = "cov8" title = "1" > if start.Time.IsZero() < / span > < span class = "cov8" title = "1" > {
return end.ToTimestamp()
}< / span >
< span class = "cov8" title = "1" > return end.ToTimestamp() - start.ToTimestamp()< / span >
}
2021-01-18 10:34:51 +08:00
// DiffInSecondsWithAbs 相差多少秒(绝对值)
2020-11-06 09:28:10 +08:00
func (start Carbon) DiffInSecondsWithAbs(end Carbon) int64 < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
diff := start.DiffInSeconds(end)
if diff < 0 < / span > < span class = "cov8" title = "1" > {
diff = -diff
}< / span >
< span class = "cov8" title = "1" > return diff< / span >
}
// DaysInYear 获取本年的总天数
func (c Carbon) DaysInYear() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
< span class = "cov8" title = "1" > days := DaysPerNormalYear
if c.IsLeapYear() < / span > < span class = "cov8" title = "1" > {
days = DaysPerLeapYear
}< / span >
< span class = "cov8" title = "1" > return days< / span >
}
// DaysInMonth 获取本月的总天数
func (c Carbon) DaysInMonth() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.EndOfMonth().Time.In(c.Loc).Day()< / span >
2020-11-02 10:47:59 +08:00
}
// MonthOfYear 获取本年的第几月
func (c Carbon) MonthOfYear() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return int(c.Time.In(c.Loc).Month())< / span >
2020-11-02 10:47:59 +08:00
}
// DayOfYear 获取本年的第几天
func (c Carbon) DayOfYear() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).YearDay()< / span >
2020-11-02 10:47:59 +08:00
}
// DayOfMonth 获取本月的第几天
func (c Carbon) DayOfMonth() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Day()< / span >
2020-11-02 10:47:59 +08:00
}
// DayOfWeek 获取本周的第几天
func (c Carbon) DayOfWeek() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return int(c.Time.In(c.Loc).Weekday())< / span >
2020-11-02 10:47:59 +08:00
}
// WeekOfYear 获取本年的第几周
func (c Carbon) WeekOfYear() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > _, week := c.Time.In(c.Loc).ISOWeek()
2020-11-02 10:47:59 +08:00
return week< / span >
}
// WeekOfMonth 获取本月的第几周
func (c Carbon) WeekOfMonth() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > day := c.Time.In(c.Loc).Day()
2020-11-02 10:47:59 +08:00
if day < DaysPerWeek < / span > < span class = "cov8" title = "1" > {
return 1
}< / span >
< span class = "cov8" title = "1" > return day%DaysPerWeek + 1< / span >
}
// Timezone 获取时区
func (c Carbon) Timezone() string < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Loc.String()
2020-11-02 10:47:59 +08:00
}< / span >
// Age 获取年龄
func (c Carbon) Age() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
< span class = "cov8" title = "1" > if c.ToTimestamp() > Now().ToTimestamp() < / span > < span class = "cov8" title = "1" > {
return 0
}< / span >
2021-01-29 09:42:51 +08:00
< span class = "cov8" title = "1" > age := time.Now().Year() - c.Time.In(c.Loc).Year()
if int(time.Now().Month())*100+time.Now().Day() < int(c.Time.In(c.Loc).Month())*100+c.Time.In(c.Loc).Day() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
age = age - 1
}< / span >
< span class = "cov8" title = "1" > return age< / span >
}
// 获取当前年
func (c Carbon) Year() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Year()< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前季度
func (c Carbon) Quarter() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > month := c.Time.In(c.Loc).Month()
2020-11-02 10:47:59 +08:00
quarter := 0
switch < / span > {
case month > = 1 & & month < = 3:< span class = "cov8" title = "1" >
quarter = 1< / span >
case month > = 4 & & month < = 6:< span class = "cov8" title = "1" >
quarter = 2< / span >
case month > = 7 & & month < = 9:< span class = "cov8" title = "1" >
quarter = 3< / span >
case month > = 10 & & month < = 12:< span class = "cov8" title = "1" >
quarter = 4< / span >
}
< span class = "cov8" title = "1" > return quarter< / span >
}
// 获取当前月
func (c Carbon) Month() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return int(c.Time.In(c.Loc).Month())< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前日
func (c Carbon) Day() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Day()< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前小时
func (c Carbon) Hour() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Hour()< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前分钟数
func (c Carbon) Minute() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Minute()< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前秒数
func (c Carbon) Second() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Second()< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前毫秒数
func (c Carbon) Millisecond() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Nanosecond() / 1e6< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前微秒数
func (c Carbon) Microsecond() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Nanosecond() / 1e9< / span >
2020-11-02 10:47:59 +08:00
}
// 获取当前纳秒数
func (c Carbon) Nanosecond() int < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
if c.IsZero() < / span > < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
return 0
}< / span >
2021-01-26 17:08:27 +08:00
< span class = "cov8" title = "1" > return c.Time.In(c.Loc).Nanosecond()< / span >
2020-11-02 10:47:59 +08:00
}
// IsZero 是否是零值
func (c Carbon) IsZero() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).IsZero()
2020-11-02 10:47:59 +08:00
}< / span >
// IsNow 是否是当前时间
func (c Carbon) IsNow() bool < span class = "cov8" title = "1" > {
return c.ToTimestamp() == c.Now().ToTimestamp()
}< / span >
// IsFuture 是否是未来时间
func (c Carbon) IsFuture() bool < span class = "cov8" title = "1" > {
return c.ToTimestamp() > c.Now().ToTimestamp()
}< / span >
// IsPast 是否是过去时间
func (c Carbon) IsPast() bool < span class = "cov8" title = "1" > {
return c.ToTimestamp() < c.Now().ToTimestamp()
}< / span >
// IsLeapYear 是否是闰年
func (c Carbon) IsLeapYear() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
year := c.Time.In(c.Loc).Year()
2020-11-02 10:47:59 +08:00
if year%400 == 0 || (year%4 == 0 & & year%100 != 0) < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// IsLongYear 是否是长年
func (c Carbon) IsLongYear() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
t := time.Date(c.Year(), time.December, 31, 0, 0, 0, 0, c.Loc)
2020-11-02 10:47:59 +08:00
_, w := t.ISOWeek()
return w == weeksPerLongYear
}< / span >
// IsJanuary 是否是一月
func (c Carbon) IsJanuary() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.January
2020-11-02 10:47:59 +08:00
}< / span >
// IsMonday 是否是二月
func (c Carbon) IsFebruary() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.February
2020-11-02 10:47:59 +08:00
}< / span >
// IsMarch 是否是三月
func (c Carbon) IsMarch() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.March
2020-11-02 10:47:59 +08:00
}< / span >
// IsApril 是否是四月
func (c Carbon) IsApril() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.April
2020-11-02 10:47:59 +08:00
}< / span >
// IsMay 是否是五月
func (c Carbon) IsMay() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.May
2020-11-02 10:47:59 +08:00
}< / span >
// IsJune 是否是六月
func (c Carbon) IsJune() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.June
2020-11-02 10:47:59 +08:00
}< / span >
// IsJuly 是否是七月
func (c Carbon) IsJuly() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.July
2020-11-02 10:47:59 +08:00
}< / span >
// IsAugust 是否是八月
func (c Carbon) IsAugust() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.August
2020-11-02 10:47:59 +08:00
}< / span >
// IsSeptember 是否是九月
func (c Carbon) IsSeptember() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.September
2020-11-02 10:47:59 +08:00
}< / span >
// IsOctober 是否是十月
func (c Carbon) IsOctober() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.October
2020-11-02 10:47:59 +08:00
}< / span >
// IsNovember 是否是十一月
func (c Carbon) IsNovember() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.November
2020-11-02 10:47:59 +08:00
}< / span >
// IsDecember 是否是十二月
func (c Carbon) IsDecember() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Month() == time.December
2020-11-02 10:47:59 +08:00
}< / span >
// IsMonday 是否是周一
func (c Carbon) IsMonday() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Weekday() == time.Monday
2020-11-02 10:47:59 +08:00
}< / span >
// IsTuesday 是否是周二
func (c Carbon) IsTuesday() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Weekday() == time.Tuesday
2020-11-02 10:47:59 +08:00
}< / span >
// IsWednesday 是否是周三
func (c Carbon) IsWednesday() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Weekday() == time.Wednesday
2020-11-02 10:47:59 +08:00
}< / span >
// IsThursday 是否是周四
func (c Carbon) IsThursday() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Weekday() == time.Thursday
2020-11-02 10:47:59 +08:00
}< / span >
// IsFriday 是否是周五
func (c Carbon) IsFriday() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Weekday() == time.Friday
2020-11-02 10:47:59 +08:00
}< / span >
// IsSaturday 是否是周六
func (c Carbon) IsSaturday() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Weekday() == time.Saturday
2020-11-02 10:47:59 +08:00
}< / span >
// IsSunday 是否是周日
func (c Carbon) IsSunday() bool < span class = "cov8" title = "1" > {
2021-01-26 17:08:27 +08:00
return c.Time.In(c.Loc).Weekday() == time.Sunday
2020-11-02 10:47:59 +08:00
}< / span >
// IsWeekday 是否是工作日
func (c Carbon) IsWeekday() bool < span class = "cov8" title = "1" > {
return !c.IsSaturday() & & !c.IsSunday()
}< / span >
// IsWeekend 是否是周末
func (c Carbon) IsWeekend() bool < span class = "cov8" title = "1" > {
return c.IsSaturday() || c.IsSunday()
}< / span >
// IsYesterday 是否是昨天
func (c Carbon) IsYesterday() bool < span class = "cov8" title = "1" > {
2021-01-29 09:42:51 +08:00
return c.ToDateString() == Now().SubDay().ToDateString()
2020-11-02 10:47:59 +08:00
}< / span >
// IsToday 是否是今天
func (c Carbon) IsToday() bool < span class = "cov8" title = "1" > {
return c.ToDateString() == c.Now().ToDateString()
}< / span >
// IsTomorrow 是否是明天
func (c Carbon) IsTomorrow() bool < span class = "cov8" title = "1" > {
2021-01-29 09:42:51 +08:00
return c.ToDateString() == Now().AddDay().ToDateString()
2020-11-02 10:47:59 +08:00
}< / span >
2020-11-06 09:28:10 +08:00
// Compare 时间比较
func (c Carbon) Compare(operator string, t Carbon) bool < span class = "cov8" title = "1" > {
switch operator < / span > {
2021-02-01 21:40:58 +08:00
case " =" :< span class = "cov8" title = "1" >
2020-11-06 09:28:10 +08:00
return c.Time.Equal(t.Time)< / span >
2021-02-01 21:40:58 +08:00
case " < > " :< span class = "cov8" title = "1" >
2020-11-06 09:28:10 +08:00
return !c.Time.Equal(t.Time)< / span >
2021-02-01 21:40:58 +08:00
case " !=" :< span class = "cov8" title = "1" >
2020-11-06 09:28:10 +08:00
return !c.Time.Equal(t.Time)< / span >
2021-02-01 21:40:58 +08:00
case " > " :< span class = "cov8" title = "1" >
2020-11-06 09:28:10 +08:00
return c.Time.After(t.Time)< / span >
2021-02-01 21:40:58 +08:00
case " > =" :< span class = "cov8" title = "1" >
2020-11-06 09:28:10 +08:00
return c.Time.After(t.Time) || c.Time.Equal(t.Time)< / span >
2021-02-01 21:40:58 +08:00
case " < " :< span class = "cov8" title = "1" >
2020-11-06 09:28:10 +08:00
return c.Time.Before(t.Time)< / span >
2021-02-01 21:40:58 +08:00
case " < =" :< span class = "cov8" title = "1" >
2020-11-06 09:28:10 +08:00
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)< / span >
}
< span class = "cov8" title = "1" > return false< / span >
}
// Gt 大于
2021-01-18 10:34:51 +08:00
func (c Carbon) Gt(t Carbon) bool < span class = "cov8" title = "1" > {
2020-11-06 09:28:10 +08:00
return c.Time.After(t.Time)
}< / span >
// Lt 小于
2021-01-18 10:34:51 +08:00
func (c Carbon) Lt(t Carbon) bool < span class = "cov8" title = "1" > {
2020-11-06 09:28:10 +08:00
return c.Time.Before(t.Time)
}< / span >
// Eq 等于
2021-01-18 10:34:51 +08:00
func (c Carbon) Eq(t Carbon) bool < span class = "cov8" title = "1" > {
2020-11-06 09:28:10 +08:00
return c.Time.Equal(t.Time)
}< / span >
// Ne 不等于
2021-01-18 10:34:51 +08:00
func (c Carbon) Ne(t Carbon) bool < span class = "cov8" title = "1" > {
2020-11-06 09:28:10 +08:00
return !c.Time.Equal(t.Time)
}< / span >
// Gte 大于等于
2021-01-18 10:34:51 +08:00
func (c Carbon) Gte(t Carbon) bool < span class = "cov8" title = "1" > {
2020-11-06 09:28:10 +08:00
return c.Time.After(t.Time) || c.Time.Equal(t.Time)
}< / span >
// Lte 小于等于
2021-01-18 10:34:51 +08:00
func (c Carbon) Lte(t Carbon) bool < span class = "cov8" title = "1" > {
2020-11-06 09:28:10 +08:00
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
}< / span >
// Between 是否在两个时间之间(不包括这两个时间)
func (c Carbon) Between(start Carbon, end Carbon) bool < span class = "cov8" title = "1" > {
if c.Time.After(start.Time) & & c.Time.Before(end.Time) < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// BetweenIncludedStartTime 是否在两个时间之间(包括开始时间)
func (c Carbon) BetweenIncludedStartTime(start Carbon, end Carbon) bool < span class = "cov8" title = "1" > {
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) & & c.Time.Before(end.Time) < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// BetweenIncludedEndTime 是否在两个时间之间(包括结束时间)
func (c Carbon) BetweenIncludedEndTime(start Carbon, end Carbon) bool < span class = "cov8" title = "1" > {
if c.Time.After(start.Time) & & (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间)
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool < span class = "cov8" title = "1" > {
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) & & (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) < / span > < span class = "cov8" title = "1" > {
return true
}< / span >
< span class = "cov8" title = "1" > return false< / span >
}
2020-11-02 10:47:59 +08:00
< / pre >
2021-02-01 21:40:58 +08:00
< pre class = "file" id = "file5" style = "display: none" > package carbon
2020-11-02 10:47:59 +08:00
import (
2021-02-01 21:40:58 +08:00
" bytes"
" errors"
" time"
2020-11-02 10:47:59 +08:00
)
2021-02-01 21:29:21 +08:00
// 常规格式化符号
var formats = map[byte]string{
2021-02-01 21:40:58 +08:00
'd': " 02" , // Day: Day of the month, 2 digits with leading zeros. Eg: 01 to 31.
'D': " Mon" , // Day: A textual representation of a day, three letters. Eg: Mon through Sun.
'j': " 2" , // Day: Day of the month without leading zeros. Eg: 1 to 31.
'l': " Monday" , // Day: A full textual representation of the day of the week. Eg: Sunday through Saturday.
'F': " January" , // Month: A full textual representation of a month, such as January or March. Eg: January through December.
'm': " 01" , // Month: Numeric representation of a month, with leading zeros. Eg: 01 through 12.
'M': " Jan" , // Month: A short textual representation of a month, three letters. Eg: Jan through Dec.
'n': " 1" , // Month: Numeric representation of a month, without leading zeros. Eg: 1 through 12.
'Y': " 2006" , // Year: A full numeric representation of a year, 4 digits. Eg: 1999 or 2003.
'y': " 06" , // Year: A two digit representation of a year. Eg: 99 or 03.
'a': " pm" , // Time: Lowercase Ante meridiem and Post meridiem. Eg: am or pm.
'A': " PM" , // Time: Uppercase Ante meridiem and Post meridiem. Eg: AM or PM.
'g': " 3" , // Time: 12-hour format of an hour without leading zeros. Eg: 1 through 12.
'h': " 03" , // Time: 12-hour format of an hour with leading zeros. Eg: 01 through 12.
'H': " 15" , // Time: 24-hour format of an hour with leading zeros. Eg: 00 through 23.
'i': " 04" , // Time: Minutes with leading zeros. Eg: 00 to 59.
's': " 05" , // Time: Seconds with leading zeros. Eg: 00 through 59.
'O': " -0700" , // Zone: Difference to Greenwich time (GMT) in hours. Eg: +0200.
'P': " -07:00" , // Zone: Difference to Greenwich time (GMT) with colon between hours and minutes. Eg: +02:00.
'T': " MST" , // Zone: Timezone abbreviation. Eg: UTC, EST, MDT ...
'c': " 2006-01-02T15:04:05-07:00" , // Format: ISO 8601 date. Eg: 2004-02-12T15:19:21+00:00.
'r': " Mon, 02 Jan 06 15:04 MST" , // Format: RFC 2822 formatted date. Eg: Thu, 21 Dec 2000 16:01:07 +0200.
2021-02-01 21:29:21 +08:00
}
2020-11-02 10:47:59 +08:00
// format转layout
func format2layout(format string) string < span class = "cov8" title = "1" > {
2021-02-01 21:29:21 +08:00
runes := []rune(format)
buffer := bytes.NewBuffer(nil)
for i := 0; i < len(runes); i++ < / span > < span class = "cov8" title = "1" > {
if layout, ok := formats[byte(runes[i])]; ok < / span > < span class = "cov8" title = "1" > {
buffer.WriteString(layout)
}< / span > else< span class = "cov8" title = "1" > {
buffer.WriteRune(runes[i])
}< / span >
}
< span class = "cov8" title = "1" > return buffer.String()< / span >
2021-01-29 09:42:51 +08:00
}
2020-11-02 10:47:59 +08:00
2021-01-26 17:08:27 +08:00
// getLocationByTimezone 通过时区获取Location实例
func getLocationByTimezone(timezone string) (*time.Location, error) < span class = "cov8" title = "1" > {
2020-11-02 10:47:59 +08:00
loc, err := time.LoadLocation(timezone)
if err != nil < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
err = errors.New(" invalid timezone \" " + timezone + " \" , please see the $GOROOT/lib/time/zoneinfo.zip file for all valid timezone" )
2021-01-26 17:08:27 +08:00
}< / span >
< span class = "cov8" title = "1" > return loc, err< / span >
2020-11-02 10:47:59 +08:00
}
// parseByLayout 通过布局模板解析
2021-01-26 17:08:27 +08:00
func parseByLayout(value string, layout string) (time.Time, error) < span class = "cov8" title = "1" > {
loc, _ := time.LoadLocation(Local)
tt, err := time.ParseInLocation(layout, value, loc)
2020-11-02 10:47:59 +08:00
if err != nil < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
err = errors.New(" the value \" " + value + " \" can't parse string as time" )
2021-01-26 17:08:27 +08:00
}< / span >
< span class = "cov8" title = "1" > return tt, err< / span >
2020-11-02 10:47:59 +08:00
}
// parseByDuration 通过持续时间解析
2021-01-26 17:08:27 +08:00
func parseByDuration(duration string) (time.Duration, error) < span class = "cov8" title = "1" > {
td, err := time.ParseDuration(duration)
2021-02-01 21:29:21 +08:00
if err != nil < / span > < span class = "cov8" title = "1" > {
2021-02-01 21:40:58 +08:00
err = errors.New(" invalid duration \" " + duration + " \" " )
2021-01-26 17:08:27 +08:00
}< / span >
< span class = "cov8" title = "1" > return td, err< / span >
2020-11-02 10:47:59 +08:00
}
< / pre >
2021-02-01 21:40:58 +08:00
< / div >
< script >
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
< / script >
< / body >
< / html >