diff --git a/README.en.md b/README.en.md index 4c25b2d9..3ea25ac4 100755 --- a/README.en.md +++ b/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 diff --git a/README.md b/README.md index fcb5e7c6..e39062ac 100755 --- a/README.md +++ b/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()方法获取时区名 diff --git a/const.go b/const.go index ac6ff9fa..bf4e3633 100644 --- a/const.go +++ b/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" diff --git a/coverage.html b/coverage.html index 4492b423..40bcc215 100644 --- a/coverage.html +++ b/coverage.html @@ -58,9 +58,9 @@ - + - + @@ -846,10 +846,6 @@ 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 } @@ -866,19 +862,19 @@ return []byte(fmt.Sprintf(`%d`, c.ToTimestamp())), nil } -func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) { +func (c ToTimestampWithSecond) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithSecond())), nil } -func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) { +func (c ToTimestampWithMillisecond) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMillisecond())), nil } -func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) { +func (c ToTimestampWithMicrosecond) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithMicrosecond())), nil } -func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) { +func (c ToTimestampWithNanosecond) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`%d`, c.ToTimestampWithNanosecond())), nil } @@ -1106,7 +1102,7 @@ } // DiffAbsInWeeks 相差多少周(绝对值) -func (start Carbon) DiffAbsInWeeks(end Carbon) int64 { +func (start Carbon) DiffInWeeksWithAbs(end Carbon) int64 { diff := start.DiffInWeeks(end) if diff < 0 { diff = -diff @@ -1120,7 +1116,7 @@ } // DiffAbsInDays 相差多少天(绝对值) -func (start Carbon) DiffAbsInDays(end Carbon) int64 { +func (start Carbon) DiffInDaysWithAbs(end Carbon) int64 { diff := start.DiffInDays(end) if diff < 0 { diff = -diff @@ -1134,7 +1130,7 @@ } // DiffAbsInHours 相差多少小时(绝对值) -func (start Carbon) DiffAbsInHours(end Carbon) int64 { +func (start Carbon) DiffInHoursWithAbs(end Carbon) int64 { diff := start.DiffInHours(end) if diff < 0 { diff = -diff @@ -1148,7 +1144,7 @@ } // DiffAbsInMinutes 相差多少分钟(绝对值) -func (start Carbon) DiffAbsInMinutes(end Carbon) int64 { +func (start Carbon) DiffInMinutesWithAbs(end Carbon) int64 { diff := start.DiffInMinutes(end) if diff < 0 { diff = -diff @@ -1172,7 +1168,7 @@ } // DiffAbsInSeconds 相差多少秒(绝对值) -func (start Carbon) DiffAbsInSeconds(end Carbon) int64 { +func (start Carbon) DiffInSecondsWithAbs(end Carbon) int64 { diff := start.DiffInSeconds(end) if diff < 0 { diff = -diff @@ -1520,6 +1516,90 @@ 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(operator string, 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 +}