mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
v1.2.4新增时间比较系列方法
This commit is contained in:
parent
5e73299843
commit
75e852562e
335
README.en.md
335
README.en.md
@ -4,7 +4,7 @@ English | [Chinese](./README.md)
|
||||
#### Description
|
||||
A simple,semantic and developer-friendly golang package for datetime
|
||||
|
||||
If you feel good, please give me a star
|
||||
If you think it helpful, please give me a star
|
||||
|
||||
github:[github.com/golang-module/carbon](https://github.com/golang-module/carbon "github.com/golang-module/carbon")
|
||||
|
||||
@ -38,8 +38,15 @@ carbon.Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
carbon.Now().ToDateString() // 2020-08-05
|
||||
// Time of today
|
||||
carbon.Now().ToTimeString() // 13:14:15
|
||||
// Timestamp of today
|
||||
// Timestamp with second of today
|
||||
carbon.Now().ToTimestamp() // 1596604455
|
||||
carbon.Now().ToTimestampWithSecond() // 1596604455
|
||||
// Timestamp with millisecond of today
|
||||
carbon.Now().ToTimestampWithMillisecond() // 1596604455000
|
||||
// Timestamp with microsecond of today
|
||||
carbon.Now().ToTimestampWithMicrosecond() // 1596604455000000
|
||||
// Timestamp with nanosecond of today
|
||||
carbon.Now().ToTimestampWithNanosecond() // 1596604455000000000
|
||||
|
||||
// Datetime of yesterday
|
||||
carbon.Yesterday().ToDateTimeString() // 2020-08-04 13:14:15
|
||||
@ -47,8 +54,15 @@ carbon.Yesterday().ToDateTimeString() // 2020-08-04 13:14:15
|
||||
carbon.Yesterday().ToDateString() // 2020-08-04
|
||||
// Time of yesterday
|
||||
carbon.Yesterday().ToTimeString() // 13:14:15
|
||||
// Timestamp of yesterday
|
||||
// Timestamp with second of yesterday
|
||||
carbon.Yesterday().ToTimestamp() // 1596518055
|
||||
carbon.Yesterday().ToTimestampWithSecond() // 1596518055
|
||||
// Timestamp with millisecond of yesterday
|
||||
carbon.Yesterday().ToTimestampWithMillisecond() // 1596518055000
|
||||
// Timestamp with microsecond of yesterday
|
||||
carbon.Yesterday().ToTimestampWithMicrosecond() // 1596518055000000
|
||||
// Timestamp with nanosecond of yesterday
|
||||
carbon.Yesterday().ToTimestampWithNanosecond() // 1596518055000000000
|
||||
|
||||
// Datetime of tomorrow
|
||||
carbon.Tomorrow().ToDateTimeString() // 2020-08-06 13:14:15
|
||||
@ -56,8 +70,15 @@ carbon.Tomorrow().ToDateTimeString() // 2020-08-06 13:14:15
|
||||
carbon.Tomorrow().ToDateString() // 2020-08-06
|
||||
// Time of tomorrow
|
||||
carbon.Tomorrow().ToTimeString() // 13:14:15
|
||||
// Timestamp of tomorrow
|
||||
// Timestamp with second of tomorrow
|
||||
carbon.Tomorrow().ToTimestamp() // 1596690855
|
||||
carbon.Tomorrow().ToTimestampWithSecond() // 1596690855
|
||||
// Timestamp with millisecond of tomorrow
|
||||
carbon.Tomorrow().ToTimestampWithMillisecond() // 1596690855000
|
||||
// Timestamp with microsecond of tomorrow
|
||||
carbon.Tomorrow().ToTimestampWithMicrosecond() // 1596690855000000
|
||||
// Timestamp with nanosecond of tomorrow
|
||||
carbon.Tomorrow().ToTimestampWithNanosecond() // 1596690855000000000
|
||||
```
|
||||
|
||||
##### Create carbon instance
|
||||
@ -130,7 +151,6 @@ carbon.Parse("2019-08-05").SetYear(2020).ToDateString() // 2020-08-05
|
||||
carbon.Parse("2020-02-29").SetYear(2019).ToDateString() // 2019-03-01
|
||||
|
||||
// Set month
|
||||
carbon.Parse("2020-01-30").SetMonth(2).ToDateString() // 2020-03-01
|
||||
carbon.Parse("2020-01-31").SetMonth(2).ToDateString() // 2020-03-02
|
||||
carbon.Parse("2020-08-05").SetMonth(2).ToDateString() // 2020-02-05
|
||||
|
||||
@ -191,14 +211,17 @@ carbon.Parse("2020-08-05 13:14:15").EndOfMinute().ToDateTimeString() // 2020-08-
|
||||
carbon.Parse("2020-02-29 13:14:15").AddYears(3).ToDateTimeString() // 2023-03-01 13:14:15
|
||||
// Next three years
|
||||
carbon.Parse("2020-02-29 13:14:15").NextYears(3).ToDateTimeString() // 2023-02-28 13:14:15
|
||||
|
||||
// Add one year
|
||||
carbon.Parse("2020-02-29 13:14:15").AddYear().ToDateTimeString() // 2021-03-01 13:14:15
|
||||
// Next one year
|
||||
carbon.Parse("2020-02-29 13:14:15").NextYear().ToDateTimeString() // 2021-02-28 13:14:15
|
||||
|
||||
// Subtract three years
|
||||
carbon.Parse("2020-02-29 13:14:15").SubYears(3).ToDateTimeString() // 2017-03-01 13:14:15
|
||||
// Previous three years
|
||||
carbon.Parse("2020-02-29 13:14:15").PreYears(3).ToDateTimeString() // 2017-02-28 13:14:15
|
||||
|
||||
// Subtract one year
|
||||
carbon.Parse("2020-02-29 13:14:15").SubYear().ToDateTimeString() // 2019-03-01 13:14:15
|
||||
// Previous one year
|
||||
@ -228,14 +251,17 @@ carbon.Parse("2020-05-31 13:14:15").PreQuarter().ToDateTimeString() // 2020-02-2
|
||||
carbon.Parse("2020-02-29 13:14:15").AddMonths(3).ToDateTimeString() // 2020-05-29 13:14:15
|
||||
// Next three months
|
||||
carbon.Parse("2020-02-29 13:14:15").NextMonths(3).ToDateTimeString() // 2020-05-29 13:14:15
|
||||
|
||||
// Add one month
|
||||
carbon.Parse("2020-01-31 13:14:15").AddMonth().ToDateTimeString() // 2020-03-02 13:14:15
|
||||
// Next one month
|
||||
carbon.Parse("2020-01-31 13:14:15").NextMonth().ToDateTimeString() // 2020-02-29 13:14:15
|
||||
|
||||
// Subtract three months
|
||||
carbon.Parse("2020-02-29 13:14:15").SubMonths(3).ToDateTimeString() // 2019-11-29 13:14:15
|
||||
// Previous three months
|
||||
carbon.Parse("2020-02-29 13:14:15").PreMonths(3).ToDateTimeString() // 2019-11-29 13:14:15
|
||||
|
||||
// Subtract one month
|
||||
carbon.Parse("2020-03-31 13:14:15").SubMonth().ToDateTimeString() // 2020-03-02 13:14:15
|
||||
// Previous one month
|
||||
@ -255,6 +281,7 @@ carbon.Parse("2020-02-29 13:14:15").SubWeek().ToDateTimeString() // 2020-02-22 1
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDays(3).ToDateTimeString() // 2020-08-08 13:14:15
|
||||
// Add one day
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDay().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
|
||||
// Subtract three days
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDays(3).ToDateTimeString() // 2020-08-02 13:14:15
|
||||
// Subtract one day
|
||||
@ -266,6 +293,7 @@ carbon.Parse("2020-08-05 13:14:15").AddHours(3).ToDateTimeString() // 2020-08-05
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5h").ToDateTimeString() // 2020-08-05 15:44:15
|
||||
// Add one hour
|
||||
carbon.Parse("2020-08-05 13:14:15").AddHour().ToDateTimeString() // 2020-08-05 14:14:15
|
||||
|
||||
// Subtract three hours
|
||||
carbon.Parse("2020-08-05 13:14:15").SubHours(3).ToDateTimeString() // 2020-08-05 10:14:15
|
||||
// Subtract two and a half hours
|
||||
@ -279,6 +307,7 @@ carbon.Parse("2020-08-05 13:14:15").AddMinutes(3).ToDateTimeString() // 2020-08-
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5m").ToDateTimeString() // 2020-08-05 13:16:45
|
||||
// Add one minute
|
||||
carbon.Parse("2020-08-05 13:14:15").AddMinute().ToDateTimeString() // 2020-08-05 13:15:15
|
||||
|
||||
// Subtract three minutes
|
||||
carbon.Parse("2020-08-05 13:14:15").SubMinutes(3).ToDateTimeString() // 2020-08-05 13:11:15
|
||||
// Subtract two and a half minutes
|
||||
@ -292,6 +321,7 @@ carbon.Parse("2020-08-05 13:14:15").AddSeconds(3).ToDateTimeString() // 2020-08-
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDuration("2.5s").ToDateTimeString() // 2020-08-05 13:14:17
|
||||
// Add one second
|
||||
carbon.Parse("2020-08-05 13:14:15").AddSecond().ToDateTimeString() // 2020-08-05 13:14:16
|
||||
|
||||
// Subtract three seconds
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSeconds(3).ToDateTimeString() // 2020-08-05 13:14:12
|
||||
// Subtract two and a half seconds
|
||||
@ -300,32 +330,169 @@ carbon.Parse("2020-08-05 13:14:15").SubDuration("2.5s").ToDateTimeString() // 20
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05 13:14:14
|
||||
```
|
||||
|
||||
##### Time difference
|
||||
##### Difference in time
|
||||
```go
|
||||
// Difference in weeks
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInWeeks(carbon.Parse("2020-07-28 13:14:15")) // -1
|
||||
// Difference absolute in weeks
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInWeeks(carbon.Parse("2020-07-28 13:14:15")) // 1
|
||||
// Difference in weeks with absolute value
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInWeeksWithAbs(carbon.Parse("2020-07-28 13:14:15")) // 1
|
||||
|
||||
// Difference in days
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInDays(carbon.Parse("2020-08-04 13:14:15")) // -1
|
||||
// Difference absolute in days
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInDays(carbon.Parse("2020-08-04 13:14:15")) // 1
|
||||
// Difference in days with absolute value
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInDaysWithAbs(carbon.Parse("2020-08-04 13:14:15")) // 1
|
||||
|
||||
// Difference in hours
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInHours(carbon.Parse("2020-08-05 12:14:15")) // -1
|
||||
// Difference absolute in hours
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInHours(carbon.Parse("2020-08-05 12:14:15")) // 1
|
||||
// Difference in hours with absolute value
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInHoursWithAbs(carbon.Parse("2020-08-05 12:14:15")) // 1
|
||||
|
||||
// Difference in minutes
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInMinutes(carbon.Parse("2020-08-05 13:13:15")) // -1
|
||||
// Difference absolute in minutes
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInMinutes(carbon.Parse("2020-08-05 13:13:15")) // 1
|
||||
// Difference in minutes with absolute value
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInMinutesWithAbs(carbon.Parse("2020-08-05 13:13:15")) // 1
|
||||
|
||||
// Difference in seconds
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInSeconds(carbon.Parse("2020-08-05 13:14:14")) // -1
|
||||
// Difference absolute in seconds
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInSeconds(carbon.Parse("2020-08-05 13:14:14")) // 1
|
||||
// Difference in seconds with absolute value
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInSecondsWithAbs(carbon.Parse("2020-08-05 13:14:14")) // 1
|
||||
```
|
||||
|
||||
##### Time compare
|
||||
```go
|
||||
// greater than
|
||||
carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">", carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">", carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
|
||||
// less than
|
||||
carbon.Parse("2020-08-05 13:14:15").Lt(carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Lt(carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<", carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<", carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
|
||||
// equal
|
||||
carbon.Parse("2020-08-05 13:14:15").Eq(carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Eq(carbon.Parse("2020-08-05 13:14:00")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("=", carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("=", carbon.Parse("2020-08-05 13:14:00")) // false
|
||||
|
||||
// not equal
|
||||
carbon.Parse("2020-08-05 13:14:15").Ne(carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Ne(carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("!=", carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<>", carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
|
||||
// greater than or equal
|
||||
carbon.Parse("2020-08-05 13:14:15").Gte(carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Gte(carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">=", carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">=", carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
|
||||
// less than or equal
|
||||
carbon.Parse("2020-08-05 13:14:15").Lte(carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Lte(carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<=", carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<=", carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
|
||||
// between
|
||||
carbon.Parse("2020-08-05 13:14:15").Between(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Between(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
|
||||
// Between included start time
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedStartTime(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedStartTime(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
|
||||
// Between included end time
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedEndTime(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedEndTime(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
|
||||
// Between included both
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedBoth(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedBoth(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
|
||||
```
|
||||
|
||||
##### Time judgment
|
||||
```go
|
||||
// Is zero time
|
||||
carbon.Parse("").IsZero() // true
|
||||
carbon.Parse("0").IsZero() // true
|
||||
carbon.Parse("0000-00-00 00:00:00").IsZero() // true
|
||||
carbon.Parse("0000-00-00").IsZero() // true
|
||||
carbon.Parse("00:00:00").IsZero() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsZero() // false
|
||||
carbon.Parse("2020-08-05").IsZero() // false
|
||||
|
||||
// Is now time
|
||||
carbon.Now().IsNow() // true
|
||||
// Is future time
|
||||
carbon.Tomorrow().IsFuture() // true
|
||||
// Is pass time
|
||||
carbon.Yesterday().IsPast() // true
|
||||
|
||||
// Is leap year
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLeapYear() // true
|
||||
// Is long year
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLongYear() // true
|
||||
|
||||
// Is january
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJanuary() // false
|
||||
// Is february
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFebruary() // false
|
||||
// Is march
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMarch() // false
|
||||
// Is april
|
||||
carbon.Parse("2020-08-05 13:14:15").IsApril() // false
|
||||
// Is may
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMay() // false
|
||||
// Is june
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJune() // false
|
||||
// Is july
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJuly() // false
|
||||
// Is august
|
||||
carbon.Parse("2020-08-05 13:14:15").IsAugust() // false
|
||||
// Is september
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSeptember() // true
|
||||
// Is october
|
||||
carbon.Parse("2020-08-05 13:14:15").IsOctober() // false
|
||||
// Is november
|
||||
carbon.Parse("2020-08-05 13:14:15").IsNovember() // false
|
||||
// Is december
|
||||
carbon.Parse("2020-08-05 13:14:15").IsDecember() // false
|
||||
|
||||
// Is monday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMonday() // false
|
||||
// Is tuesday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsTuesday() // true
|
||||
// Is wednesday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWednesday() // false
|
||||
// Is thursday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsThursday() // false
|
||||
// Is friday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFriday() // false
|
||||
// Is saturday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSaturday() // false
|
||||
// Is sunday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSunday() // false
|
||||
// Is weekday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekday() // false
|
||||
// Is weekend
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekend() // true
|
||||
|
||||
// Is yesterday
|
||||
carbon.Parse("2020-08-04 13:14:15").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04 00:00:00").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04").IsYesterday() // true
|
||||
// Is today
|
||||
carbon.Parse("2020-08-05 13:14:15").IsToday() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsToday() // true
|
||||
carbon.Parse("2020-08-05").IsToday() // true
|
||||
// Is tomorrow
|
||||
carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06").IsTomorrow() // true
|
||||
```
|
||||
|
||||
##### Time output
|
||||
@ -435,87 +602,6 @@ carbon.Parse("2002-01-01 13:14:15").Age() // 17
|
||||
carbon.Parse("2002-12-31 13:14:15").Age() // 18
|
||||
```
|
||||
|
||||
##### Time judgment
|
||||
```go
|
||||
// Is zero time
|
||||
carbon.Parse("").IsZero() // true
|
||||
carbon.Parse("0").IsZero() // true
|
||||
carbon.Parse("0000-00-00 00:00:00").IsZero() // true
|
||||
carbon.Parse("0000-00-00").IsZero() // true
|
||||
carbon.Parse("00:00:00").IsZero() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsZero() // false
|
||||
carbon.Parse("2020-08-05").IsZero() // false
|
||||
|
||||
// Is now time
|
||||
carbon.Parse(carbon.Now().ToDateTimeString()).IsNow() // true
|
||||
// Is future time
|
||||
carbon.Parse("2020-08-06 13:14:15").IsFuture() // true
|
||||
// Is pass time
|
||||
carbon.Parse("2020-08-04 13:14:15").IsPast() // true
|
||||
|
||||
// Is leap year
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLeapYear() // true
|
||||
// Is long year
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLongYear() // true
|
||||
|
||||
// Is january
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJanuary() // false
|
||||
// Is february
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFebruary() // false
|
||||
// Is march
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMarch() // false
|
||||
// Is april
|
||||
carbon.Parse("2020-08-05 13:14:15").IsApril() // false
|
||||
// Is may
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMay() // false
|
||||
// Is june
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJune() // false
|
||||
// Is july
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJuly() // false
|
||||
// Is august
|
||||
carbon.Parse("2020-08-05 13:14:15").IsAugust() // false
|
||||
// Is september
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSeptember() // true
|
||||
// Is october
|
||||
carbon.Parse("2020-08-05 13:14:15").IsOctober() // false
|
||||
// Is november
|
||||
carbon.Parse("2020-08-05 13:14:15").IsNovember() // false
|
||||
// Is december
|
||||
carbon.Parse("2020-08-05 13:14:15").IsDecember() // false
|
||||
|
||||
// Is monday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMonday() // false
|
||||
// Is tuesday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsTuesday() // true
|
||||
// Is wednesday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWednesday() // false
|
||||
// Is thursday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsThursday() // false
|
||||
// Is friday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFriday() // false
|
||||
// Is saturday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSaturday() // false
|
||||
// Is sunday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSunday() // false
|
||||
// Is weekday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekday() // false
|
||||
// Is weekend
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekend() // true
|
||||
|
||||
// Is yesterday
|
||||
carbon.Parse("2020-08-04 13:14:15").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04 00:00:00").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04").IsYesterday() // true
|
||||
// Is today
|
||||
carbon.Parse("2020-08-05 13:14:15").IsToday() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsToday() // true
|
||||
carbon.Parse("2020-08-05").IsToday() // true
|
||||
// Is tomorrow
|
||||
carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06").IsTomorrow() // true
|
||||
```
|
||||
|
||||
##### Calendar
|
||||
```go
|
||||
// To year of the animal
|
||||
@ -550,7 +636,7 @@ carbon.Parse("2020-08-05 13:14:15").IsYearOfPig() // false
|
||||
```
|
||||
|
||||
##### Database
|
||||
Assuming the database table is users, its fields have id(int), name(varchar), age(int), graduated_at(date), birthday(date), created_at(datetime), updated_at(datetime), deleted_at(datetime)
|
||||
Assuming the database table is users, its fields have id(int), name(varchar), age(int), birthday(datetime), graduated_at(datetime), created_at(datetime), updated_at(datetime), date_time1(datetime), date_time2(datetime), date_time3(datetime), date_time4(datetime)
|
||||
|
||||
###### Define model
|
||||
```go
|
||||
@ -558,11 +644,14 @@ type UserModel struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Birthday carbon.Carbon `json:"birthday"`
|
||||
CreatedAt carbon.ToDateTimeString `json:"created_at"`
|
||||
DeletedAt carbon.ToTimestamp `json:"deleted_at"`
|
||||
Birthday carbon.ToDateTimeString `json:"birthday"`
|
||||
GraduatedAt carbon.ToDateString `json:"graduated_at"`
|
||||
UpdatedAt carbon.ToTimeString `json:"updated_at"`
|
||||
CreatedAt carbon.ToTimeString `json:"created_at"`
|
||||
UpdatedAt carbon.ToTimestamp `json:"updated_at"`
|
||||
DateTime1 carbon.ToTimestampWithSecond `json:"date_time1"`
|
||||
DateTime2 carbon.ToTimestampWithMillisecond `json:"date_time2"`
|
||||
DateTime3 carbon.ToTimestampWithMicrosecond `json:"date_time3"`
|
||||
DateTime4 carbon.ToTimestampWithNanosecond `json:"date_time4"`
|
||||
}
|
||||
```
|
||||
|
||||
@ -571,23 +660,30 @@ type UserModel struct {
|
||||
user := UserModel {
|
||||
Name: "勾国印",
|
||||
Age: 18,
|
||||
Birthday: carbon.Now().SubYears(18),
|
||||
CreatedAt: carbon.ToDateTimeString{carbon.Now()},
|
||||
DeletedAt: carbon.ToTimestamp{carbon.Parse("2020-08-05 13:14:15")},
|
||||
Birthday: carbon.ToDateTimeString{carbon.Now().SubYears(18)},
|
||||
GraduatedAt: carbon.ToDateString{carbon.Parse("2012-09-09")},
|
||||
UpdatedAt: carbon.ToTimeString{carbon.Now()},
|
||||
CreatedAt: carbon.ToTimeString{carbon.Now()},
|
||||
UpdatedAt: carbon.ToTimestamp{carbon.Now()},
|
||||
DateTime1: carbon.ToTimestampWithSecond{carbon.Now()},
|
||||
DateTime2: carbon.ToTimestampWithMillisecond{carbon.Now()},
|
||||
DateTime3: carbon.ToTimestampWithMicrosecond{carbon.Now()},
|
||||
DateTime4: carbon.ToTimestampWithNanosecond{carbon.Now()},
|
||||
}
|
||||
```
|
||||
|
||||
###### Output fields
|
||||
```go
|
||||
user.ID // 18
|
||||
user.ID // 42
|
||||
user.Name // 勾国印
|
||||
user.Birthday.ToDateString() // 2012-08-05
|
||||
user.CreatedAt.ToTimestamp() // 1596604455
|
||||
user.DeletedAt.ToDateTimeString() // 2012-08-05 13:14:15
|
||||
user.GraduatedAt.AddDay().ToDateString() // 2012-09-10
|
||||
user.UpdatedAt.ToDateString() // 2012-08-05
|
||||
user.Age // 18
|
||||
user.Birthday.ToDateTimeString() // 2012-08-05 13:14:15
|
||||
user.GraduatedAt.ToDateString() // 2012-09-09
|
||||
user.CreatedAt.ToTimeString() // 13:14:15
|
||||
user.UpdatedAt.ToTimestamp() // 1596604455
|
||||
user.DateTime1.ToTimestampWithSecond() // 1596604455
|
||||
user.DateTime2.ToTimestampWithMillisecond() // 1596604455000
|
||||
user.DateTime3.ToTimestampWithMicrosecond() // 1596604455000000
|
||||
user.DateTime4.ToTimestampWithNanosecond() // 1596604455000000000
|
||||
```
|
||||
|
||||
###### Output model by json
|
||||
@ -599,11 +695,14 @@ fmt.Print(string(data))
|
||||
"id": 42,
|
||||
"name": "勾国印",
|
||||
"age": 18,
|
||||
"birthday": "2012-08-05 00:00:00",
|
||||
"created_at": "2020-08-05 13:14:15",
|
||||
"deleted_at": 1596604455
|
||||
"birthday": "2012-08-05 13:14:15",
|
||||
"graduated_at": "2012-09-09",
|
||||
"updated_at": "13:14:15",
|
||||
"created_at": "13:14:15",
|
||||
"updated_at": 1596604455,
|
||||
"date_time1": 1596604455,
|
||||
"date_time2": 1596604455000,
|
||||
"date_time3": 1596604455000000,
|
||||
"date_time4": 1596604455000000000,
|
||||
}
|
||||
```
|
||||
|
||||
@ -616,12 +715,12 @@ type ToRssString struct {
|
||||
|
||||
// Define model
|
||||
type UserModel struct {
|
||||
Birthday ToRssString `json:"birthday"`
|
||||
Birthday carbon.ToRssString `json:"birthday"`
|
||||
}
|
||||
|
||||
// Instantiate model
|
||||
user := UserModel {
|
||||
Birthday: ToRssString{carbon.Now()},
|
||||
Birthday: carbon.ToRssString{carbon.Now()},
|
||||
}
|
||||
|
||||
// Overload MarshalJSON method
|
||||
|
344
README.md
344
README.md
@ -31,32 +31,53 @@ import (
|
||||
|
||||
##### 昨天、今天、明天
|
||||
```go
|
||||
// 今天
|
||||
// 今天此刻
|
||||
carbon.Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
// 今天日期
|
||||
carbon.Now().ToDateString() // 2020-08-05
|
||||
// 今天时间
|
||||
carbon.Now().ToTimeString() // 13:14:15
|
||||
// 今天时间戳
|
||||
// 今天秒级时间戳
|
||||
carbon.Now().ToTimestamp() // 1596604455
|
||||
carbon.Now().ToTimestampWithSecond() // 1596604455
|
||||
// 今天毫秒级时间戳
|
||||
carbon.Now().ToTimestampWithMillisecond() // 1596604455000
|
||||
// 今天微秒级时间戳
|
||||
carbon.Now().ToTimestampWithMicrosecond() // 1596604455000000
|
||||
// 今天纳秒级时间戳
|
||||
carbon.Now().ToTimestampWithNanosecond() // 1596604455000000000
|
||||
|
||||
// 昨天
|
||||
// 昨天此刻
|
||||
carbon.Yesterday().ToDateTimeString() // 2020-08-04 13:14:15
|
||||
// 昨天日期
|
||||
carbon.Yesterday().ToDateString() // 2020-08-04
|
||||
// 昨天时间
|
||||
carbon.Yesterday().ToTimeString() // 13:14:15
|
||||
// 昨天时间戳
|
||||
// 昨天秒级时间戳
|
||||
carbon.Yesterday().ToTimestamp() // 1596518055
|
||||
carbon.Yesterday().ToTimestampWithSecond() // 1596518055
|
||||
// 明天毫秒级时间戳
|
||||
carbon.Yesterday().ToTimestampWithMillisecond() // 1596518055000
|
||||
// 明天微秒级时间戳
|
||||
carbon.Yesterday().ToTimestampWithMicrosecond() // 1596518055000000
|
||||
// 明天纳秒级时间戳
|
||||
carbon.Yesterday().ToTimestampWithNanosecond() // 1596518055000000000
|
||||
|
||||
// 明天
|
||||
// 明天此刻
|
||||
carbon.Tomorrow().ToDateTimeString() // 2020-08-06 13:14:15
|
||||
// 明天日期
|
||||
carbon.Tomorrow().ToDateString() // 2020-08-06
|
||||
// 明天时间
|
||||
carbon.Tomorrow().ToTimeString() // 13:14:15
|
||||
// 明天时间戳
|
||||
// 明天秒级时间戳
|
||||
carbon.Tomorrow().ToTimestamp() // 1596690855
|
||||
carbon.Tomorrow().ToTimestampWithSecond() // 1596690855
|
||||
// 明天毫秒级时间戳
|
||||
carbon.Tomorrow().ToTimestampWithMillisecond() // 1596690855000
|
||||
// 明天微秒级时间戳
|
||||
carbon.Tomorrow().ToTimestampWithMicrosecond() // 1596690855000000
|
||||
// 明天纳秒级时间戳
|
||||
carbon.Tomorrow().ToTimestampWithNanosecond() // 1596690855000000000
|
||||
```
|
||||
|
||||
##### 创建Carbon实例
|
||||
@ -132,7 +153,6 @@ carbon.Parse("2019-08-05").SetYear(2020).ToDateString() // 2020-08-05
|
||||
carbon.Parse("2020-02-29").SetYear(2019).ToDateString() // 2019-03-01
|
||||
|
||||
// 设置月
|
||||
carbon.Parse("2020-01-30").SetMonth(2).ToDateString() // 2020-03-01
|
||||
carbon.Parse("2020-01-31").SetMonth(2).ToDateString() // 2020-03-02
|
||||
carbon.Parse("2020-08-05").SetMonth(2).ToDateString() // 2020-02-05
|
||||
|
||||
@ -305,27 +325,165 @@ carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05
|
||||
// 相差多少周
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInWeeks(carbon.Parse("2020-07-28 13:14:15")) // -1
|
||||
// 相差多少周(绝对值)
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInWeeks(carbon.Parse("2020-07-28 13:14:15")) // 1
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInWeeksWithAbs(carbon.Parse("2020-07-28 13:14:15")) // 1
|
||||
|
||||
// 相差多少天
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInDays(carbon.Parse("2020-08-04 13:14:15")) // -1
|
||||
// 相差多少天(绝对值)
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInDays(carbon.Parse("2020-08-04 13:14:15")) // 1
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInDaysWithAbs(carbon.Parse("2020-08-04 13:14:15")) // 1
|
||||
|
||||
// 相差多少小时
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInHours(carbon.Parse("2020-08-05 12:14:15")) // -1
|
||||
// 相差多少小时(绝对值)
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInHours(carbon.Parse("2020-08-05 12:14:15")) // 1
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInHoursWithAbs(carbon.Parse("2020-08-05 12:14:15")) // 1
|
||||
|
||||
// 相差多少分
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInMinutes(carbon.Parse("2020-08-05 13:13:15")) // -1
|
||||
// 相差多少分(绝对值)
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInMinutes(carbon.Parse("2020-08-05 13:13:15")) // 1
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInMinutesWithAbs(carbon.Parse("2020-08-05 13:13:15")) // 1
|
||||
|
||||
// 相差多少秒
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInSeconds(carbon.Parse("2020-08-05 13:14:14")) // -1
|
||||
// 相差多少秒(绝对值)
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffAbsInSeconds(carbon.Parse("2020-08-05 13:14:14")) // 1
|
||||
carbon.Parse("2020-08-05 13:14:15").DiffInSecondsWithAbs(carbon.Parse("2020-08-05 13:14:14")) // 1
|
||||
```
|
||||
|
||||
##### 时间比较
|
||||
```go
|
||||
// 是否大于
|
||||
carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Gt(carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">", carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">", carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
|
||||
// 是否小于
|
||||
carbon.Parse("2020-08-05 13:14:15").Lt(carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Lt(carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<", carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<", carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
|
||||
// 是否等于
|
||||
carbon.Parse("2020-08-05 13:14:15").Eq(carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Eq(carbon.Parse("2020-08-05 13:14:00")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("=", carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("=", carbon.Parse("2020-08-05 13:14:00")) // false
|
||||
|
||||
// 是否不等于
|
||||
carbon.Parse("2020-08-05 13:14:15").Ne(carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Ne(carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("!=", carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<>", carbon.Parse("2020-08-05 13:14:15")) // false
|
||||
|
||||
// 是否大于等于
|
||||
carbon.Parse("2020-08-05 13:14:15").Gte(carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Gte(carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">=", carbon.Parse("2020-08-04 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare(">=", carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
|
||||
// 是否小于等于
|
||||
carbon.Parse("2020-08-05 13:14:15").Lte(carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Lte(carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<=", carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").Compare("<=", carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
|
||||
// 是否在两个时间之间(不包括这两个时间)
|
||||
carbon.Parse("2020-08-05 13:14:15").Between(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // false
|
||||
carbon.Parse("2020-08-05 13:14:15").Between(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
|
||||
// 是否在两个时间之间(包括开始时间)
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedStartTime(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedStartTime(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
|
||||
// 是否在两个时间之间(包括结束时间)
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedEndTime(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedEndTime(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
|
||||
// 是否在两个时间之间(包括这两个时间)
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedBoth(carbon.Parse("2020-08-05 13:14:15"), carbon.Parse("2020-08-06 13:14:15")) // true
|
||||
carbon.Parse("2020-08-05 13:14:15").BetweenIncludedBoth(carbon.Parse("2020-08-04 13:14:15"), carbon.Parse("2020-08-05 13:14:15")) // true
|
||||
|
||||
```
|
||||
|
||||
##### 时间判断
|
||||
```go
|
||||
// 是否是零值时间
|
||||
carbon.Parse("").IsZero() // true
|
||||
carbon.Parse("0").IsZero() // true
|
||||
carbon.Parse("0000-00-00 00:00:00").IsZero() // true
|
||||
carbon.Parse("0000-00-00").IsZero() // true
|
||||
carbon.Parse("00:00:00").IsZero() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsZero() // false
|
||||
carbon.Parse("2020-08-05").IsZero() // false
|
||||
|
||||
// 是否是当前时间
|
||||
carbon.Now().IsNow() // true
|
||||
// 是否是未来时间
|
||||
carbon.Tomorrow().IsFuture() // true
|
||||
// 是否是过去时间
|
||||
carbon.Yesterday().IsPast() // true
|
||||
|
||||
// 是否是闰年
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLeapYear() // true
|
||||
// 是否是长年
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLongYear() // true
|
||||
|
||||
// 是否是一月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJanuary() // false
|
||||
// 是否是二月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFebruary() // false
|
||||
// 是否是三月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMarch() // false
|
||||
// 是否是四月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsApril() // false
|
||||
// 是否是五月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMay() // false
|
||||
// 是否是六月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJune() // false
|
||||
// 是否是七月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJuly() // false
|
||||
// 是否是八月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsAugust() // false
|
||||
// 是否是九月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSeptember() // true
|
||||
// 是否是十月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsOctober() // false
|
||||
// 是否是十一月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsNovember() // false
|
||||
// 是否是十二月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsDecember() // false
|
||||
|
||||
// 是否是周一
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMonday() // false
|
||||
// 是否是周二
|
||||
carbon.Parse("2020-08-05 13:14:15").IsTuesday() // true
|
||||
// 是否是周三
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWednesday() // false
|
||||
// 是否是周四
|
||||
carbon.Parse("2020-08-05 13:14:15").IsThursday() // false
|
||||
// 是否是周五
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFriday() // false
|
||||
// 是否是周六
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSaturday() // false
|
||||
// 是否是周日
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSunday() // false
|
||||
|
||||
// 是否是工作日
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekday() // false
|
||||
// 是否是周末
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekend() // true
|
||||
|
||||
// 是否是昨天
|
||||
carbon.Parse("2020-08-04 13:14:15").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04 00:00:00").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04").IsYesterday() // true
|
||||
// 是否是今天
|
||||
carbon.Parse("2020-08-05 13:14:15").IsToday() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsToday() // true
|
||||
carbon.Parse("2020-08-05").IsToday() // true
|
||||
// 是否是明天
|
||||
carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06").IsTomorrow() // true
|
||||
```
|
||||
|
||||
##### 时间输出
|
||||
@ -438,88 +596,6 @@ carbon.Parse("2002-12-31 13:14:15").Age() // 18
|
||||
|
||||
```
|
||||
|
||||
##### 时间判断
|
||||
```go
|
||||
// 是否是零值时间
|
||||
carbon.Parse("").IsZero() // true
|
||||
carbon.Parse("0").IsZero() // true
|
||||
carbon.Parse("0000-00-00 00:00:00").IsZero() // true
|
||||
carbon.Parse("0000-00-00").IsZero() // true
|
||||
carbon.Parse("00:00:00").IsZero() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsZero() // false
|
||||
carbon.Parse("2020-08-05").IsZero() // false
|
||||
|
||||
// 是否是当前时间
|
||||
carbon.Parse(carbon.Now().ToDateTimeString()).IsNow() // true
|
||||
// 是否是未来时间
|
||||
carbon.Parse("2020-08-06 13:14:15").IsFuture() // true
|
||||
// 是否是过去时间
|
||||
carbon.Parse("2020-08-04 13:14:15").IsPast() // true
|
||||
|
||||
// 是否是闰年
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLeapYear() // true
|
||||
// 是否是长年
|
||||
carbon.Parse("2020-08-05 13:14:15").IsLongYear() // true
|
||||
|
||||
// 是否是一月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJanuary() // false
|
||||
// 是否是二月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFebruary() // false
|
||||
// 是否是三月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMarch() // false
|
||||
// 是否是四月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsApril() // false
|
||||
// 是否是五月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMay() // false
|
||||
// 是否是六月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJune() // false
|
||||
// 是否是七月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJuly() // false
|
||||
// 是否是八月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsAugust() // false
|
||||
// 是否是九月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSeptember() // true
|
||||
// 是否是十月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsOctober() // false
|
||||
// 是否是十一月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsNovember() // false
|
||||
// 是否是十二月
|
||||
carbon.Parse("2020-08-05 13:14:15").IsDecember() // false
|
||||
|
||||
// 是否是周一
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMonday() // false
|
||||
// 是否是周二
|
||||
carbon.Parse("2020-08-05 13:14:15").IsTuesday() // true
|
||||
// 是否是周三
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWednesday() // false
|
||||
// 是否是周四
|
||||
carbon.Parse("2020-08-05 13:14:15").IsThursday() // false
|
||||
// 是否是周五
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFriday() // false
|
||||
// 是否是周六
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSaturday() // false
|
||||
// 是否是周日
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSunday() // false
|
||||
|
||||
// 是否是工作日
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekday() // false
|
||||
// 是否是周末
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekend() // true
|
||||
|
||||
// 是否是昨天
|
||||
carbon.Parse("2020-08-04 13:14:15").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04 00:00:00").IsYesterday() // true
|
||||
carbon.Parse("2020-08-04").IsYesterday() // true
|
||||
// 是否是今天
|
||||
carbon.Parse("2020-08-05 13:14:15").IsToday() // true
|
||||
carbon.Parse("2020-08-05 00:00:00").IsToday() // true
|
||||
carbon.Parse("2020-08-05").IsToday() // true
|
||||
// 是否是明天
|
||||
carbon.Parse("2020-08-06 13:14:15").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06 00:00:00").IsTomorrow() // true
|
||||
carbon.Parse("2020-08-06").IsTomorrow() // true
|
||||
```
|
||||
|
||||
##### 农历支持
|
||||
```go
|
||||
// 获取生肖年
|
||||
@ -554,7 +630,7 @@ carbon.Parse("2020-08-05 13:14:15").IsYearOfPig() // false
|
||||
```
|
||||
|
||||
##### 数据库支持
|
||||
假设数据表为users,字段有id(int)、name(varchar)、age(int)、graduated_at(date)、birthday(date)、created_at(datetime)、updated_at(datetime)、deleted_at(datetime)
|
||||
假设数据表为users,字段有id(int)、name(varchar)、age(int)、birthday(datetime)、graduated_at(datetime)、created_at(datetime)、updated_at(datetime)、date_time1(datetime)、date_time2(datetime)、date_time3(datetime)、date_time4(datetime)
|
||||
|
||||
###### 定义模型
|
||||
```go
|
||||
@ -562,11 +638,14 @@ type UserModel struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Birthday carbon.Carbon `json:"birthday"`
|
||||
CreatedAt carbon.ToDateTimeString `json:"created_at"`
|
||||
DeletedAt carbon.ToTimestamp `json:"deleted_at"`
|
||||
Birthday carbon.ToDateTimeString `json:"birthday"`
|
||||
GraduatedAt carbon.ToDateString `json:"graduated_at"`
|
||||
UpdatedAt carbon.ToTimeString `json:"updated_at"`
|
||||
CreatedAt carbon.ToTimeString `json:"created_at"`
|
||||
UpdatedAt carbon.ToTimestamp `json:"updated_at"`
|
||||
DateTime1 carbon.ToTimestampWithSecond `json:"date_time1"`
|
||||
DateTime2 carbon.ToTimestampWithMillisecond `json:"date_time2"`
|
||||
DateTime3 carbon.ToTimestampWithMicrosecond `json:"date_time3"`
|
||||
DateTime4 carbon.ToTimestampWithNanosecond `json:"date_time4"`
|
||||
}
|
||||
```
|
||||
|
||||
@ -575,23 +654,30 @@ type UserModel struct {
|
||||
user := UserModel {
|
||||
Name: "勾国印",
|
||||
Age: 18,
|
||||
Birthday: carbon.Now().SubYears(18),
|
||||
CreatedAt: carbon.ToDateTimeString{carbon.Now()},
|
||||
DeletedAt: carbon.ToTimestamp{carbon.Parse("2020-08-05 13:14:15")},
|
||||
Birthday: carbon.ToDateTimeString{carbon.Now().SubYears(18)},
|
||||
GraduatedAt: carbon.ToDateString{carbon.Parse("2012-09-09")},
|
||||
UpdatedAt: carbon.ToTimeString{carbon.Now()},
|
||||
CreatedAt: carbon.ToTimeString{carbon.Now()},
|
||||
UpdatedAt: carbon.ToTimestamp{carbon.Now()},
|
||||
DateTime1: carbon.ToTimestampWithSecond{carbon.Now()},
|
||||
DateTime2: carbon.ToTimestampWithMillisecond{carbon.Now()},
|
||||
DateTime3: carbon.ToTimestampWithMicrosecond{carbon.Now()},
|
||||
DateTime4: carbon.ToTimestampWithNanosecond{carbon.Now()},
|
||||
}
|
||||
```
|
||||
|
||||
###### 输出模型字段
|
||||
```go
|
||||
user.ID // 18
|
||||
user.ID // 42
|
||||
user.Name // 勾国印
|
||||
user.Birthday.ToDateString() // 2012-08-05
|
||||
user.CreatedAt.ToTimestamp() // 1596604455
|
||||
user.DeletedAt.ToDateTimeString() // 2012-08-05 13:14:15
|
||||
user.GraduatedAt.AddDay().ToDateString() // 2012-09-10
|
||||
user.UpdatedAt.ToDateString() // 2012-08-05
|
||||
user.Age // 18
|
||||
user.Birthday.ToDateTimeString() // 2012-08-05 13:14:15
|
||||
user.GraduatedAt.ToDateString() // 2012-09-09
|
||||
user.CreatedAt.ToTimeString() // 13:14:15
|
||||
user.UpdatedAt.ToTimestamp() // 1596604455
|
||||
user.DateTime1.ToTimestampWithSecond() // 1596604455
|
||||
user.DateTime2.ToTimestampWithMillisecond() // 1596604455000
|
||||
user.DateTime3.ToTimestampWithMicrosecond() // 1596604455000000
|
||||
user.DateTime4.ToTimestampWithNanosecond() // 1596604455000000000
|
||||
```
|
||||
|
||||
###### JSON输出模型
|
||||
@ -603,11 +689,14 @@ fmt.Print(string(data))
|
||||
"id": 42,
|
||||
"name": "勾国印",
|
||||
"age": 18,
|
||||
"birthday": "2012-08-05 00:00:00",
|
||||
"created_at": "2020-08-05 13:14:15",
|
||||
"deleted_at": 1596604455
|
||||
"birthday": "2012-08-05 13:14:15",
|
||||
"graduated_at": "2012-09-09",
|
||||
"updated_at": "13:14:15",
|
||||
"created_at": "13:14:15",
|
||||
"updated_at": 1596604455,
|
||||
"date_time1": 1596604455,
|
||||
"date_time2": 1596604455000,
|
||||
"date_time3": 1596604455000000,
|
||||
"date_time4": 1596604455000000000,
|
||||
}
|
||||
```
|
||||
|
||||
@ -620,12 +709,12 @@ type ToRssString struct {
|
||||
|
||||
// 定义模型
|
||||
type UserModel struct {
|
||||
Birthday ToRssString `json:"birthday"`
|
||||
Birthday carbon.ToRssString `json:"birthday"`
|
||||
}
|
||||
|
||||
// 实例化模型
|
||||
user := UserModel {
|
||||
Birthday: ToRssString{carbon.Now()},
|
||||
Birthday: carbon.ToRssString{carbon.Now()},
|
||||
}
|
||||
|
||||
// 重写MarshalJSON方法
|
||||
@ -667,6 +756,20 @@ func (c ToRssString) MarshalJSON() ([]byte, error) {
|
||||
* [araddon/dateparse](https://github.com/araddon/dateparse)
|
||||
|
||||
#### 更新日志
|
||||
##### 2020-11-06
|
||||
* 弃用Duration()方法,拆分为AddDuration()和SubDuration()
|
||||
* 新增Compare()方法比较时间
|
||||
* 新增Gt()方法判断是否大于
|
||||
* 新增Lt()方法判断是否小于
|
||||
* 新增Eq()方法判断是否等于
|
||||
* 新增Ne()方法判断是否不等于
|
||||
* 新增Gte()方法判断是否大于等于
|
||||
* 新增Lte()方法判断是否小于等于
|
||||
* 新增Between()方法判断是否在两个时间之间(不包括这两个时间)
|
||||
* 新增BetweenIncludedStartTime()方法判断是否在两个时间之间(包括开始时间)
|
||||
* 新增BetweenIncludedEndTime()方法判断是否在两个时间之间(包括结束时间)
|
||||
* 新增BetweenIncludedBoth()方法判断是否在两个时间之间(包括这两个时间)
|
||||
|
||||
##### 2020-11-02
|
||||
* 新增测试覆盖率报告文件coverage.html
|
||||
* CreateFromTimestamp()方法支持秒、毫秒、微秒、纳秒级时间戳
|
||||
@ -674,6 +777,7 @@ func (c ToRssString) MarshalJSON() ([]byte, error) {
|
||||
* 新增ToTimestampWithMillisecond()方法获取毫秒级时间戳
|
||||
* 新增ToTimestampWithMicrosecond()方法获取微秒级时间戳
|
||||
* 新增ToTimestampWithNanosecond()方法获取微秒级时间戳
|
||||
|
||||
##### 2020-10-22
|
||||
* 新增SetYear()方法设置年
|
||||
* 新增SetMonth()方法设置月
|
||||
@ -682,15 +786,15 @@ func (c ToRssString) MarshalJSON() ([]byte, error) {
|
||||
* 新增SetMinute()方法设置分
|
||||
* 新增SetSecond方法设置秒
|
||||
* 新增DiffInWeeks()方法计算相差多少周
|
||||
* 新增DiffAbsInWeeks()方法计算相差多少周(绝对值)
|
||||
* 新增DiffInWeeksWithAbs()方法计算相差多少周(绝对值)
|
||||
* 新增DiffInDays()方法计算相差多少天
|
||||
* 新增DiffAbsInDays()方法计算相差多少天(绝对值)
|
||||
* 新增DiffInDaysWithAbs()方法计算相差多少天(绝对值)
|
||||
* 新增DiffInHours()方法计算相差多少小时
|
||||
* 新增DiffAbsInHours()方法计算相差多少小时(绝对值)
|
||||
* 新增DiffInHoursWithAbs()方法计算相差多少小时(绝对值)
|
||||
* 新增DiffInMinutes()方法计算相差多少分钟
|
||||
* 新增DiffAbsInMinutes()方法计算相差多少分钟(绝对值)
|
||||
* 新增DiffInMinutesWithAbs()方法计算相差多少分钟(绝对值)
|
||||
* 新增DiffInSeconds()方法计算相差多少秒
|
||||
* 新增DiffAbsInSeconds()方法计算相差多少秒(绝对值)
|
||||
* 新增DiffInSecondsWithAbs()方法计算相差多少秒(绝对值)
|
||||
|
||||
##### 2020-10-16
|
||||
* 新增Timezone()方法获取时区名
|
||||
|
2
const.go
2
const.go
@ -4,7 +4,7 @@ import "time"
|
||||
|
||||
// 版权信息
|
||||
const (
|
||||
Version = "v1.2.3"
|
||||
Version = "v1.2.4"
|
||||
Author = "gouguoyin"
|
||||
Email = "mail@gouguoyin.cn"
|
||||
Blog = "www.gouguoyin.cn"
|
||||
|
110
coverage.html
110
coverage.html
@ -58,9 +58,9 @@
|
||||
|
||||
<option value="file1">github.com/golang-module/carbon/carbon.go (100.0%)</option>
|
||||
|
||||
<option value="file2">github.com/golang-module/carbon/database.go (26.3%)</option>
|
||||
<option value="file2">github.com/golang-module/carbon/database.go (44.4%)</option>
|
||||
|
||||
<option value="file3">github.com/golang-module/carbon/final.go (100.0%)</option>
|
||||
<option value="file3">github.com/golang-module/carbon/final.go (97.5%)</option>
|
||||
|
||||
<option value="file4">github.com/golang-module/carbon/private.go (100.0%)</option>
|
||||
|
||||
@ -846,10 +846,6 @@ func (c Carbon) Value() (driver.Value, error) <span class="cov0" title="0">{
|
||||
<span class="cov0" title="0">return timeTime, nil</span>
|
||||
}
|
||||
|
||||
func (c Carbon) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
|
||||
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToDateTimeString) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
|
||||
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
|
||||
}</span>
|
||||
@ -866,19 +862,19 @@ func (c ToTimestamp) MarshalJSON() ([]byte, error) <span class="cov8" title="1">
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil
|
||||
}</span>
|
||||
|
||||
func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) <span class="cov0" title="0">{
|
||||
func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) <span class="cov8" title="1">{
|
||||
return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil
|
||||
}</span>
|
||||
</pre>
|
||||
@ -1106,7 +1102,7 @@ func (start Carbon) DiffInWeeks(end Carbon) int64 <span class="cov8" title="1">{
|
||||
}</span>
|
||||
|
||||
// DiffAbsInWeeks 相差多少周(绝对值)
|
||||
func (start Carbon) DiffAbsInWeeks(end Carbon) int64 <span class="cov8" title="1">{
|
||||
func (start Carbon) DiffInWeeksWithAbs(end Carbon) int64 <span class="cov8" title="1">{
|
||||
diff := start.DiffInWeeks(end)
|
||||
if diff < 0 </span><span class="cov8" title="1">{
|
||||
diff = -diff
|
||||
@ -1120,7 +1116,7 @@ func (start Carbon) DiffInDays(end Carbon) int64 <span class="cov8" title="1">{
|
||||
}</span>
|
||||
|
||||
// DiffAbsInDays 相差多少天(绝对值)
|
||||
func (start Carbon) DiffAbsInDays(end Carbon) int64 <span class="cov8" title="1">{
|
||||
func (start Carbon) DiffInDaysWithAbs(end Carbon) int64 <span class="cov8" title="1">{
|
||||
diff := start.DiffInDays(end)
|
||||
if diff < 0 </span><span class="cov8" title="1">{
|
||||
diff = -diff
|
||||
@ -1134,7 +1130,7 @@ func (start Carbon) DiffInHours(end Carbon) int64 <span class="cov8" title="1">{
|
||||
}</span>
|
||||
|
||||
// DiffAbsInHours 相差多少小时(绝对值)
|
||||
func (start Carbon) DiffAbsInHours(end Carbon) int64 <span class="cov8" title="1">{
|
||||
func (start Carbon) DiffInHoursWithAbs(end Carbon) int64 <span class="cov8" title="1">{
|
||||
diff := start.DiffInHours(end)
|
||||
if diff < 0 </span><span class="cov8" title="1">{
|
||||
diff = -diff
|
||||
@ -1148,7 +1144,7 @@ func (start Carbon) DiffInMinutes(end Carbon) int64 <span class="cov8" title="1"
|
||||
}</span>
|
||||
|
||||
// DiffAbsInMinutes 相差多少分钟(绝对值)
|
||||
func (start Carbon) DiffAbsInMinutes(end Carbon) int64 <span class="cov8" title="1">{
|
||||
func (start Carbon) DiffInMinutesWithAbs(end Carbon) int64 <span class="cov8" title="1">{
|
||||
diff := start.DiffInMinutes(end)
|
||||
if diff < 0 </span><span class="cov8" title="1">{
|
||||
diff = -diff
|
||||
@ -1172,7 +1168,7 @@ func (start Carbon) DiffInSeconds(end Carbon) int64 <span class="cov8" title="1"
|
||||
}
|
||||
|
||||
// DiffAbsInSeconds 相差多少秒(绝对值)
|
||||
func (start Carbon) DiffAbsInSeconds(end Carbon) int64 <span class="cov8" title="1">{
|
||||
func (start Carbon) DiffInSecondsWithAbs(end Carbon) int64 <span class="cov8" title="1">{
|
||||
diff := start.DiffInSeconds(end)
|
||||
if diff < 0 </span><span class="cov8" title="1">{
|
||||
diff = -diff
|
||||
@ -1520,6 +1516,90 @@ func (c Carbon) IsToday() bool <span class="cov8" title="1">{
|
||||
func (c Carbon) IsTomorrow() bool <span class="cov8" title="1">{
|
||||
return c.ToDateString() == c.Tomorrow().ToDateString()
|
||||
}</span>
|
||||
|
||||
// Compare 时间比较
|
||||
func (c Carbon) Compare(operator string, t Carbon) bool <span class="cov8" title="1">{
|
||||
switch operator </span>{
|
||||
case "=":<span class="cov8" title="1">
|
||||
return c.Time.Equal(t.Time)</span>
|
||||
case "<>":<span class="cov8" title="1">
|
||||
return !c.Time.Equal(t.Time)</span>
|
||||
case "!=":<span class="cov8" title="1">
|
||||
return !c.Time.Equal(t.Time)</span>
|
||||
case ">":<span class="cov8" title="1">
|
||||
return c.Time.After(t.Time)</span>
|
||||
case ">=":<span class="cov8" title="1">
|
||||
return c.Time.After(t.Time) || c.Time.Equal(t.Time)</span>
|
||||
case "<":<span class="cov8" title="1">
|
||||
return c.Time.Before(t.Time)</span>
|
||||
case "<=":<span class="cov8" title="1">
|
||||
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)</span>
|
||||
}
|
||||
|
||||
<span class="cov8" title="1">return false</span>
|
||||
}
|
||||
|
||||
// Gt 大于
|
||||
func (c Carbon) Gt(t Carbon) bool {
|
||||
return c.Time.After(t.Time)
|
||||
}</span>
|
||||
|
||||
// Lt 小于
|
||||
func (c Carbon) Lt(t Carbon) bool {
|
||||
return c.Time.Before(t.Time)
|
||||
}</span>
|
||||
|
||||
// Eq 等于
|
||||
func (c Carbon) Eq(operator string, t Carbon) bool <span class="cov0" title="0">{
|
||||
return c.Time.Equal(t.Time)
|
||||
}</span>
|
||||
|
||||
// Ne 不等于
|
||||
func (c Carbon) Ne(t Carbon) bool {
|
||||
return !c.Time.Equal(t.Time)
|
||||
}</span>
|
||||
|
||||
// Gte 大于等于
|
||||
func (c Carbon) Gte(t Carbon) bool {
|
||||
return c.Time.After(t.Time) || c.Time.Equal(t.Time)
|
||||
}</span>
|
||||
|
||||
// Lte 小于等于
|
||||
func (c Carbon) Lte(t Carbon) bool {
|
||||
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
|
||||
}</span>
|
||||
|
||||
// Between 是否在两个时间之间(不包括这两个时间)
|
||||
func (c Carbon) Between(start Carbon, end Carbon) bool <span class="cov8" title="1">{
|
||||
if c.Time.After(start.Time) && c.Time.Before(end.Time) </span><span class="cov8" title="1">{
|
||||
return true
|
||||
}</span>
|
||||
<span class="cov8" title="1">return false</span>
|
||||
}
|
||||
|
||||
// BetweenIncludedStartTime 是否在两个时间之间(包括开始时间)
|
||||
func (c Carbon) BetweenIncludedStartTime(start Carbon, end Carbon) bool <span class="cov8" title="1">{
|
||||
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && c.Time.Before(end.Time) </span><span class="cov8" title="1">{
|
||||
return true
|
||||
}</span>
|
||||
<span class="cov8" title="1">return false</span>
|
||||
}
|
||||
|
||||
// BetweenIncludedEndTime 是否在两个时间之间(包括结束时间)
|
||||
func (c Carbon) BetweenIncludedEndTime(start Carbon, end Carbon) bool <span class="cov8" title="1">{
|
||||
if c.Time.After(start.Time) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) </span><span class="cov8" title="1">{
|
||||
return true
|
||||
}</span>
|
||||
<span class="cov8" title="1">return false</span>
|
||||
}
|
||||
|
||||
// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间)
|
||||
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool <span class="cov8" title="1">{
|
||||
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) </span><span class="cov8" title="1">{
|
||||
return true
|
||||
}</span>
|
||||
<span class="cov8" title="1">return false</span>
|
||||
}
|
||||
</pre>
|
||||
|
||||
<pre class="file" id="file4" style="display: none">package carbon
|
||||
|
@ -56,10 +56,6 @@ func (c Carbon) Value() (driver.Value, error) {
|
||||
return timeTime, nil
|
||||
}
|
||||
|
||||
func (c Carbon) MarshalJSON() ([]byte, error) {
|
||||
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
|
||||
}
|
||||
|
||||
func (c ToDateTimeString) MarshalJSON() ([]byte, error) {
|
||||
return []byte(fmt.Sprintf(`"%s"`, c.ToDateTimeString())), nil
|
||||
}
|
||||
|
@ -7,24 +7,31 @@ import (
|
||||
)
|
||||
|
||||
var user = struct {
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Birthday Carbon `json:"birthday"`
|
||||
CreatedAt ToDateTimeString `json:"created_at"`
|
||||
DeletedAt ToTimestamp `json:"deleted_at"`
|
||||
GraduatedAt ToDateString `json:"graduated_at"`
|
||||
UpdatedAt ToTimeString `json:"updated_at"`
|
||||
}{Name: "勾国印",
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
Birthday ToDateTimeString `json:"birthday"`
|
||||
GraduatedAt ToDateString `json:"graduated_at"`
|
||||
CreatedAt ToTimeString `json:"created_at"`
|
||||
UpdatedAt ToTimestamp `json:"updated_at"`
|
||||
DateTime1 ToTimestampWithSecond `json:"date_time1"`
|
||||
DateTime2 ToTimestampWithMillisecond `json:"date_time2"`
|
||||
DateTime3 ToTimestampWithMicrosecond `json:"date_time3"`
|
||||
DateTime4 ToTimestampWithNanosecond `json:"date_time4"`
|
||||
}{
|
||||
Name: "勾国印",
|
||||
Age: 18,
|
||||
Birthday: Now().SubYears(18),
|
||||
CreatedAt: ToDateTimeString{Now()},
|
||||
DeletedAt: ToTimestamp{Parse("2020-08-05 13:14:15")},
|
||||
Birthday: ToDateTimeString{Now().SubYears(18)},
|
||||
GraduatedAt: ToDateString{Parse("2012-09-09")},
|
||||
UpdatedAt: ToTimeString{Now()},
|
||||
CreatedAt: ToTimeString{Now()},
|
||||
UpdatedAt: ToTimestamp{Now()},
|
||||
DateTime1: ToTimestampWithSecond{Now()},
|
||||
DateTime2: ToTimestampWithMillisecond{Now()},
|
||||
DateTime3: ToTimestampWithMicrosecond{Now()},
|
||||
DateTime4: ToTimestampWithNanosecond{Now()},
|
||||
}
|
||||
|
||||
func TestCarbon_MarshalJSON(t *testing.T) {
|
||||
data, _ := json.Marshal(&user)
|
||||
fmt.Print("Model output by json:\n", string(data))
|
||||
fmt.Print("Model output by json:\n", string(data)+"\n")
|
||||
}
|
||||
|
94
final.go
94
final.go
@ -221,7 +221,7 @@ func (start Carbon) DiffInWeeks(end Carbon) int64 {
|
||||
}
|
||||
|
||||
// DiffAbsInWeeks 相差多少周(绝对值)
|
||||
func (start Carbon) DiffAbsInWeeks(end Carbon) int64 {
|
||||
func (start Carbon) DiffInWeeksWithAbs(end Carbon) int64 {
|
||||
diff := start.DiffInWeeks(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
@ -235,7 +235,7 @@ func (start Carbon) DiffInDays(end Carbon) int64 {
|
||||
}
|
||||
|
||||
// DiffAbsInDays 相差多少天(绝对值)
|
||||
func (start Carbon) DiffAbsInDays(end Carbon) int64 {
|
||||
func (start Carbon) DiffInDaysWithAbs(end Carbon) int64 {
|
||||
diff := start.DiffInDays(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
@ -249,7 +249,7 @@ func (start Carbon) DiffInHours(end Carbon) int64 {
|
||||
}
|
||||
|
||||
// DiffAbsInHours 相差多少小时(绝对值)
|
||||
func (start Carbon) DiffAbsInHours(end Carbon) int64 {
|
||||
func (start Carbon) DiffInHoursWithAbs(end Carbon) int64 {
|
||||
diff := start.DiffInHours(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
@ -263,7 +263,7 @@ func (start Carbon) DiffInMinutes(end Carbon) int64 {
|
||||
}
|
||||
|
||||
// DiffAbsInMinutes 相差多少分钟(绝对值)
|
||||
func (start Carbon) DiffAbsInMinutes(end Carbon) int64 {
|
||||
func (start Carbon) DiffInMinutesWithAbs(end Carbon) int64 {
|
||||
diff := start.DiffInMinutes(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
@ -287,7 +287,7 @@ func (start Carbon) DiffInSeconds(end Carbon) int64 {
|
||||
}
|
||||
|
||||
// DiffAbsInSeconds 相差多少秒(绝对值)
|
||||
func (start Carbon) DiffAbsInSeconds(end Carbon) int64 {
|
||||
func (start Carbon) DiffInSecondsWithAbs(end Carbon) int64 {
|
||||
diff := start.DiffInSeconds(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
@ -635,3 +635,87 @@ func (c Carbon) IsToday() bool {
|
||||
func (c Carbon) IsTomorrow() bool {
|
||||
return c.ToDateString() == c.Tomorrow().ToDateString()
|
||||
}
|
||||
|
||||
// Compare 时间比较
|
||||
func (c Carbon) Compare(operator string, t Carbon) bool {
|
||||
switch operator {
|
||||
case "=":
|
||||
return c.Time.Equal(t.Time)
|
||||
case "<>":
|
||||
return !c.Time.Equal(t.Time)
|
||||
case "!=":
|
||||
return !c.Time.Equal(t.Time)
|
||||
case ">":
|
||||
return c.Time.After(t.Time)
|
||||
case ">=":
|
||||
return c.Time.After(t.Time) || c.Time.Equal(t.Time)
|
||||
case "<":
|
||||
return c.Time.Before(t.Time)
|
||||
case "<=":
|
||||
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// Gt 大于
|
||||
func (c Carbon) Gt(t Carbon) bool {
|
||||
return c.Time.After(t.Time)
|
||||
}
|
||||
|
||||
// Lt 小于
|
||||
func (c Carbon) Lt(t Carbon) bool {
|
||||
return c.Time.Before(t.Time)
|
||||
}
|
||||
|
||||
// Eq 等于
|
||||
func (c Carbon) Eq(t Carbon) bool {
|
||||
return c.Time.Equal(t.Time)
|
||||
}
|
||||
|
||||
// Ne 不等于
|
||||
func (c Carbon) Ne(t Carbon) bool {
|
||||
return !c.Time.Equal(t.Time)
|
||||
}
|
||||
|
||||
// Gte 大于等于
|
||||
func (c Carbon) Gte(t Carbon) bool {
|
||||
return c.Time.After(t.Time) || c.Time.Equal(t.Time)
|
||||
}
|
||||
|
||||
// Lte 小于等于
|
||||
func (c Carbon) Lte(t Carbon) bool {
|
||||
return c.Time.Before(t.Time) || c.Time.Equal(t.Time)
|
||||
}
|
||||
|
||||
// Between 是否在两个时间之间(不包括这两个时间)
|
||||
func (c Carbon) Between(start Carbon, end Carbon) bool {
|
||||
if c.Time.After(start.Time) && c.Time.Before(end.Time) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// BetweenIncludedStartTime 是否在两个时间之间(包括开始时间)
|
||||
func (c Carbon) BetweenIncludedStartTime(start Carbon, end Carbon) bool {
|
||||
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && c.Time.Before(end.Time) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// BetweenIncludedEndTime 是否在两个时间之间(包括结束时间)
|
||||
func (c Carbon) BetweenIncludedEndTime(start Carbon, end Carbon) bool {
|
||||
if c.Time.After(start.Time) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// BetweenIncludedBoth 是否在两个时间之间(包括这两个时间)
|
||||
func (c Carbon) BetweenIncludedBoth(start Carbon, end Carbon) bool {
|
||||
if (c.Time.After(start.Time) || c.Time.Equal(start.Time)) && (c.Time.Before(end.Time) || c.Time.Equal(end.Time)) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
379
final_test.go
379
final_test.go
@ -608,7 +608,7 @@ func TestCarbon_DiffInWeeks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInWeeks(t *testing.T) {
|
||||
func TestCarbon_DiffInWeeksWithAbs(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
@ -631,7 +631,7 @@ func TestCarbon_DiffAbsInWeeks(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInWeeks(Parse(v.input2))
|
||||
output := Parse(v.input1).DiffInWeeksWithAbs(Parse(v.input2))
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
|
||||
@ -670,7 +670,7 @@ func TestCarbon_DiffInDays(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInDays(t *testing.T) {
|
||||
func TestCarbon_DiffInDaysWithAbs(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
@ -693,7 +693,7 @@ func TestCarbon_DiffAbsInDays(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInDays(Parse(v.input2))
|
||||
output := Parse(v.input1).DiffInDaysWithAbs(Parse(v.input2))
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
|
||||
@ -732,7 +732,7 @@ func TestCarbon_DiffInHours(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInHours(t *testing.T) {
|
||||
func TestCarbon_DiffInHoursWithAbs(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
@ -755,7 +755,7 @@ func TestCarbon_DiffAbsInHours(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInHours(Parse(v.input2))
|
||||
output := Parse(v.input1).DiffInHoursWithAbs(Parse(v.input2))
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
|
||||
@ -794,7 +794,7 @@ func TestCarbon_DiffInMinutes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInMinutes(t *testing.T) {
|
||||
func TestCarbon_DiffInMinutesWithAbs(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
@ -817,7 +817,7 @@ func TestCarbon_DiffAbsInMinutes(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInMinutes(Parse(v.input2))
|
||||
output := Parse(v.input1).DiffInMinutesWithAbs(Parse(v.input2))
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
|
||||
@ -848,7 +848,7 @@ func TestCarbon_DiffInSeconds(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInSeconds(t *testing.T) {
|
||||
func TestCarbon_DiffInSecondsWithAbs(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
@ -863,7 +863,7 @@ func TestCarbon_DiffAbsInSeconds(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInSeconds(Parse(v.input2))
|
||||
output := Parse(v.input1).DiffInSecondsWithAbs(Parse(v.input2))
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input start time %s and end time %s, expected %d, but got %d", v.input1, v.input2, v.output, output)
|
||||
@ -2295,3 +2295,362 @@ func TestCarbon_IsTomorrow(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Compare(t *testing.T) {
|
||||
now := Now()
|
||||
tomorrow := Tomorrow()
|
||||
yesterday := Yesterday()
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
operator string // 输入参数
|
||||
time Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{now, ">", yesterday, true},
|
||||
{now, "<", yesterday, false},
|
||||
{now, "<", tomorrow, true},
|
||||
{now, ">", tomorrow, false},
|
||||
{now, "=", now, true},
|
||||
{now, ">=", now, true},
|
||||
{now, "<=", now, true},
|
||||
{now, "!=", now, false},
|
||||
{now, "<>", now, false},
|
||||
{now, "!=", yesterday, true},
|
||||
{now, "<>", yesterday, true},
|
||||
{now, "+", yesterday, false},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Compare(v.operator, v.time)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s %s %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.operator, v.time.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Gt(t *testing.T) {
|
||||
now := Now()
|
||||
tomorrow := Tomorrow()
|
||||
yesterday := Yesterday()
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{now, now, false},
|
||||
{now, yesterday, true},
|
||||
{now, tomorrow, false},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Gt(v.time)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s > %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Lt(t *testing.T) {
|
||||
now := Now()
|
||||
tomorrow := Tomorrow()
|
||||
yesterday := Yesterday()
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{now, now, false},
|
||||
{now, yesterday, false},
|
||||
{now, tomorrow, true},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Lt(v.time)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s < %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Eq(t *testing.T) {
|
||||
now := Now()
|
||||
tomorrow := Tomorrow()
|
||||
yesterday := Yesterday()
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{now, now, true},
|
||||
{now, yesterday, false},
|
||||
{now, tomorrow, false},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Eq(v.time)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s = %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Ne(t *testing.T) {
|
||||
now := Now()
|
||||
tomorrow := Tomorrow()
|
||||
yesterday := Yesterday()
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{now, now, false},
|
||||
{now, yesterday, true},
|
||||
{now, tomorrow, true},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Ne(v.time)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s != %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Gte(t *testing.T) {
|
||||
now := Now()
|
||||
tomorrow := Tomorrow()
|
||||
yesterday := Yesterday()
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{now, now, true},
|
||||
{now, yesterday, true},
|
||||
{now, tomorrow, false},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Gte(v.time)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s >= %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Lte(t *testing.T) {
|
||||
now := Now()
|
||||
tomorrow := Tomorrow()
|
||||
yesterday := Yesterday()
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{now, now, true},
|
||||
{now, yesterday, false},
|
||||
{now, tomorrow, true},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Lte(v.time)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s <= %s, expected %s, but got %s\n", v.input.ToDateTimeString(), v.time.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Between(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time1 Carbon // 输入参数
|
||||
time2 Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), false},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), false},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), false},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.Between(v.time1, v.time2)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s < %s < %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BetweenIncludedStartTime(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time1 Carbon // 输入参数
|
||||
time2 Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), false},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), true},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), false},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.BetweenIncludedStartTime(v.time1, v.time2)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s <= %s < %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BetweenIncludedEndTime(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time1 Carbon // 输入参数
|
||||
time2 Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), false},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), false},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), true},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.BetweenIncludedEndTime(v.time1, v.time2)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s < %s <= %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BetweenIncludedBoth(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input Carbon // 输入值
|
||||
time1 Carbon // 输入参数
|
||||
time2 Carbon // 输入参数
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), true},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), true},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-05 13:14:15"), true},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-04 13:14:15"), Parse("2020-08-06 13:14:15"), true},
|
||||
{Parse("2020-08-05 13:14:15"), Parse("2020-08-06 13:14:15"), Parse("2020-08-06 13:14:15"), false},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := v.input.BetweenIncludedBoth(v.time1, v.time2)
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s <= %s <= %s, expected %s, but got %s\n", v.time1.ToDateTimeString(), v.input.ToDateTimeString(), v.time2.ToDateTimeString(), expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user