Merge branch 'v2'

This commit is contained in:
gouguoyin@juzifenqi.com 2022-04-14 13:46:11 +08:00
commit 40215af6f9
19 changed files with 438 additions and 532 deletions

View File

@ -71,7 +71,6 @@ carbon.Now().ToDateString() // 2020-08-05
carbon.Now().ToTimeString() // 13:14:15
// 指定时区的今天此刻
carbon.Now(Carbon.NewYork).ToDateTimeString() // 2020-08-05 14:14:15
carbon.SetTimezone(Carbon.NewYork).Now().ToDateTimeString() // 2020-08-05 14:14:15
// 今天秒级时间戳
carbon.Now().Timestamp() // 1596604455
// 今天毫秒级时间戳
@ -92,7 +91,6 @@ carbon.Yesterday().ToTimeString() // 13:14:15
carbon.Parse("2021-01-28 13:14:15").Yesterday().ToDateTimeString() // 2021-01-27 13:14:15
// 指定时区的昨天此刻
carbon.Yesterday(Carbon.NewYork).ToDateTimeString() // 2020-08-04 14:14:15
carbon.SetTimezone(Carbon.NewYork).Yesterday().ToDateTimeString() // 2020-08-04 14:14:15
// 昨天秒级时间戳
carbon.Yesterday().Timestamp() // 1596518055
// 昨天毫秒级时间戳
@ -113,7 +111,6 @@ carbon.Tomorrow().ToTimeString() // 13:14:15
carbon.Parse("2021-01-28 13:14:15").Tomorrow().ToDateTimeString() // 2021-01-29 13:14:15
// 指定时区的明天此刻
carbon.Tomorrow(Carbon.NewYork).ToDateTimeString() // 2020-08-06 14:14:15
carbon.SetTimezone(Carbon.NewYork).Tomorrow().ToDateTimeString() // 2020-08-06 14:14:15
// 明天秒级时间戳
carbon.Tomorrow().Timestamp() // 1596690855
// 明天毫秒级时间戳
@ -129,42 +126,29 @@ carbon.Tomorrow().TimestampNano() // 1596690855000000000
```go
// 从秒级时间戳创建 Carbon 实例
carbon.CreateFromTimestamp(-1).ToString() // 1970-01-01 07:59:59 +0800 CST
carbon.CreateFromTimestamp(-1, carbon.Tokyo).ToString() // 1970-01-01 08:59:59 +0900 JST
carbon.CreateFromTimestamp(0).ToString() // 1970-01-01 08:00:00 +0800 CST
carbon.CreateFromTimestamp(0, carbon.Tokyo).ToString() // 1970-01-01 09:00:00 +0900 JST
carbon.CreateFromTimestamp(1).ToString() // 1970-01-01 08:00:01 +0800 CST
carbon.CreateFromTimestamp(1, carbon.Tokyo).ToString() // 1970-01-01 09:00:01 +0900 JST
carbon.CreateFromTimestamp(1649735755).ToString() // 2022-04-12 11:55:55 +0800 CST
carbon.CreateFromTimestamp(1649735755, carbon.Tokyo).ToString() // 2022-04-12 12:55:55 +0900 JST
// 从毫秒级时间戳创建 Carbon 实例
carbon.CreateFromTimestampMilli(1649735755981).ToString() // 2022-04-12 11:55:55.981 +0800 CST
carbon.CreateFromTimestampMilli(1649735755981, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981 +0900 JST
// 从微秒级时间戳创建 Carbon 实例
carbon.CreateFromTimestampMicro(1649735755981566).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
carbon.CreateFromTimestampMicro(1649735755981566, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981566 +0900 JST
// 从纳秒级时间戳创建 Carbon 实例
carbon.CreateFromTimestampNano(1649735755981566000).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
carbon.CreateFromTimestampNano(1649735755981566000, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981566 +0900 CST
// 从年月日时分秒创建 Carbon 实例
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
// 从年月日时分秒创建 Carbon 实例,包含毫秒
carbon.CreateFromDateTimeMilli(2020, 1, 1, 13, 14, 15, 999).ToString() // 2020-01-01 13:14:15.999 +0800 CST
carbon.CreateFromDateTimeMilli(2020, 1, 1, 13, 14, 15, 999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999 +0900 JST
// 从年月日时分秒创建 Carbon 实例,包含微秒
carbon.CreateFromDateTimeMicro(2020, 1, 1, 13, 14, 15, 999999).ToString() // 2020-01-01 13:14:15.999999 +0800 CST
carbon.CreateFromDateTimeMicro(2020, 1, 1, 13, 14, 15, 999999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999999 +0900 JST
// 从年月日时分秒创建 Carbon 实例,包含纳秒
carbon.CreateFromDateTimeNano(2020, 1, 1, 13, 14, 15, 999999999).ToString() // 2020-01-01 13:14:15.999999999 +0800 CST
carbon.CreateFromDateTimeNano(2020, 1, 1, 13, 14, 15, 999999999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999999999 +0900 JST
// 从年月日创建 Carbon 实例(时分秒默认为当前时分秒)
carbon.CreateFromDate(2020, 8, 5).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromDate(2020, 8, 5, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
// 从时分秒创建 Carbon 实例(年月日默认为当前年月日)
carbon.CreateFromTime(13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromTime(13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
```
##### 将标准格式时间字符串解析成 Carbon 实例
@ -298,7 +282,7 @@ carbon.Parse("2020-02-29 13:14:15").SubCentury().ToDateTimeString() // 1920-02-2
carbon.Parse("2020-02-29 13:14:15").SubCenturyNoOverflow().ToDateTimeString() // 1920-02-29 13:14:15
// 三个年代后
carbon.Parse("2020-02-29 13:14:15").Decades(3).ToDateTimeString() // 2050-03-01 13:14:15
carbon.Parse("2020-02-29 13:14:15").AddDecades(3).ToDateTimeString() // 2050-03-01 13:14:15
// 三个年代后(月份不溢出)
carbon.Parse("2020-02-29 13:14:15").AddDecadesNoOverflow(3).ToDateTimeString() // 2050-02-28 13:14:15
// 一个年代后
@ -727,6 +711,13 @@ carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
// 获取当前年月日时分秒
carbon.Parse("2020-08-05 13:14:15").DateTime() // 2020,8,5,13,14,15
// 获取当前年月日时分秒毫秒
carbon.Parse("2020-08-05 13:14:15").DateTimeMilli() // 2020,8,5,13,14,15,999
// 获取当前年月日时分秒微秒
carbon.Parse("2020-08-05 13:14:15").DateTimeMicro() // 2020,8,5,13,14,15,999999
// 获取当前年月日时分秒纳秒
carbon.Parse("2020-08-05 13:14:15").DateTimeNano() // 2020,8,5,13,14,15,999999999
// 获取当前年月日
carbon.Parse("2020-08-05 13:14:15").Date() // 2020,8,5
// 获取当前时分秒
@ -808,133 +799,95 @@ carbon.Parse("2002-12-31 13:14:15").Age() // 18
```go
// 输出日期时间字符串
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeString() // 2020-08-05 13:14:15
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeString(carbon.Tokyo) // 2020-08-05 14:14:15
// 输出日期时间字符串,包含毫秒
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMilliString() // 2020-08-05 13:14:15.999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMilliString(carbon.Tokyo) // 2020-08-05 14:14:15.999
// 输出日期时间字符串,包含微秒
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMicroString() // 2020-08-05 13:14:15.999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMicroString(carbon.Tokyo) // 2020-08-05 14:14:15.999999
// 输出日期时间字符串,包含纳秒
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeNanoString() // 2020-08-05 13:14:15.999999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeNanoString(carbon.Tokyo) // 2020-08-05 14:14:15.999999999
// 输出简写日期时间字符串
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeString() // 20200805131415
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeString(carbon.Tokyo) // 20200805141415
// 输出简写日期时间字符串,包含毫秒
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMilliString() // 20200805131415.999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMilliString(carbon.Tokyo) // 20200805141415.999
// 输出简写日期时间字符串,包含微秒
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMicroString() // 20200805131415.999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMicroString(carbon.Tokyo) // 20200805141415.999999
// 输出简写日期时间字符串,包含纳秒
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeNanoString() // 20200805131415.999999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeNanoString(carbon.Tokyo) // 20200805141415.999999999
// 输出日期字符串
carbon.Parse("2020-08-05 13:14:15").ToDateString() // 2020-08-05
carbon.Parse("2020-08-05 13:14:15").ToDateString(carbon.Tokyo) // 2020-08-05
// 输出简写日期字符串
carbon.Parse("2020-08-05 13:14:15").ToShortDateString() // 20200805
carbon.Parse("2020-08-05 13:14:15").ToShortDateString(carbon.Tokyo) // 20200805
// 输出时间字符串
carbon.Parse("2020-08-05 13:14:15").ToTimeString() // 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToTimeString(carbon.Tokyo) // 14:14:15
// 输出简写时间字符串
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString() // 131415
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString(carbon.Tokyo) // 141415
// 输出 Ansic 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
carbon.Parse("2020-08-05 13:14:15").ToAnsicString(carbon.Tokyo) // Wed Aug 5 14:14:15 2020
// 输出 Atom 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToAtomString(carbon.Tokyo) // 2020-08-05T14:14:15+08:00
// 输出 UnixDate 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString() // Wed Aug 5 13:14:15 CST 2020
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString(carbon.Tokyo) // Wed Aug 5 14:14:15 JST 2020
// 输出 RubyDate 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString() // Wed Aug 05 13:14:15 +0800 2020
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString(carbon.Tokyo) // Wed Aug 05 14:14:15 +0900 2020
// 输出 Kitchen 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToKitchenString() // 1:14PM
carbon.Parse("2020-08-05 13:14:15").ToKitchenString(carbon.Tokyo) // 2:14PM
// 输出 Cookie 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToCookieString() // Wednesday, 05-Aug-2020 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToCookieString(carbon.Tokyo) // Wednesday, 05-Aug-2020 14:14:15 JST
// 输出 DayDateTime 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString() // Wed, Aug 5, 2020 1:14 PM
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString(carbon.Tokyo) // Wed, Aug 5, 2020 2:14 PM
// 输出 RSS 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRssString() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRssString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
// 输出 W3C 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToW3cString(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// 输出 ISO8601 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToIso8601String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToIso8601String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// 输出 RFC822 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc822String(carbon.Tokyo) // 05 Aug 20 14:14 JST
// 输出 RFC822Z 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString() // 05 Aug 20 13:14 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString(carbon.Tokyo) // 05 Aug 20 14:14 +0900
// 输出 RFC850 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc850String() // Wednesday, 05-Aug-20 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc850String(carbon.Tokyo) // Wednesday, 05-Aug-20 14:14:15 JST
// 输出 RFC1036 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String() // Wed, 05 Aug 20 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String(carbon.Tokyo) // Wed, 05 Aug 20 14:14:15 +0900
// 输出 RFC1123 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String() // Wed, 05 Aug 2020 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 JST
// 输出 RFC1123Z 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 0800
// 输出 RFC2822 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
// 输出 RFC7231 格式字符串
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String() // Wed, 05 Aug 2020 13:14:15 GMT
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 GMT
// 输出 RFC3339 格式字符串
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// 输出 RFC3339Milli 格式字符串
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MilliString() // 2020-08-05T13:14:15.999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MilliString(carbon.Tokyo) // 2020-08-05T13:14:15.999+09:00
// 输出 RFC3339Micro 格式字符串
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MicroString() // 2020-08-05T13:14:15.999999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MicroString(carbon.Tokyo) // 2020-08-05T13:14:15.999999+09:00
// 输出 RFC3339Nano 格式字符串
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339NanoString() // 2020-08-05T13:14:15.999999999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339NanoString(carbon.Tokyo) // 2020-08-05T13:14:15.999999999+09:00
// 输出日期时间字符串
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15")) // 2020-08-05 13:14:15
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15", carbon.Tokyo)) // 2020-08-05 13:14:15
// 输出"2006-01-02 15:04:05.999999999 -0700 MST"格式字符串
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05 13:14:15").ToString(carbon.Tokyo) // 2020-08-05 14:14:15999999 +0900 JST
// 输出指定布局的字符串,Layout()是ToLayoutString()的简写
carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").Layout("2006-01-02 15:04:05", carbon.Tokyo) // 2020-08-05 14:14:15
// 输出指定格式的字符串,Format()是ToFormatString()的简写(如果使用的字母与格式化字符冲突时,请使用\符号转义该字符)
carbon.Parse("2020-08-05 13:14:15").Format("YmdHis") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Format("Y年m月d日 H时i分s秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Format("l jS \\o\\f F Y h:i:s A") // Wednesday 5th of August 2020 01:14:15 PM
carbon.Parse("2020-08-05 13:14:15").Format("\\I\\t \\i\\s Y-m-d H:i:s") // It is 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").Format("Y-m-d H:i:s", carbon.Tokyo) // 2020-08-05 14:14:15
```
> 更多格式化输出符号请查看附录 <a href="#format-sign-table">格式化符号表</a>

View File

@ -71,7 +71,6 @@ carbon.Now().ToDateString() // 2020-08-05
carbon.Now().ToTimeString() // 13:14:15
// タイムゾーン指定の今日
carbon.Now(Carbon.NewYork).ToDateTimeString() // 2020-08-05 14:14:15
carbon.SetTimezone(Carbon.NewYork).Now().ToDateTimeString() // 2020-08-05 14:14:15
// 今日は秒タイムスタンプ
carbon.Now().Timestamp() // 1596604455
// 今日のミリ秒タイムスタンプ
@ -92,7 +91,6 @@ carbon.Yesterday().ToTimeString() // 13:14:15
carbon.Parse("2021-01-28 13:14:15").Yesterday().ToDateTimeString() // 2021-01-27 13:14:15
// タイムゾーン指定の昨日
carbon.Yesterday(Carbon.NewYork).ToDateTimeString() // 2020-08-04 14:14:15
carbon.SetTimezone(Carbon.NewYork).Yesterday().ToDateTimeString() // 2020-08-04 14:14:15
// 昨日の秒タイムスタンプ
carbon.Yesterday().Timestamp() // 1596518055
// 昨日のミリ秒タイムスタンプ
@ -113,7 +111,6 @@ carbon.Tomorrow().ToTimeString() // 13:14:15
carbon.Parse("2021-01-28 13:14:15").Tomorrow().ToDateTimeString() // 2021-01-29 13:14:15
// タイムゾーン指定の明日
carbon.Tomorrow(Carbon.NewYork).ToDateTimeString() // 2020-08-06 14:14:15
carbon.SetTimezone(Carbon.NewYork).Tomorrow().ToDateTimeString() // 2020-08-06 14:14:15
// 明日の秒タイムスタンプ
carbon.Tomorrow().Timestamp() // 1596690855
// 明日のミリ秒タイムスタンプ
@ -129,42 +126,29 @@ carbon.Tomorrow().TimestampNano() // 1596690855000000000
```go
// 秒タイムスタンプから Carbon オブジェクトを作成します
carbon.CreateFromTimestamp(-1).ToString() // 1970-01-01 07:59:59 +0800 CST
carbon.CreateFromTimestamp(-1, carbon.Tokyo).ToString() // 1970-01-01 08:59:59 +0900 JST
carbon.CreateFromTimestamp(0).ToString() // 1970-01-01 08:00:00 +0800 CST
carbon.CreateFromTimestamp(0, carbon.Tokyo).ToString() // 1970-01-01 09:00:00 +0900 JST
carbon.CreateFromTimestamp(1).ToString() // 1970-01-01 08:00:01 +0800 CST
carbon.CreateFromTimestamp(1, carbon.Tokyo).ToString() // 1970-01-01 09:00:01 +0900 JST
carbon.CreateFromTimestamp(1649735755).ToString() // 2022-04-12 11:55:55 +0800 CST
carbon.CreateFromTimestamp(1649735755, carbon.Tokyo).ToString() // 2022-04-12 12:55:55 +0900 JST
// ミリ秒のタイムスタンプから Carbon オブジェクトを作成します
carbon.CreateFromTimestampMilli(1649735755981).ToString() // 2022-04-12 11:55:55.981 +0800 CST
carbon.CreateFromTimestampMilli(1649735755981, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981 +0900 JST
// マイクロ秒タイムスタンプから Carbon オブジェクトを作成します
carbon.CreateFromTimestampMicro(1649735755981566).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
carbon.CreateFromTimestampMicro(1649735755981566, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981566 +0900 JST
// ナノタイムスタンプから Carbon オブジェクトを作成します
carbon.CreateFromTimestampNano(1649735755981566000).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
carbon.CreateFromTimestampNano(1649735755981566000, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981566 +0900 CST
// 年月日から分秒で Carbon オブジェクトを作成します
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
// 年月日から分秒で Carbon オブジェクトを作成します,,ミリ秒を含む
carbon.CreateFromDateTimeMilli(2020, 1, 1, 13, 14, 15, 999).ToString() // 2020-01-01 13:14:15.999 +0800 CST
carbon.CreateFromDateTimeMilli(2020, 1, 1, 13, 14, 15, 999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999 +0900 JST
// 年月日から分秒で Carbon オブジェクトを作成します,マイクロ秒を含む
carbon.CreateFromDateTimeMicro(2020, 1, 1, 13, 14, 15, 999999).ToString() // 2020-01-01 13:14:15.999999 +0800 CST
carbon.CreateFromDateTimeMicro(2020, 1, 1, 13, 14, 15, 999999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999999 +0900 JST
// 年月日から分秒で Carbon オブジェクトを作成します,ナノ秒を含む
carbon.CreateFromDateTimeNano(2020, 1, 1, 13, 14, 15, 999999999).ToString() // 2020-01-01 13:14:15.999999999 +0800 CST
carbon.CreateFromDateTimeNano(2020, 1, 1, 13, 14, 15, 999999999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999999999 +0900 JST
// 年月日から Carbon オブジェクトを作成します(時分秒はデフォルトで現在の時分秒です)
carbon.CreateFromDate(2020, 8, 5).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromDate(2020, 8, 5, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
// 時分秒から Carbon オブジェクトを作成します(年月日のデフォルトは現在の年月日です)
carbon.CreateFromTime(13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromTime(13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
```
##### 標準形式の時間文字列を Carbon オブジェクトに解析します
@ -192,7 +176,6 @@ carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:
carbon.ParseByFormat("2020|08|05 13|14|15", "Y|m|d H|i|s").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByFormat("It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByFormat("今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByFormat("2020-08-05 13:14:15", "Y-m-d H:i:s", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
```
##### レイアウト文字を使用して文字列を Carbon オブジェクトに解析します
@ -296,7 +279,7 @@ carbon.Parse("2020-02-29 13:14:15").SubCentury().ToDateTimeString() // 1920-02-2
carbon.Parse("2020-02-29 13:14:15").SubCenturyNoOverflow().ToDateTimeString() // 1920-02-29 13:14:15
// 三ヶ年代を追加
carbon.Parse("2020-02-29 13:14:15").Decades(3).ToDateTimeString() // 2050-03-01 13:14:15
carbon.Parse("2020-02-29 13:14:15").AddDecades(3).ToDateTimeString() // 2050-03-01 13:14:15
// 三ヶ年代を追加(オーバーフローなし)
carbon.Parse("2020-02-29 13:14:15").AddDecadesNoOverflow(3).ToDateTimeString() // 2050-02-28 13:14:15
// 一ヶ年代を追加
@ -725,6 +708,13 @@ carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
// 現在の年月日時分秒を取得
carbon.Parse("2020-08-05 13:14:15").DateTime() // 2020,8,5,13,14,15
// 現在の年月日時分秒ミリ秒を取得
carbon.Parse("2020-08-05 13:14:15").DateTimeMilli() // 2020,8,5,13,14,15,999
// 現在の年月日時分秒マイクロ秒を取得
carbon.Parse("2020-08-05 13:14:15").DateTimeMicro() // 2020,8,5,13,14,15,999999
// 現在の年月日時分秒ナノ秒を取得
carbon.Parse("2020-08-05 13:14:15").DateTimeNano() // 2020,8,5,13,14,15,999999999
// 現在の年月日を取得
carbon.Parse("2020-08-05 13:14:15").Date() // 2020,8,5
// 現在の時分秒を取得
@ -777,7 +767,7 @@ carbon.SetTimezone(carbon.Tokyo).Timezone() // JST
carbon.SetTimezone(carbon.PRC).Location() // PRC
carbon.SetTimezone(carbon.Tokyo).Location() // Asia/Tokyo
// UTCタイムゾーンからのオフセットを取得単位秒
// UTCタイムゾーンからのオフセットを取得単位秒
carbon.SetTimezone(carbon.PRC).Offset() // 28800
carbon.SetTimezone(carbon.Tokyo).Offset() // 32400
@ -805,133 +795,95 @@ carbon.Parse("2002-12-31 13:14:15").Age() // 18
```go
// 日期时间文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString() // 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString(carbon.Tokyo) // 2020-08-05 14:14:15
// 日期时间文字列を出力,ミリ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMilliString() // 2020-08-05 13:14:15.999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMilliString(carbon.Tokyo) // 2020-08-05 14:14:15.999
// 日期时间文字列を出力,マイクロ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMicroString() // 2020-08-05 13:14:15.999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMicroString(carbon.Tokyo) // 2020-08-05 14:14:15.999999
// 日期时间文字列を出力,ナノ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeNanoString() // 2020-08-05 13:14:15.999999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeNanoString(carbon.Tokyo) // 2020-08-05 14:14:15.999999999
// 略語日期时间文字列を出力
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeString() // 20200805131415
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeString(carbon.Tokyo) // 20200805141415
// 略語日期时间文字列を出力,ミリ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMilliString() // 20200805131415.999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMilliString(carbon.Tokyo) // 20200805141415.999
// 略語日期时间文字列を出力,マイクロ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMicroString() // 20200805131415.999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMicroString(carbon.Tokyo) // 20200805141415.999999
// 略語日期时间文字列を出力,ナノ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeNanoString() // 20200805131415.999999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeNanoString(carbon.Tokyo) // 20200805141415.999999999
// 日期文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToDateString() // 2020-08-05
carbon.Parse("2020-08-05 13:14:15").ToDateString(carbon.Tokyo) // 2020-08-05
// 略語日期文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToShortDateString() // 20200805
carbon.Parse("2020-08-05 13:14:15").ToShortDateString(carbon.Tokyo) // 20200805
// 時間文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToTimeString() // 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToTimeString(carbon.Tokyo) // 14:14:15
// 略語時間文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString() // 131415
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString(carbon.Tokyo) // 141415
// Ansic フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
carbon.Parse("2020-08-05 13:14:15").ToAnsicString(carbon.Tokyo) // Wed Aug 5 14:14:15 2020
// Atom フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToAtomString(carbon.Tokyo) // 2020-08-05T14:14:15+08:00
// UnixDate フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString() // Wed Aug 5 13:14:15 CST 2020
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString(carbon.Tokyo) // Wed Aug 5 14:14:15 JST 2020
// RubyDate フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString() // Wed Aug 05 13:14:15 +0800 2020
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString(carbon.Tokyo) // Wed Aug 05 14:14:15 +0900 2020
// Kitchen フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToKitchenString() // 1:14PM
carbon.Parse("2020-08-05 13:14:15").ToKitchenString(carbon.Tokyo) // 2:14PM
// Cookie フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToCookieString() // Wednesday, 05-Aug-2020 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToCookieString(carbon.Tokyo) // Wednesday, 05-Aug-2020 14:14:15 JST
// DayDateTime フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString() // Wed, Aug 5, 2020 1:14 PM
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString(carbon.Tokyo) // Wed, Aug 5, 2020 2:14 PM
// RSS フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRssString() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRssString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
// W3C フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToW3cString(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// ISO8601 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToIso8601String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToIso8601String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// RFC822 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc822String(carbon.Tokyo) // 05 Aug 20 14:14 JST
// RFC822Z フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString() // 05 Aug 20 13:14 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString(carbon.Tokyo) // 05 Aug 20 14:14 +0900
// RFC850 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc850String() // Wednesday, 05-Aug-20 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc850String(carbon.Tokyo) // Wednesday, 05-Aug-20 14:14:15 JST
// RFC1036 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String() // Wed, 05 Aug 20 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String(carbon.Tokyo) // Wed, 05 Aug 20 14:14:15 +0900
// RFC1123 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String() // Wed, 05 Aug 2020 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 JST
// RFC1123Z フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 0800
// RFC2822 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
// RFC7231 フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String() // Wed, 05 Aug 2020 13:14:15 GMT
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 GMT
// RFC3339 フォーマット文字列を出力
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// RFC3339 フォーマット文字列を出力,ミリ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MilliString() // 2020-08-05T13:14:15.999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MilliString(carbon.Tokyo) // 2020-08-05T13:14:15.999+09:00
// RFC3339 フォーマット文字列を出力,マイクロ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MicroString() // 2020-08-05T13:14:15.999999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MicroString(carbon.Tokyo) // 2020-08-05T13:14:15.999999+09:00
// RFC3339 フォーマット文字列を出力,ナノ秒を含む
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339NanoString() // 2020-08-05T13:14:15.999999999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339NanoString(carbon.Tokyo) // 2020-08-05T13:14:15.999999999+09:00
// 日付時間文字列を出力
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15")) // 2020-08-05 13:14:15
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15", carbon.Tokyo)) // 2020-08-05 13:14:15
// "2006-01-02 15:04:05.999999999 -0700 MST" フォーマット文字列を出力
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05 13:14:15").ToString(carbon.Tokyo) // 2020-08-05 14:14:15999999 +0900 JST
// レイアウトを指定する文字列を出力, Layout() は 是ToLayoutString() の略記です
carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").Layout("2006-01-02 15:04:05", carbon.Tokyo) // 2020-08-05 14:14:15
// 指定されたフォーマットの文字列を出力, Format() は 是ToFormatString() の略記です
carbon.Parse("2020-08-05 13:14:15").Format("YmdHis") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Format("Y年m月d日 H时i分s秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Format("l jS \\o\\f F Y h:i:s A") // Wednesday 5th of August 2020 01:14:15 PM
carbon.Parse("2020-08-05 13:14:15").Format("\\I\\t \\i\\s Y-m-d H:i:s") // It is 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").Format("Y-m-d H:i:s", carbon.Tokyo) // 2020-08-05 14:14:15
```
> もっとフォーマットした出力記号は付録を見てください <a href="#format-sign-table">書式設定記号表</a>

View File

@ -74,7 +74,6 @@ carbon.Now().ToDateString() // 2020-08-05
carbon.Now().ToTimeString() // 13:14:15
// Return datetime of today in a given timezone
carbon.Now(Carbon.NewYork).ToDateTimeString() // 2020-08-05 14:14:15
carbon.SetTimezone(Carbon.NewYork).Now().ToDateTimeString() // 2020-08-05 14:14:15
// Return timestamp with second of today
carbon.Now().Timestamp() // 1596604455
// Return timestamp with millisecond of today
@ -95,7 +94,6 @@ carbon.Yesterday().ToTimeString() // 13:14:15
carbon.Parse("2021-01-28 13:14:15").Yesterday().ToDateTimeString() // 2021-01-27 13:14:15
// Return datetime of yesterday in a given timezone
carbon.Yesterday(Carbon.NewYork).ToDateTimeString() // 2020-08-04 14:14:15
carbon.SetTimezone(Carbon.NewYork).Yesterday().ToDateTimeString() // 2020-08-04 14:14:15
// Return timestamp with second of yesterday
carbon.Yesterday().Timestamp() // 1596518055
// Return timestamp with millisecond of yesterday
@ -116,7 +114,6 @@ carbon.Tomorrow().ToTimeString() // 13:14:15
carbon.Parse("2021-01-28 13:14:15").Tomorrow().ToDateTimeString() // 2021-01-29 13:14:15
// Return datetime of tomorrow in a given timezone
carbon.Tomorrow(Carbon.NewYork).ToDateTimeString() // 2020-08-06 14:14:15
carbon.SetTimezone(Carbon.NewYork).Tomorrow().ToDateTimeString() // 2020-08-06 14:14:15
// Return timestamp with second of tomorrow
carbon.Tomorrow().Timestamp() // 1596690855
// Return timestamp with millisecond of tomorrow
@ -132,42 +129,29 @@ carbon.Tomorrow().TimestampNano() // 1596690855000000000
```go
// Create a Carbon instance from a given timestamp with second
carbon.CreateFromTimestamp(-1).ToString() // 1970-01-01 07:59:59 +0800 CST
carbon.CreateFromTimestamp(-1, carbon.Tokyo).ToString() // 1970-01-01 08:59:59 +0900 JST
carbon.CreateFromTimestamp(0).ToString() // 1970-01-01 08:00:00 +0800 CST
carbon.CreateFromTimestamp(0, carbon.Tokyo).ToString() // 1970-01-01 09:00:00 +0900 JST
carbon.CreateFromTimestamp(1).ToString() // 1970-01-01 08:00:01 +0800 CST
carbon.CreateFromTimestamp(1, carbon.Tokyo).ToString() // 1970-01-01 09:00:01 +0900 JST
carbon.CreateFromTimestamp(1649735755).ToString() // 2022-04-12 11:55:55 +0800 CST
carbon.CreateFromTimestamp(1649735755, carbon.Tokyo).ToString() // 2022-04-12 12:55:55 +0900 JST
// Create a Carbon instance from a given timestamp with millisecond
carbon.CreateFromTimestampMilli(1649735755981).ToString() // 2022-04-12 11:55:55.981 +0800 CST
carbon.CreateFromTimestampMilli(1649735755981, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981 +0900 JST
// Create a Carbon instance from a given timestamp with microsecond
carbon.CreateFromTimestampMicro(1649735755981566).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
carbon.CreateFromTimestampMicro(1649735755981566, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981566 +0900 JST
// Create a Carbon instance from a given timestamp with nanosecond
carbon.CreateFromTimestampNano(1649735755981566000).ToString() // 2022-04-12 11:55:55.981566 +0800 CST
carbon.CreateFromTimestampNano(1649735755981566000, carbon.Tokyo).ToString() // 2022-04-12 11:55:55.981566 +0900 CST
// Create a Carbon instance from a given date and time.
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromDateTime(2020, 8, 5, 13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
// Create a Carbon instance from a given date and time with millisecond.
carbon.CreateFromDateTimeMilli(2020, 1, 1, 13, 14, 15, 999).ToString() // 2020-01-01 13:14:15.999 +0800 CST
carbon.CreateFromDateTimeMilli(2020, 1, 1, 13, 14, 15, 999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999 +0900 JST
// Create a Carbon instance from a given date and time with microsecond.
carbon.CreateFromDateTimeMicro(2020, 1, 1, 13, 14, 15, 999999).ToString() // 2020-01-01 13:14:15.999999 +0800 CST
carbon.CreateFromDateTimeMicro(2020, 1, 1, 13, 14, 15, 999999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999999 +0900 JST
// Create a Carbon instance from a given date and time with nanosecond.
carbon.CreateFromDateTimeNano(2020, 1, 1, 13, 14, 15, 999999999).ToString() // 2020-01-01 13:14:15.999999999 +0800 CST
carbon.CreateFromDateTimeNano(2020, 1, 1, 13, 14, 15, 999999999, carbon.Tokyo).ToString() // 2020-01-01 13:14:15.999999999 +0900 JST
// Create a Carbon instance from a given year, month and day
carbon.CreateFromDate(2020, 8, 5).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromDate(2020, 8, 5, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
// Create a Carbon instance from a given hour, minute and second
carbon.CreateFromTime(13, 14, 15).ToDateTimeString() // 2020-08-05 13:14:15
carbon.CreateFromTime(13, 14, 15, carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
```
##### Parse a standard string as a Carbon instance
@ -193,7 +177,6 @@ carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToString() // 2020-08-05 13:
carbon.ParseByFormat("2020|08|05 13|14|15", "Y|m|d H|i|s").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByFormat("It is 2020-08-05 13:14:15", "\\I\\t \\i\\s Y-m-d H:i:s").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByFormat("今天是 2020年08月05日13时14分15秒", "今天是 Y年m月d日H时i分s秒").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByFormat("2020-08-05 13:14:15", "Y-m-d H:i:s", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
```
##### Parse a string as a Carbon instance by layout
@ -202,7 +185,6 @@ carbon.ParseByFormat("2020-08-05 13:14:15", "Y-m-d H:i:s", carbon.Tokyo).ToDateT
carbon.ParseByLayout("2020|08|05 13|14|15", "2006|01|02 15|04|05").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByLayout("It is 2020-08-05 13:14:15", "It is 2006-01-02 15:04:05").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByLayout("今天是 2020年08月05日13时14分15秒", "今天是 2006年01月02日15时04分05秒").ToDateTimeString() // 2020-08-05 13:14:15
carbon.ParseByLayout("2020-08-05 13:14:15", "2006-01-02 15:04:05", carbon.Tokyo).ToDateTimeString() // 2020-08-05 14:14:15
```
##### Convert between Carbon and Time
@ -297,7 +279,7 @@ carbon.Parse("2020-02-29 13:14:15").SubCentury().ToDateTimeString() // 1920-02-2
carbon.Parse("2020-02-29 13:14:15").SubCenturyNoOverflow().ToDateTimeString() // 1920-02-20 13:14:15
// Add three decades
carbon.Parse("2020-02-29 13:14:15").Decades(3).ToDateTimeString() // 2050-03-01 13:14:15
carbon.Parse("2020-02-29 13:14:15").AddDecades(3).ToDateTimeString() // 2050-03-01 13:14:15
// Add three decades without overflowing month
carbon.Parse("2020-02-29 13:14:15").AddDecadesNoOverflow(3).ToDateTimeString() // 2050-02-28 13:14:15
// Add one decade
@ -723,6 +705,13 @@ carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
// Get current year, month, day, hour, minute and second
carbon.Parse("2020-08-05 13:14:15").DateTime() // 2020,8,5,13,14,15
// Get current year, month, day, hour, minute, second and millisecond
carbon.Parse("2020-08-05 13:14:15").DateTimeMilli() // 2020,8,5,13,14,15,999
// Get current year, month, day, hour, minute, second and microsecond
carbon.Parse("2020-08-05 13:14:15").DateTimeMicro() // 2020,8,5,13,14,15,999999
//Get current year, month, day, hour, minute, second and nanosecond
carbon.Parse("2020-08-05 13:14:15").DateTimeNano() // 2020,8,5,13,14,15,999999999
// Get current year, month and day
carbon.Parse("2020-08-05 13:14:15").Date() // 2020,8,5
// Get current hour, minute and second
@ -739,7 +728,7 @@ carbon.Parse("2020-08-05 13:14:15").Year() // 2020
carbon.Parse("2020-08-05 13:14:15").Quarter() // 3
// Get current month
carbon.Parse("2020-08-05 13:14:15").Month() // 8
// Get current week(start with 0)
// Get current week(start from 0)
carbon.Parse("2020-08-02 13:14:15").Week() // 0
carbon.Parse("2020-08-02").SetWeekStartsAt(carbon.Sunday).Week() // 0
carbon.Parse("2020-08-02").SetWeekStartsAt(carbon.Monday).Week() // 6
@ -803,133 +792,95 @@ carbon.Parse("2002-12-31 13:14:15").Age() // 18
```go
// Output a string in date and time format
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString() // 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString(carbon.Tokyo) // 2020-08-05 14:14:15
// Output a string in date and time with millisecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMilliString() // 2020-08-05 13:14:15.999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMilliString(carbon.Tokyo) // 2020-08-05 14:14:15.999
// Output a string in date and time with microsecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMicroString() // 2020-08-05 13:14:15.999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeMicroString(carbon.Tokyo) // 2020-08-05 14:14:15.999999
// Output a string in date and time with nanosecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeNanoString() // 2020-08-05 13:14:15.999999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToDateTimeNanoString(carbon.Tokyo) // 2020-08-05 14:14:15.999999999
// Output a string in short date and time format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeString() // 20200805131415
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeString(carbon.Tokyo) // 20200805141415
// Output a string in short date and time with millisecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMilliString() // 20200805131415.999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMilliString(carbon.Tokyo) // 20200805141415.999
// Output a string in short date and time with microsecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMicroString() // 20200805131415.999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeMicroString(carbon.Tokyo) // 20200805141415.999999
// Output a string in short date and time with nanosecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeNanoString() // 20200805131415.999999999
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToShortDateTimeNanoString(carbon.Tokyo) // 20200805141415.999999999
// Output a in date format string
carbon.Parse("2020-08-05 13:14:15").ToDateString() // 2020-08-05
carbon.Parse("2020-08-05 13:14:15").ToDateString(carbon.Tokyo) // 2020-08-05
// Output a string in short date format
carbon.Parse("2020-08-05 13:14:15").ToShortDateString() // 20200805
carbon.Parse("2020-08-05 13:14:15").ToShortDateString(carbon.Tokyo) // 20200805
// Output a string in time format
carbon.Parse("2020-08-05 13:14:15").ToTimeString() // 13:14:15
carbon.Parse("2020-08-05 13:14:15").ToTimeString(carbon.Tokyo) // 14:14:15
// Output a string in short time format
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString() // 131415
carbon.Parse("2020-08-05 13:14:15").ToShortTimeString(carbon.Tokyo) // 141415
// Output a string in Ansic format
carbon.Parse("2020-08-05 13:14:15").ToAnsicString() // Wed Aug 5 13:14:15 2020
carbon.Parse("2020-08-05 13:14:15").ToAnsicString(carbon.Tokyo) // Wed Aug 5 14:14:15 2020
// Output a string in Atom format
carbon.Parse("2020-08-05 13:14:15").ToAtomString() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToAtomString(carbon.Tokyo) // 2020-08-05T14:14:15+08:00
// Output a string in unix date format
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString() // Wed Aug 5 13:14:15 CST 2020
carbon.Parse("2020-08-05 13:14:15").ToUnixDateString(carbon.Tokyo) // Wed Aug 5 14:14:15 JST 2020
// Output a string in ruby date format
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString() // Wed Aug 05 13:14:15 +0800 2020
carbon.Parse("2020-08-05 13:14:15").ToRubyDateString(carbon.Tokyo) // Wed Aug 05 14:14:15 +0900 2020
// Output a string in Kitchen format
carbon.Parse("2020-08-05 13:14:15").ToKitchenString() // 1:14PM
carbon.Parse("2020-08-05 13:14:15").ToKitchenString(carbon.Tokyo) // 2:14PM
// Output a string in Cookie format
carbon.Parse("2020-08-05 13:14:15").ToCookieString() // Wednesday, 05-Aug-2020 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToCookieString(carbon.Tokyo) // Wednesday, 05-Aug-2020 14:14:15 JST
// Output a string in day, date and time format
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString() // Wed, Aug 5, 2020 1:14 PM
carbon.Parse("2020-08-05 13:14:15").ToDayDateTimeString(carbon.Tokyo) // Wed, Aug 5, 2020 2:14 PM
// Output a string in RSS format
carbon.Parse("2020-08-05 13:14:15").ToRssString() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRssString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
// Output a string in W3C format
carbon.Parse("2020-08-05 13:14:15").ToW3cString() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToW3cString(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// Output a string in ISO8601 format
carbon.Parse("2020-08-05 13:14:15").ToIso8601String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05 13:14:15").ToIso8601String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// Output a string in RFC822 format
carbon.Parse("2020-08-05 13:14:15").ToRfc822String() // 05 Aug 20 13:14 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc822String(carbon.Tokyo) // 05 Aug 20 14:14 JST
// Output a string in RFC822Z format
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString() // 05 Aug 20 13:14 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc822zString(carbon.Tokyo) // 05 Aug 20 14:14 +0900
// Output a string in RFC850 format
carbon.Parse("2020-08-05 13:14:15").ToRfc850String() // Wednesday, 05-Aug-20 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc850String(carbon.Tokyo) // Wednesday, 05-Aug-20 14:14:15 JST
// Output a string in RFC1036 format
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String() // Wed, 05 Aug 20 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc1036String(carbon.Tokyo) // Wed, 05 Aug 20 14:14:15 +0900
// Output a string in RFC1123 format
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String() // Wed, 05 Aug 2020 13:14:15 CST
carbon.Parse("2020-08-05 13:14:15").ToRfc1123String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 JST
// Output a string in RFC1123Z format
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc1123zString(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 0800
// Output a string in RFC2822 format
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String() // Wed, 05 Aug 2020 13:14:15 +0800
carbon.Parse("2020-08-05 13:14:15").ToRfc2822String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 +0900
// Output a string in RFC7231 format
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String() // Wed, 05 Aug 2020 13:14:15 GMT
carbon.Parse("2020-08-05 13:14:15").ToRfc7231String(carbon.Tokyo) // Wed, 05 Aug 2020 14:14:15 GMT
// Output a string in RFC3339 format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339String() // 2020-08-05T13:14:15+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339String(carbon.Tokyo) // 2020-08-05T14:14:15+09:00
// Output a string in RFC3339 with millisecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MilliString() // 2020-08-05T13:14:15.999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MilliString(carbon.Tokyo) // 2020-08-05T13:14:15.999+09:00
// Output a string in RFC3339 with microsecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MicroString() // 2020-08-05T13:14:15.999999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339MicroString(carbon.Tokyo) // 2020-08-05T13:14:15.999999+09:00
// Output a string in RFC3339 with nanosecond format
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339NanoString() // 2020-08-05T13:14:15.999999999+08:00
carbon.Parse("2020-08-05T13:14:15.999999999+08:00").ToRfc3339NanoString(carbon.Tokyo) // 2020-08-05T13:14:15.999999999+09:00
// Output a string in date and time format
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15")) // 2020-08-05 13:14:15
fmt.Sprintf("%s", carbon.Parse("2020-08-05 13:14:15", carbon.Tokyo)) // 2020-08-05 13:14:15
// Output a string in "2006-01-02 15:04:05.999999999 -0700 MST" format
carbon.Parse("2020-08-05 13:14:15").ToString() // 2020-08-05 13:14:15.999999 +0800 CST
carbon.Parse("2020-08-05 13:14:15").ToString(carbon.Tokyo) // 2020-08-05 14:14:15.999999 +0900 JST
// Output a string by layout, Layout() is shorthand for ToLayoutString()
carbon.Parse("2020-08-05 13:14:15").Layout("20060102150405") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Layout("2006年01月02日 15时04分05秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Layout("It is 2006-01-02 15:04:05") // It is 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").Layout("2006-01-02 15:04:05", carbon.Tokyo) // 2020-08-05 14:14:15
// Output a string by format, Format() is shorthand for ToFormatString()
carbon.Parse("2020-08-05 13:14:15").Format("YmdHis") // 20200805131415
carbon.Parse("2020-08-05 13:14:15").Format("Y年m月d日 H时i分s秒") // 2020年08月05日 13时14分15秒
carbon.Parse("2020-08-05 13:14:15").Format("l jS \\o\\f F Y h:i:s A") // Wednesday 5th of August 2020 01:14:15 PM
carbon.Parse("2020-08-05 13:14:15").Format("\\I\\t \\i\\s Y-m-d H:i:s") // It is 2020-08-05 13:14:15
carbon.Parse("2020-08-05 13:14:15").Format("Y-m-d H:i:s", carbon.Tokyo) // 2020-08-05 14:14:15
```
> For more supported format signs, please see the <a href="#format-sign-table">Format sign table</a>

View File

@ -1,7 +1,7 @@
// @Package carbon
// @Description a simple, semantic and developer-friendly golang package for datetime
// @Page github.com/golang-module/carbon
// @Version v2.1.0
// @Version v2.1.1
// @Author gouguoyin
// @Blog www.gouguoyin.cn
// @Email contact@gouguoyin.cn
@ -130,11 +130,11 @@ const (
RssFormat = time.RFC1123Z
KitchenFormat = time.Kitchen
RFC2822Format = time.RFC1123Z
CookieFormat = "Monday, 02-Jan-2006 15:04:05 MST"
RFC3339Format = "2006-01-02T15:04:05Z07:00"
RFC3339MilliFormat = "2006-01-02T15:04:05.999Z07:00"
RFC3339MicroFormat = "2006-01-02T15:04:05.999999Z07:00"
RFC3339NanoFormat = "2006-01-02T15:04:05.999999999Z07:00"
CookieFormat = "Monday, 02-Jan-2006 15:04:05 MST"
ISO8601Format = "2006-01-02T15:04:05-07:00"
RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700"
RFC7231Format = "Mon, 02 Jan 2006 15:04:05 GMT"

View File

@ -19,12 +19,6 @@ func TestCarbon_Now(t *testing.T) {
assert.Equal(expected2, actual2)
}
func TestError_Now(t *testing.T) {
timezone := "xxx"
c := Now(timezone)
assert.NotNil(t, c.Error, "It should catch an exception in Now()")
}
func TestCarbon_Yesterday(t *testing.T) {
assert := assert.New(t)
@ -43,12 +37,6 @@ func TestCarbon_Yesterday(t *testing.T) {
assert.Equal("2020-08-04", c3.ToDateString(), "It should be equal to 2020-08-04")
}
func TestError_Yesterday(t *testing.T) {
timezone := "xxx"
c := Yesterday(timezone)
assert.NotNil(t, c.Error, "It should catch an exception in Yesterday()")
}
func TestCarbon_Tomorrow(t *testing.T) {
assert := assert.New(t)
@ -67,12 +55,6 @@ func TestCarbon_Tomorrow(t *testing.T) {
assert.Equal("2020-08-06", c3.ToDateString(), "It should be equal to 2020-08-06")
}
func TestError_Tomorrow(t *testing.T) {
timezone := "xxx"
c := Tomorrow(timezone)
assert.NotNil(t, c.Error, "It should catch an exception in Tomorrow()")
}
func TestCarbon_Time2Carbon(t *testing.T) {
assert := assert.New(t)
@ -88,3 +70,10 @@ func TestCarbon_Carbon2Time(t *testing.T) {
actual := Now().Carbon2Time().Format(DateTimeFormat)
assert.Equal(expected, actual)
}
func TestError_Carbon(t *testing.T) {
timezone := "xxx"
assert.NotNil(t, Now(timezone).Error, "It should catch an exception in Now()")
assert.NotNil(t, Tomorrow(timezone).Error, "It should catch an exception in Tomorrow()")
assert.NotNil(t, Yesterday(timezone).Error, "It should catch an exception in Yesterday()")
}

View File

@ -30,11 +30,6 @@ func TestCarbon_IsZero(t *testing.T) {
}
}
func TestError_IsZero(t *testing.T) {
c := Parse("2020-13-50")
assert.True(t, c.IsZero(), "It should catch an exception in IsZero()")
}
func TestCarbon_IsInvalid(t *testing.T) {
assert := assert.New(t)
@ -58,11 +53,6 @@ func TestCarbon_IsInvalid(t *testing.T) {
}
}
func TestError_IsInvalid(t *testing.T) {
c := SetTimezone("xxx")
assert.True(t, c.IsInvalid(), "It should catch an exception in IsInvalid()")
}
func TestCarbon_IsNow(t *testing.T) {
assert := assert.New(t)
@ -1178,3 +1168,9 @@ func TestCarbon_BetweenIncludedBoth(t *testing.T) {
assert.Equal(test.expected, c1.BetweenIncludedBoth(c2, c3), "Current test index is "+strconv.Itoa(index))
}
}
func TestError_Comparer(t *testing.T) {
c := Parse("2020-13-50")
assert.True(t, c.IsZero(), "It should catch an exception in IsZero()")
assert.True(t, c.IsInvalid(), "It should catch an exception in IsInvalid()")
}

View File

@ -4,7 +4,7 @@ import (
"strings"
)
// Constellation gets constellation name, i18n is supported.
// Constellation gets constellation name like "Aries", i18n is supported.
// 获取星座支持i18n
func (c Carbon) Constellation() string {
if c.IsInvalid() {

View File

@ -83,7 +83,8 @@ func CreateFromTimestampNano(timestamp int64, timezone ...string) Carbon {
// CreateFromDateTime creates a Carbon instance from a given date and time.
// 从给定的年月日时分秒创建 Carbon 实例
func (c Carbon) CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) Carbon {
return c.create(year, month, day, hour, minute, second, time.Now().Nanosecond(), timezone...)
now := c.Now(timezone...)
return c.create(year, month, day, hour, minute, second, now.Nanosecond(), timezone...)
}
// CreateFromDateTime creates a Carbon instance from a given date and time.

View File

@ -111,42 +111,6 @@ func TestCarbon_CreateFromTimestampNano(t *testing.T) {
}
}
func TestError_CreateFromTimestamp(t *testing.T) {
timestamp, timezone := int64(1577855655), "xxx"
c1 := SetTimezone(timezone).CreateFromTimestamp(timestamp)
assert.NotNil(t, c1.Error, "It should catch an exception in CreateFromTimestamp()")
c2 := CreateFromTimestamp(timestamp, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in CreateFromTimestamp()")
}
func TestError_CreateFromTimestampMilli(t *testing.T) {
timestamp, timezone := int64(1577855655), "xxx"
c1 := SetTimezone(timezone).CreateFromTimestampMilli(timestamp)
assert.NotNil(t, c1.Error, "It should catch an exception in CreateFromTimestampMilli()")
c2 := CreateFromTimestampMilli(timestamp, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in CreateFromTimestampMilli()")
}
func TestError_CreateFromTimestampMicro(t *testing.T) {
timestamp, timezone := int64(1577855655), "xxx"
c1 := SetTimezone(timezone).CreateFromTimestampMicro(timestamp)
assert.NotNil(t, c1.Error, "It should catch an exception in CreateFromTimestampMicro()")
c2 := CreateFromTimestampMicro(timestamp, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in CreateFromTimestampMicro()")
}
func TestError_CreateFromTimestampNano(t *testing.T) {
timestamp, timezone := int64(1577855655), "xxx"
c1 := SetTimezone(timezone).CreateFromTimestampNano(timestamp)
assert.NotNil(t, c1.Error, "It should catch an exception in CreateFromTimestampNano()")
c2 := CreateFromTimestampNano(timestamp, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in CreateFromTimestampNano()")
}
func TestCarbon_CreateFromDateTime(t *testing.T) {
assert := assert.New(t)
@ -174,15 +138,6 @@ func TestCarbon_CreateFromDateTime(t *testing.T) {
}
}
func TestError_CreateFromDateTime(t *testing.T) {
year, month, day, hour, minute, second, timezone := 2020, 1, 1, 13, 14, 15, "xxx"
c1 := SetTimezone(timezone).CreateFromDateTime(year, month, day, hour, minute, second)
assert.NotNil(t, c1.Error, "It should catch an exception in CreateFromDateTime()")
c2 := CreateFromDateTime(year, month, day, hour, minute, second, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in CreateFromDateTime()")
}
func TestCarbon_CreateFromDateTimeMilli(t *testing.T) {
assert := assert.New(t)
@ -292,15 +247,6 @@ func TestCarbon_CreateFromDate(t *testing.T) {
}
}
func TestError_CreateFromDate(t *testing.T) {
year, month, day, timezone := 13, 14, 15, "xxx"
c1 := SetTimezone(timezone).CreateFromDate(year, month, day)
assert.NotNil(t, c1.Error, "It should catch an exception in CreateFromDate()")
c2 := CreateFromDate(year, month, day, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in CreateFromDate()")
}
func TestCarbon_CreateFromTime(t *testing.T) {
assert := assert.New(t)
@ -328,11 +274,15 @@ func TestCarbon_CreateFromTime(t *testing.T) {
}
}
func TestError_CreateFromTime(t *testing.T) {
hour, minute, second, timezone := 13, 14, 15, "xxx"
c1 := SetTimezone(timezone).CreateFromTime(hour, minute, second)
assert.NotNil(t, c1.Error, "It should catch an exception in CreateFromTime()")
func TestError_Creator(t *testing.T) {
year, month, day, hour, minute, second, timestamp, timezone := 2020, 8, 5, 13, 14, 15, int64(1577855655), "xxx"
c2 := CreateFromTime(hour, minute, second, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in CreateFromTime()")
c := SetTimezone(timezone)
assert.NotNil(t, c.CreateFromDateTime(year, month, day, hour, minute, second).Error, "It should catch an exception in CreateFromDateTime()")
assert.NotNil(t, c.CreateFromDate(year, month, day).Error, "It should catch an exception in CreateFromDate()")
assert.NotNil(t, c.CreateFromTime(hour, minute, second).Error, "It should catch an exception in CreateFromTime()")
assert.NotNil(t, c.CreateFromTimestamp(timestamp).Error, "It should catch an exception in CreateFromTime()")
assert.NotNil(t, c.CreateFromTimestampMilli(timestamp).Error, "It should catch an exception in CreateFromTimestampMilli()")
assert.NotNil(t, c.CreateFromTimestampMicro(timestamp).Error, "It should catch an exception in CreateFromTimestampMicro()")
assert.NotNil(t, c.CreateFromTimestampNano(timestamp).Error, "It should catch an exception in CreateFromTimestampNano()")
}

View File

@ -15,12 +15,6 @@ func TestCarbon_Scan(t *testing.T) {
assert.Equal(t, c.ToDateTimeString(), Now().ToDateTimeString())
}
func TestError_Scan(t *testing.T) {
c, v := NewCarbon(), "xxx"
err := c.Scan(v)
assert.Equal(t, err, fmt.Errorf("can not convert %v to carbon", v))
}
func TestCarbon_Value(t *testing.T) {
c := Now()
v, err := c.Value()
@ -28,6 +22,12 @@ func TestCarbon_Value(t *testing.T) {
assert.Equal(t, v, c.time)
}
func TestError_Scan(t *testing.T) {
c, v := NewCarbon(), "xxx"
err := c.Scan(v)
assert.Equal(t, err, fmt.Errorf("can not convert %v to carbon", v))
}
func TestError_Value(t *testing.T) {
c := Parse("")
v, err := c.Value()

112
getter.go
View File

@ -2,7 +2,7 @@ package carbon
import "time"
// DaysInYear gets total days in year.
// DaysInYear gets total days in year like 365.
// 获取本年的总天数
func (c Carbon) DaysInYear() int {
if c.IsInvalid() {
@ -14,7 +14,7 @@ func (c Carbon) DaysInYear() int {
return DaysPerNormalYear
}
// DaysInMonth gets total days in month.
// DaysInMonth gets total days in month like 30.
// 获取本月的总天数
func (c Carbon) DaysInMonth() int {
if c.IsInvalid() {
@ -23,7 +23,7 @@ func (c Carbon) DaysInMonth() int {
return c.EndOfMonth().time.In(c.loc).Day()
}
// MonthOfYear gets month of year.
// MonthOfYear gets month of year like 12.
// 获取本年的第几月
func (c Carbon) MonthOfYear() int {
if c.IsInvalid() {
@ -32,7 +32,7 @@ func (c Carbon) MonthOfYear() int {
return int(c.time.In(c.loc).Month())
}
// DayOfYear gets day of year.
// DayOfYear gets day of year like 365.
// 获取本年的第几天
func (c Carbon) DayOfYear() int {
if c.IsInvalid() {
@ -41,7 +41,7 @@ func (c Carbon) DayOfYear() int {
return c.time.In(c.loc).YearDay()
}
// DayOfMonth gets day of month.
// DayOfMonth gets day of month like 30.
// 获取本月的第几天
func (c Carbon) DayOfMonth() int {
if c.IsInvalid() {
@ -50,7 +50,7 @@ func (c Carbon) DayOfMonth() int {
return c.time.In(c.loc).Day()
}
// DayOfWeek gets day of week.
// DayOfWeek gets day of week like 6.
// 获取本周的第几天
func (c Carbon) DayOfWeek() int {
if c.IsInvalid() {
@ -63,7 +63,7 @@ func (c Carbon) DayOfWeek() int {
return day
}
// WeekOfYear gets week of year, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
// WeekOfYear gets week of year like 1, see https://en.wikipedia.org/wiki/ISO_8601#Week_dates.
// 获取本年的第几周
func (c Carbon) WeekOfYear() int {
if c.IsInvalid() {
@ -73,7 +73,7 @@ func (c Carbon) WeekOfYear() int {
return week
}
// WeekOfMonth gets week of month.
// WeekOfMonth gets week of month like 1.
// 获取本月的第几周
func (c Carbon) WeekOfMonth() int {
if c.IsInvalid() {
@ -86,11 +86,11 @@ func (c Carbon) WeekOfMonth() int {
return days/DaysPerWeek + 1
}
// DateTime gets current year, month, day, hour, minute, and second.
// DateTime gets current year, month, day, hour, minute, and second like 2020, 8, 5, 13, 14, 15.
// 获取当前年月日时分秒
func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
if c.IsInvalid() {
return 0, 0, 0, 0, 0, 0
return
}
carbon := c.time.In(c.loc)
var tm time.Month
@ -99,27 +99,57 @@ func (c Carbon) DateTime() (year, month, day, hour, minute, second int) {
return year, int(tm), day, hour, minute, second
}
// Date gets current year, month, and day.
// DateTimeMilli gets current year, month, day, hour, minute, second and millisecond like 2020, 8, 5, 13, 14, 15, 999.
// 获取当前年月日时分秒毫秒
func (c Carbon) DateTimeMilli() (year, month, day, hour, minute, second, millisecond int) {
if c.IsInvalid() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Millisecond()
}
// DateTimeMicro gets current year, month, day, hour, minute, second and microsecond like 2020, 8, 5, 13, 14, 15, 999999.
// 获取当前年月日时分秒微秒
func (c Carbon) DateTimeMicro() (year, month, day, hour, minute, second, microsecond int) {
if c.IsInvalid() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Microsecond()
}
// DateTimeNano gets current year, month, day, hour, minute, second and nanosecond like 2020, 8, 5, 13, 14, 15, 999999999.
// 获取当前年月日时分秒纳秒
func (c Carbon) DateTimeNano() (year, month, day, hour, minute, second, nanosecond int) {
if c.IsInvalid() {
return
}
year, month, day, hour, minute, second = c.DateTime()
return year, month, day, hour, minute, second, c.Nanosecond()
}
// Date gets current year, month, and day like 2020, 8, 5.
// 获取当前年月日
func (c Carbon) Date() (year, month, day int) {
if c.IsInvalid() {
return 0, 0, 0
return
}
var tm time.Month
year, tm, day = c.time.In(c.loc).Date()
return year, int(tm), day
}
// Time gets current hour, minute, and second.
// Time gets current hour, minute, and second like 13, 14, 15.
// 获取当前时分秒
func (c Carbon) Time() (hour, minute, second int) {
if c.IsInvalid() {
return 0, 0, 0
return
}
return c.time.In(c.loc).Clock()
}
// Century gets current century.
// Century gets current century like 21.
// 获取当前世纪
func (c Carbon) Century() int {
if c.IsInvalid() {
@ -128,7 +158,7 @@ func (c Carbon) Century() int {
return c.Year()/YearsPerCentury + 1
}
// Decade gets current decade.
// Decade gets current decade like 20.
// 获取当前年代
func (c Carbon) Decade() int {
if c.IsInvalid() {
@ -137,7 +167,7 @@ func (c Carbon) Decade() int {
return c.Year() % YearsPerCentury / YearsPerDecade * YearsPerDecade
}
// Year gets current year.
// Year gets current year like 2020.
// 获取当前年
func (c Carbon) Year() int {
if c.IsInvalid() {
@ -146,7 +176,7 @@ func (c Carbon) Year() int {
return c.time.In(c.loc).Year()
}
// Quarter gets current quarter.
// Quarter gets current quarter like 3.
// 获取当前季度
func (c Carbon) Quarter() (quarter int) {
if c.IsInvalid() {
@ -166,13 +196,13 @@ func (c Carbon) Quarter() (quarter int) {
return
}
// Month gets current month.
// Month gets current month like 8.
// 获取当前月
func (c Carbon) Month() int {
return c.MonthOfYear()
}
// Week gets current week, start from 0.
// Week gets current week like 6, start from 0.
// 获取当前周(从0开始)
func (c Carbon) Week() int {
if c.IsInvalid() {
@ -181,13 +211,13 @@ func (c Carbon) Week() int {
return (c.DayOfWeek() + DaysPerWeek - int(c.weekStartsAt)) % DaysPerWeek
}
// Day gets current day.
// Day gets current day like 5.
// 获取当前日
func (c Carbon) Day() int {
return c.DayOfMonth()
}
// Hour gets current hour.
// Hour gets current hour like 13.
// 获取当前小时
func (c Carbon) Hour() int {
if c.IsInvalid() {
@ -196,7 +226,7 @@ func (c Carbon) Hour() int {
return c.time.In(c.loc).Hour()
}
// Minute gets current minute.
// Minute gets current minute like 14.
// 获取当前分钟数
func (c Carbon) Minute() int {
if c.IsInvalid() {
@ -205,7 +235,7 @@ func (c Carbon) Minute() int {
return c.time.In(c.loc).Minute()
}
// Second gets current second.
// Second gets current second like 15.
// 获取当前秒数
func (c Carbon) Second() int {
if c.IsInvalid() {
@ -214,7 +244,7 @@ func (c Carbon) Second() int {
return c.time.In(c.loc).Second()
}
// Millisecond gets current millisecond.
// Millisecond gets current millisecond like 999.
// 获取当前毫秒数3位数字
func (c Carbon) Millisecond() int {
if c.IsInvalid() {
@ -223,7 +253,7 @@ func (c Carbon) Millisecond() int {
return c.time.In(c.loc).Nanosecond() / 1e6
}
// Microsecond gets current microsecond.
// Microsecond gets current microsecond like 999999.
// 获取当前微秒数6位数字
func (c Carbon) Microsecond() int {
if c.IsInvalid() {
@ -232,7 +262,7 @@ func (c Carbon) Microsecond() int {
return c.time.In(c.loc).Nanosecond() / 1e3
}
// Nanosecond gets current nanosecond.
// Nanosecond gets current nanosecond like 999999999.
// 获取当前纳秒数9位数字
func (c Carbon) Nanosecond() int {
if c.IsInvalid() {
@ -241,7 +271,7 @@ func (c Carbon) Nanosecond() int {
return c.time.In(c.loc).Nanosecond()
}
// TimestampWithSecond gets timestamp with second.
// TimestampWithSecond gets timestamp with second like 1596604455.
// 输出秒级时间戳
func (c Carbon) TimestampWithSecond() int64 {
if c.IsInvalid() {
@ -250,13 +280,13 @@ func (c Carbon) TimestampWithSecond() int64 {
return c.time.Unix()
}
// Timestamp gets timestamp with second, it is shorthand for TimestampWithSecond.
// Timestamp gets timestamp with second like 1596604455, it is shorthand for TimestampWithSecond.
// 获取秒级时间戳, 是 TimestampWithSecond 的简写
func (c Carbon) Timestamp() int64 {
return c.TimestampWithSecond()
}
// TimestampWithMillisecond gets timestamp with millisecond.
// TimestampWithMillisecond gets timestamp with millisecond like 1596604455000.
// 获取毫秒级时间戳
func (c Carbon) TimestampWithMillisecond() int64 {
if c.IsInvalid() {
@ -265,13 +295,13 @@ func (c Carbon) TimestampWithMillisecond() int64 {
return c.time.Unix()*1e3 + int64(c.time.Nanosecond())/1e6
}
// TimestampMilli gets timestamp with millisecond, it is shorthand for TimestampWithMillisecond.
// TimestampMilli gets timestamp with millisecond like 1596604455000, it is shorthand for TimestampWithMillisecond.
// 获取毫秒级时间戳, 是 TimestampMilli 的简写
func (c Carbon) TimestampMilli() int64 {
return c.TimestampWithMillisecond()
}
// TimestampWithMicrosecond gets timestamp with microsecond.
// TimestampWithMicrosecond gets timestamp with microsecond like 1596604455000000.
// 获取微秒级时间戳
func (c Carbon) TimestampWithMicrosecond() int64 {
if c.IsInvalid() {
@ -280,13 +310,13 @@ func (c Carbon) TimestampWithMicrosecond() int64 {
return c.time.Unix()*1e6 + int64(c.time.Nanosecond())/1e3
}
// TimestampMicro gets timestamp with microsecond, it is shorthand for TimestampWithMicrosecond.
// TimestampMicro gets timestamp with microsecond like 1596604455000000, it is shorthand for TimestampWithMicrosecond.
// 获取微秒级时间戳, 是 TimestampWithMicrosecond 的简写
func (c Carbon) TimestampMicro() int64 {
return c.TimestampWithMicrosecond()
}
// TimestampWithNanosecond gets timestamp with nanosecond.
// TimestampWithNanosecond gets timestamp with nanosecond like 1596604455000000000.
// 获取纳秒级时间戳
func (c Carbon) TimestampWithNanosecond() int64 {
if c.IsInvalid() {
@ -295,45 +325,45 @@ func (c Carbon) TimestampWithNanosecond() int64 {
return c.time.UnixNano()
}
// TimestampNano gets timestamp with nanosecond, it is shorthand for TimestampWithNanosecond.
// TimestampNano gets timestamp with nanosecond like 1596604455000000000, it is shorthand for TimestampWithNanosecond.
// 获取纳秒级时间戳, 是 TimestampWithNanosecond 的简写
func (c Carbon) TimestampNano() int64 {
return c.TimestampWithNanosecond()
}
// Location gets location name.
// Location gets location name like "PRC".
// 获取位置
func (c Carbon) Location() string {
return c.loc.String()
}
// Timezone gets timezone name.
// Timezone gets timezone name like "CST".
// 获取时区
func (c Carbon) Timezone() string {
name, _ := c.time.Zone()
return name
}
// Offset gets offset seconds from the UTC timezone.
// Offset gets offset seconds from the UTC timezone like 28800.
// 获取距离UTC时区的偏移量单位秒
func (c Carbon) Offset() int {
_, offset := c.time.Zone()
return offset
}
// Locale gets locale name.
// Locale gets locale name like "zh-CN".
// 获取语言区域
func (c Carbon) Locale() string {
return c.lang.locale
}
// Age gets age.
// Age gets age like 18.
// 获取年龄
func (c Carbon) Age() int {
if c.IsInvalid() {
return 0
}
now := Now()
now := c.Now()
if c.Timestamp() > now.Timestamp() {
return 0
}

View File

@ -349,6 +349,99 @@ func TestCarbon_DateTime(t *testing.T) {
}
}
func TestCarbon_DateTimeMilli(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year, month, day, hour, minute, second, millisecond int // 期望值
}{
{"", 0, 0, 0, 0, 0, 0, 0},
{"0", 0, 0, 0, 0, 0, 0, 0},
{"0000-00-00", 0, 0, 0, 0, 0, 0, 0},
{"00:00:00", 0, 0, 0, 0, 0, 0, 0},
{"0000-00-00 00:00:00", 0, 0, 0, 0, 0, 0, 0},
{"2020-08-05", 2020, 8, 5, 0, 0, 0, 0},
{"2020-08-05 13:14:15.999999999", 2020, 8, 5, 13, 14, 15, 999},
}
for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
year, month, day, hour, minute, second, millisecond := c.DateTimeMilli()
assert.Equal(test.year, year, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.month, month, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.day, day, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.hour, hour, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.minute, minute, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.second, second, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.millisecond, millisecond, "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_DateTimeMicro(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year, month, day, hour, minute, second, microsecond int // 期望值
}{
{"", 0, 0, 0, 0, 0, 0, 0},
{"0", 0, 0, 0, 0, 0, 0, 0},
{"0000-00-00", 0, 0, 0, 0, 0, 0, 0},
{"00:00:00", 0, 0, 0, 0, 0, 0, 0},
{"0000-00-00 00:00:00", 0, 0, 0, 0, 0, 0, 0},
{"2020-08-05", 2020, 8, 5, 0, 0, 0, 0},
{"2020-08-05 13:14:15.999999999", 2020, 8, 5, 13, 14, 15, 999999},
}
for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
year, month, day, hour, minute, second, microsecond := c.DateTimeMicro()
assert.Equal(test.year, year, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.month, month, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.day, day, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.hour, hour, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.minute, minute, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.second, second, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.microsecond, microsecond, "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_DateTimeNano(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year, month, day, hour, minute, second, nanosecond int // 期望值
}{
{"", 0, 0, 0, 0, 0, 0, 0},
{"0", 0, 0, 0, 0, 0, 0, 0},
{"0000-00-00", 0, 0, 0, 0, 0, 0, 0},
{"00:00:00", 0, 0, 0, 0, 0, 0, 0},
{"0000-00-00 00:00:00", 0, 0, 0, 0, 0, 0, 0},
{"2020-08-05", 2020, 8, 5, 0, 0, 0, 0},
{"2020-08-05 13:14:15.999999999", 2020, 8, 5, 13, 14, 15, 999999999},
}
for index, test := range tests {
c := Parse(test.input, PRC)
assert.Nil(c.Error)
year, month, day, hour, minute, second, nanosecond := c.DateTimeNano()
assert.Equal(test.year, year, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.month, month, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.day, day, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.hour, hour, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.minute, minute, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.second, second, "Current test index is "+strconv.Itoa(index))
assert.Equal(test.nanosecond, nanosecond, "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Century(t *testing.T) {
assert := assert.New(t)

View File

@ -13,7 +13,7 @@ func (c Carbon) String() string {
}
// ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" format.
// 输出"2006-01-02 15:04:05.999999999 -0700 MST"格式字符串
// 输出 "2006-01-02 15:04:05.999999999 -0700 MST" 格式字符串
func (c Carbon) ToString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -24,7 +24,7 @@ func (c Carbon) ToString(timezone ...string) string {
return c.time.In(c.loc).String()
}
// ToMonthString outputs a string in month format, i18n is supported.
// ToMonthString outputs a string in month format like "January", i18n is supported.
// 输出完整月份字符串支持i18n
func (c Carbon) ToMonthString(timezone ...string) string {
if len(timezone) > 0 {
@ -45,7 +45,7 @@ func (c Carbon) ToMonthString(timezone ...string) string {
return ""
}
// ToShortMonthString outputs a string in short month format, i18n is supported.
// ToShortMonthString outputs a string in short month format like "Jan", i18n is supported.
// 输出缩写月份字符串支持i18n
func (c Carbon) ToShortMonthString(timezone ...string) string {
if len(timezone) > 0 {
@ -66,7 +66,7 @@ func (c Carbon) ToShortMonthString(timezone ...string) string {
return ""
}
// ToWeekString outputs a string in week format, i18n is supported.
// ToWeekString outputs a string in week format like "Sunday", i18n is supported.
// 输出完整星期字符串支持i18n
func (c Carbon) ToWeekString(timezone ...string) string {
if len(timezone) > 0 {
@ -87,7 +87,7 @@ func (c Carbon) ToWeekString(timezone ...string) string {
return ""
}
// ToShortWeekString outputs a string in short week format, i18n is supported.
// ToShortWeekString outputs a string in short week format like "Sun", i18n is supported.
// 输出缩写星期字符串支持i18n
func (c Carbon) ToShortWeekString(timezone ...string) string {
if len(timezone) > 0 {
@ -108,8 +108,8 @@ func (c Carbon) ToShortWeekString(timezone ...string) string {
return ""
}
// ToDayDateTimeString outputs a string in day, date and time format.
// 输出天数日期时间字符串
// ToDayDateTimeString outputs a string in "Mon, Jan 2, 2006 3:04 PM" format.
// 输出 "Mon, Jan 2, 2006 3:04 PM" 格式字符串
func (c Carbon) ToDayDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -120,8 +120,8 @@ func (c Carbon) ToDayDateTimeString(timezone ...string) string {
return c.time.In(c.loc).Format(DayDateTimeFormat)
}
// ToDateTimeString outputs a string in date and time format.
// 输出日期时间字符串
// ToDateTimeString outputs a string in "2006-01-02 15:04:05" format.
// 输出 "2006-01-02 15:04:05" 格式字符串
func (c Carbon) ToDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -132,8 +132,8 @@ func (c Carbon) ToDateTimeString(timezone ...string) string {
return c.time.In(c.loc).Format(DateTimeFormat)
}
// ToDateTimeMilliString outputs a string in date and time with millisecond format.
// 输出日期时间字符串,包含毫秒
// ToDateTimeMilliString outputs a string in "2006-01-02 15:04:05.999" format.
// 输出 "2006-01-02 15:04:05.999" 格式字符串
func (c Carbon) ToDateTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -144,8 +144,8 @@ func (c Carbon) ToDateTimeMilliString(timezone ...string) string {
return c.time.In(c.loc).Format(DateTimeMilliFormat)
}
// ToDateTimeMicroString outputs a string in date and time with microsecond format.
// 输出日期时间字符串,包含微秒
// ToDateTimeMicroString outputs a string in "2006-01-02 15:04:05.999999" format.
// 输出 "2006-01-02 15:04:05.999999" 格式字符串
func (c Carbon) ToDateTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -156,8 +156,8 @@ func (c Carbon) ToDateTimeMicroString(timezone ...string) string {
return c.time.In(c.loc).Format(DateTimeMicroFormat)
}
// ToDateTimeNanoString outputs a string in date and time with nanosecond format.
// 输出日期时间字符串,包含纳秒
// ToDateTimeNanoString outputs a string in "2006-01-02 15:04:05.999999999" format.
// 输出 "2006-01-02 15:04:05.999999999" 格式字符串
func (c Carbon) ToDateTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -168,8 +168,8 @@ func (c Carbon) ToDateTimeNanoString(timezone ...string) string {
return c.time.In(c.loc).Format(DateTimeNanoFormat)
}
// ToShortDateTimeString outputs a string in short date and time format.
// 输出简写日期时间字符串
// ToShortDateTimeString outputs a string in "20060102150405" format.
// 输出 "20060102150405" 格式字符串
func (c Carbon) ToShortDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -180,8 +180,8 @@ func (c Carbon) ToShortDateTimeString(timezone ...string) string {
return c.time.In(c.loc).Format(ShortDateTimeFormat)
}
// ToShortDateTimeMilliString outputs a string in short date and time with millisecond format.
// 输出简写日期时间字符串,包含毫秒
// ToShortDateTimeMilliString outputs a string in "20060102150405.999" format.
// 输出 "20060102150405.999" 格式字符串
func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -192,8 +192,8 @@ func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string {
return c.time.In(c.loc).Format(ShortDateTimeMilliFormat)
}
// ToShortDateTimeMicroString outputs a string in short date and time with microsecond format.
// 输出简写日期时间字符串,包含微秒
// ToShortDateTimeMicroString outputs a string in "20060102150405.999999" format.
// 输出 "20060102150405.999999" 格式字符串
func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -204,8 +204,8 @@ func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string {
return c.time.In(c.loc).Format(ShortDateTimeMicroFormat)
}
// ToShortDateTimeNanoString outputs a string in short date and time with nanosecond format.
// 输出简写日期时间字符串,包含纳秒
// ToShortDateTimeNanoString outputs a string in "20060102150405.999999999" format.
// 输出 "20060102150405.999999999" 格式字符串
func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -216,8 +216,8 @@ func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string {
return c.time.In(c.loc).Format(ShortDateTimeNanoFormat)
}
// ToDateString outputs a string in date format.
// 输出日期字符串
// ToDateString outputs a string in "2006-01-02" format.
// 输出 "2006-01-02" 格式字符串
func (c Carbon) ToDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -228,8 +228,8 @@ func (c Carbon) ToDateString(timezone ...string) string {
return c.time.In(c.loc).Format(DateFormat)
}
// ToShortDateString outputs a string in short date format.
// 输出简写日期字符串
// ToShortDateString outputs a string in "20060102" format.
// 输出 "20060102" 格式字符串
func (c Carbon) ToShortDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -240,8 +240,8 @@ func (c Carbon) ToShortDateString(timezone ...string) string {
return c.time.In(c.loc).Format(ShortDateFormat)
}
// ToTimeString outputs a string in time format.
// 输出时间字符串
// ToTimeString outputs a string in "15:04:05" format.
// 输出 "15:04:05" 格式字符串
func (c Carbon) ToTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -252,8 +252,8 @@ func (c Carbon) ToTimeString(timezone ...string) string {
return c.time.In(c.loc).Format(TimeFormat)
}
// ToShortTimeString outputs a string in short time format.
// 输出简写时间字符串
// ToShortTimeString outputs a string in "150405" format.
// 输出 "150405" 格式字符串
func (c Carbon) ToShortTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -264,14 +264,14 @@ func (c Carbon) ToShortTimeString(timezone ...string) string {
return c.time.In(c.loc).Format(ShortTimeFormat)
}
// ToAtomString outputs a string in ATOM format.
// 输出 ATOM 格式字符串
// ToAtomString outputs a string in "2006-01-02T15:04:05Z07:00" format.
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToAtomString(timezone ...string) string {
return c.ToRfc3339String(timezone...)
}
// ToAnsicString outputs a string in ANSIC format.
// 输出 ANSIC 格式字符串
// ToAnsicString outputs a string in "Mon Jan _2 15:04:05 2006" format.
// 输出 "Mon Jan _2 15:04:05 2006" 格式字符串
func (c Carbon) ToAnsicString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -282,8 +282,8 @@ func (c Carbon) ToAnsicString(timezone ...string) string {
return c.time.In(c.loc).Format(AnsicFormat)
}
// ToCookieString outputs a string in COOKIE format.
// 输出 COOKIE 格式字符串
// ToCookieString outputs a string in "Monday, 02-Jan-2006 15:04:05 MST" format.
// 输出 "Monday, 02-Jan-2006 15:04:05 MST" 格式字符串
func (c Carbon) ToCookieString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -294,8 +294,8 @@ func (c Carbon) ToCookieString(timezone ...string) string {
return c.time.In(c.loc).Format(CookieFormat)
}
// ToRssString outputs a string in RSS format.
// 输出 RSS 格式字符串
// ToRssString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format.
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRssString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -306,14 +306,14 @@ func (c Carbon) ToRssString(timezone ...string) string {
return c.time.In(c.loc).Format(RssFormat)
}
// ToW3cString outputs a string in W3C format.
// 输出 W3C 格式字符串
// ToW3cString outputs a string in "2006-01-02T15:04:05Z07:00" format.
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToW3cString(timezone ...string) string {
return c.ToRfc3339String(timezone...)
}
// ToUnixDateString outputs a string in unix date format.
// 输出 UnixDate 格式字符串
// ToUnixDateString outputs a string in "Mon Jan _2 15:04:05 MST 2006" format.
// 输出 "Mon Jan _2 15:04:05 MST 2006" 格式字符串
func (c Carbon) ToUnixDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -324,8 +324,8 @@ func (c Carbon) ToUnixDateString(timezone ...string) string {
return c.time.In(c.loc).Format(UnixDateFormat)
}
// ToRubyDateString outputs a string in ruby date format.
// 输出 RubyDate 格式字符串
// ToRubyDateString outputs a string in "Mon Jan 02 15:04:05 -0700 2006" format.
// 输出 "Mon Jan 02 15:04:05 -0700 2006" 格式字符串
func (c Carbon) ToRubyDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -336,8 +336,8 @@ func (c Carbon) ToRubyDateString(timezone ...string) string {
return c.time.In(c.loc).Format(RubyDateFormat)
}
// ToKitchenString outputs a string in KITCHEN format.
// 输出 KITCHEN 格式字符串
// ToKitchenString outputs a string in "3:04PM" format.
// 输出 "3:04PM" 格式字符串
func (c Carbon) ToKitchenString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -348,8 +348,8 @@ func (c Carbon) ToKitchenString(timezone ...string) string {
return c.time.In(c.loc).Format(KitchenFormat)
}
// ToIso8601String outputs a string in ISO8601 format.
// 输出 ISO8601 格式字符串
// ToIso8601String outputs a string in "2006-01-02T15:04:05-07:00" format.
// 输出 "2006-01-02T15:04:05-07:00" 格式字符串
func (c Carbon) ToIso8601String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -360,8 +360,8 @@ func (c Carbon) ToIso8601String(timezone ...string) string {
return c.time.In(c.loc).Format(ISO8601Format)
}
// ToRfc822String outputs a string in RFC822 format.
// 输出 RFC822 格式字符串
// ToRfc822String outputs a string in "02 Jan 06 15:04 MST" format.
// 输出 "02 Jan 06 15:04 MST" 格式字符串
func (c Carbon) ToRfc822String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -372,8 +372,8 @@ func (c Carbon) ToRfc822String(timezone ...string) string {
return c.time.In(c.loc).Format(RFC822Format)
}
// ToRfc822zString outputs a string in RFC822Z format.
// 输出 RFC822Z 格式字符串
// ToRfc822zString outputs a string in "02 Jan 06 15:04 -0700" format.
// 输出 "02 Jan 06 15:04 -0700" 格式字符串
func (c Carbon) ToRfc822zString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -384,8 +384,8 @@ func (c Carbon) ToRfc822zString(timezone ...string) string {
return c.time.In(c.loc).Format(RFC822ZFormat)
}
// ToRfc850String outputs a string in RFC850 format.
// 输出 RFC850 格式字符串
// ToRfc850String outputs a string in "Monday, 02-Jan-06 15:04:05 MST" format.
// 输出 "Monday, 02-Jan-06 15:04:05 MST" 格式字符串
func (c Carbon) ToRfc850String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -396,8 +396,8 @@ func (c Carbon) ToRfc850String(timezone ...string) string {
return c.time.In(c.loc).Format(RFC850Format)
}
// ToRfc1036String outputs a string in RFC1036 format.
// 输出 RFC1036 格式字符串
// ToRfc1036String outputs a string in "Mon, 02 Jan 06 15:04:05 -0700" format.
// 输出 "Mon, 02 Jan 06 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc1036String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -408,8 +408,8 @@ func (c Carbon) ToRfc1036String(timezone ...string) string {
return c.time.In(c.loc).Format(RFC1036Format)
}
// ToRfc1123String outputs a string in RFC1123 format.
// 输出 RFC1123 格式字符串
// ToRfc1123String outputs a string in "Mon, 02 Jan 2006 15:04:05 MST" format.
// 输出 "Mon, 02 Jan 2006 15:04:05 MST" 格式字符串
func (c Carbon) ToRfc1123String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -420,8 +420,8 @@ func (c Carbon) ToRfc1123String(timezone ...string) string {
return c.time.In(c.loc).Format(RFC1123Format)
}
// ToRfc1123zString outputs a string in RFC1123z format.
// 输出 RFC1123z 格式字符串
// ToRfc1123zString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format.
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc1123zString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -432,8 +432,8 @@ func (c Carbon) ToRfc1123zString(timezone ...string) string {
return c.time.In(c.loc).Format(RFC1123ZFormat)
}
// ToRfc2822String outputs a string in RFC2822 format.
// 输出 RFC2822 格式字符串
// ToRfc2822String outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format.
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc2822String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -444,8 +444,8 @@ func (c Carbon) ToRfc2822String(timezone ...string) string {
return c.time.In(c.loc).Format(RFC2822Format)
}
// ToRfc3339String outputs a string in RFC3339 format.
// 输出 RFC3339 格式字符串
// ToRfc3339String outputs a string in "2006-01-02T15:04:05Z07:00" format.
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToRfc3339String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -456,8 +456,8 @@ func (c Carbon) ToRfc3339String(timezone ...string) string {
return c.time.In(c.loc).Format(RFC3339Format)
}
// ToRfc3339MilliString outputs a string in RFC3339 with millisecond format.
// 输出 RFC3339 格式字符串,包含豪秒
// ToRfc3339MilliString outputs a string in "2006-01-02T15:04:05.999Z07:00" format.
// 输出 "2006-01-02T15:04:05.999Z07:00" 格式字符串
func (c Carbon) ToRfc3339MilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -468,8 +468,8 @@ func (c Carbon) ToRfc3339MilliString(timezone ...string) string {
return c.time.In(c.loc).Format(RFC3339MilliFormat)
}
// ToRfc3339MicroString outputs a string in RFC3339 with microsecond format.
// 输出 RFC3339 格式字符串,包含微秒
// ToRfc3339MicroString outputs a string in "2006-01-02T15:04:05.999999Z07:00" format.
// 输出 "2006-01-02T15:04:05.999999Z07:00" 格式字符串
func (c Carbon) ToRfc3339MicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -480,8 +480,8 @@ func (c Carbon) ToRfc3339MicroString(timezone ...string) string {
return c.time.In(c.loc).Format(RFC3339MicroFormat)
}
// ToRfc3339NanoString outputs a string in RFC3339 with nanosecond format.
// 输出 RFC3339 格式字符串,包含纳秒
// ToRfc3339NanoString outputs a string in "2006-01-02T15:04:05.999999999Z07:00" format.
// 输出 "2006-01-02T15:04:05.999999999Z07:00" 格式字符串
func (c Carbon) ToRfc3339NanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
@ -492,8 +492,8 @@ func (c Carbon) ToRfc3339NanoString(timezone ...string) string {
return c.time.In(c.loc).Format(RFC3339NanoFormat)
}
// ToRfc7231String outputs a string in RFC7231 format.
// 输出 RFC7231 格式字符串
// ToRfc7231String outputs a string in "Mon, 02 Jan 2006 15:04:05 GMT" format.
// 输出 "Mon, 02 Jan 2006 15:04:05 GMT" 格式字符串
func (c Carbon) ToRfc7231String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])

View File

@ -46,7 +46,7 @@ func Parse(value string, timezone ...string) Carbon {
// ParseByFormat parses a string as a Carbon instance by format.
// 通过格式化字符将字符串解析成 carbon 实例
func (c Carbon) ParseByFormat(value string, format string, timezone ...string) Carbon {
func (c Carbon) ParseByFormat(value, format string, timezone ...string) Carbon {
carbon := c.ParseByLayout(value, format2layout(format), timezone...)
if carbon.Error != nil {
carbon.Error = invalidFormatError(value, format)
@ -56,13 +56,13 @@ func (c Carbon) ParseByFormat(value string, format string, timezone ...string) C
// ParseByFormat parses a string as a Carbon instance by format.
// 通过布局字符将字符串解析成 carbon 实例
func ParseByFormat(value string, format string, timezone ...string) Carbon {
func ParseByFormat(value, format string, timezone ...string) Carbon {
return NewCarbon().ParseByFormat(value, format, timezone...)
}
// ParseByLayout parses a string as a Carbon instance by layout.
// 通过布局字符将字符串解析成 carbon 实例
func (c Carbon) ParseByLayout(value string, layout string, timezone ...string) Carbon {
func (c Carbon) ParseByLayout(value, layout string, timezone ...string) Carbon {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
@ -83,6 +83,6 @@ func (c Carbon) ParseByLayout(value string, layout string, timezone ...string) C
// ParseByLayout parses a string as a Carbon instance by layout.
// 将布局时间字符串解析成 Carbon 实例
func ParseByLayout(value string, layout string, timezone ...string) Carbon {
func ParseByLayout(value, layout string, timezone ...string) Carbon {
return NewCarbon().ParseByLayout(value, layout, timezone...)
}

View File

@ -43,15 +43,6 @@ func TestCarbon_Parse(t *testing.T) {
}
}
func TestError_Parse(t *testing.T) {
value, timezone := "2020-13-50", "xxx"
c1 := Parse(value)
assert.NotNil(t, c1.Error, "It should catch an exception in Parse()")
c2 := Parse(value, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in Parse()")
}
func TestCarbon_ParseByFormat(t *testing.T) {
assert := assert.New(t)
@ -85,17 +76,6 @@ func TestCarbon_ParseByFormat(t *testing.T) {
}
}
func TestError_ParseByFormat(t *testing.T) {
assert := assert.New(t)
value, format, timezone := "2020-08-50", "Y-m-d", "xxx"
c1 := ParseByFormat(value, format)
assert.NotNil(t, c1.Error, "It should catch an exception in ParseByFormat()")
c2 := ParseByFormat(value, format, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in ParseByFormat()")
}
func TestCarbon_ParseByLayout(t *testing.T) {
assert := assert.New(t)
@ -128,13 +108,9 @@ func TestCarbon_ParseByLayout(t *testing.T) {
}
}
func TestError_ParseByLayout(t *testing.T) {
assert := assert.New(t)
value, layout, timezone := "2020-08-50", "2006-01-02", "xxx"
c1 := ParseByLayout(value, layout)
assert.NotNil(t, c1.Error, "It should catch an exception in ParseByLayout()")
c2 := ParseByLayout(value, layout, timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in ParseByLayout()")
func TestError_Parse(t *testing.T) {
date, layout, format, timezone := "2020-08-50", "2006-01-02", DateFormat, "xxx"
assert.NotNil(t, Parse(date).Error, "It should catch an exception in Parse()")
assert.NotNil(t, ParseByLayout(date, layout, timezone).Error, "It should catch an exception in ParseByLayout()")
assert.NotNil(t, ParseByFormat(date, format, timezone).Error, "It should catch an exception in ParseByFormat()")
}

View File

@ -4,7 +4,7 @@ import (
"strings"
)
// Season gets season name according to the meteorological division method, i18n is supported.
// Season gets season name according to the meteorological division method like "Spring", i18n is supported.
// 获取当前季节(以气象划分)支持i18n
func (c Carbon) Season() string {
if c.IsInvalid() {

View File

@ -68,6 +68,33 @@ func (c Carbon) SetDateTime(year, month, day, hour, minute, second int) Carbon {
return c.create(year, month, day, hour, minute, second, c.Nanosecond())
}
// SetDateTimeMilli sets year, month, day, hour, minute, second and millisecond.
// 设置年月日时分秒毫秒
func (c Carbon) SetDateTimeMilli(year, month, day, hour, minute, second, millisecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, millisecond*1e6)
}
// SetDateTimeMicro sets year, month, day, hour, minute, second and microsecond.
// 设置年月日时分秒微秒
func (c Carbon) SetDateTimeMicro(year, month, day, hour, minute, second, microsecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, microsecond*1e3)
}
// SetDateTimeNano sets year, month, day, hour, minute, second and nanosecond.
// 设置年月日时分秒纳秒
func (c Carbon) SetDateTimeNano(year, month, day, hour, minute, second, nanosecond int) Carbon {
if c.IsInvalid() {
return c
}
return c.create(year, month, day, hour, minute, second, nanosecond)
}
// SetDate sets year, month and day.
// 设置年月日
func (c Carbon) SetDate(year, month, day int) Carbon {

View File

@ -27,15 +27,6 @@ func TestCarbon_SetTimezone(t *testing.T) {
}
}
func TestError_SetTimezone(t *testing.T) {
timezone := "xxx"
c1 := SetTimezone(timezone)
assert.NotNil(t, c1.Error, "It should catch an exception in SetTimezone()")
c2 := Now(timezone).SetTimezone(timezone)
assert.NotNil(t, c2.Error, "It should catch an exception in SetTimezone()")
}
func TestCarbon_SetLocale(t *testing.T) {
assert := assert.New(t)
@ -63,16 +54,6 @@ func TestCarbon_SetLocale(t *testing.T) {
}
}
func TestError_SetLocale(t *testing.T) {
locale := "xxx"
c1 := SetLocale(locale)
assert.NotNil(t, c1.Error, "It should catch an exception in SetLocale()")
c2 := SetLocale(locale).SetLocale(PRC)
assert.NotNil(t, c2.Error, "It should catch an exception in SetLocale()")
}
func TestCarbon_SetLanguage(t *testing.T) {
lang := NewLanguage()
resources := map[string]string{
@ -106,17 +87,6 @@ func TestCarbon_SetLanguage(t *testing.T) {
}
}
func TestError_SetLanguage(t *testing.T) {
lang, locale := NewLanguage(), "xxx"
lang.SetLocale(locale)
c1 := SetLanguage(lang)
assert.NotNil(t, c1.Error, "It should catch an exception in SetLanguage()")
c2 := SetLocale(locale).SetLanguage(lang)
assert.NotNil(t, c2.Error, "It should catch an exception in SetLanguage()")
}
func TestCarbon_SetDateTime(t *testing.T) {
assert := assert.New(t)
@ -136,10 +106,61 @@ func TestCarbon_SetDateTime(t *testing.T) {
}
}
func TestError_SetDateTime(t *testing.T) {
input, year, month, day, hour, minute, second := "2020-08-50", 2020, 8, 50, 13, 14, 15
c := Parse(input).SetDateTime(year, month, day, hour, minute, second)
assert.NotNil(t, c.Error, "It should catch an exception in SetDateTime()")
func TestCarbon_SetDateTimeMilli(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year, month, day, hour, minute, second, millisecond int // 输入参数
expected string // 期望值
}{
{"2020-01-01", 2019, 02, 02, 13, 14, 15, 999, "2019-02-02 13:14:15.999 +0800 CST"},
{"2020-01-01", 2019, 02, 31, 13, 14, 15, 999, "2019-03-03 13:14:15.999 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SetDateTimeMilli(test.year, test.month, test.day, test.hour, test.minute, test.second, test.millisecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetDateTimeMicro(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year, month, day, hour, minute, second, microsecond int // 输入参数
expected string // 期望值
}{
{"2020-01-01", 2019, 02, 02, 13, 14, 15, 999999, "2019-02-02 13:14:15.999999 +0800 CST"},
{"2020-01-01", 2019, 02, 31, 13, 14, 15, 999999, "2019-03-03 13:14:15.999999 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SetDateTimeMicro(test.year, test.month, test.day, test.hour, test.minute, test.second, test.microsecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetDateTimeNano(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string // 输入值
year, month, day, hour, minute, second, nanosecond int // 输入参数
expected string // 期望值
}{
{"2020-01-01", 2019, 02, 02, 13, 14, 15, 999999999, "2019-02-02 13:14:15.999999999 +0800 CST"},
{"2020-01-01", 2019, 02, 31, 13, 14, 15, 999999999, "2019-03-03 13:14:15.999999999 +0800 CST"},
}
for index, test := range tests {
c := Parse(test.input, PRC).SetDateTimeNano(test.year, test.month, test.day, test.hour, test.minute, test.second, test.nanosecond)
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetDate(t *testing.T) {
@ -161,12 +182,6 @@ func TestCarbon_SetDate(t *testing.T) {
}
}
func TestError_SetDate(t *testing.T) {
input, year, month, day := "2020-08-50", 2020, 8, 50
c := Parse(input).SetDate(year, month, day)
assert.NotNil(t, c.Error, "It should catch an exception in SeDate()")
}
func TestCarbon_SetTime(t *testing.T) {
assert := assert.New(t)
@ -186,12 +201,6 @@ func TestCarbon_SetTime(t *testing.T) {
}
}
func TestError_SetTime(t *testing.T) {
input, hour, minute, second := "2020-08-50", 13, 14, 15
c := Parse(input).SetTime(hour, minute, second)
assert.NotNil(t, c.Error, "It should catch an exception in SetTime()")
}
func TestCarbon_SetYear(t *testing.T) {
assert := assert.New(t)
@ -236,18 +245,6 @@ func TestCarbon_SetYearNoOverflow(t *testing.T) {
}
}
func TestError_SetYear(t *testing.T) {
input, year := "2020-08-50", 2020
c := Parse(input).SetYear(year)
assert.NotNil(t, c.Error, "It should catch an exception in SetYear()")
}
func TestError_SetYearNoOverflow(t *testing.T) {
value, year := "2020-08-50", 2020
c := Parse(value).SetYearNoOverflow(year)
assert.NotNil(t, c.Error, "It should catch an exception in SetYearNoOverflow()")
}
func TestCarbon_SetMonth(t *testing.T) {
assert := assert.New(t)
@ -290,18 +287,6 @@ func TestCarbon_SetMonthNoOverflow(t *testing.T) {
}
}
func TestError_SetMonth(t *testing.T) {
input, month := "2020-08-50", 12
c := Parse(input).SetMonth(month)
assert.NotNil(t, c.Error, "It should catch an exception in SetMonth()")
}
func TestError_SetMonthNoOverflow(t *testing.T) {
input, month := "2020-08-50", 12
c := Parse(input).SetMonthNoOverflow(month)
assert.NotNil(t, c.Error, "It should catch an exception in SetMonthNoOverflow()")
}
func TestCarbon_SetWeekStartsAt(t *testing.T) {
assert := assert.New(t)
@ -372,12 +357,6 @@ func TestCarbon_SetDay(t *testing.T) {
}
}
func TestError_SetDay(t *testing.T) {
input, day := "2020-08-50", 30
c := Parse(input).SetDay(day)
assert.NotNil(t, c.Error, "It should catch an exception in SetDay()")
}
func TestCarbon_SetHour(t *testing.T) {
assert := assert.New(t)
@ -397,12 +376,6 @@ func TestCarbon_SetHour(t *testing.T) {
}
}
func TestError_SetHour(t *testing.T) {
value, hour := "2020-08-50", 12
c := Parse(value).SetHour(hour)
assert.NotNil(t, c.Error, "It should catch an exception in SetHour()")
}
func TestCarbon_SetMinute(t *testing.T) {
assert := assert.New(t)
@ -422,12 +395,6 @@ func TestCarbon_SetMinute(t *testing.T) {
}
}
func TestError_SetMinute(t *testing.T) {
input, minute := "2020-08-50", 30
c := Parse(input).SetMinute(minute)
assert.NotNil(t, c.Error, "It should catch an exception in SetMinute()")
}
func TestCarbon_SetSecond(t *testing.T) {
assert := assert.New(t)
@ -447,12 +414,6 @@ func TestCarbon_SetSecond(t *testing.T) {
}
}
func TestError_SetSecond(t *testing.T) {
input, second := "2020-08-50", 30
c := Parse(input).SetSecond(second)
assert.NotNil(t, c.Error, "It should catch an exception in SetSecond()")
}
func TestCarbon_SetMillisecond(t *testing.T) {
assert := assert.New(t)
tests := []struct {
@ -471,12 +432,6 @@ func TestCarbon_SetMillisecond(t *testing.T) {
}
}
func TestError_SetMillisecond(t *testing.T) {
input, millisecond := "2020-08-50", 100
c := Parse(input).SetMillisecond(millisecond)
assert.NotNil(t, c.Error, "It should catch an exception in SetMillisecond()")
}
func TestCarbon_SetMicrosecond(t *testing.T) {
assert := assert.New(t)
@ -496,12 +451,6 @@ func TestCarbon_SetMicrosecond(t *testing.T) {
}
}
func TestError_SetMicrosecond(t *testing.T) {
input, microsecond := "2020-08-50", 100000
c := Parse(input).SetMicrosecond(microsecond)
assert.NotNil(t, c.Error, "It should catch an exception in SetMicrosecond()")
}
func TestCarbon_SetNanosecond(t *testing.T) {
assert := assert.New(t)
@ -521,8 +470,47 @@ func TestCarbon_SetNanosecond(t *testing.T) {
}
}
func TestError_SetNanosecond(t *testing.T) {
input, nanosecond := "2020-08-50", 100000000
c := Parse(input).SetNanosecond(nanosecond)
assert.NotNil(t, c.Error, "It should catch an exception in SetNanosecond()")
func TestError_Setter(t *testing.T) {
input, timezone, locale, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond := "2020-08-50 13:14:15", "xxx", "xxx", 2020, 8, 50, 13, 14, 15, 999, 999999, 999999999
c := Parse(input)
tz1 := SetTimezone(timezone)
assert.NotNil(t, tz1.Error, "It should catch an exception in SetTimezone()")
tz2 := Now(timezone).SetTimezone(timezone)
assert.NotNil(t, tz2.Error, "It should catch an exception in SetTimezone()")
loc1 := SetLocale(locale)
assert.NotNil(t, loc1.Error, "It should catch an exception in SetLocale()")
loc2 := SetLocale(locale).SetLocale(PRC)
assert.NotNil(t, loc2.Error, "It should catch an exception in SetLocale()")
lang := NewLanguage()
lang.SetLocale(locale)
lang1 := SetLanguage(lang)
lang2 := SetLocale(locale).SetLanguage(lang)
assert.NotNil(t, lang1.Error, "It should catch an exception in SetLanguage()")
assert.NotNil(t, lang2.SetLanguage(lang).Error, "It should catch an exception in SetLanguage()")
dt1 := c.SetDateTime(year, month, day, hour, minute, second)
assert.NotNil(t, dt1.Error, "It should catch an exception in SetDateTime()")
dt2 := c.SetDateTimeMilli(year, month, day, hour, minute, second, millisecond)
assert.NotNil(t, dt2.Error, "It should catch an exception in SetDateTimeMilli()")
dt3 := c.SetDateTimeMicro(year, month, day, hour, minute, second, microsecond)
assert.NotNil(t, dt3.Error, "It should catch an exception in SetDateTimeMicro()")
dt4 := c.SetDateTimeNano(year, month, day, hour, minute, second, nanosecond)
assert.NotNil(t, dt4.Error, "It should catch an exception in SetDateTimeNano()")
assert.NotNil(t, c.SetDate(year, month, day).Error, "It should catch an exception in SeDate()")
assert.NotNil(t, c.SetTime(hour, minute, second).Error, "It should catch an exception in SetTime()")
assert.NotNil(t, c.SetYear(year).Error, "It should catch an exception in SetYear()")
assert.NotNil(t, c.SetYearNoOverflow(year).Error, "It should catch an exception in SetYearNoOverflow()")
assert.NotNil(t, c.SetMonth(month).Error, "It should catch an exception in SetMonth()")
assert.NotNil(t, c.SetMonthNoOverflow(month).Error, "It should catch an exception in SetMonthNoOverflow()")
assert.NotNil(t, c.SetDay(day).Error, "It should catch an exception in SetDay()")
assert.NotNil(t, c.SetHour(hour).Error, "It should catch an exception in SetHour()")
assert.NotNil(t, c.SetMinute(minute).Error, "It should catch an exception in SetMinute()")
assert.NotNil(t, c.SetSecond(second).Error, "It should catch an exception in SetSecond()")
assert.NotNil(t, c.SetMillisecond(millisecond).Error, "It should catch an exception in SetMillisecond()")
assert.NotNil(t, c.SetMicrosecond(microsecond).Error, "It should catch an exception in SetMicrosecond()")
assert.NotNil(t, c.SetNanosecond(nanosecond).Error, "It should catch an exception in SetNanosecond()")
}

View File

@ -38,12 +38,6 @@ func TestCarbon_AddDuration(t *testing.T) {
}
}
func TestError_AddDuration(t *testing.T) {
duration := "10x"
c := Parse("2020-08-05").AddDuration(duration)
assert.NotNil(t, c.Error, "It should catch an exception in AddDuration()")
}
func TestCarbon_SubDuration(t *testing.T) {
assert := assert.New(t)
@ -1740,3 +1734,9 @@ func TestCarbon_SubSecond(t *testing.T) {
assert.Equal(test.expected, c.ToDateTimeString(), "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()")
}