mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-29 18:57:37 +08:00
v1.2.2
This commit is contained in:
parent
56b256b049
commit
51668647ba
260
README.en.md
260
README.en.md
@ -1,5 +1,5 @@
|
||||
# Carbon
|
||||
Englsih | [Chinese](./README.md)
|
||||
English | [Chinese](./README.md)
|
||||
|
||||
#### Description
|
||||
A simple,semantic and developer-friendly golang package for datetime
|
||||
@ -24,22 +24,12 @@ go get -u gitee.com/go-package/carbon
|
||||
|
||||
import (
|
||||
"gitee.com/go-package/carbon"
|
||||
)
|
||||
|
||||
)
|
||||
```
|
||||
|
||||
#### Usage and example
|
||||
> The default timezone is Local, assuming the current time is 2020-08-05 13:14:15
|
||||
|
||||
##### Set timezone
|
||||
```go
|
||||
carbon.Timezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
carbon.Timezone(carbon.Tokyo).Now().ToDateTimeString() // 2020-08-05 14:14:15
|
||||
carbon.Timezone(carbon.Tokyo).Timezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
```
|
||||
|
||||
> For more timezone constants, please see the [const.go](./const.go) file
|
||||
|
||||
##### Yesterday,today and tomorrow
|
||||
```go
|
||||
// Datetime of today
|
||||
@ -70,39 +60,6 @@ carbon.Tomorrow().ToTimeString() // 13:14:15
|
||||
carbon.Tomorrow().ToTimestamp() // 1596690855
|
||||
```
|
||||
|
||||
##### Beginning and end
|
||||
```go
|
||||
// Beginning of the year
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfYear().ToDateTimeString() // 2020-01-01 00:00:00
|
||||
// End of the year
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfYear().ToEndTimeString() // 2020-12-31 23:59:59
|
||||
|
||||
// Beginning of the month
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfMonth().ToStartTimeString() // 2020-08-01 00:00:00
|
||||
// End of the month
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToEndTimeString() // 2020-08-31 23:59:59
|
||||
|
||||
// Beginning of the week
|
||||
carbon.Parse("2020-08-05 13:14:15").FirstOfWeek().ToStartTimeString() // 2020-08-03 00:00:00
|
||||
// End of the week
|
||||
carbon.Parse("2020-08-05 13:14:15").LastOfWeek().ToEndTimeString() // 2020-08-09 23:59:59
|
||||
|
||||
// Beginning of the day
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfDay().ToDateTimeString() // 2020-08-05 00:00:00
|
||||
// End of the day
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfDay().ToDateTimeString() // 2020-08-05 23:59:59
|
||||
|
||||
// Beginning of the hour
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfHour().ToDateTimeString() // 2020-08-05 13:00:00
|
||||
// End of the hour
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfHour().ToDateTimeString() // 2020-08-05 13:59:59
|
||||
|
||||
// Beginning of the minute
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfMinute().ToDateTimeString() // 2020-08-05 13:14:00
|
||||
// End of the minute
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMinute().ToDateTimeString() // 2020-08-05 13:14:59
|
||||
```
|
||||
|
||||
##### Create carbon instance
|
||||
```go
|
||||
// Create Carbon instance from timestamp
|
||||
@ -152,12 +109,78 @@ carbon.ParseByDuration("-10.5m").ToDateTimeString // 2020-08-05 13:03:45
|
||||
carbon.ParseByDuration("10s").ToDateTimeString // 2020-08-05 13:14:25
|
||||
// Ten seconds ago
|
||||
carbon.ParseByDuration("-10.5s").ToDateTimeString // 2020-08-05 13:14:04
|
||||
```
|
||||
|
||||
##### Time setter
|
||||
```go
|
||||
// Set timezone
|
||||
carbon.SetTimezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
carbon.SetTimezone(carbon.Tokyo).Now().ToDateTimeString() // 2020-08-05 14:14:15
|
||||
carbon.SetTimezone(carbon.Tokyo).SetTimezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
|
||||
// Set year
|
||||
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
|
||||
|
||||
// Set day
|
||||
carbon.Parse("2019-08-05").SetDay(31).ToDateString() // 2020-08-31
|
||||
carbon.Parse("2020-02-01").SetDay(31).ToDateString() // 2020-03-02
|
||||
|
||||
// Set hour
|
||||
carbon.Parse("2020-08-05 13:14:15").SetHour(10).ToDateTimeString() // 2020-08-05 10:14:15
|
||||
carbon.Parse("2020-08-05 13:14:15").SetHour(24).ToDateTimeString() // 2020-08-06 00:14:15
|
||||
|
||||
// Set minute
|
||||
carbon.Parse("2020-08-05 13:14:15").SetMinute(10).ToDateTimeString() // 2020-08-05 13:10:15
|
||||
carbon.Parse("2020-08-05 13:14:15").SetMinute(60).ToDateTimeString() // 2020-08-05 14:00:15
|
||||
|
||||
// Set second
|
||||
carbon.Parse("2020-08-05 13:14:15").SetSecond(10).ToDateTimeString() // 2020-08-05 13:14:10
|
||||
carbon.Parse("2020-08-05 13:14:15").SetSecond(60).ToDateTimeString() // 2020-08-05 13:15:00
|
||||
```
|
||||
> For more timezone constants, please see the [const.go](./const.go) file
|
||||
|
||||
##### Start and end
|
||||
```go
|
||||
// Start of the year
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfYear().ToDateTimeString() // 2020-01-01 00:00:00
|
||||
// End of the year
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfYear().ToEndTimeString() // 2020-12-31 23:59:59
|
||||
|
||||
// Start of the month
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfMonth().ToStartTimeString() // 2020-08-01 00:00:00
|
||||
// End of the month
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToEndTimeString() // 2020-08-31 23:59:59
|
||||
|
||||
// Start of the week
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek().ToStartTimeString() // 2020-08-03 00:00:00
|
||||
// End of the week
|
||||
carbon.Parse("2020-08-05 13:14:15").LastOfWeek().ToEndTimeString() // 2020-08-09 23:59:59
|
||||
|
||||
// Start of the day
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfDay().ToDateTimeString() // 2020-08-05 00:00:00
|
||||
// End of the day
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfDay().ToDateTimeString() // 2020-08-05 23:59:59
|
||||
|
||||
// Start of the hour
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfHour().ToDateTimeString() // 2020-08-05 13:00:00
|
||||
// End of the hour
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfHour().ToDateTimeString() // 2020-08-05 13:59:59
|
||||
|
||||
// Start of the minute
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfMinute().ToDateTimeString() // 2020-08-05 13:14:00
|
||||
// End of the minute
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMinute().ToDateTimeString() // 2020-08-05 13:14:59
|
||||
```
|
||||
|
||||
##### Time travel
|
||||
```go
|
||||
// After Three years
|
||||
// After three years
|
||||
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
|
||||
@ -191,9 +214,9 @@ carbon.Parse("2020-03-31 13:14:15").SubMonth().ToDateTimeString() // 2020-03-02
|
||||
// Previous one month
|
||||
carbon.Parse("2020-03-31 13:14:15").PreMonth().ToDateTimeString() // 2020-02-29 13:14:15
|
||||
|
||||
// After Three days
|
||||
// After three days
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDays(3).ToDateTimeString() // 2020-08-08 13:14:15
|
||||
// After One day
|
||||
// After one day
|
||||
carbon.Parse("2020-08-05 13:14:15").AddDay().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
// After three days
|
||||
carbon.Parse("2020-08-05 13:14:15").SubDays(3).ToDateTimeString() // 2020-08-02 13:14:15
|
||||
@ -238,11 +261,37 @@ carbon.Parse("2020-08-05 13:14:15").SubSeconds(3).ToDateTimeString() // 2020-08-
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5s").ToDateTimeString() // 2020-08-05 13:14:12
|
||||
// Before one second
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05 13:14:14
|
||||
|
||||
```
|
||||
|
||||
##### Time output
|
||||
|
||||
##### Time difference
|
||||
```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 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 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 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 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
|
||||
```
|
||||
|
||||
##### Time output
|
||||
```go
|
||||
// To timestamp
|
||||
carbon.Parse("2020-08-05 13:14:15").ToTimestamp() // 1596604455
|
||||
@ -294,50 +343,50 @@ carbon.Parse("2020-08-05 13:14:15").ToRFC2822String() // Wed, 05 Aug 2020 13:14:
|
||||
carbon.Parse("2020-08-05 13:14:15").ToRFC3339String() // 2020-08-05T13:14:15+08:00
|
||||
// To string of RFC7231 format
|
||||
carbon.Parse("2020-08-05 13:14:15").ToRFC7231String() // Wed, 05 Aug 2020 05:14:15 GMT
|
||||
|
||||
```
|
||||
> For more format signs, please see the <a href="#format-sign-table">Format sign table</a>
|
||||
|
||||
##### Statistics
|
||||
##### Time getter
|
||||
```go
|
||||
// Total days of the year
|
||||
// Get total days of the year
|
||||
carbon.Parse("2019-08-05 13:14:15").DaysInYear() // 365
|
||||
carbon.Parse("2020-08-05 13:14:15").DaysInYear() // 366
|
||||
// Total days of the month
|
||||
// Get total days of the month
|
||||
carbon.Parse("2020-02-01 13:14:15").DaysInMonth() // 29
|
||||
carbon.Parse("2020-04-01 13:14:15").DaysInMonth() // 30
|
||||
carbon.Parse("2020-08-01 13:14:15").DaysInMonth() // 31
|
||||
|
||||
// current age
|
||||
carbon.Parse("1990-01-01 13:14:15").Age() // 30
|
||||
carbon.Parse("1990-12-31 13:14:15").Age() // 29
|
||||
|
||||
// Current year
|
||||
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
|
||||
// Current month
|
||||
carbon.Parse("2020-08-05 13:14:15").Month() // 8
|
||||
// Current day
|
||||
carbon.Parse("2020-08-05 13:14:15").Day() // 5
|
||||
// Current hour
|
||||
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
|
||||
// Current minute
|
||||
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
|
||||
// Current second
|
||||
carbon.Parse("2020-08-05 13:14:15").Second() // 15
|
||||
```
|
||||
|
||||
##### Week and day
|
||||
```go
|
||||
// Day of the year
|
||||
// Get day of the year
|
||||
carbon.Parse("2020-08-05 13:14:15").DayOfYear() // 218
|
||||
// Week of the year
|
||||
// Get week of the year
|
||||
carbon.Parse("2020-08-05 13:14:15").WeekOfYear() // 32
|
||||
// Day of the month
|
||||
// Get day of the month
|
||||
carbon.Parse("2020-08-05 13:14:15").DayOfMonth() // 5
|
||||
// Week of the month
|
||||
// Get week of the month
|
||||
carbon.Parse("2020-08-05 13:14:15").WeekOfMonth() // 1
|
||||
// Day of the week
|
||||
// Get day of the week
|
||||
carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
|
||||
|
||||
// Get current year
|
||||
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
|
||||
// Get current month
|
||||
carbon.Parse("2020-08-05 13:14:15").Month() // 8
|
||||
// Get current day
|
||||
carbon.Parse("2020-08-05 13:14:15").Day() // 5
|
||||
// Get current hour
|
||||
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
|
||||
// Get current minute
|
||||
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
|
||||
// Get current second
|
||||
carbon.Parse("2020-08-05 13:14:15").Second() // 15
|
||||
|
||||
// Get timezone name
|
||||
carbon.SetTimezone(carbon.PRC).Timezone() // PRC
|
||||
carbon.SetTimezone(carbon.Tokyo).Timezone() // Asia/Tokyo
|
||||
|
||||
// Get current age
|
||||
carbon.Parse("2002-01-01 13:14:15").Age() // 17
|
||||
carbon.Parse("2002-12-31 13:14:15").Age() // 18
|
||||
```
|
||||
|
||||
##### Time judgment
|
||||
@ -357,62 +406,65 @@ carbon.Parse(carbon.Now().ToDateTimeString()).IsNow() // true
|
||||
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
|
||||
// Is january
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJanuary() // false
|
||||
// Is February
|
||||
// Is february
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFebruary() // false
|
||||
// Is March
|
||||
// Is march
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMarch() // false
|
||||
// Is April
|
||||
// Is april
|
||||
carbon.Parse("2020-08-05 13:14:15").IsApril() // false
|
||||
// Is May
|
||||
// Is may
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMay() // false
|
||||
// Is June
|
||||
// Is june
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJune() // false
|
||||
// Is July
|
||||
// Is july
|
||||
carbon.Parse("2020-08-05 13:14:15").IsJuly() // false
|
||||
// Is August
|
||||
// Is august
|
||||
carbon.Parse("2020-08-05 13:14:15").IsAugust() // false
|
||||
// Is September
|
||||
// Is september
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSeptember() // true
|
||||
// Is October
|
||||
// Is october
|
||||
carbon.Parse("2020-08-05 13:14:15").IsOctober() // false
|
||||
// Is November
|
||||
// Is november
|
||||
carbon.Parse("2020-08-05 13:14:15").IsNovember() // false
|
||||
// Is December
|
||||
// Is december
|
||||
carbon.Parse("2020-08-05 13:14:15").IsDecember() // false
|
||||
|
||||
// Is Monday
|
||||
// Is monday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsMonday() // false
|
||||
// Is Tuesday
|
||||
// Is tuesday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsTuesday() // true
|
||||
// Is Wednesday
|
||||
// Is wednesday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWednesday() // false
|
||||
// Is Thursday
|
||||
// Is thursday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsThursday() // false
|
||||
// Is Friday
|
||||
// Is friday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsFriday() // false
|
||||
// Is Saturday
|
||||
// Is saturday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSaturday() // false
|
||||
// Is Sunday
|
||||
// Is sunday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsSunday() // false
|
||||
// Is Weekday
|
||||
// Is weekday
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekday() // false
|
||||
// Is Weekend
|
||||
// Is weekend
|
||||
carbon.Parse("2020-08-05 13:14:15").IsWeekend() // true
|
||||
|
||||
// Is Yesterday
|
||||
// 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
|
||||
// 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
|
||||
// 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
|
||||
|
215
README.md
215
README.md
@ -29,15 +29,6 @@ import (
|
||||
#### 用法示例
|
||||
> 默认时区为Local,即服务器所在时区,假设当前时间为2020-08-05 13:14:15
|
||||
|
||||
##### 设置时区
|
||||
```go
|
||||
carbon.Timezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
carbon.Timezone(carbon.Tokyo).Now().ToDateTimeString() // 2020-08-05 14:14:15
|
||||
carbon.Timezone(carbon.Tokyo).Timezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
```
|
||||
|
||||
>更多时区常量请查看[const.go](./const.go)文件
|
||||
|
||||
##### 昨天、今天、明天
|
||||
```go
|
||||
// 今天
|
||||
@ -68,39 +59,6 @@ carbon.Tomorrow().ToTimeString() // 13:14:15
|
||||
carbon.Tomorrow().ToTimestamp() // 1596690855
|
||||
```
|
||||
|
||||
##### 开始时间、结束时间
|
||||
```go
|
||||
// 本年开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfYear().ToDateTimeString() // 2020-01-01 00:00:00
|
||||
// 本年结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfYear().ToDateTimeString() // 2020-12-31 23:59:59
|
||||
|
||||
// 本月开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfMonth().ToDateTimeString() // 2020-08-01 00:00:00
|
||||
// 本月结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToDateTimeString() // 2020-08-31 23:59:59
|
||||
|
||||
// 本周开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfWeek().ToDateTimeString() // 2020-08-03 00:00:00
|
||||
// 本周结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfWeek().ToDateTimeString() // 2020-08-09 23:59:59
|
||||
|
||||
// 本日开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfDay().ToDateTimeString() // 2020-08-05 00:00:00
|
||||
// 本日结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfDay().ToDateTimeString() // 2020-08-05 23:59:59
|
||||
|
||||
// 本小时开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfHour().ToDateTimeString() // 2020-08-05 13:00:00
|
||||
// 本小时结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfHour().ToDateTimeString() // 2020-08-05 13:59:59
|
||||
|
||||
// 本分钟开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").BeginningOfMinute().ToDateTimeString() // 2020-08-05 13:14:00
|
||||
// 本分钟结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMinute().ToDateTimeString() // 2020-08-05 13:14:59
|
||||
```
|
||||
|
||||
##### 创建Carbon实例
|
||||
```go
|
||||
// 从时间戳创建Carbon实例
|
||||
@ -153,7 +111,73 @@ carbon.ParseByDuration("-10.5m").ToDateTimeString() // 2020-08-05 13:03:45
|
||||
carbon.ParseByDuration("10s").ToDateTimeString() // 2020-08-05 13:14:25
|
||||
// 十秒半前
|
||||
carbon.ParseByDuration("-10.5s").ToDateTimeString() // 2020-08-05 13:14:04
|
||||
```
|
||||
|
||||
##### 时间设置
|
||||
```go
|
||||
// 设置时区
|
||||
carbon.SetTimezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
carbon.SetTimezone(carbon.Tokyo).Now().ToDateTimeString() // 2020-08-05 14:14:15
|
||||
carbon.SetTimezone(carbon.Tokyo).SetTimezone(carbon.PRC).Now().ToDateTimeString() // 2020-08-05 13:14:15
|
||||
|
||||
// 设置年
|
||||
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
|
||||
|
||||
// 设置日
|
||||
carbon.Parse("2019-08-05").SetDay(31).ToDateString() // 2020-08-31
|
||||
carbon.Parse("2020-02-01").SetDay(31).ToDateString() // 2020-03-02
|
||||
|
||||
// 设置时
|
||||
carbon.Parse("2020-08-05 13:14:15").SetHour(10).ToDateTimeString() // 2020-08-05 10:14:15
|
||||
carbon.Parse("2020-08-05 13:14:15").SetHour(24).ToDateTimeString() // 2020-08-06 00:14:15
|
||||
|
||||
// 设置分
|
||||
carbon.Parse("2020-08-05 13:14:15").SetMinute(10).ToDateTimeString() // 2020-08-05 13:10:15
|
||||
carbon.Parse("2020-08-05 13:14:15").SetMinute(60).ToDateTimeString() // 2020-08-05 14:00:15
|
||||
|
||||
// 设置秒
|
||||
carbon.Parse("2020-08-05 13:14:15").SetSecond(10).ToDateTimeString() // 2020-08-05 13:14:10
|
||||
carbon.Parse("2020-08-05 13:14:15").SetSecond(60).ToDateTimeString() // 2020-08-05 13:15:00
|
||||
```
|
||||
>更多时区常量请查看[const.go](./const.go)文件
|
||||
|
||||
##### 开始时间、结束时间
|
||||
```go
|
||||
// 本年开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfYear().ToDateTimeString() // 2020-01-01 00:00:00
|
||||
// 本年结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfYear().ToDateTimeString() // 2020-12-31 23:59:59
|
||||
|
||||
// 本月开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfMonth().ToDateTimeString() // 2020-08-01 00:00:00
|
||||
// 本月结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMonth().ToDateTimeString() // 2020-08-31 23:59:59
|
||||
|
||||
// 本周开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfWeek().ToDateTimeString() // 2020-08-03 00:00:00
|
||||
// 本周结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfWeek().ToDateTimeString() // 2020-08-09 23:59:59
|
||||
|
||||
// 本日开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfDay().ToDateTimeString() // 2020-08-05 00:00:00
|
||||
// 本日结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfDay().ToDateTimeString() // 2020-08-05 23:59:59
|
||||
|
||||
// 本小时开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfHour().ToDateTimeString() // 2020-08-05 13:00:00
|
||||
// 本小时结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfHour().ToDateTimeString() // 2020-08-05 13:59:59
|
||||
|
||||
// 本分钟开始时间
|
||||
carbon.Parse("2020-08-05 13:14:15").StartOfMinute().ToDateTimeString() // 2020-08-05 13:14:00
|
||||
// 本分钟结束时间
|
||||
carbon.Parse("2020-08-05 13:14:15").EndOfMinute().ToDateTimeString() // 2020-08-05 13:14:59
|
||||
```
|
||||
|
||||
##### 时间旅行
|
||||
@ -241,7 +265,34 @@ carbon.Parse("2020-08-05 13:14:15").SubSeconds(3).ToDateTimeString() // 2020-08-
|
||||
carbon.Parse("2020-08-05 13:14:15").Duration("-2.5s").ToDateTimeString() // 2020-08-05 13:14:12
|
||||
// 一秒钟前
|
||||
carbon.Parse("2020-08-05 13:14:15").SubSecond().ToDateTimeString() // 2020-08-05 13:14:14
|
||||
```
|
||||
|
||||
##### 时间差
|
||||
```go
|
||||
// 相差多少周
|
||||
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").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").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").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").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
|
||||
```
|
||||
|
||||
##### 时间输出
|
||||
@ -300,46 +351,48 @@ carbon.Parse("2020-08-05 13:14:15").ToRFC7231String() // Wed, 05 Aug 2020 05:14:
|
||||
```
|
||||
>更多格式化输出符号请查看附录 <a href="#格式化符号表">格式化符号表</a>
|
||||
|
||||
##### 统计数字
|
||||
##### 时间获取
|
||||
```go
|
||||
// 本年总天数
|
||||
// 获取本年总天数
|
||||
carbon.Parse("2019-08-05 13:14:15").DaysInYear() // 365
|
||||
carbon.Parse("2020-08-05 13:14:15").DaysInYear() // 366
|
||||
// 本月总天数
|
||||
// 获取本月总天数
|
||||
carbon.Parse("2020-02-01 13:14:15").DaysInMonth() // 29
|
||||
carbon.Parse("2020-04-01 13:14:15").DaysInMonth() // 30
|
||||
carbon.Parse("2020-08-01 13:14:15").DaysInMonth() // 31
|
||||
|
||||
// 获取年龄
|
||||
carbon.Parse("1990-01-01 13:14:15").Age() // 30
|
||||
carbon.Parse("1990-12-31 13:14:15").Age() // 29
|
||||
|
||||
// 当前年
|
||||
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
|
||||
// 当前月
|
||||
carbon.Parse("2020-08-05 13:14:15").Month() // 8
|
||||
// 当前日
|
||||
carbon.Parse("2020-08-05 13:14:15").Day() // 5
|
||||
// 当前时
|
||||
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
|
||||
// 当前分
|
||||
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
|
||||
// 当前秒
|
||||
carbon.Parse("2020-08-05 13:14:15").Second() // 15
|
||||
```
|
||||
|
||||
##### 第几周/天
|
||||
```go
|
||||
// 本年第几天
|
||||
// 获取本年第几天
|
||||
carbon.Parse("2020-08-05 13:14:15").DayOfYear() // 218
|
||||
// 本年第几周
|
||||
// 获取本年第几周
|
||||
carbon.Parse("2020-08-05 13:14:15").WeekOfYear() // 32
|
||||
// 本月第几天
|
||||
// 获取本月第几天
|
||||
carbon.Parse("2020-08-05 13:14:15").DayOfMonth() // 5
|
||||
// 本月第几周
|
||||
// 获取本月第几周
|
||||
carbon.Parse("2020-08-05 13:14:15").WeekOfMonth() // 1
|
||||
// 本周第几天
|
||||
// 获取本周第几天
|
||||
carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
|
||||
|
||||
// 获取当前年
|
||||
carbon.Parse("2020-08-05 13:14:15").Year() // 2020
|
||||
// 获取当前月
|
||||
carbon.Parse("2020-08-05 13:14:15").Month() // 8
|
||||
// 获取当前日
|
||||
carbon.Parse("2020-08-05 13:14:15").Day() // 5
|
||||
// 获取当前时
|
||||
carbon.Parse("2020-08-05 13:14:15").Hour() // 13
|
||||
// 获取当前分
|
||||
carbon.Parse("2020-08-05 13:14:15").Minute() // 14
|
||||
// 获取当前秒
|
||||
carbon.Parse("2020-08-05 13:14:15").Second() // 15
|
||||
|
||||
// 获取时区
|
||||
carbon.SetTimezone(carbon.PRC).Timezone() // PRC
|
||||
carbon.SetTimezone(carbon.Tokyo).Timezone() // Asia/Tokyo
|
||||
|
||||
// 获取年龄
|
||||
carbon.Parse("2002-01-01 13:14:15").Age() // 17
|
||||
carbon.Parse("2002-12-31 13:14:15").Age() // 18
|
||||
|
||||
```
|
||||
|
||||
##### 时间判断
|
||||
@ -359,8 +412,11 @@ 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
|
||||
@ -568,7 +624,26 @@ func (c ToRssString) MarshalJSON() ([]byte, error) {
|
||||
* [araddon/dateparse](https://github.com/araddon/dateparse)
|
||||
|
||||
#### 更新日志
|
||||
##### 2020-10-22
|
||||
* 新增SetYear()方法设置年
|
||||
* 新增SetMonth()方法设置月
|
||||
* 新增SetDay()方法设置日
|
||||
* 新增SetHour()方法设置时
|
||||
* 新增SetMinute()方法设置分
|
||||
* 新增SetSecond方法设置秒
|
||||
* 新增DiffInWeeks()方法计算相差多少周
|
||||
* 新增DiffAbsInWeeks()方法计算相差多少周(绝对值)
|
||||
* 新增DiffInDays()方法计算相差多少天
|
||||
* 新增DiffAbsInDays()方法计算相差多少天(绝对值)
|
||||
* 新增DiffInHours()方法计算相差多少小时
|
||||
* 新增DiffAbsInHours()方法计算相差多少小时(绝对值)
|
||||
* 新增DiffInMinutes()方法计算相差多少分钟
|
||||
* 新增DiffAbsInMinutes()方法计算相差多少分钟(绝对值)
|
||||
* 新增DiffInSeconds()方法计算相差多少秒
|
||||
* 新增DiffAbsInSeconds()方法计算相差多少秒(绝对值)
|
||||
|
||||
##### 2020-10-16
|
||||
* 新增Timezone()方法获取时区名
|
||||
* 新增Age()方法获取年龄
|
||||
* 新增Year()方法获取当前年
|
||||
* 新增Month()方法获取当前月
|
||||
|
68
carbon.go
68
carbon.go
@ -11,16 +11,52 @@ type Carbon struct {
|
||||
}
|
||||
|
||||
// Timezone 设置时区
|
||||
func Timezone(name string) Carbon {
|
||||
func SetTimezone(name string) Carbon {
|
||||
return Carbon{loc: getLocalByTimezone(name)}
|
||||
}
|
||||
|
||||
// Timezone 设置时区
|
||||
func (c Carbon) Timezone(name string) Carbon {
|
||||
func (c Carbon) SetTimezone(name string) Carbon {
|
||||
loc := getLocalByTimezone(name)
|
||||
return Carbon{Time: c.Time.In(c.loc), loc: loc}
|
||||
}
|
||||
|
||||
// SetYear 设置年
|
||||
func (c Carbon) SetYear(year int) Carbon {
|
||||
c.Time = time.Date(year, c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 0, c.loc)
|
||||
return c
|
||||
}
|
||||
|
||||
// SetMonth 设置月
|
||||
func (c Carbon) SetMonth(month int) Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), time.Month(month), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 0, c.loc)
|
||||
return c
|
||||
}
|
||||
|
||||
// SetDay 设置日
|
||||
func (c Carbon) SetDay(day int) Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), day, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 0, c.loc)
|
||||
return c
|
||||
}
|
||||
|
||||
// SetHour 设置时
|
||||
func (c Carbon) SetHour(hour int) Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), hour, c.Time.Minute(), c.Time.Second(), 0, c.loc)
|
||||
return c
|
||||
}
|
||||
|
||||
// SetMinute 设置分
|
||||
func (c Carbon) SetMinute(minute int) Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), minute, c.Time.Second(), 0, c.loc)
|
||||
return c
|
||||
}
|
||||
|
||||
// SetSecond 设置秒
|
||||
func (c Carbon) SetSecond(second int) Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), second, 0, c.loc)
|
||||
return c
|
||||
}
|
||||
|
||||
// Now 当前
|
||||
func Now() Carbon {
|
||||
return newCarbon(time.Now())
|
||||
@ -312,7 +348,7 @@ func (c Carbon) NextYears(years int) Carbon {
|
||||
day := c.Time.Day()
|
||||
|
||||
// 获取N年后本月的最后一天
|
||||
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.loc).AddDate(0, 1, -1)
|
||||
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 0, c.loc).AddDate(0, 1, -1)
|
||||
|
||||
if day > last.Day() {
|
||||
day = last.Day()
|
||||
@ -334,7 +370,7 @@ func (c Carbon) NextMonths(months int) Carbon {
|
||||
day := c.Time.Day()
|
||||
|
||||
// 获取N月后的最后一天
|
||||
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), c.Time.Nanosecond(), c.loc).AddDate(0, 1, -1)
|
||||
last := time.Date(year, month, 1, c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 0, c.loc).AddDate(0, 1, -1)
|
||||
|
||||
if day > last.Day() {
|
||||
day = last.Day()
|
||||
@ -369,8 +405,8 @@ func (c Carbon) PreMonth() Carbon {
|
||||
return c.NextMonths(-1)
|
||||
}
|
||||
|
||||
// BeginningOfYear 本年开始时间
|
||||
func (c Carbon) BeginningOfYear() Carbon {
|
||||
// StartOfYear 本年开始时间
|
||||
func (c Carbon) StartOfYear() Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), 1, 1, 0, 0, 0, 0, c.loc)
|
||||
return c
|
||||
}
|
||||
@ -381,8 +417,8 @@ func (c Carbon) EndOfYear() Carbon {
|
||||
return c
|
||||
}
|
||||
|
||||
// BeginningOfMonth 本月开始时间
|
||||
func (c Carbon) BeginningOfMonth() Carbon {
|
||||
// StartOfMonth 本月开始时间
|
||||
func (c Carbon) StartOfMonth() Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), 1, 0, 0, 0, 0, c.loc)
|
||||
return c
|
||||
}
|
||||
@ -394,8 +430,8 @@ func (c Carbon) EndOfMonth() Carbon {
|
||||
return c
|
||||
}
|
||||
|
||||
// BeginningOfWeek 本周开始时间
|
||||
func (c Carbon) BeginningOfWeek() Carbon {
|
||||
// StartOfWeek 本周开始时间
|
||||
func (c Carbon) StartOfWeek() Carbon {
|
||||
days := c.Time.Weekday()
|
||||
if days == 0 {
|
||||
days = DaysPerWeek
|
||||
@ -416,8 +452,8 @@ func (c Carbon) EndOfWeek() Carbon {
|
||||
return c
|
||||
}
|
||||
|
||||
// BeginningOfDay 本日开始时间
|
||||
func (c Carbon) BeginningOfDay() Carbon {
|
||||
// StartOfDay 本日开始时间
|
||||
func (c Carbon) StartOfDay() Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.loc)
|
||||
return c
|
||||
}
|
||||
@ -428,8 +464,8 @@ func (c Carbon) EndOfDay() Carbon {
|
||||
return c
|
||||
}
|
||||
|
||||
// BeginningOfHour 小时开始时间
|
||||
func (c Carbon) BeginningOfHour() Carbon {
|
||||
// StartOfHour 小时开始时间
|
||||
func (c Carbon) StartOfHour() Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 0, 0, 0, c.loc)
|
||||
return c
|
||||
}
|
||||
@ -440,8 +476,8 @@ func (c Carbon) EndOfHour() Carbon {
|
||||
return c
|
||||
}
|
||||
|
||||
// BeginningOfMinute 分钟开始时间
|
||||
func (c Carbon) BeginningOfMinute() Carbon {
|
||||
// StartOfMinute 分钟开始时间
|
||||
func (c Carbon) StartOfMinute() Carbon {
|
||||
c.Time = time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 0, 0, c.loc)
|
||||
return c
|
||||
}
|
||||
|
245
carbon_test.go
245
carbon_test.go
@ -16,7 +16,7 @@ var TimezoneTests = []struct {
|
||||
{"2020-08-05", "Hangzhou", "panic"}, // 异常情况
|
||||
}
|
||||
|
||||
func TestCarbon_Timezone1(t *testing.T) {
|
||||
func TestCarbon_SetTimezone1(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Printf("catch an exception in Timezone():%s\n", r)
|
||||
@ -24,7 +24,7 @@ func TestCarbon_Timezone1(t *testing.T) {
|
||||
}()
|
||||
|
||||
for _, v := range TimezoneTests {
|
||||
output := Timezone(v.timezone).Parse(v.input).ToDateTimeString()
|
||||
output := SetTimezone(v.timezone).Parse(v.input).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -32,7 +32,7 @@ func TestCarbon_Timezone1(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Timezone2(t *testing.T) {
|
||||
func TestCarbon_SetTimezone2(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Printf("catch an exception in Timezone():%s\n", r)
|
||||
@ -40,7 +40,128 @@ func TestCarbon_Timezone2(t *testing.T) {
|
||||
}()
|
||||
|
||||
for _, v := range TimezoneTests {
|
||||
output := Timezone(PRC).Timezone(v.timezone).Parse(v.input).ToDateTimeString()
|
||||
output := SetTimezone(PRC).SetTimezone(v.timezone).Parse(v.input).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetYear(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
year int // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-01-01", 2019, "2019-01-01"},
|
||||
{"2020-01-31", 2019, "2019-01-31"},
|
||||
{"2020-02-01", 2019, "2019-02-01"},
|
||||
{"2020-02-28", 2019, "2019-02-28"},
|
||||
{"2020-02-29", 2019, "2019-03-01"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).SetYear(v.year).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetMonth(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
month int // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-01-01", 2, "2020-02-01"},
|
||||
{"2020-01-30", 2, "2020-03-01"},
|
||||
{"2020-01-31", 2, "2020-03-02"},
|
||||
{"2020-08-05", 2, "2020-02-05"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).SetMonth(v.month).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetDay(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
day int // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-01-01", 31, "2020-01-31"},
|
||||
{"2020-02-01", 31, "2020-03-02"},
|
||||
{"2020-02-28", 31, "2020-03-02"},
|
||||
{"2020-02-29", 31, "2020-03-02"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).SetDay(v.day).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetHour(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
hour int // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-08-05 13:14:15", 10, "2020-08-05 10:14:15"},
|
||||
{"2020-08-05 13:14:15", 24, "2020-08-06 00:14:15"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).SetHour(v.hour).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetMinute(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
minute int // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-08-05 13:14:15", 10, "2020-08-05 13:10:15"},
|
||||
{"2020-08-05 13:14:15", 60, "2020-08-05 14:00:15"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).SetMinute(v.minute).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_SetSecond(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
second int // 输入参数
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"2020-08-05 13:14:15", 10, "2020-08-05 13:14:10"},
|
||||
{"2020-08-05 13:14:15", 60, "2020-08-05 13:15:00"},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).SetSecond(v.second).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -57,7 +178,7 @@ func TestCarbon_Now(t *testing.T) {
|
||||
t.Fatalf("Expected %s, but got %s", expected, output)
|
||||
}
|
||||
|
||||
output = Timezone(PRC).Now().Time.Format(DateTimeFormat)
|
||||
output = SetTimezone(PRC).Now().Time.Format(DateTimeFormat)
|
||||
|
||||
if expected != output {
|
||||
t.Fatalf("Expected %s, but got %s", expected, output)
|
||||
@ -74,7 +195,7 @@ func TestCarbon_Yesterday(t *testing.T) {
|
||||
t.Fatalf("Expected %s, but got %s", expected, output)
|
||||
}
|
||||
|
||||
output = Timezone(PRC).Yesterday().Time.Format(DateTimeFormat)
|
||||
output = SetTimezone(PRC).Yesterday().Time.Format(DateTimeFormat)
|
||||
|
||||
if expected != output {
|
||||
t.Fatalf("Expected %s, but got %s", expected, output)
|
||||
@ -90,14 +211,14 @@ func TestCarbon_Tomorrow(t *testing.T) {
|
||||
t.Fatalf("Expected %s, but got %s", expected, output)
|
||||
}
|
||||
|
||||
output = Timezone(PRC).Tomorrow().Time.Format(DateTimeFormat)
|
||||
output = SetTimezone(PRC).Tomorrow().Time.Format(DateTimeFormat)
|
||||
|
||||
if expected != output {
|
||||
t.Fatalf("Expected %s, but got %s", expected, output)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BeginningOfYear(t *testing.T) {
|
||||
func TestCarbon_StartOfYear(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
@ -110,7 +231,7 @@ func TestCarbon_BeginningOfYear(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).BeginningOfYear().ToDateTimeString()
|
||||
output := Parse(v.input).StartOfYear().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -139,7 +260,7 @@ func TestCarbon_EndOfYear(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BeginningOfMonth(t *testing.T) {
|
||||
func TestCarbon_StartOfMonth(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
@ -152,7 +273,7 @@ func TestCarbon_BeginningOfMonth(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).BeginningOfMonth().ToDateTimeString()
|
||||
output := Parse(v.input).StartOfMonth().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -181,7 +302,7 @@ func TestCarbon_EndOfMonth(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BeginningOfWeek(t *testing.T) {
|
||||
func TestCarbon_StartOfWeek(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
@ -195,7 +316,7 @@ func TestCarbon_BeginningOfWeek(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).BeginningOfWeek().ToDateTimeString()
|
||||
output := Parse(v.input).StartOfWeek().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -225,7 +346,7 @@ func TestCarbon_EndOfWeek(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BeginningOfDay(t *testing.T) {
|
||||
func TestCarbon_StartOfDay(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
@ -238,7 +359,7 @@ func TestCarbon_BeginningOfDay(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).BeginningOfDay().ToDateTimeString()
|
||||
output := Parse(v.input).StartOfDay().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -267,7 +388,7 @@ func TestCarbon_EndOfDay(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BeginningOfHour(t *testing.T) {
|
||||
func TestCarbon_StartOfHour(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
@ -280,7 +401,7 @@ func TestCarbon_BeginningOfHour(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).BeginningOfHour().ToDateTimeString()
|
||||
output := Parse(v.input).StartOfHour().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -309,7 +430,7 @@ func TestCarbon_EndOfHour(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_BeginningOfMinute(t *testing.T) {
|
||||
func TestCarbon_StartOfMinute(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
@ -322,7 +443,7 @@ func TestCarbon_BeginningOfMinute(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).BeginningOfMinute().ToDateTimeString()
|
||||
output := Parse(v.input).StartOfMinute().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
@ -372,7 +493,7 @@ func TestCarbon_CreateFromTimestamp(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).CreateFromTimestamp(v.timestamp).ToDateTimeString()
|
||||
output := SetTimezone(PRC).CreateFromTimestamp(v.timestamp).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Expected %s, but got %s", v.output, output)
|
||||
@ -401,7 +522,7 @@ func TestCarbon_CreateFromDateTime(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).CreateFromDateTime(v.year, v.month, v.day, v.hour, v.minute, v.second).ToDateTimeString()
|
||||
output := SetTimezone(PRC).CreateFromDateTime(v.year, v.month, v.day, v.hour, v.minute, v.second).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Expected %s, but got %s", v.output, output)
|
||||
@ -432,7 +553,7 @@ func TestCarbon_CreateFromDate(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).CreateFromDate(v.year, v.month, v.day).ToDateTimeString()
|
||||
output := SetTimezone(PRC).CreateFromDate(v.year, v.month, v.day).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Expected %s, but got %s", v.output, output)
|
||||
@ -462,7 +583,7 @@ func TestCarbon_CreateFromTime(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).CreateFromTime(v.hour, v.minute, v.second).ToDateTimeString()
|
||||
output := SetTimezone(PRC).CreateFromTime(v.hour, v.minute, v.second).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Expected %s, but got %s", v.output, output)
|
||||
@ -488,7 +609,7 @@ func TestCarbon_CreateFromGoTime(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).CreateFromGoTime(v.time).ToDateTimeString()
|
||||
output := SetTimezone(PRC).CreateFromGoTime(v.time).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Expected %s, but got %s", v.output, output)
|
||||
@ -501,6 +622,10 @@ func TestCarbon_Parse(t *testing.T) {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{"", ""},
|
||||
{"0", ""},
|
||||
{"0000-00-00", ""},
|
||||
{"0000-00-00 00:00:00", ""},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:15"},
|
||||
{"20200805131415", "2020-08-05 13:14:15"},
|
||||
{"20200805", "2020-08-05 00:00:00"},
|
||||
@ -524,7 +649,7 @@ func TestCarbon_Parse(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -566,7 +691,7 @@ func TestCarbon_ParseByFormat2(t *testing.T) {
|
||||
}()
|
||||
|
||||
for _, v := range ParseByFormatTests {
|
||||
output := Timezone(PRC).ParseByFormat(v.input, v.format).ToDateTimeString()
|
||||
output := SetTimezone(PRC).ParseByFormat(v.input, v.format).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -621,7 +746,7 @@ func TestCarbon_ParseByDuration2(t *testing.T) {
|
||||
}()
|
||||
|
||||
for _, v := range ParseByDurationTests {
|
||||
output := Timezone(PRC).ParseByDuration(v.duration).ToDateTimeString()
|
||||
output := SetTimezone(PRC).ParseByDuration(v.duration).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -668,7 +793,7 @@ func TestCarbon_Duration(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).Duration(v.duration).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).Duration(v.duration).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -698,7 +823,7 @@ func TestCarbon_AddYears(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddYears(v.years).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddYears(v.years).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -728,7 +853,7 @@ func TestCarbon_NextYears(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).NextYears(v.years).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).NextYears(v.years).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -760,7 +885,7 @@ func TestCarbon_SubYears(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubYears(v.years).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubYears(v.years).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -790,7 +915,7 @@ func TestCarbon_PreYears(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).PreYears(v.years).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).PreYears(v.years).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -819,7 +944,7 @@ func TestCarbon_AddYear(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddYear().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddYear().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -848,7 +973,7 @@ func TestCarbon_NextYear(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).NextYear().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).NextYear().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -877,7 +1002,7 @@ func TestCarbon_SubYear(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubYear().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubYear().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -906,7 +1031,7 @@ func TestCarbon_PreYear(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).PreYear().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).PreYear().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -936,7 +1061,7 @@ func TestCarbon_AddMonths(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddMonths(v.months).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddMonths(v.months).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -966,7 +1091,7 @@ func TestCarbon_NextMonths(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).NextMonths(v.months).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).NextMonths(v.months).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -996,7 +1121,7 @@ func TestCarbon_SubMonths(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubMonths(v.months).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubMonths(v.months).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1026,7 +1151,7 @@ func TestCarbon_PreMonths(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).PreMonths(v.months).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).PreMonths(v.months).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1055,7 +1180,7 @@ func TestCarbon_AddMonth(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddMonth().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddMonth().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1084,7 +1209,7 @@ func TestCarbon_NextMonth(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).NextMonth().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).NextMonth().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1113,7 +1238,7 @@ func TestCarbon_SubMonth(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubMonth().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubMonth().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1142,7 +1267,7 @@ func TestCarbon_PreMonth(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).PreMonth().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).PreMonth().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1172,7 +1297,7 @@ func TestCarbon_AddDays(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddDays(v.days).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddDays(v.days).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1202,7 +1327,7 @@ func TestCarbon_SubDays(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubDays(v.days).ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubDays(v.days).ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1231,7 +1356,7 @@ func TestCarbon_AddDay(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddDay().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddDay().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1260,7 +1385,7 @@ func TestCarbon_SubDay(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubDay().ToDateString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubDay().ToDateString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1290,7 +1415,7 @@ func TestCarbon_AddHours(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddHours(v.hours).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddHours(v.hours).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1320,7 +1445,7 @@ func TestCarbon_SubHours(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubHours(v.hours).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubHours(v.hours).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1349,7 +1474,7 @@ func TestCarbon_AddHour(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddHour().ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddHour().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1378,7 +1503,7 @@ func TestCarbon_SubHour(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubHour().ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubHour().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1408,7 +1533,7 @@ func TestCarbon_AddMinutes(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddMinutes(v.minutes).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddMinutes(v.minutes).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1438,7 +1563,7 @@ func TestCarbon_SubMinutes(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubMinutes(v.minutes).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubMinutes(v.minutes).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1467,7 +1592,7 @@ func TestCarbon_AddMinute(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddMinute().ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddMinute().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1496,7 +1621,7 @@ func TestCarbon_SubMinute(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubMinute().ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubMinute().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1526,7 +1651,7 @@ func TestCarbon_AddSeconds(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddSeconds(v.seconds).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddSeconds(v.seconds).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1556,7 +1681,7 @@ func TestCarbon_SubSeconds(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubSeconds(v.seconds).ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubSeconds(v.seconds).ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1585,7 +1710,7 @@ func TestCarbon_AddSecond(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).AddSecond().ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).AddSecond().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
@ -1614,7 +1739,7 @@ func TestCarbon_SubSecond(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Timezone(PRC).Parse(v.input).SubSecond().ToDateTimeString()
|
||||
output := SetTimezone(PRC).Parse(v.input).SubSecond().ToDateTimeString()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, v.output, output)
|
||||
|
8
const.go
8
const.go
@ -4,7 +4,7 @@ import "time"
|
||||
|
||||
// 版权信息
|
||||
const (
|
||||
Version = "v1.2.1"
|
||||
Version = "v1.2.2"
|
||||
Author = "gouguoyin"
|
||||
Email = "mail@gouguoyin.cn"
|
||||
Blog = "www.gouguoyin.cn"
|
||||
@ -84,7 +84,8 @@ const (
|
||||
QuartersPerYear = 4 // 每年4季度
|
||||
MonthsPerYear = 12 // 每年12月
|
||||
MonthsPerQuarter = 3 // 每季度3月
|
||||
WeeksPerYear = 52 // 每年52周
|
||||
WeeksPerNormalYear = 52 // 每常规年52周
|
||||
weeksPerLongYear = 53 // 每长年53周
|
||||
WeeksPerMonth = 4 // 每月4周
|
||||
DaysPerLeapYear = 366 // 每闰年366天
|
||||
DaysPerNormalYear = 365 // 每常规年365天
|
||||
@ -93,8 +94,9 @@ const (
|
||||
HoursPerDay = 24 // 每天24小时
|
||||
MinutesPerDay = 1440 // 每天1440分钟
|
||||
MinutesPerHour = 60 // 每小时60分钟
|
||||
SecondsPerWeek = 691200 // 每周691200秒
|
||||
SecondsPerWeek = 604800 // 每周604800秒
|
||||
SecondsPerDay = 86400 // 每天86400秒
|
||||
SecondsPerHour = 3600 // 每小时3600秒
|
||||
SecondsPerMinute = 60 // 每分钟60秒
|
||||
MillisecondsPerSecond = 1000 // 每秒1000毫秒
|
||||
MicrosecondsPerMillisecond = 1000 // 每毫秒1000微秒
|
||||
|
101
final.go
101
final.go
@ -1,6 +1,8 @@
|
||||
package carbon
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// ToString 输出字符串
|
||||
func (c Carbon) ToString() string {
|
||||
@ -193,6 +195,86 @@ func (c Carbon) ToRFC7231String() string {
|
||||
return c.Time.Format(RFC7231Format)
|
||||
}
|
||||
|
||||
// DiffInWeeks 相差多少周
|
||||
func (start Carbon) DiffInWeeks(end Carbon) int64 {
|
||||
return start.DiffInSeconds(end) / SecondsPerWeek
|
||||
}
|
||||
|
||||
// DiffAbsInWeeks 相差多少周(绝对值)
|
||||
func (start Carbon) DiffAbsInWeeks(end Carbon) int64 {
|
||||
diff := start.DiffInWeeks(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// DiffInDays 相差多少天
|
||||
func (start Carbon) DiffInDays(end Carbon) int64 {
|
||||
return start.DiffInSeconds(end) / SecondsPerDay
|
||||
}
|
||||
|
||||
// DiffAbsInDays 相差多少天(绝对值)
|
||||
func (start Carbon) DiffAbsInDays(end Carbon) int64 {
|
||||
diff := start.DiffInDays(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// DiffInHours 相差多少小时
|
||||
func (start Carbon) DiffInHours(end Carbon) int64 {
|
||||
return start.DiffInSeconds(end) / SecondsPerHour
|
||||
}
|
||||
|
||||
// DiffAbsInHours 相差多少小时(绝对值)
|
||||
func (start Carbon) DiffAbsInHours(end Carbon) int64 {
|
||||
diff := start.DiffInHours(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// DiffInMinutes 相差多少分钟
|
||||
func (start Carbon) DiffInMinutes(end Carbon) int64 {
|
||||
return start.DiffInSeconds(end) / SecondsPerMinute
|
||||
}
|
||||
|
||||
// DiffAbsInMinutes 相差多少分钟(绝对值)
|
||||
func (start Carbon) DiffAbsInMinutes(end Carbon) int64 {
|
||||
diff := start.DiffInMinutes(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// DiffInSeconds 相差多少秒
|
||||
func (start Carbon) DiffInSeconds(end Carbon) int64 {
|
||||
if start.Time.IsZero() && end.Time.IsZero() {
|
||||
return 0
|
||||
}
|
||||
if end.Time.IsZero() {
|
||||
return -start.ToTimestamp()
|
||||
}
|
||||
if start.Time.IsZero() {
|
||||
return end.ToTimestamp()
|
||||
}
|
||||
|
||||
return end.ToTimestamp() - start.ToTimestamp()
|
||||
}
|
||||
|
||||
// DiffAbsInSeconds 相差多少秒(绝对值)
|
||||
func (start Carbon) DiffAbsInSeconds(end Carbon) int64 {
|
||||
diff := start.DiffInSeconds(end)
|
||||
if diff < 0 {
|
||||
diff = -diff
|
||||
}
|
||||
return diff
|
||||
}
|
||||
|
||||
// DaysInYear 获取本年的总天数
|
||||
func (c Carbon) DaysInYear() int {
|
||||
if c.Time.IsZero() {
|
||||
@ -266,13 +348,21 @@ func (c Carbon) WeekOfMonth() int {
|
||||
return day%DaysPerWeek + 1
|
||||
}
|
||||
|
||||
// Timezone 获取时区
|
||||
func (c Carbon) Timezone() string {
|
||||
return c.loc.String()
|
||||
}
|
||||
|
||||
// Age 获取年龄
|
||||
func (c Carbon) Age() int {
|
||||
if c.Time.IsZero() {
|
||||
return 0
|
||||
}
|
||||
if c.ToTimestamp() > Now().ToTimestamp() {
|
||||
return 0
|
||||
}
|
||||
age := time.Now().Year() - c.Time.Year()
|
||||
if int(time.Now().Month())*10+time.Now().Day() < int(c.Time.Month())*10+c.Time.Day() {
|
||||
if int(time.Now().Month())*100+time.Now().Day() < int(c.Time.Month())*100+c.Time.Day() {
|
||||
age = age - 1
|
||||
}
|
||||
return age
|
||||
@ -355,6 +445,13 @@ func (c Carbon) IsLeapYear() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsLongYear 是否是长年
|
||||
func (c Carbon) IsLongYear() bool {
|
||||
t := time.Date(c.Year(), time.December, 31, 0, 0, 0, 0, c.loc)
|
||||
_, w := t.ISOWeek()
|
||||
return w == weeksPerLongYear
|
||||
}
|
||||
|
||||
// IsJanuary 是否是一月
|
||||
func (c Carbon) IsJanuary() bool {
|
||||
return c.Time.In(c.loc).Month() == time.January
|
||||
|
347
final_test.go
347
final_test.go
@ -493,6 +493,300 @@ func TestCarbon_ToRFC7231String(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffInWeeks(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", -2639},
|
||||
{"", "2020-08-05 13:14:15", 2639},
|
||||
{"2020-08-05 13:14:15", "2020-07-28 13:14:00", -1},
|
||||
{"2020-08-05 13:14:15", "2020-07-28 13:14:15", -1},
|
||||
{"2020-08-05 13:14:15", "2020-07-28 13:14:59", -1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-12 13:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-12 13:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-12 13:14:59", 1},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffInWeeks(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInWeeks(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", 2639},
|
||||
{"", "2020-08-05 13:14:15", 2639},
|
||||
{"2020-08-05 13:14:15", "2020-07-28 13:14:00", 1},
|
||||
{"2020-08-05 13:14:15", "2020-07-28 13:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-07-28 13:14:59", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-12 13:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-12 13:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-12 13:14:59", 1},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInWeeks(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffInDays(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", -18479},
|
||||
{"", "2020-08-05 13:14:15", 18479},
|
||||
{"2020-08-05 13:14:15", "2020-08-04 13:00:00", -1},
|
||||
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", -1},
|
||||
{"2020-08-05 13:14:15", "2020-08-04 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:00:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-06 13:00:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-06 13:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-06 13:14:59", 1},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffInDays(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInDays(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", 18479},
|
||||
{"", "2020-08-05 13:14:15", 18479},
|
||||
{"2020-08-05 13:14:15", "2020-08-04 13:00:00", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-04 13:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-04 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:00:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-06 13:00:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-06 13:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-06 13:14:59", 1},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInDays(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffInHours(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", -443501},
|
||||
{"", "2020-08-05 13:14:15", 443501},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 12:14:00", -1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 12:14:15", -1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 12:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 14:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 14:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 14:14:59", 1},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffInHours(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInHours(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", 443501},
|
||||
{"", "2020-08-05 13:14:15", 443501},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 12:14:00", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 12:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 12:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:15", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:14:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 14:14:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 14:14:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 14:14:59", 1},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInHours(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffInMinutes(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:13:00", -1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:13:15", -1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:13:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:15:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:15:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:15:59", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:16:00", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:16:15", 2},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:16:59", 2},
|
||||
{"2020-08-05 13:14:15", "", -26610074},
|
||||
{"", "2010-08-05 13:14:15", 21349754},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffInMinutes(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInMinutes(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:13:00", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:13:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:13:59", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:15:00", 0},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:15:15", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:15:59", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:16:00", 1},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:16:15", 2},
|
||||
{"2020-08-05 13:14:15", "2020-08-05 13:16:59", 2},
|
||||
{"2020-08-05 13:14:15", "", 26610074},
|
||||
{"", "2010-08-05 13:14:15", 21349754},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInMinutes(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffInSeconds(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", -1596604455},
|
||||
{"", "2010-08-05 13:14:15", 1280985255},
|
||||
{"2020-08-05 13:14:15", "2010-08-05 13:14:15", -315619200},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffInSeconds(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DiffAbsInSeconds(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input1 string // 输入值1
|
||||
input2 string // 输入值2
|
||||
output int64 // 期望输出值
|
||||
}{
|
||||
{"0000-00-00 00:00:00", "", 0},
|
||||
{"", "0000-00-00 00:00:00", 0},
|
||||
{"", "", 0},
|
||||
{"2020-08-05 13:14:15", "", 1596604455},
|
||||
{"", "2010-08-05 13:14:15", 1280985255},
|
||||
{"2020-08-05 13:14:15", "2010-08-05 13:14:15", 315619200},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input1).DiffAbsInSeconds(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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_DaysInYear(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
@ -676,13 +970,35 @@ func TestCarbon_WeekOfMonth(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Timezone(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output string // 期望输出值
|
||||
}{
|
||||
{PRC, PRC},
|
||||
{Tokyo, Tokyo},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := SetTimezone(v.input).Timezone()
|
||||
|
||||
if output != v.output {
|
||||
t.Fatalf("Input %s, expected %s, but got %s", v.input, v.output, output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_Age(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output int // 期望输出值
|
||||
}{
|
||||
{"", 0},
|
||||
{"0", 0},
|
||||
{"0000-00-00", 0},
|
||||
{Now().AddYears(18).ToDateTimeString(), 0},
|
||||
{Now().SubYears(18).ToDateTimeString(), 18},
|
||||
{CreateFromDate(Now().Year(), 12, 31).SubYears(18).ToDateTimeString(), 17},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
@ -964,6 +1280,37 @@ func TestCarbon_IsLeapYear(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_IsLongYear(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
output bool // 期望输出值
|
||||
}{
|
||||
{"2015-01-01", true},
|
||||
{"2016-01-01", false},
|
||||
{"2017-01-01", false},
|
||||
{"2018-01-01", false},
|
||||
{"2019-01-01", false},
|
||||
{"2020-01-01", true},
|
||||
}
|
||||
|
||||
for _, v := range Tests {
|
||||
output := Parse(v.input).IsLongYear()
|
||||
|
||||
if output != v.output {
|
||||
expected := "false"
|
||||
if v.output == true {
|
||||
expected = "true"
|
||||
}
|
||||
|
||||
reality := "false"
|
||||
if output == true {
|
||||
reality = "true"
|
||||
}
|
||||
t.Fatalf("Input %s, expected %s, but got %s\n", v.input, expected, reality)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCarbon_IsJanuary(t *testing.T) {
|
||||
Tests := []struct {
|
||||
input string // 输入值
|
||||
|
Loading…
Reference in New Issue
Block a user