carbon/coverage.html

1778 lines
60 KiB
HTML
Raw Normal View History

2020-11-02 10:47:59 +08:00
<!DOCTYPE html>
<!-- saved from url=(0098)file:///private/var/folders/bt/6b6lck_j5kjc_g91vxwqt70w0000gn/T/cover877896953/coverage.html#file0 -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
2020-11-02 10:47:59 +08:00
<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>
2020-11-06 09:28:10 +08:00
<option value="file2">github.com/golang-module/carbon/database.go (44.4%)</option>
2020-11-02 10:47:59 +08:00
<option value="file3">github.com/golang-module/carbon/final.go (100.0%)</option>
2020-11-02 10:47:59 +08:00
<option value="file4">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 (
// 十二生肖
SymbolicAnimals = [12]string{"猴", "鸡", "狗", "猪", "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊"}
// 天干
HeavenlyStems = [10]string{"庚", "辛", "壬", "癸", "甲", "乙", "丙", "丙", "戊", "己"}
// 地支
EarthlyBranches = [12]string{"申", "酉", "戌", "亥", "子", "丑", "寅", "卯", "辰", "巳", "午", "未"}
)
// ToAnimalYear 获取生肖年
func (c Carbon) ToAnimalYear() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</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">{
return ""
}</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>
<pre class="file" id="file1" style="display: none">package carbon
import (
"strconv"
"strings"
"time"
)
type Carbon struct {
Time time.Time
Loc *time.Location
Error error
2020-11-02 10:47:59 +08:00
}
// Now 当前(指定时区)
func (c Carbon) Now() Carbon <span class="cov8" title="1">{
c.Time = time.Now()
return c
2020-11-02 10:47:59 +08:00
}</span>
// Now 当前(默认时区)
func Now() Carbon <span class="cov8" title="1">{
return SetTimezone(Local).Now()
2020-11-02 10:47:59 +08:00
}</span>
// Tomorrow 明天(指定时区)
func (c Carbon) Tomorrow() Carbon <span class="cov8" title="1">{
c.Time = time.Now().AddDate(0, 0, 1)
return c
2020-11-02 10:47:59 +08:00
}</span>
// Tomorrow 明天(默认时区)
func Tomorrow() Carbon <span class="cov8" title="1">{
return SetTimezone(Local).Tomorrow()
2020-11-02 10:47:59 +08:00
}</span>
// Yesterday 昨天(指定时区)
func (c Carbon) Yesterday() Carbon <span class="cov8" title="1">{
c.Time = time.Now().AddDate(0, 0, -1)
return c
2020-11-02 10:47:59 +08:00
}</span>
// Yesterday 昨天(默认时区)
func Yesterday() Carbon <span class="cov8" title="1">{
return SetTimezone(Local).Yesterday()
}</span>
// CreateFromTimestamp 从时间戳创建Carbon实例(指定时区)
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>
}
<span class="cov8" title="1">c.Time = time.Unix(ts, 0)
return c</span>
2020-11-02 10:47:59 +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>
// CreateFromDateTime 从年月日时分秒创建Carbon实例(指定时区)
func (c Carbon) CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) Carbon <span class="cov8" title="1">{
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>
// 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>
// CreateFromDate 从年月日创建Carbon实例(指定时区)
func (c Carbon) CreateFromDate(year int, month int, day int) Carbon <span class="cov8" title="1">{
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>
// 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>
// CreateFromTime 从时分秒创建Carbon实例(指定时区)
func (c Carbon) CreateFromTime(hour int, minute int, second int) Carbon <span class="cov8" title="1">{
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>
// 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>
// CreateFromGoTime 从原生time.Time创建Carbon实例(指定时区)
func (c Carbon) CreateFromGoTime(tt time.Time) Carbon <span class="cov8" title="1">{
c.Time = tt
return c
2020-11-02 10:47:59 +08:00
}</span>
// CreateFromGoTime 从原生time.Time创建Carbon实例(默认时区)
func CreateFromGoTime(tt time.Time) Carbon <span class="cov8" title="1">{
return SetTimezone(Local).CreateFromGoTime(tt)
}</span>
2020-11-02 10:47:59 +08:00
// Parse 解析标准格式时间字符串(指定时区)
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>
<span class="cov8" title="1">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">{
return c
}</span>
<span class="cov8" title="1">layout := DateTimeFormat
if len(value) == 10 &amp;&amp; strings.Count(value, "-") == 2 </span><span class="cov8" title="1">{
2020-11-02 10:47:59 +08:00
layout = DateFormat
}</span>
<span class="cov8" title="1">if len(value) == 14 </span><span class="cov8" title="1">{
layout = ShortDateTimeFormat
}</span>
<span class="cov8" title="1">if len(value) == 8 </span><span class="cov8" title="1">{
layout = ShortDateFormat
}</span>
<span class="cov8" title="1">if strings.Index(value, "T") == 10 </span><span class="cov8" title="1">{
layout = RFC3339Format
}</span>
<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
}
// 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>
// ParseByFormat 解析指定格式时间字符串(指定时区)
func (c Carbon) ParseByFormat(value string, format 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">layout := format2layout(format)
tt, err := parseByLayout(value, layout)
c.Time = tt
c.Error = err
return c</span>
}
2020-11-02 10:47:59 +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>
// ParseByDuration 解析持续时间字符串(指定时区)
// 支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
2020-11-02 10:47:59 +08:00
func (c Carbon) ParseByDuration(duration 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">td, err := parseByDuration(duration)
c.Time = time.Now().Add(td)
c.Error = err
return c</span>
}
// ParseByDuration 解析持续时间字符串(默认时区)
// 支持正负整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func ParseByDuration(duration string) Carbon <span class="cov8" title="1">{
return SetTimezone(Local).ParseByDuration(duration)
2020-11-02 10:47:59 +08:00
}</span>
// AddDurations 按照持续时间字符串增加时间
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) AddDuration(duration 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">td, err := parseByDuration(duration)
c.Time = c.Time.Add(td)
c.Error = err
return c</span>
}
2020-11-02 10:47:59 +08:00
// SubDurations 按照持续时间字符串减少时间
// 支持整数/浮点数和符号ns(纳秒)、us(微妙)、ms(毫秒)、s(秒)、m(分钟)、h(小时)的组合
func (c Carbon) SubDuration(duration 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">td, err := parseByDuration("-" + duration)
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>
// AddCentury 1世纪后
func (c Carbon) AddCentury() Carbon <span class="cov8" title="1">{
return c.AddYears(YearsPerCentury)
}</span>
// SubCenturies N世纪前
func (c Carbon) SubCenturies(centuries int) Carbon <span class="cov8" title="1">{
return c.SubYears(YearsPerCentury * centuries)
}</span>
// SubCentury 1世纪前
func (c Carbon) SubCentury() Carbon <span class="cov8" title="1">{
return c.SubYears(YearsPerCentury)
}</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>
// AddYear 1年后
func (c Carbon) AddYear() Carbon <span class="cov8" title="1">{
c.Time = c.Time.AddDate(1, 0, 0)
return c
}</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>
// SubYear 1年前
func (c Carbon) SubYear() Carbon <span class="cov8" title="1">{
c.Time = c.Time.AddDate(-1, 0, 0)
return c
}</span>
// AddMonths N季度后
func (c Carbon) AddQuarters(quarters int) Carbon <span class="cov8" title="1">{
return c.AddMonths(quarters * MonthsPerQuarter)
}</span>
// AddMonth 1季度后
func (c Carbon) AddQuarter() Carbon <span class="cov8" title="1">{
return c.AddQuarters(1)
}</span>
// SubMonths N季度前
func (c Carbon) SubQuarters(quarters int) Carbon <span class="cov8" title="1">{
return c.SubMonths(quarters * MonthsPerQuarter)
}</span>
// SubMonth 1季度前
func (c Carbon) SubQuarter() Carbon <span class="cov8" title="1">{
return c.SubQuarters(1)
}</span>
// 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>
// AddMonth 1月后
func (c Carbon) AddMonth() Carbon <span class="cov8" title="1">{
return c.AddMonths(1)
}</span>
// 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>
// SubMonth 1月前
func (c Carbon) SubMonth() Carbon <span class="cov8" title="1">{
return c.SubMonths(1)
}</span>
// 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">{
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">{
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">{
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">{
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">{
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">{
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>
// NextCenturies N世纪后
func (c Carbon) NextCenturies(centuries int) Carbon <span class="cov8" title="1">{
return c.NextYears(centuries * YearsPerCentury)
}</span>
// NextCentury 1世纪后
func (c Carbon) NextCentury() Carbon <span class="cov8" title="1">{
return c.NextYears(YearsPerCentury)
}</span>
// PreCenturies N世纪前
func (c Carbon) PreCenturies(centuries int) Carbon <span class="cov8" title="1">{
return c.PreYears(centuries * YearsPerCentury)
}</span>
// PreYears 1世纪前
func (c Carbon) PreCentury() Carbon <span class="cov8" title="1">{
return c.PreYears(YearsPerCentury)
}</span>
2020-11-02 10:47:59 +08:00
// NextYears N年后
func (c Carbon) NextYears(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)
2020-11-02 10:47:59 +08:00
if day &gt; 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)
2020-11-02 10:47:59 +08:00
return c</span>
}
// NextYear 1年后
func (c Carbon) NextYear() Carbon <span class="cov8" title="1">{
return c.NextYears(1)
}</span>
// PreYears N年前
func (c Carbon) PreYears(years int) Carbon <span class="cov8" title="1">{
return c.NextYears(-years)
}</span>
// PreYear 1年前
func (c Carbon) PreYear() Carbon <span class="cov8" title="1">{
return c.NextYears(-1)
}</span>
// NextQuarters N季度后
func (c Carbon) NextQuarters(quarters int) Carbon <span class="cov8" title="1">{
return c.NextMonths(quarters * MonthsPerQuarter)
}</span>
// NextQuarters 1季度后
func (c Carbon) NextQuarter() Carbon <span class="cov8" title="1">{
return c.NextQuarters(1)
}</span>
// PreQuarters N季度前
func (c Carbon) PreQuarters(quarters int) Carbon <span class="cov8" title="1">{
return c.NextMonths(-quarters * MonthsPerQuarter)
}</span>
// PreQuarter 1季度前
func (c Carbon) PreQuarter() Carbon <span class="cov8" title="1">{
return c.PreQuarters(1)
}</span>
// NextMonths N月后
func (c Carbon) NextMonths(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)
2020-11-02 10:47:59 +08:00
if day &gt; 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)
2020-11-02 10:47:59 +08:00
return c</span>
}
// NextMonth 1月后
func (c Carbon) NextMonth() Carbon <span class="cov8" title="1">{
return c.NextMonths(1)
}</span>
// PreMonths N月前
func (c Carbon) PreMonths(months int) Carbon <span class="cov8" title="1">{
return c.NextMonths(-months)
}</span>
// PreMonth 1月前
func (c Carbon) PreMonth() Carbon <span class="cov8" title="1">{
return c.PreMonths(1)
}</span>
// StartOfYear 本年开始时间
func (c Carbon) StartOfYear() Carbon <span class="cov8" title="1">{
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">{
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">{
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">{
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>
<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>
<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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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>
<pre class="file" id="file2" style="display: none">package carbon
import (
"database/sql/driver"
"fmt"
"time"
)
type ToDateTimeString struct {
Carbon
}
type ToDateString struct {
Carbon
}
type ToTimeString struct {
Carbon
}
type ToTimestamp struct {
Carbon
}
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>
<span class="cov0" title="0">return fmt.Errorf("can not convert %v to timestamp", v)</span>
}
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">{
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
}</span>
func (c ToDateString) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`"%s"`, c.ToDateString())), nil
}</span>
func (c ToTimeString) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`"%s"`, c.ToTimeString())), nil
}</span>
func (c ToTimestamp) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
}</span>
2020-11-06 09:28:10 +08:00
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
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">{
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">{
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">{
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
}</span>
2020-11-02 10:47:59 +08:00
</pre>
<pre class="file" id="file3" style="display: none">package carbon
import (
"time"
)
// ToGoTime 输出carbon实例
func (c Carbon) ToGoTime() time.Time <span class="cov8" title="1">{
return c.Time
}</span>
2020-11-02 10:47:59 +08:00
// ToString 输出字符串
func (c Carbon) ToString() string <span class="cov8" title="1">{
return c.Time.String()
}</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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(format2layout(format))</span>
2020-11-02 10:47:59 +08:00
}
// ToDayDateTimeString 输出天数日期时间字符串
func (c Carbon) ToDayDateTimeString() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.Format(DayDateTimeFormat)</span>
}
// ToDateTimeString 输出日期时间字符串
func (c Carbon) ToDateTimeString() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC3339Format)</span>
2020-11-02 10:47:59 +08:00
}
// ToAnsicString 输出ANSIC格式字符串
func (c Carbon) ToAnsicString() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(KitchenFormat)</span>
2020-11-02 10:47:59 +08:00
}
// ToRfc822String 输出RFC822格式字符串
func (c Carbon) ToRFC822String() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC822Format)</span>
2020-11-02 10:47:59 +08:00
}
// ToRfc822String 输出RFC822Z格式字符串
func (c Carbon) ToRFC822zString() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC822ZFormat)</span>
2020-11-02 10:47:59 +08:00
}
// ToRfc850String 输出RFC850格式字符串
func (c Carbon) ToRFC850String() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC850Format)</span>
2020-11-02 10:47:59 +08:00
}
// ToRfc1036String 输出RFC1036格式字符串
func (c Carbon) ToRFC1036String() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC1036Format)</span>
2020-11-02 10:47:59 +08:00
}
// ToRfc1123String 输出RFC1123格式字符串
func (c Carbon) ToRFC1123String() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC1123Format)</span>
2020-11-02 10:47:59 +08:00
}
// ToRFC1123ZString 输出RFC1123Z格式字符串
func (c Carbon) ToRFC1123ZString() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC1123ZFormat)</span>
2020-11-02 10:47:59 +08:00
}
// ToRFC2822String 输出RFC2822格式字符串
func (c Carbon) ToRFC2822String() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC2822Format)</span>
2020-11-02 10:47:59 +08:00
}
// ToRfc3339String 输出RFC3339格式字符串
func (c Carbon) ToRFC3339String() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<span class="cov8" title="1">return c.Time.In(c.Loc).Format(RFC3339Format)</span>
2020-11-02 10:47:59 +08:00
}
// ToRfc7231String 输出RFC7231格式字符串
func (c Carbon) ToRFC7231String() string <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return ""
}</span>
<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>
// 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 &lt; 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>
// 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 &lt; 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>
// 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 &lt; 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>
// 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 &lt; 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() &amp;&amp; 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>
}
// 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 &lt; 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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<span class="cov8" title="1">day := c.Time.In(c.Loc).Day()
2020-11-02 10:47:59 +08:00
if day &lt; 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">{
return c.Loc.String()
2020-11-02 10:47:59 +08:00
}</span>
// Age 获取年龄
func (c Carbon) Age() int <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<span class="cov8" title="1">if c.ToTimestamp() &gt; Now().ToTimestamp() </span><span class="cov8" title="1">{
return 0
}</span>
<span class="cov8" title="1">age := time.Now().Year() - c.Time.Year()
if int(time.Now().Month())*100+time.Now().Day() &lt; int(c.Time.Month())*100+c.Time.Day() </span><span class="cov8" title="1">{
age = age - 1
}</span>
<span class="cov8" title="1">return age</span>
}
// 获取当前年
func (c Carbon) Year() int <span class="cov8" title="1">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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 &gt;= 1 &amp;&amp; month &lt;= 3:<span class="cov8" title="1">
quarter = 1</span>
case month &gt;= 4 &amp;&amp; month &lt;= 6:<span class="cov8" title="1">
quarter = 2</span>
case month &gt;= 7 &amp;&amp; month &lt;= 9:<span class="cov8" title="1">
quarter = 3</span>
case month &gt;= 10 &amp;&amp; month &lt;= 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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
if c.Time.IsZero() </span><span class="cov8" title="1">{
return 0
}</span>
<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">{
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() &gt; c.Now().ToTimestamp()
}</span>
// IsPast 是否是过去时间
func (c Carbon) IsPast() bool <span class="cov8" title="1">{
return c.ToTimestamp() &lt; c.Now().ToTimestamp()
}</span>
// IsLeapYear 是否是闰年
func (c Carbon) IsLeapYear() bool <span class="cov8" title="1">{
year := c.Time.In(c.Loc).Year()
2020-11-02 10:47:59 +08:00
if year%400 == 0 || (year%4 == 0 &amp;&amp; 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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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">{
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() &amp;&amp; !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">{
return c.ToDateString() == c.Yesterday().ToDateString()
}</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">{
return c.ToDateString() == c.Tomorrow().ToDateString()
}</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>{
case "=":<span class="cov8" title="1">
return c.Time.Equal(t.Time)</span>
case "&lt;&gt;":<span class="cov8" title="1">
return !c.Time.Equal(t.Time)</span>
case "!=":<span class="cov8" title="1">
return !c.Time.Equal(t.Time)</span>
case "&gt;":<span class="cov8" title="1">
return c.Time.After(t.Time)</span>
case "&gt;=":<span class="cov8" title="1">
return c.Time.After(t.Time) || c.Time.Equal(t.Time)</span>
case "&lt;":<span class="cov8" title="1">
return c.Time.Before(t.Time)</span>
case "&lt;=":<span class="cov8" title="1">
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)</span>
}
<span class="cov8" title="1">return false</span>
}
// Gt 大于
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 小于
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 等于
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 不等于
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 大于等于
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 小于等于
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) &amp;&amp; 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)) &amp;&amp; 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) &amp;&amp; (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)) &amp;&amp; (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>
<pre class="file" id="file4" style="display: none">package carbon
import (
"errors"
2020-11-02 10:47:59 +08:00
"strings"
"time"
)
// format转layout
func format2layout(format string) string <span class="cov8" title="1">{
layout := strings.Replace(format, "Y", "2006", 1)
layout = strings.Replace(layout, "y", "06", 1)
layout = strings.Replace(layout, "M", "Jan", 1)
layout = strings.Replace(layout, "m", "01", 1)
layout = strings.Replace(layout, "F", "January", 1)
layout = strings.Replace(layout, "n", "1", 1)
layout = strings.Replace(layout, "l", "Monday", 1)
layout = strings.Replace(layout, "D", "Mon", 1)
layout = strings.Replace(layout, "d", "02", 1)
layout = strings.Replace(layout, "j", "2", 1)
layout = strings.Replace(layout, "H", "15", 1)
layout = strings.Replace(layout, "h", "03", 1)
layout = strings.Replace(layout, "i", "04", 1)
layout = strings.Replace(layout, "s", "05", 1)
layout = strings.Replace(layout, "P", "PM", 1)
layout = strings.Replace(layout, "p", "pm", 1)
return layout
}</span>
// 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">{
err = errors.New("invalid timezone \"" + timezone + "\", please see the $GOROOT/lib/time/zoneinfo.zip file for all valid timezone")
}</span>
<span class="cov8" title="1">return loc, err</span>
2020-11-02 10:47:59 +08:00
}
// parseByLayout 通过布局模板解析
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">{
err = errors.New("the value \"" + value + "\" and layout \"" + layout + "\" don't match")
}</span>
<span class="cov8" title="1">return tt, err</span>
2020-11-02 10:47:59 +08:00
}
// parseByDuration 通过持续时间解析
func parseByDuration(duration string) (time.Duration, error) <span class="cov8" title="1">{
td, err := time.ParseDuration(duration)
2020-11-02 10:47:59 +08:00
if err != nil </span><span class="cov8" title="1">{
err = errors.New("invalid duration \"" + duration + "\"")
}</span>
<span class="cov8" title="1">return td, err</span>
2020-11-02 10:47:59 +08:00
}
</pre>
</div>
2020-11-02 10:47:59 +08:00
<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>