1 Star 0 Fork 64

ajunlonglive/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
traveler_test.go 47.28 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
package carbon
import (
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCarbon_Now(t *testing.T) {
assert := assert.New(t)
actual1 := Now().ToDateString()
expected1 := time.Now().Format(DateLayout)
assert.Equal(expected1, actual1)
actual2 := Now(Local).ToDateString()
expected2 := time.Now().In(time.Local).Format(DateLayout)
assert.Equal(expected2, actual2)
}
func TestCarbon_Yesterday(t *testing.T) {
assert := assert.New(t)
c1 := Yesterday()
expected1 := time.Now().AddDate(0, 0, -1).Format(DateLayout)
assert.Nil(c1.Error)
assert.Equal(expected1, c1.ToDateString())
c2 := Yesterday(Local)
expected2 := time.Now().In(time.Local).AddDate(0, 0, -1).Format(DateLayout)
assert.Nil(c2.Error)
assert.Equal(expected2, c2.ToDateString())
c3 := Parse("2020-08-05").Yesterday()
assert.Nil(c3.Error)
assert.Equal("2020-08-04", c3.ToDateString(), "It should be equal to 2020-08-04")
}
func TestCarbon_Tomorrow(t *testing.T) {
assert := assert.New(t)
c1 := Tomorrow()
expected1 := time.Now().AddDate(0, 0, 1).Format(DateLayout)
assert.Nil(c1.Error)
assert.Equal(expected1, c1.ToDateString())
c2 := Tomorrow(Local)
expected2 := time.Now().In(time.Local).AddDate(0, 0, 1).Format(DateLayout)
assert.Nil(c2.Error)
assert.Equal(expected2, c2.ToDateString())
c3 := Parse("2020-08-05").Tomorrow()
assert.Nil(c3.Error)
assert.Equal("2020-08-06", c3.ToDateString(), "It should be equal to 2020-08-06")
}
func TestCarbon_AddDuration(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
duration string // 输入参数
expected string // 期望值
}{
{"", "10h", ""},
{"0", "10h", ""},
{"0000-00-00", "10h", ""},
{"00:00:00", "10h", ""},
{"0000-00-00 00:00:00", "10h", ""},
{"2020-01-01 13:14:15", "10h", "2020-01-01 23:14:15"},
{"2020-01-01 13:14:15", "10.5h", "2020-01-01 23:44:15"},
{"2020-01-01 13:14:15", "10m", "2020-01-01 13:24:15"},
{"2020-01-01 13:14:15", "10.5m", "2020-01-01 13:24:45"},
{"2020-01-01 13:14:15", "10s", "2020-01-01 13:14:25"},
{"2020-01-01 13:14:15", "10.5s", "2020-01-01 13:14:25"},
}
for index, test := range tests {
c := Parse(test.input).AddDuration(test.duration)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubDuration(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
duration string // 输入参数
expected string // 期望值
}{
{"", "10h", ""},
{"0", "10h", ""},
{"0000-00-00", "10h", ""},
{"00:00:00", "10h", ""},
{"0000-00-00 00:00:00", "10h", ""},
{"2020-01-01 13:14:15", "10h", "2020-01-01 03:14:15"},
{"2020-01-01 13:14:15", "10.5h", "2020-01-01 02:44:15"},
{"2020-01-01 13:14:15", "10m", "2020-01-01 13:04:15"},
{"2020-01-01 13:14:15", "10.5m", "2020-01-01 13:03:45"},
{"2020-01-01 13:14:15", "10s", "2020-01-01 13:14:05"},
{"2020-01-01 13:14:15", "10.5s", "2020-01-01 13:14:04"},
}
for index, test := range tests {
c := Parse(test.input).SubDuration(test.duration)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddCenturies(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
centuries int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2320-01-01"},
{"2020-01-31", 3, "2320-01-31"},
{"2020-02-01", 3, "2320-02-01"},
{"2020-02-28", 3, "2320-02-28"},
{"2020-02-29", 3, "2320-02-29"},
}
for index, test := range tests {
c := Parse(test.input).AddCenturies(test.centuries)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddCenturiesNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
centuries int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2320-01-01"},
{"2020-01-31", 3, "2320-01-31"},
{"2020-02-01", 3, "2320-02-01"},
{"2020-02-28", 3, "2320-02-28"},
{"2020-02-29", 3, "2320-02-29"},
}
for index, test := range tests {
c := Parse(test.input).AddCenturiesNoOverflow(test.centuries)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubCenturies(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
centuries int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "1720-01-01"},
{"2020-01-31", 3, "1720-01-31"},
{"2020-02-01", 3, "1720-02-01"},
{"2020-02-28", 3, "1720-02-28"},
{"2020-02-29", 3, "1720-02-29"},
}
for index, test := range tests {
c := Parse(test.input).SubCenturies(test.centuries)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubCenturiesNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
centuries int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "1720-01-01"},
{"2020-01-31", 3, "1720-01-31"},
{"2020-02-01", 3, "1720-02-01"},
{"2020-02-28", 3, "1720-02-28"},
{"2020-02-29", 3, "1720-02-29"},
}
for index, test := range tests {
c := Parse(test.input).SubCenturiesNoOverflow(test.centuries)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddCentury(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2120-01-01"},
{"2020-01-31", "2120-01-31"},
{"2020-02-01", "2120-02-01"},
{"2020-02-28", "2120-02-28"},
{"2020-02-29", "2120-02-29"},
}
for index, test := range tests {
c := Parse(test.input).AddCentury()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddCenturyNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2120-01-01"},
{"2020-01-31", "2120-01-31"},
{"2020-02-01", "2120-02-01"},
{"2020-02-28", "2120-02-28"},
{"2020-02-29", "2120-02-29"},
}
for index, test := range tests {
c := Parse(test.input).AddCenturyNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubCentury(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "1920-01-01"},
{"2020-01-31", "1920-01-31"},
{"2020-02-01", "1920-02-01"},
{"2020-02-28", "1920-02-28"},
{"2020-02-29", "1920-02-29"},
}
for index, test := range tests {
c := Parse(test.input).SubCentury()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubCenturyNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "1920-01-01"},
{"2020-01-31", "1920-01-31"},
{"2020-02-01", "1920-02-01"},
{"2020-02-28", "1920-02-28"},
{"2020-02-29", "1920-02-29"},
}
for index, test := range tests {
c := Parse(test.input).SubCenturyNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddDecades(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
decades int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2050-01-01"},
{"2020-01-31", 3, "2050-01-31"},
{"2020-02-01", 3, "2050-02-01"},
{"2020-02-28", 3, "2050-02-28"},
{"2020-02-29", 3, "2050-03-01"},
}
for index, test := range tests {
c := Parse(test.input).AddDecades(test.decades)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddDecadesNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
centuries int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2050-01-01"},
{"2020-01-31", 3, "2050-01-31"},
{"2020-02-01", 3, "2050-02-01"},
{"2020-02-28", 3, "2050-02-28"},
{"2020-02-29", 3, "2050-02-28"},
}
for index, test := range tests {
c := Parse(test.input).AddDecadesNoOverflow(test.centuries)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddDecade(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2030-01-01"},
{"2020-01-31", "2030-01-31"},
{"2020-02-01", "2030-02-01"},
{"2020-02-28", "2030-02-28"},
{"2020-02-29", "2030-03-01"},
}
for index, test := range tests {
c := Parse(test.input).AddDecade()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddDecadeNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2030-01-01"},
{"2020-01-31", "2030-01-31"},
{"2020-02-01", "2030-02-01"},
{"2020-02-28", "2030-02-28"},
{"2020-02-29", "2030-02-28"},
}
for index, test := range tests {
c := Parse(test.input).AddDecadeNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubDecades(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
decades int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "1990-01-01"},
{"2020-01-31", 3, "1990-01-31"},
{"2020-02-01", 3, "1990-02-01"},
{"2020-02-28", 3, "1990-02-28"},
{"2020-02-29", 3, "1990-03-01"},
}
for index, test := range tests {
c := Parse(test.input).SubDecades(test.decades)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubDecadesNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
centuries int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "1990-01-01"},
{"2020-01-31", 3, "1990-01-31"},
{"2020-02-01", 3, "1990-02-01"},
{"2020-02-28", 3, "1990-02-28"},
{"2020-02-29", 3, "1990-02-28"},
}
for index, test := range tests {
c := Parse(test.input).SubDecadesNoOverflow(test.centuries)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubDecade(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2010-01-01"},
{"2020-01-31", "2010-01-31"},
{"2020-02-01", "2010-02-01"},
{"2020-02-28", "2010-02-28"},
{"2020-02-29", "2010-03-01"},
}
for index, test := range tests {
c := Parse(test.input).SubDecade()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubDecadeNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2010-01-01"},
{"2020-01-31", "2010-01-31"},
{"2020-02-01", "2010-02-01"},
{"2020-02-28", "2010-02-28"},
{"2020-02-29", "2010-02-28"},
}
for index, test := range tests {
c := Parse(test.input).SubDecadeNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddYears(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
years int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2023-01-01"},
{"2020-01-31", 3, "2023-01-31"},
{"2020-02-01", 3, "2023-02-01"},
{"2020-02-28", 3, "2023-02-28"},
{"2020-02-29", 3, "2023-03-01"},
}
for index, test := range tests {
c := Parse(test.input).AddYears(test.years)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddYearsNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
years int // 输入参数
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2023-01-01"},
{"2020-01-31", 3, "2023-01-31"},
{"2020-02-01", 3, "2023-02-01"},
{"2020-02-28", 3, "2023-02-28"},
{"2020-02-29", 3, "2023-02-28"},
}
for index, test := range tests {
c := Parse(test.input).AddYearsNoOverflow(test.years)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubYears(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
years int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2017-01-01"},
{"2020-01-31", 3, "2017-01-31"},
{"2020-02-01", 3, "2017-02-01"},
{"2020-02-28", 3, "2017-02-28"},
{"2020-02-29", 3, "2017-03-01"},
}
for index, test := range tests {
c := Parse(test.input).SubYears(test.years)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubYearsNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
years int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2017-01-01"},
{"2020-01-31", 3, "2017-01-31"},
{"2020-02-01", 3, "2017-02-01"},
{"2020-02-28", 3, "2017-02-28"},
{"2020-02-29", 3, "2017-02-28"},
}
for index, test := range tests {
c := Parse(test.input).SubYearsNoOverflow(test.years)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2021-01-01"},
{"2020-01-31", "2021-01-31"},
{"2020-02-01", "2021-02-01"},
{"2020-02-28", "2021-02-28"},
{"2020-02-29", "2021-03-01"},
}
for index, test := range tests {
c := Parse(test.input).AddYear()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddYearNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2021-01-01"},
{"2020-01-31", "2021-01-31"},
{"2020-02-01", "2021-02-01"},
{"2020-02-28", "2021-02-28"},
{"2020-02-29", "2021-02-28"},
}
for index, test := range tests {
c := Parse(test.input).AddYearNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubYear(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2019-01-01"},
{"2020-01-31", "2019-01-31"},
{"2020-02-01", "2019-02-01"},
{"2020-02-28", "2019-02-28"},
{"2020-02-29", "2019-03-01"},
}
for index, test := range tests {
c := Parse(test.input).SubYear()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubYearNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2019-01-01"},
{"2020-01-31", "2019-01-31"},
{"2020-02-01", "2019-02-01"},
{"2020-02-28", "2019-02-28"},
{"2020-02-29", "2019-02-28"},
}
for index, test := range tests {
c := Parse(test.input).SubYearNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddQuarters(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
quarters int
expected string // 期望值
}{
{"", 2, ""},
{"0", 2, ""},
{"0000-00-00", 2, ""},
{"00:00:00", 2, ""},
{"0000-00-00 00:00:00", 2, ""},
{"2019-08-01", 2, "2020-02-01"},
{"2019-08-31", 2, "2020-03-02"},
{"2020-01-01", 2, "2020-07-01"},
{"2020-02-28", 2, "2020-08-28"},
{"2020-02-29", 2, "2020-08-29"},
{"2020-08-05", 2, "2021-02-05"},
{"2020-08-31", 2, "2021-03-03"},
}
for index, test := range tests {
c := Parse(test.input).AddQuarters(test.quarters)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddQuartersNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
quarters int
expected string // 期望值
}{
{"", 2, ""},
{"0", 2, ""},
{"0000-00-00", 2, ""},
{"00:00:00", 2, ""},
{"0000-00-00 00:00:00", 2, ""},
{"2019-08-01", 2, "2020-02-01"},
{"2019-08-31", 2, "2020-02-29"},
{"2020-01-01", 2, "2020-07-01"},
{"2020-02-28", 2, "2020-08-28"},
{"2020-02-29", 2, "2020-08-29"},
{"2020-08-05", 2, "2021-02-05"},
{"2020-08-31", 2, "2021-02-28"},
}
for index, test := range tests {
c := Parse(test.input).AddQuartersNoOverflow(test.quarters)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubQuarters(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
quarters int
expected string // 期望值
}{
{"", 2, ""},
{"0", 2, ""},
{"0000-00-00", 2, ""},
{"00:00:00", 2, ""},
{"0000-00-00 00:00:00", 2, ""},
{"2019-08-01", 2, "2019-02-01"},
{"2019-08-31", 2, "2019-03-03"},
{"2020-01-01", 2, "2019-07-01"},
{"2020-02-28", 2, "2019-08-28"},
{"2020-02-29", 2, "2019-08-29"},
{"2020-08-05", 2, "2020-02-05"},
{"2020-08-31", 2, "2020-03-02"},
}
for index, test := range tests {
c := Parse(test.input).SubQuarters(test.quarters)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubQuartersNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
quarters int
expected string // 期望值
}{
{"", 2, ""},
{"0", 2, ""},
{"0000-00-00", 2, ""},
{"00:00:00", 2, ""},
{"0000-00-00 00:00:00", 2, ""},
{"2019-08-01", 2, "2019-02-01"},
{"2019-08-31", 2, "2019-02-28"},
{"2020-01-01", 2, "2019-07-01"},
{"2020-02-28", 2, "2019-08-28"},
{"2020-02-29", 2, "2019-08-29"},
{"2020-08-05", 2, "2020-02-05"},
{"2020-08-31", 2, "2020-02-29"},
}
for index, test := range tests {
c := Parse(test.input).SubQuartersNoOverflow(test.quarters)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddQuarter(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2019-11-01", "2020-02-01"},
{"2019-11-30", "2020-03-01"},
{"2020-02-28", "2020-05-28"},
{"2020-02-29", "2020-05-29"},
{"2020-08-31", "2020-12-01"},
{"2020-11-01", "2021-02-01"},
{"2020-11-30", "2021-03-02"},
}
for index, test := range tests {
c := Parse(test.input).AddQuarter()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddQuarterNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2019-11-01", "2020-02-01"},
{"2019-11-30", "2020-02-29"},
{"2020-02-28", "2020-05-28"},
{"2020-02-29", "2020-05-29"},
{"2020-08-31", "2020-11-30"},
{"2020-11-01", "2021-02-01"},
{"2020-11-30", "2021-02-28"},
}
for index, test := range tests {
c := Parse(test.input).AddQuarterNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubQuarter(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2019-04-01", "2019-01-01"},
{"2019-04-30", "2019-01-30"},
{"2020-04-01", "2020-01-01"},
{"2020-04-30", "2020-01-30"},
{"2020-05-01", "2020-02-01"},
{"2020-05-31", "2020-03-02"},
{"2020-08-05", "2020-05-05"},
}
for index, test := range tests {
c := Parse(test.input).SubQuarter()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubQuarterNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2019-04-01", "2019-01-01"},
{"2019-04-30", "2019-01-30"},
{"2020-04-01", "2020-01-01"},
{"2020-04-30", "2020-01-30"},
{"2020-05-01", "2020-02-01"},
{"2020-05-31", "2020-02-29"},
{"2020-08-05", "2020-05-05"},
}
for index, test := range tests {
c := Parse(test.input).SubQuarterNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMonths(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
months int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2020-04-01"},
{"2020-01-31", 3, "2020-05-01"},
{"2020-02-01", 3, "2020-05-01"},
{"2020-02-28", 3, "2020-05-28"},
{"2020-02-29", 3, "2020-05-29"},
}
for index, test := range tests {
c := Parse(test.input).AddMonths(test.months)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMonthsNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
months int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2020-04-01"},
{"2020-01-31", 3, "2020-04-30"},
{"2020-02-01", 3, "2020-05-01"},
{"2020-02-28", 3, "2020-05-28"},
{"2020-02-29", 3, "2020-05-29"},
}
for index, test := range tests {
c := Parse(test.input).AddMonthsNoOverflow(test.months)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMonths(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
months int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2019-10-01"},
{"2020-01-31", 3, "2019-10-31"},
{"2020-02-01", 3, "2019-11-01"},
{"2020-02-28", 3, "2019-11-28"},
{"2020-02-29", 3, "2019-11-29"},
}
for index, test := range tests {
c := Parse(test.input).SubMonths(test.months)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMonthsNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
months int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2019-10-01"},
{"2020-01-31", 3, "2019-10-31"},
{"2020-02-01", 3, "2019-11-01"},
{"2020-02-28", 3, "2019-11-28"},
{"2020-02-29", 3, "2019-11-29"},
}
for index, test := range tests {
c := Parse(test.input).SubMonthsNoOverflow(test.months)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMonth(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2020-02-01"},
{"2020-01-31", "2020-03-02"},
{"2020-02-01", "2020-03-01"},
{"2020-02-28", "2020-03-28"},
{"2020-02-29", "2020-03-29"},
}
for index, test := range tests {
c := Parse(test.input).AddMonth()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMonthNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2020-02-01"},
{"2020-01-31", "2020-02-29"},
{"2020-02-01", "2020-03-01"},
{"2020-02-28", "2020-03-28"},
{"2020-02-29", "2020-03-29"},
}
for index, test := range tests {
c := Parse(test.input).AddMonthNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMonth(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2019-12-01"},
{"2020-01-31", "2019-12-31"},
{"2020-02-01", "2020-01-01"},
{"2020-02-28", "2020-01-28"},
{"2020-02-29", "2020-01-29"},
}
for index, test := range tests {
c := Parse(test.input).SubMonth()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMonthNoOverflow(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2019-12-01"},
{"2020-01-31", "2019-12-31"},
{"2020-02-01", "2020-01-01"},
{"2020-02-28", "2020-01-28"},
{"2020-02-29", "2020-01-29"},
}
for index, test := range tests {
c := Parse(test.input).SubMonthNoOverflow()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddWeeks(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
weeks int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2020-01-22"},
{"2020-01-31", 3, "2020-02-21"},
{"2020-02-01", 3, "2020-02-22"},
{"2020-02-28", 3, "2020-03-20"},
{"2020-02-29", 3, "2020-03-21"},
}
for index, test := range tests {
c := Parse(test.input).AddWeeks(test.weeks)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubWeeks(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
weeks int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2019-12-11"},
{"2020-01-31", 3, "2020-01-10"},
{"2020-02-01", 3, "2020-01-11"},
{"2020-02-28", 3, "2020-02-07"},
{"2020-02-29", 3, "2020-02-08"},
}
for index, test := range tests {
c := Parse(test.input).SubWeeks(test.weeks)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddWeek(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2020-01-08"},
{"2020-01-31", "2020-02-07"},
{"2020-02-01", "2020-02-08"},
{"2020-02-28", "2020-03-06"},
{"2020-02-29", "2020-03-07"},
}
for index, test := range tests {
c := Parse(test.input).AddWeek()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubWeek(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2019-12-25"},
{"2020-01-31", "2020-01-24"},
{"2020-02-01", "2020-01-25"},
{"2020-02-28", "2020-02-21"},
{"2020-02-29", "2020-02-22"},
}
for index, test := range tests {
c := Parse(test.input).SubWeek()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddDays(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
days int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2020-01-04"},
{"2020-01-31", 3, "2020-02-03"},
{"2020-02-01", 3, "2020-02-04"},
{"2020-02-28", 3, "2020-03-02"},
{"2020-02-29", 3, "2020-03-03"},
}
for index, test := range tests {
c := Parse(test.input).AddDays(test.days)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubDays(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
days int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-01-01", 3, "2019-12-29"},
{"2020-01-31", 3, "2020-01-28"},
{"2020-02-01", 3, "2020-01-29"},
{"2020-02-28", 3, "2020-02-25"},
{"2020-02-29", 3, "2020-02-26"},
}
for index, test := range tests {
c := Parse(test.input).SubDays(test.days)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddDay(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2020-01-02"},
{"2020-01-31", "2020-02-01"},
{"2020-02-01", "2020-02-02"},
{"2020-02-28", "2020-02-29"},
{"2020-02-29", "2020-03-01"},
}
for index, test := range tests {
c := Parse(test.input).AddDay()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubDay(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-01-01", "2019-12-31"},
{"2020-01-31", "2020-01-30"},
{"2020-02-01", "2020-01-31"},
{"2020-02-28", "2020-02-27"},
{"2020-02-29", "2020-02-28"},
}
for index, test := range tests {
c := Parse(test.input).SubDay()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddHours(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
hours int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15", 3, "2020-08-05 16:14:15"},
}
for index, test := range tests {
c := Parse(test.input).AddHours(test.hours)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubHours(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
hours int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15", 3, "2020-08-05 10:14:15"},
}
for index, test := range tests {
c := Parse(test.input).SubHours(test.hours)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddHour(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 14:14:15"},
}
for index, test := range tests {
c := Parse(test.input).AddHour()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubHour(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 12:14:15"},
}
for index, test := range tests {
c := Parse(test.input).SubHour()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMinutes(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
minutes int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15", 3, "2020-08-05 13:17:15"},
}
for index, test := range tests {
c := Parse(test.input).AddMinutes(test.minutes)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMinutes(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
minutes int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15", 3, "2020-08-05 13:11:15"},
}
for index, test := range tests {
c := Parse(test.input).SubMinutes(test.minutes)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMinute(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 13:15:15"},
}
for index, test := range tests {
c := Parse(test.input).AddMinute()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMinute(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 13:13:15"},
}
for index, test := range tests {
c := Parse(test.input).SubMinute()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddSeconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
seconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15", 3, "2020-08-05 13:14:18"},
}
for index, test := range tests {
c := Parse(test.input).AddSeconds(test.seconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubSeconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
seconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15", 3, "2020-08-05 13:14:12"},
}
for index, test := range tests {
c := Parse(test.input).SubSeconds(test.seconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddSecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 13:14:16"},
}
for index, test := range tests {
c := Parse(test.input).AddSecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubSecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15", "2020-08-05 13:14:14"},
}
for index, test := range tests {
c := Parse(test.input).SubSecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMilliseconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
milliseconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15.222222222", 3, "2020-08-05 13:14:15.225222222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).AddMilliseconds(test.milliseconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMilliseconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
milliseconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15.222222222", 3, "2020-08-05 13:14:15.219222222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SubMilliseconds(test.milliseconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMillisecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15.222222222", "2020-08-05 13:14:15.223222222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).AddMillisecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMillisecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15.222222222", "2020-08-05 13:14:15.221222222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SubMillisecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMicroseconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
microseconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15.222222222", 3, "2020-08-05 13:14:15.222225222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).AddMicroseconds(test.microseconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMicroseconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
microseconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15.222222222", 3, "2020-08-05 13:14:15.222219222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SubMicroseconds(test.microseconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddMicrosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15.222222222", "2020-08-05 13:14:15.222223222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).AddMicrosecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubMicrosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15.222222222", "2020-08-05 13:14:15.222221222 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SubMicrosecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddNanoseconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
nanoseconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15.222222222", 3, "2020-08-05 13:14:15.222222225 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).AddNanoseconds(test.nanoseconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubNanoseconds(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
nanoseconds int
expected string // 期望值
}{
{"", 3, ""},
{"0", 3, ""},
{"0000-00-00", 3, ""},
{"00:00:00", 3, ""},
{"0000-00-00 00:00:00", 3, ""},
{"2020-08-05 13:14:15.222222222", 3, "2020-08-05 13:14:15.222222219 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SubNanoseconds(test.nanoseconds)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_AddNanosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15.222222222", "2020-08-05 13:14:15.222222223 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).AddNanosecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SubNanosecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
expected string // 期望值
}{
{"", ""},
{"0", ""},
{"0000-00-00", ""},
{"00:00:00", ""},
{"0000-00-00 00:00:00", ""},
{"2020-08-05 13:14:15.222222222", "2020-08-05 13:14:15.222222221 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SubNanosecond()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_Traveler(t *testing.T) {
duration := "10x"
c := Parse("2020-08-05").AddDuration(duration)
assert.NotNil(t, c.Error, "It should catch an exception in AddDuration()")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/ajunlonglive/carbon.git
git@gitee.com:ajunlonglive/carbon.git
ajunlonglive
carbon
carbon
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385