Skip to content

Commit 58e2c23

Browse files
committed
v1.2.1版本
1 parent 39dd41f commit 58e2c23

File tree

5 files changed

+265
-7
lines changed

5 files changed

+265
-7
lines changed

README.en.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,12 @@ carbon.CreateFromGoTime(time.Now()).ToTimestamp() // 1596604455
119119

120120
##### Parse standard time format string
121121
```go
122+
carbon.Parse("").ToDateTimeString() // empty string
123+
carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // empty string
124+
carbon.Parse("0000-00-00").ToDateTimeString() // empty string
122125
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString() // 2020-08-05 13:14:15
123-
carbon.Parse("20200805131415").ToDateTimeString() // 2020-08-05 13:14:15
124126
carbon.Parse("2020-08-05").ToDateTimeString() // 2020-08-05 00:00:00
127+
carbon.Parse("20200805131415").ToDateTimeString() // 2020-08-05 13:14:15
125128
carbon.Parse("20200805").ToDateTimeString() // 2020-08-05 00:00:00
126129
carbon.Parse("2020-08-05T13:14:15+08:00").ToDateTimeString() // 2020-08-05 00:00:00
127130
```
@@ -295,12 +298,32 @@ carbon.Parse("2020-08-05 13:14:15").ToRFC7231String() // Wed, 05 Aug 2020 05:14:
295298
```
296299
> For more format signs, please see the <a href="#format-sign-table">Format sign table</a>
297300
298-
##### Total days
301+
##### Statistics
299302
```go
300303
// Total days of the year
304+
carbon.Parse("2019-08-05 13:14:15").DaysInYear() // 365
301305
carbon.Parse("2020-08-05 13:14:15").DaysInYear() // 366
302306
// Total days of the month
303-
carbon.Parse("2020-08-05 13:14:15").DaysInMonth() // 31
307+
carbon.Parse("2020-02-01 13:14:15").DaysInMonth() // 29
308+
carbon.Parse("2020-04-01 13:14:15").DaysInMonth() // 30
309+
carbon.Parse("2020-08-01 13:14:15").DaysInMonth() // 31
310+
311+
// current age
312+
carbon.Parse("1990-01-01 13:14:15").Age() // 30
313+
carbon.Parse("1990-12-31 13:14:15").Age() // 29
314+
315+
// Current year
316+
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
317+
// Current month
318+
carbon.Parse("2020-08-05 13:14:15").Month() // 8
319+
// Current day
320+
carbon.Parse("2020-08-05 13:14:15").Day() // 5
321+
// Current hour
322+
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
323+
// Current minute
324+
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
325+
// Current second
326+
carbon.Parse("2020-08-05 13:14:15").Second() // 15
304327
```
305328

306329
##### Week and day

README.md

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,12 @@ carbon.CreateFromGoTime(time.Now()).ToTimestamp() // 1596604455
117117

118118
##### 解析标准格式时间字符串
119119
```go
120+
carbon.Parse("").ToDateTimeString() // 空字符串
121+
carbon.Parse("0000-00-00 00:00:00").ToDateTimeString() // 空字符串
122+
carbon.Parse("0000-00-00").ToDateTimeString() // 空字符串
120123
carbon.Parse("2020-08-05 13:14:15").ToDateTimeString() // 2020-08-05 13:14:15
121-
carbon.Parse("20200805131415").ToDateTimeString() // 2020-08-05 13:14:15
122124
carbon.Parse("2020-08-05").ToDateTimeString() // 2020-08-05 00:00:00
125+
carbon.Parse("20200805131415").ToDateTimeString() // 2020-08-05 13:14:15
123126
carbon.Parse("20200805").ToDateTimeString() // 2020-08-05 00:00:00
124127
carbon.Parse("2020-08-05T13:14:15+08:00").ToDateTimeString() // 2020-08-05 00:00:00
125128
```
@@ -297,12 +300,32 @@ carbon.Parse("2020-08-05 13:14:15").ToRFC7231String() // Wed, 05 Aug 2020 05:14:
297300
```
298301
>更多格式化输出符号请查看附录 <a href="#格式化符号表">格式化符号表</a>
299302
300-
##### 总天数
303+
##### 统计数字
301304
```go
302305
// 本年总天数
306+
carbon.Parse("2019-08-05 13:14:15").DaysInYear() // 365
303307
carbon.Parse("2020-08-05 13:14:15").DaysInYear() // 366
304308
// 本月总天数
305-
carbon.Parse("2020-08-05 13:14:15").DaysInMonth() // 31
309+
carbon.Parse("2020-02-01 13:14:15").DaysInMonth() // 29
310+
carbon.Parse("2020-04-01 13:14:15").DaysInMonth() // 30
311+
carbon.Parse("2020-08-01 13:14:15").DaysInMonth() // 31
312+
313+
// 获取年龄
314+
carbon.Parse("1990-01-01 13:14:15").Age() // 30
315+
carbon.Parse("1990-12-31 13:14:15").Age() // 29
316+
317+
// 当前年
318+
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
319+
// 当前月
320+
carbon.Parse("2020-08-05 13:14:15").Month() // 8
321+
// 当前日
322+
carbon.Parse("2020-08-05 13:14:15").Day() // 5
323+
// 当前时
324+
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
325+
// 当前分
326+
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
327+
// 当前秒
328+
carbon.Parse("2020-08-05 13:14:15").Second() // 15
306329
```
307330

308331
##### 第几周/天
@@ -545,6 +568,15 @@ func (c ToRssString) MarshalJSON() ([]byte, error) {
545568
* [araddon/dateparse](https://github.com/araddon/dateparse)
546569

547570
#### 更新日志
571+
##### 2020-10-16
572+
* 新增Age()方法获取年龄
573+
* 新增Year()方法获取当前年
574+
* 新增Month()方法获取当前月
575+
* 新增Day()方法获取当前日
576+
* 新增Hour()方法获取当前小时
577+
* 新增Minute()方法获取当前分钟数
578+
* 新增Second()方法获取当前秒数
579+
548580
##### 2020-10-12
549581
* 完善单元测试,代码覆盖率100%
550582
* 统一异常处理

const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "time"
44

55
// 版权信息
66
const (
7-
Version = "v1.2.0"
7+
Version = "v1.2.1"
88
Author = "gouguoyin"
99
1010
Blog = "www.gouguoyin.cn"

final.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,66 @@ func (c Carbon) WeekOfMonth() int {
266266
return day%DaysPerWeek + 1
267267
}
268268

269+
// Age 获取年龄
270+
func (c Carbon) Age() int {
271+
if c.Time.IsZero() {
272+
return 0
273+
}
274+
age := time.Now().Year() - c.Time.Year()
275+
if int(time.Now().Month())*10+time.Now().Day() < int(c.Time.Month())*10+c.Time.Day() {
276+
age = age - 1
277+
}
278+
return age
279+
}
280+
281+
// 获取当前年
282+
func (c Carbon) Year() int {
283+
if c.Time.IsZero() {
284+
return 0
285+
}
286+
return c.Time.Year()
287+
}
288+
289+
// 获取当前月
290+
func (c Carbon) Month() int {
291+
if c.Time.IsZero() {
292+
return 0
293+
}
294+
return int(c.Time.Month())
295+
}
296+
297+
// 获取当前日
298+
func (c Carbon) Day() int {
299+
if c.Time.IsZero() {
300+
return 0
301+
}
302+
return c.Time.Day()
303+
}
304+
305+
// 获取当前小时
306+
func (c Carbon) Hour() int {
307+
if c.Time.IsZero() {
308+
return 0
309+
}
310+
return c.Time.Hour()
311+
}
312+
313+
// 获取当前分钟数
314+
func (c Carbon) Minute() int {
315+
if c.Time.IsZero() {
316+
return 0
317+
}
318+
return c.Time.Minute()
319+
}
320+
321+
// 获取当前秒数
322+
func (c Carbon) Second() int {
323+
if c.Time.IsZero() {
324+
return 0
325+
}
326+
return c.Time.Second()
327+
}
328+
269329
// IsZero 是否是零值
270330
func (c Carbon) IsZero() bool {
271331
return c.Time.IsZero()

final_test.go

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,155 @@ func TestCarbon_WeekOfMonth(t *testing.T) {
676676
}
677677
}
678678

679+
func TestCarbon_Age(t *testing.T) {
680+
Tests := []struct {
681+
input string // 输入值
682+
output int // 期望输出值
683+
}{
684+
{"0000-00-00", 0},
685+
{Now().SubYears(18).ToDateTimeString(), 18},
686+
}
687+
688+
for _, v := range Tests {
689+
output := Parse(v.input).Age()
690+
691+
if output != v.output {
692+
t.Fatalf("Input %s, expected %d, but got %d", v.input, v.output, output)
693+
}
694+
}
695+
}
696+
697+
func TestCarbon_Year(t *testing.T) {
698+
Tests := []struct {
699+
input string // 输入值
700+
output int // 期望输出值
701+
}{
702+
{"", 0},
703+
{"0000-00-00", 0},
704+
{"2020-08-05", 2020},
705+
}
706+
707+
for _, v := range Tests {
708+
output := Parse(v.input).Year()
709+
710+
if output != v.output {
711+
t.Fatalf("Input %s, expected %d, but got %d", v.input, v.output, output)
712+
}
713+
}
714+
}
715+
716+
func TestCarbon_Month(t *testing.T) {
717+
Tests := []struct {
718+
input string // 输入值
719+
output int // 期望输出值
720+
}{
721+
{"", 0},
722+
{"0000-00-00 00:00:00", 0},
723+
{"0000-00-00", 0},
724+
{"2020-08-05 13:14:15", 8},
725+
{"2020-08-05", 8},
726+
}
727+
728+
for _, v := range Tests {
729+
output := Parse(v.input).Month()
730+
731+
if output != v.output {
732+
t.Fatalf("Input %s, expected %d, but got %d", v.input, v.output, output)
733+
}
734+
}
735+
}
736+
737+
func TestCarbon_Day(t *testing.T) {
738+
Tests := []struct {
739+
input string // 输入值
740+
output int // 期望输出值
741+
}{
742+
{"", 0},
743+
{"0000-00-00 00:00:00", 0},
744+
{"0000-00-00", 0},
745+
{"2020-08-05 13:14:15", 5},
746+
{"2020-08-05", 5},
747+
}
748+
749+
for _, v := range Tests {
750+
output := Parse(v.input).Day()
751+
752+
if output != v.output {
753+
t.Fatalf("Input %s, expected %d, but got %d", v.input, v.output, output)
754+
}
755+
}
756+
}
757+
758+
func TestCarbon_Hour(t *testing.T) {
759+
Tests := []struct {
760+
input string // 输入值
761+
output int // 期望输出值
762+
}{
763+
{"", 0},
764+
{"0000-00-00 00:00:00", 0},
765+
{"0000-00-00", 0},
766+
{"2020-08-05 13:14:15", 13},
767+
{"2020-08-05", 0},
768+
}
769+
770+
for _, v := range Tests {
771+
output := Parse(v.input).Hour()
772+
773+
if output != v.output {
774+
t.Fatalf("Input %s, expected %d, but got %d", v.input, v.output, output)
775+
}
776+
}
777+
}
778+
779+
func TestCarbon_Minute(t *testing.T) {
780+
Tests := []struct {
781+
input string // 输入值
782+
output int // 期望输出值
783+
}{
784+
{"", 0},
785+
{"0000-00-00 00:00:00", 0},
786+
{"0000-00-00", 0},
787+
{"2020-08-05 13:14:15", 14},
788+
{"2020-08-05", 0},
789+
}
790+
791+
for _, v := range Tests {
792+
output := Parse(v.input).Minute()
793+
794+
if output != v.output {
795+
t.Fatalf("Input %s, expected %d, but got %d", v.input, v.output, output)
796+
}
797+
}
798+
}
799+
800+
func TestCarbon_Second(t *testing.T) {
801+
Tests := []struct {
802+
input string // 输入值
803+
output int // 期望输出值
804+
}{
805+
{"", 0},
806+
{"0000-00-00 00:00:00", 0},
807+
{"0000-00-00", 0},
808+
{"2020-08-05 13:14:15", 15},
809+
{"2020-08-05", 0},
810+
}
811+
812+
for _, v := range Tests {
813+
output := Parse(v.input).Second()
814+
815+
if output != v.output {
816+
t.Fatalf("Input %s, expected %d, but got %d", v.input, v.output, output)
817+
}
818+
}
819+
}
820+
679821
func TestCarbon_IsZero(t *testing.T) {
680822
Tests := []struct {
681823
input string // 输入值
682824
output bool // 期望输出值
683825
}{
684826
{"0000-00-00 00:00:00", true},
827+
{"0000-00-00", true},
685828
{"2020-08-05 00:00:00", false},
686829
{"2020-08-05", false},
687830
}

0 commit comments

Comments
 (0)