This commit is contained in:
Peleus 2024-01-12 10:57:10 +08:00
parent d59dd04c56
commit f3a30386cf
22 changed files with 624 additions and 171 deletions

View File

@ -52,17 +52,18 @@ import "gitee.com/golang-module/carbon"
> 假设当前时间为 2020-08-05 13:14:15.999999999 +0800 CST
##### 设置全局默认值
##### 设置默认值(全局生效)
```go
carbon.SetDefault(carbon.Default{
Layout: carbon.RFC3339Layout,
Timezone: carbon.PRC,
WeekStartsAt: carbon.Sunday,
Locale: "zh-CN",
})
```
> 如果没有设置,默认布局模板是 `2006-01-02 15:04:05`, 默认时区是 `Local`, 默认语言是 `en`
> 如果没有设置,默认布局模板是 `2006-01-02 15:04:05`, 默认时区是 `Local`, 默认一周开始日期是 `Sunday(周日)`, 默认语言是 `en`
##### 昨天、今天、明天
@ -555,20 +556,21 @@ carbon.Parse("2022-08-05 13:14:15").DiffForHumans(carbon.Now()) // 2 years after
##### 时间极值
```go
c := carbon.Parse("2023-04-01")
c0 := carbon.Parse("xxx")
c0 := carbon.Parse("2023-04-01")
c1 := carbon.Parse("2023-03-28")
c2 := carbon.Parse("2023-04-16")
// 返回最近的 Carbon 实例
c.Closest(c0, c1) // c1
c.Closest(c0, c2) // c2
c.Closest(c1, c2) // c1
c0.Closest(c1, c2) // c1
// 返回最远的 Carbon 实例
c.Farthest(c0, c1) // c1
c.Farthest(c0, c2) // c2
c.Farthest(c1, c2) // c2
c0.Farthest(c1, c2) // c2
yesterday := carbon.Yesterday()
today := carbon.Now()
tomorrow := carbon.Tomorrow()
// 返回最大的 Carbon 实例
carbon.Max(yesterday, today, tomorrow) // tomorrow
// 返回最小的 Carbon 实例
carbon.Min(yesterday, today, tomorrow) // yesterday
```
##### 时间判断
@ -613,6 +615,17 @@ carbon.Parse("2020-08-05 00:00:00").IsInvalid() // false
carbon.Parse("2020-08-05").IsInvalid() // false
carbon.Parse("2020-08-05").SetTimezone("xxx").IsInvalid() // true
// 是否是上午
carbon.Parse("2020-08-05 00:00:00").IsAM() // true
carbon.Parse("2020-08-05 08:00:00").IsAM() // true
carbon.Parse("2020-08-05 12:00:00").IsAM() // false
carbon.Parse("2020-08-05 13:00:00").IsAM() // false
// 是否是下午
carbon.Parse("2020-08-05 00:00:00").IsPM() // false
carbon.Parse("2020-08-05 08:00:00").IsPM() // false
carbon.Parse("2020-08-05 12:00:00").IsPM() // true
carbon.Parse("2020-08-05 13:00:00").IsPM() // true
// 是否是当前时间
carbon.Now().IsNow() // true
// 是否是未来时间

View File

@ -56,17 +56,18 @@ import "gitee.com/golang-module/carbon"
> 現在時刻が 2020-08-05 13:14:15.999999999 +0800 CST であると仮定します。
##### グローバルデフォルトを設定する
##### デフォルト値の設定 (グローバルに有効)
```go
carbon.SetDefault(carbon.Default{
Layout: carbon.RFC3339Layout,
Timezone: carbon.PRC,
WeekStartsAt: carbon.Sunday,
Locale: "jp",
})
```
> 設定されていない場合、デフォルトのレイアウト テンプレートは `2006-01-02 15:04:05`、デフォルトのタイム ゾーンは `Local`、デフォルトの言語は `en` になります。
> 設定されていない場合,デフォルトのレイアウト テンプレートは `2006-01-02 15:04:05`,デフォルトのタイムゾーンは `Local`,デフォルトの週の開始日は `日曜日`,デフォルトの言語は `en`す。
##### 昨日、今日、明日
@ -555,20 +556,21 @@ carbon.Parse("2022-08-05 13:14:15").DiffForHumans(carbon.Now()) // 2 years after
##### 时间极值
```go
c := carbon.Parse("2023-04-01")
c0 := carbon.Parse("xxx")
c0 := carbon.Parse("2023-04-01")
c1 := carbon.Parse("2023-03-28")
c2 := carbon.Parse("2023-04-16")
// 最近のCarbonインスタンスを返す
c.Closest(c0, c1) // c1
c.Closest(c0, c2) // c2
c.Closest(c1, c2) // c1
c0.Closest(c1, c2) // c1
// 最も遠いCarbonインスタンスを返す
c.Farthest(c0, c1) // c1
c.Farthest(c0, c2) // c2
c.Farthest(c1, c2) // c2
c0.Farthest(c1, c2) // c2
yesterday := carbon.Yesterday()
today := carbon.Now()
tomorrow := carbon.Tomorrow()
// 最大の Carbon インスタンスを返します
carbon.Max(yesterday, today, tomorrow) // tomorrow
// 最小の Carbon インスタンスを返します
carbon.Min(yesterday, today, tomorrow) // yesterday
```
##### 时间比較
@ -613,6 +615,17 @@ carbon.Parse("2020-08-05 00:00:00").IsInvalid() // false
carbon.Parse("2020-08-05").IsInvalid() // false
carbon.Parse("2020-08-05").SetTimezone("xxx").IsInvalid() // true
// 朝かどうかを判断する
carbon.Parse("2020-08-05 00:00:00").IsAM() // true
carbon.Parse("2020-08-05 08:00:00").IsAM() // true
carbon.Parse("2020-08-05 12:00:00").IsAM() // false
carbon.Parse("2020-08-05 13:00:00").IsAM() // false
// 午後かどうかを判断します
carbon.Parse("2020-08-05 00:00:00").IsPM() // false
carbon.Parse("2020-08-05 08:00:00").IsPM() // false
carbon.Parse("2020-08-05 12:00:00").IsPM() // true
carbon.Parse("2020-08-05 13:00:00").IsPM() // true
// 現在かどうか
carbon.Now().IsNow() // true
// 未来かどうか

View File

@ -12,7 +12,7 @@ English | [简体中文](README.cn.md) | [日本語](README.jp.md)
#### Introduction
A simple, semantic and developer-friendly golang package for datetime, has been included
A simple, semantic and developer-friendly golang package for time, has been included
by [awesome-go](https://github.com/avelino/awesome-go#date-and-time "awesome-go")
[github.com/golang-module/carbon](https://github.com/golang-module/carbon "github.com/golang-module/carbon")
@ -55,17 +55,18 @@ import "gitee.com/golang-module/carbon"
> Assuming the current time is 2020-08-05 13:14:15.999999999 +0800 CST
##### Set global default
##### Set default values (globally effective)
```go
carbon.SetDefault(carbon.Default{
Layout: carbon.RFC3339Layout,
Timezone: carbon.PRC,
Layout: carbon.DateTimeLayout,
Timezone: carbon.Local,
WeekStartsAt: carbon.Sunday,
Locale: "en",
})
```
> If not set, the default layout is `2006-01-02 15:04:05`, the default timezone is `Local`, and the default language locale is `en`
> If not set, the default layout is `2006-01-02 15:04:05`, the default timezone is `Local`, the default week start date is `Sunday` and the default language locale is `en`
##### Yesterday, today and tomorrow
@ -552,20 +553,21 @@ carbon.Parse("2022-08-05 13:14:15").DiffForHumans(carbon.Now()) // 2 years after
##### Extremum
```go
c := carbon.Parse("2023-04-01")
c0 := carbon.Parse("xxx")
c0 := carbon.Parse("2023-04-01")
c1 := carbon.Parse("2023-03-28")
c2 := carbon.Parse("2023-04-16")
// Return the closest Carbon instance
c.Closest(c0, c1) // c1
c.Closest(c0, c2) // c2
c.Closest(c1, c2) // c1
c0.Closest(c1, c2) // c1
// Return the farthest Carbon instance
c.Farthest(c0, c1) // c1
c.Farthest(c0, c2) // c2
c.Farthest(c1, c2) // c2
c0.Farthest(c1, c2) // c2
yesterday := carbon.Yesterday()
today := carbon.Now()
tomorrow := carbon.Tomorrow()
// Return the maximum Carbon instance
carbon.Max(yesterday, today, tomorrow) // tomorrow
// Return the minimum Carbon instance
carbon.Min(yesterday, today, tomorrow) // yesterday
```
##### Comparison
@ -610,6 +612,17 @@ carbon.Parse("2020-08-05 00:00:00").IsInvalid() // false
carbon.Parse("2020-08-05").IsInvalid() // false
carbon.Parse("2020-08-05").SetTimezone("xxx").IsInvalid() // true
// Whether is before noon
carbon.Parse("2020-08-05 00:00:00").IsAM() // true
carbon.Parse("2020-08-05 08:00:00").IsAM() // true
carbon.Parse("2020-08-05 12:00:00").IsAM() // false
carbon.Parse("2020-08-05 13:00:00").IsAM() // false
// Whether is after noon
carbon.Parse("2020-08-05 00:00:00").IsPM() // false
carbon.Parse("2020-08-05 08:00:00").IsPM() // false
carbon.Parse("2020-08-05 12:00:00").IsPM() // true
carbon.Parse("2020-08-05 13:00:00").IsPM() // true
// Whether is now time
carbon.Now().IsNow() // true
// Whether is future time
@ -1471,7 +1484,7 @@ The following methods are supported
* `Constellation()`get constellation name, like `Aries`
* `Season()`get season name, like `Spring`
* `DiffForHumans()`get the difference with human-readable format, like `1 year from now`
* `DiffForHumans()`get the difference with human-readable format string, like `1 year from now`
* `ToMonthString()`output month format string, like `January`
* `ToShortMonthString()`output short month format string, like `Jan`
* `ToWeekString()`output week format string, like `Sunday`

View File

@ -14,7 +14,7 @@ import (
// Version current version
// 当前版本号
const Version = "2.3.4"
const Version = "2.3.5"
// timezone constants
// 时区常量
@ -53,14 +53,22 @@ const (
Macao = "Asia/Macao" // 澳门
Taipei = "Asia/Taipei" // 台北
Tokyo = "Asia/Tokyo" // 东京
HoChiMinh = "Asia/Ho_Chi_Minh" // 胡志明
Hanoi = "Asia/Hanoi" // 河内
Saigon = "Asia/Saigon" // 西贡
Seoul = "Asia/Seoul" // 首尔
Pyongyang = "Asia/Pyongyang" // 平壤
Bangkok = "Asia/Bangkok" // 曼谷
Dubai = "Asia/Dubai" // 迪拜
India = "Asia/Kolkata" // 印度
Qatar = "Asia/Qatar" // 卡塔尔
Bangalore = "Asia/Bangalore" // 班加罗尔
Kolkata = "Asia/Kolkata" // 加尔各答
Mumbai = "Asia/Mumbai" // 孟买
MexicoCity = "America/Mexico_City" // 墨西哥
NewYork = "America/New_York" // 纽约
LosAngeles = "America/Los_Angeles" // 洛杉矶
Chicago = "America/Chicago" // 芝加哥
SaoPaulo = "America/Sao_Paulo" // 圣保罗
Moscow = "Europe/Moscow" // 莫斯科
London = "Europe/London" // 伦敦
Berlin = "Europe/Berlin" // 柏林
@ -256,9 +264,10 @@ type Carbon struct {
// NewCarbon returns a new Carbon instance.
// 初始化 Carbon 结构体
func NewCarbon() Carbon {
c := Carbon{weekStartsAt: time.Sunday, lang: NewLanguage()}
c := Carbon{lang: NewLanguage()}
c.loc, c.Error = getLocationByTimezone(defaultTimezone)
c.lang.rw.Lock()
defer c.lang.rw.Unlock()
if weekday, ok := weekdays[defaultWeekStartsAt]; ok {
c.weekStartsAt = weekday
}
return c
}

View File

@ -31,6 +31,18 @@ func (c Carbon) IsInvalid() bool {
return !c.IsValid()
}
// IsAM reports whether is before noon.
// 是否是上午
func (c Carbon) IsAM() bool {
return c.Format("a") == "am"
}
// IsPM reports whether is after noon.
// 是否是下午
func (c Carbon) IsPM() bool {
return c.Format("a") == "pm"
}
// IsNow reports whether is now time.
// 是否是当前时间
func (c Carbon) IsNow() bool {

View File

@ -42,6 +42,20 @@ func BenchmarkCarbon_IsInvalid(b *testing.B) {
}
}
func BenchmarkCarbon_IsAM(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
now.IsAM()
}
}
func BenchmarkCarbon_IsPM(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
now.IsPM()
}
}
func BenchmarkCarbon_IsNow(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {

View File

@ -108,6 +108,58 @@ func TestCarbon_IsInvalid(t *testing.T) {
}
}
func TestCarbon_IsAM(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string
expected bool
}{
0: {"", false},
1: {"0", false},
2: {"0000-00-00", false},
3: {"00:00:00", false},
4: {"0000-00-00 00:00:00", false},
5: {"2020-08-05 00:00:00", true},
6: {"2020-08-05 08:00:00", true},
7: {"2020-08-05 12:00:00", false},
8: {"2020-08-05 13:00:00", false},
}
for index, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsAM(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsPM(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string
expected bool
}{
0: {"", false},
1: {"0", false},
2: {"0000-00-00", false},
3: {"00:00:00", false},
4: {"0000-00-00 00:00:00", false},
5: {"2020-08-05 00:00:00", false},
6: {"2020-08-05 08:00:00", false},
7: {"2020-08-05 12:00:00", true},
8: {"2020-08-05 13:00:00", true},
}
for index, test := range tests {
c := Parse(test.input)
assert.Nil(c.Error)
assert.Equal(test.expected, c.IsPM(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_IsNow(t *testing.T) {
assert := assert.New(t)

View File

@ -9,17 +9,22 @@ var (
// 默认时区
defaultTimezone = Local
// default locale
// 默认区域
// default week start date
// 默认一周开始日期
defaultWeekStartsAt = Sunday
// default language locale
// 默认语言区域
defaultLocale = "en"
)
// Default defines a Default struct.
// 定义 Default 结构体
type Default struct {
Layout string
Timezone string
Locale string
Layout string
Timezone string
WeekStartsAt string
Locale string
}
// SetDefault sets default.
@ -31,6 +36,9 @@ func SetDefault(d Default) {
if d.Timezone != "" {
defaultTimezone = d.Timezone
}
if d.WeekStartsAt != "" {
defaultWeekStartsAt = d.WeekStartsAt
}
if d.Locale != "" {
defaultLocale = d.Locale
}

View File

@ -4,9 +4,10 @@ import "testing"
func BenchmarkCarbon_SetDefault(b *testing.B) {
d := Default{
Layout: DateTimeLayout,
Timezone: Local,
Locale: "en",
Layout: DateTimeLayout,
Timezone: Local,
Locale: "en",
WeekStartsAt: Sunday,
}
for n := 0; n < b.N; n++ {
SetDefault(d)

View File

@ -1,17 +1,20 @@
package carbon
import (
"github.com/stretchr/testify/assert"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCarbon_SetDefault(t *testing.T) {
SetDefault(Default{
Layout: DateTimeLayout,
Timezone: Local,
Locale: "en",
Layout: DateTimeLayout,
Timezone: Local,
Locale: "en",
WeekStartsAt: Sunday,
})
assert.Equal(t, DateTimeLayout, defaultLayout)
assert.Equal(t, Local, defaultTimezone)
assert.Equal(t, "en", defaultLocale)
assert.Equal(t, "Sunday", defaultWeekStartsAt)
}

View File

@ -1,6 +1,6 @@
package carbon
// Closest returns the closest Carbon instance from the given Carbon instance (second-precision).
// Closest returns the closest Carbon instance from the given Carbon instance.
// 返回离给定 carbon 实例最近的 Carbon 实例
func (c Carbon) Closest(c1 Carbon, c2 Carbon) Carbon {
if c1.IsZero() || c1.IsInvalid() {
@ -15,7 +15,7 @@ func (c Carbon) Closest(c1 Carbon, c2 Carbon) Carbon {
return c2
}
// Farthest returns the farthest Carbon instance from the given Carbon instance (second-precision).
// Farthest returns the farthest Carbon instance from the given Carbon instance.
// 返回离给定 carbon 实例最远的 Carbon 实例
func (c Carbon) Farthest(c1 Carbon, c2 Carbon) Carbon {
if c1.IsZero() || c1.IsInvalid() {
@ -29,3 +29,27 @@ func (c Carbon) Farthest(c1 Carbon, c2 Carbon) Carbon {
}
return c2
}
// Max returns the maximum Carbon instance from the given Carbon instance (second-precision).
// 返回最大的 Carbon 实例
func Max(c1 Carbon, c2 ...Carbon) Carbon {
max := c1
for i := range c2 {
if c2[i].Gte(max) {
max = c2[i]
}
}
return max
}
// Min returns the minimum Carbon instance from the given Carbon instance (second-precision).
// 返回最小的 Carbon 实例
func Min(c1 Carbon, c2 ...Carbon) Carbon {
min := c1
for i := range c2 {
if c2[i].Lte(min) {
min = c2[i]
}
}
return min
}

View File

@ -33,3 +33,17 @@ func BenchmarkCarbon_Farthest(b *testing.B) {
now.Farthest(Parse("2020-08-05"), Parse("xxx"))
}
}
func BenchmarkCarbon_Max(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
Max(now.SubDay(), now.AddDay())
}
}
func BenchmarkCarbon_Min(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
Min(now.SubDay(), now.AddDay())
}
}

View File

@ -56,3 +56,15 @@ func TestCarbon_Farthest(t *testing.T) {
assert.Equal(test.expected, c.ToDateString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_Max(t *testing.T) {
now := Now()
max := Max(now.SubDay(), now, now.AddDay())
assert.Equal(t, now.AddDay().Timestamp(), max.Timestamp())
}
func TestCarbon_Min(t *testing.T) {
now := Now()
min := Min(now, now.SubDay(), now.AddDay())
assert.Equal(t, now.SubDay().Timestamp(), min.Timestamp())
}

View File

@ -7,6 +7,11 @@ func BenchmarkCarbon_DaysInYear(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DaysInYear()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DaysInYear()
}
}
func BenchmarkCarbon_DaysInMonth(b *testing.B) {
@ -14,6 +19,11 @@ func BenchmarkCarbon_DaysInMonth(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DaysInMonth()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DaysInMonth()
}
}
func BenchmarkCarbon_MonthOfYear(b *testing.B) {
@ -21,6 +31,11 @@ func BenchmarkCarbon_MonthOfYear(b *testing.B) {
for n := 0; n < b.N; n++ {
now.MonthOfYear()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.MonthOfYear()
}
}
func BenchmarkCarbon_DayOfYear(b *testing.B) {
@ -28,6 +43,11 @@ func BenchmarkCarbon_DayOfYear(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DayOfYear()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DayOfYear()
}
}
func BenchmarkCarbon_DayOfMonth(b *testing.B) {
@ -35,6 +55,11 @@ func BenchmarkCarbon_DayOfMonth(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DayOfMonth()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DayOfMonth()
}
}
func BenchmarkCarbon_DayOfWeek(b *testing.B) {
@ -42,6 +67,11 @@ func BenchmarkCarbon_DayOfWeek(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DayOfWeek()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DayOfWeek()
}
}
func BenchmarkCarbon_WeekOfYear(b *testing.B) {
@ -49,6 +79,11 @@ func BenchmarkCarbon_WeekOfYear(b *testing.B) {
for n := 0; n < b.N; n++ {
now.WeekOfYear()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.WeekOfYear()
}
}
func BenchmarkCarbon_WeekOfMonth(b *testing.B) {
@ -56,6 +91,11 @@ func BenchmarkCarbon_WeekOfMonth(b *testing.B) {
for n := 0; n < b.N; n++ {
now.WeekOfMonth()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.WeekOfMonth()
}
}
func BenchmarkCarbon_DateTime(b *testing.B) {
@ -63,6 +103,11 @@ func BenchmarkCarbon_DateTime(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DateTime()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DateTime()
}
}
func BenchmarkCarbon_DateTimeMilli(b *testing.B) {
@ -70,6 +115,11 @@ func BenchmarkCarbon_DateTimeMilli(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DateTimeMilli()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DateTimeMilli()
}
}
func BenchmarkCarbon_DateTimeMicro(b *testing.B) {
@ -77,6 +127,11 @@ func BenchmarkCarbon_DateTimeMicro(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DateTimeMilli()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DateTimeMilli()
}
}
func BenchmarkCarbon_DateTimeNano(b *testing.B) {
@ -84,6 +139,11 @@ func BenchmarkCarbon_DateTimeNano(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DateTimeNano()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DateTimeNano()
}
}
func BenchmarkCarbon_Date(b *testing.B) {
@ -91,6 +151,11 @@ func BenchmarkCarbon_Date(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Date()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Date()
}
}
func BenchmarkCarbon_DateMilli(b *testing.B) {
@ -98,6 +163,11 @@ func BenchmarkCarbon_DateMilli(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DateMilli()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DateMilli()
}
}
func BenchmarkCarbon_DateMicro(b *testing.B) {
@ -105,6 +175,11 @@ func BenchmarkCarbon_DateMicro(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DateMicro()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DateMicro()
}
}
func BenchmarkCarbon_DateNano(b *testing.B) {
@ -112,6 +187,11 @@ func BenchmarkCarbon_DateNano(b *testing.B) {
for n := 0; n < b.N; n++ {
now.DateNano()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.DateNano()
}
}
func BenchmarkCarbon_Time(b *testing.B) {
@ -119,6 +199,11 @@ func BenchmarkCarbon_Time(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Time()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Time()
}
}
func BenchmarkCarbon_TimeMilli(b *testing.B) {
@ -126,6 +211,11 @@ func BenchmarkCarbon_TimeMilli(b *testing.B) {
for n := 0; n < b.N; n++ {
now.TimeMilli()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.TimeMilli()
}
}
func BenchmarkCarbon_TimeMicro(b *testing.B) {
@ -133,6 +223,11 @@ func BenchmarkCarbon_TimeMicro(b *testing.B) {
for n := 0; n < b.N; n++ {
now.TimeMicro()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.TimeMicro()
}
}
func BenchmarkCarbon_TimeNano(b *testing.B) {
@ -140,6 +235,11 @@ func BenchmarkCarbon_TimeNano(b *testing.B) {
for n := 0; n < b.N; n++ {
now.TimeNano()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.TimeNano()
}
}
func BenchmarkCarbon_Century(b *testing.B) {
@ -147,6 +247,11 @@ func BenchmarkCarbon_Century(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Century()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Century()
}
}
func BenchmarkCarbon_Decade(b *testing.B) {
@ -154,6 +259,11 @@ func BenchmarkCarbon_Decade(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Decade()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Decade()
}
}
func BenchmarkCarbon_Year(b *testing.B) {
@ -161,6 +271,11 @@ func BenchmarkCarbon_Year(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Year()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Year()
}
}
func BenchmarkCarbon_Quarter(b *testing.B) {
@ -168,6 +283,11 @@ func BenchmarkCarbon_Quarter(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Quarter()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Quarter()
}
}
func BenchmarkCarbon_Month(b *testing.B) {
@ -175,6 +295,11 @@ func BenchmarkCarbon_Month(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Month()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Month()
}
}
func BenchmarkCarbon_Week(b *testing.B) {
@ -182,6 +307,11 @@ func BenchmarkCarbon_Week(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Week()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Week()
}
}
func BenchmarkCarbon_Day(b *testing.B) {
@ -189,6 +319,11 @@ func BenchmarkCarbon_Day(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Day()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Day()
}
}
func BenchmarkCarbon_Hour(b *testing.B) {
@ -196,6 +331,11 @@ func BenchmarkCarbon_Hour(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Hour()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Hour()
}
}
func BenchmarkCarbon_Minute(b *testing.B) {
@ -203,6 +343,11 @@ func BenchmarkCarbon_Minute(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Minute()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Minute()
}
}
func BenchmarkCarbon_Second(b *testing.B) {
@ -210,6 +355,11 @@ func BenchmarkCarbon_Second(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Second()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Second()
}
}
func BenchmarkCarbon_Millisecond(b *testing.B) {
@ -217,6 +367,11 @@ func BenchmarkCarbon_Millisecond(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Millisecond()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Millisecond()
}
}
func BenchmarkCarbon_Microsecond(b *testing.B) {
@ -224,6 +379,11 @@ func BenchmarkCarbon_Microsecond(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Microsecond()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Microsecond()
}
}
func BenchmarkCarbon_Nanosecond(b *testing.B) {
@ -231,6 +391,11 @@ func BenchmarkCarbon_Nanosecond(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Nanosecond()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Nanosecond()
}
}
func BenchmarkCarbon_Timestamp(b *testing.B) {
@ -238,6 +403,11 @@ func BenchmarkCarbon_Timestamp(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Timestamp()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Timestamp()
}
}
func BenchmarkCarbon_TimestampMilli(b *testing.B) {
@ -245,6 +415,11 @@ func BenchmarkCarbon_TimestampMilli(b *testing.B) {
for n := 0; n < b.N; n++ {
now.TimestampMilli()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.TimestampMilli()
}
}
func BenchmarkCarbon_TimestampMicro(b *testing.B) {
@ -252,6 +427,11 @@ func BenchmarkCarbon_TimestampMicro(b *testing.B) {
for n := 0; n < b.N; n++ {
now.TimestampMicro()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.TimestampMicro()
}
}
func BenchmarkCarbon_TimestampNano(b *testing.B) {
@ -259,6 +439,11 @@ func BenchmarkCarbon_TimestampNano(b *testing.B) {
for n := 0; n < b.N; n++ {
now.TimestampNano()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.TimestampNano()
}
}
func BenchmarkCarbon_Location(b *testing.B) {
@ -266,6 +451,11 @@ func BenchmarkCarbon_Location(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Location()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Location()
}
}
func BenchmarkCarbon_Timezone(b *testing.B) {
@ -273,6 +463,11 @@ func BenchmarkCarbon_Timezone(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Timezone()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Timezone()
}
}
func BenchmarkCarbon_Offset(b *testing.B) {
@ -280,6 +475,11 @@ func BenchmarkCarbon_Offset(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Offset()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Offset()
}
}
func BenchmarkCarbon_Locale(b *testing.B) {
@ -287,6 +487,11 @@ func BenchmarkCarbon_Locale(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Locale()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Locale()
}
}
func BenchmarkCarbon_Age(b *testing.B) {
@ -294,4 +499,9 @@ func BenchmarkCarbon_Age(b *testing.B) {
for n := 0; n < b.N; n++ {
now.Age()
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.Age()
}
}

View File

@ -5,6 +5,18 @@ import (
"time"
)
// week days
// 工作日
var weekdays = map[string]time.Weekday{
Monday: time.Monday,
Tuesday: time.Tuesday,
Wednesday: time.Wednesday,
Thursday: time.Thursday,
Friday: time.Friday,
Saturday: time.Saturday,
Sunday: time.Sunday,
}
// common formatting symbols
// 常规格式化符号
var formats = map[byte]string{

View File

@ -2,6 +2,13 @@ package carbon
import "testing"
func BenchmarkCarbon_SetLanguage(b *testing.B) {
lang := NewLanguage()
for n := 0; n < b.N; n++ {
SetLanguage(lang)
}
}
func BenchmarkCarbon_NewLanguage(b *testing.B) {
for n := 0; n < b.N; n++ {
NewLanguage()

View File

@ -4,6 +4,24 @@ import (
"time"
)
// SetWeekStartsAt sets start day of the week.
// 设置一周的开始日期
func (c Carbon) SetWeekStartsAt(day string) Carbon {
if c.Error != nil {
return c
}
if weekday, ok := weekdays[day]; ok {
c.weekStartsAt = weekday
}
return c
}
// SetWeekStartsAt sets start day of the week.
// 设置一周的开始日期
func SetWeekStartsAt(day string) Carbon {
return NewCarbon().SetWeekStartsAt(day)
}
// SetTimezone sets timezone.
// 设置时区
func (c Carbon) SetTimezone(name string) Carbon {
@ -213,31 +231,6 @@ func (c Carbon) SetMonthNoOverflow(month int) Carbon {
return c.AddMonthsNoOverflow(month - c.Month())
}
// SetWeekStartsAt sets start day of the week.
// 设置一周的开始日期
func (c Carbon) SetWeekStartsAt(day string) Carbon {
if c.IsInvalid() {
return c
}
switch day {
case Sunday:
c.weekStartsAt = time.Sunday
case Monday:
c.weekStartsAt = time.Monday
case Tuesday:
c.weekStartsAt = time.Tuesday
case Wednesday:
c.weekStartsAt = time.Wednesday
case Thursday:
c.weekStartsAt = time.Thursday
case Friday:
c.weekStartsAt = time.Friday
case Saturday:
c.weekStartsAt = time.Saturday
}
return c
}
// SetDay sets day.
// 设置日期
func (c Carbon) SetDay(day int) Carbon {

View File

@ -5,10 +5,26 @@ import (
"time"
)
func BenchmarkCarbon_SetWeekStartsAt(b *testing.B) {
for n := 0; n < b.N; n++ {
SetWeekStartsAt(Sunday)
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.SetWeekStartsAt(Sunday)
}
}
func BenchmarkCarbon_SetTimezone(b *testing.B) {
for n := 0; n < b.N; n++ {
SetTimezone(PRC)
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.SetTimezone(PRC)
}
}
func BenchmarkCarbon_SetLocation(b *testing.B) {
@ -16,161 +32,181 @@ func BenchmarkCarbon_SetLocation(b *testing.B) {
for n := 0; n < b.N; n++ {
SetLocation(loc)
}
c := NewCarbon()
for n := 0; n < b.N; n++ {
c.SetLocation(loc)
}
}
func BenchmarkCarbon_SetLocale(b *testing.B) {
for n := 0; n < b.N; n++ {
SetLocale("en")
}
}
func BenchmarkCarbon_SetLanguage(b *testing.B) {
lang := NewLanguage()
c := NewCarbon()
for n := 0; n < b.N; n++ {
SetLanguage(lang)
c.SetLocale("en")
}
}
func BenchmarkCarbon_SetDateTime(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDateTime(2020, 8, 5, 0, 0, 0)
c.SetDateTime(2020, 8, 5, 0, 0, 0)
}
}
func BenchmarkCarbon_SetDateTimeMilli(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDateTimeMilli(2020, 8, 5, 0, 0, 0, 0)
c.SetDateTimeMilli(2020, 8, 5, 0, 0, 0, 0)
}
}
func BenchmarkCarbon_SetDateTimeMicro(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDateTimeMicro(2020, 8, 5, 0, 0, 0, 0)
c.SetDateTimeMicro(2020, 8, 5, 0, 0, 0, 0)
}
}
func BenchmarkCarbon_SetDateTimeNano(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDateTimeNano(2020, 8, 5, 0, 0, 0, 0)
c.SetDateTimeNano(2020, 8, 5, 0, 0, 0, 0)
}
}
func BenchmarkCarbon_SetDate(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDate(2020, 8, 5)
c.SetDate(2020, 8, 5)
}
}
func BenchmarkCarbon_SetDateMilli(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDateMilli(2020, 8, 5, 0)
c.SetDateMilli(2020, 8, 5, 0)
}
}
func BenchmarkCarbon_SetDateMicro(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDateMicro(2020, 8, 5, 0)
c.SetDateMicro(2020, 8, 5, 0)
}
}
func BenchmarkCarbon_SetDateNano(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDateNano(2020, 8, 5, 0)
c.SetDateNano(2020, 8, 5, 0)
}
}
func BenchmarkCarbon_SetTime(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetTime(13, 14, 15)
c.SetTime(13, 14, 15)
}
}
func BenchmarkCarbon_SetTimeMilli(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetTimeMilli(13, 14, 15, 0)
c.SetTimeMilli(13, 14, 15, 0)
}
}
func BenchmarkCarbon_SetTimeMicro(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetTimeMicro(13, 14, 15, 0)
c.SetTimeMicro(13, 14, 15, 0)
}
}
func BenchmarkCarbon_SetTimeNano(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetTimeNano(13, 14, 15, 0)
c.SetTimeNano(13, 14, 15, 0)
}
}
func BenchmarkCarbon_SetYear(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetYear(2020)
c.SetYear(2020)
}
}
func BenchmarkCarbon_SetYearNoOverflow(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetYearNoOverflow(2020)
c.SetYearNoOverflow(2020)
}
}
func BenchmarkCarbon_SetMonth(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetMonth(8)
c.SetMonth(8)
}
}
func BenchmarkCarbon_SetMonthNoOverflow(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetMonthNoOverflow(8)
}
}
func BenchmarkCarbon_SetWeekStartsAt(b *testing.B) {
for n := 0; n < b.N; n++ {
NewCarbon().SetWeekStartsAt(Sunday)
c.SetMonthNoOverflow(8)
}
}
func BenchmarkCarbon_SetDay(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetDay(20)
c.SetDay(20)
}
}
func BenchmarkCarbon_SetHour(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetHour(20)
c.SetHour(20)
}
}
func BenchmarkCarbon_SetMinute(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetMinute(20)
c.SetMinute(20)
}
}
func BenchmarkCarbon_SetSecond(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetSecond(20)
c.SetSecond(20)
}
}
func BenchmarkCarbon_SetMillisecond(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetMillisecond(20)
c.SetMillisecond(20)
}
}
func BenchmarkCarbon_SetMicrosecond(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetMicrosecond(20)
c.SetMicrosecond(20)
}
}
func BenchmarkCarbon_SetNanosecond(b *testing.B) {
c := NewCarbon()
for n := 0; n < b.N; n++ {
NewCarbon().SetNanosecond(20)
c.SetNanosecond(20)
}
}

View File

@ -7,6 +7,61 @@ import (
"time"
)
func TestCarbon_SetWeekStartsAt(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string
week string
expected string
}{
{"", Sunday, ""},
{"0000-00-00 00:00:00", Sunday, ""},
{"", Monday, ""},
{"0000-00-00 00:00:00", Monday, ""},
{"2021-06-13", Sunday, "2021-06-13 00:00:00"},
{"2021-06-14", Sunday, "2021-06-13 00:00:00"},
{"2021-06-18", Sunday, "2021-06-13 00:00:00"},
{"2021-06-13", Monday, "2021-06-07 00:00:00"},
{"2021-06-14", Monday, "2021-06-14 00:00:00"},
{"2021-06-18", Monday, "2021-06-14 00:00:00"},
{"2021-06-13", Tuesday, "2021-06-08 00:00:00"},
{"2021-06-14", Tuesday, "2021-06-08 00:00:00"},
{"2021-06-18", Tuesday, "2021-06-15 00:00:00"},
{"2021-06-13", Wednesday, "2021-06-09 00:00:00"},
{"2021-06-14", Wednesday, "2021-06-09 00:00:00"},
{"2021-06-18", Wednesday, "2021-06-16 00:00:00"},
{"2021-06-13", Thursday, "2021-06-10 00:00:00"},
{"2021-06-14", Thursday, "2021-06-10 00:00:00"},
{"2021-06-18", Thursday, "2021-06-17 00:00:00"},
{"2021-06-13", Friday, "2021-06-11 00:00:00"},
{"2021-06-14", Friday, "2021-06-11 00:00:00"},
{"2021-06-18", Friday, "2021-06-18 00:00:00"},
{"2021-06-13", Saturday, "2021-06-12 00:00:00"},
{"2021-06-14", Saturday, "2021-06-12 00:00:00"},
{"2021-06-18", Saturday, "2021-06-12 00:00:00"},
}
for index, test := range tests {
c := Parse(test.input).SetWeekStartsAt(test.week).StartOfWeek()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current1 test index is "+strconv.Itoa(index))
}
for index, test := range tests {
c := SetWeekStartsAt(test.week).Parse(test.input).StartOfWeek()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current1 test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetTimezone(t *testing.T) {
assert := assert.New(t)
@ -399,55 +454,6 @@ func TestCarbon_SetMonthNoOverflow(t *testing.T) {
}
}
func TestCarbon_SetWeekStartsAt(t *testing.T) {
assert := assert.New(t)
tests := []struct {
input string
week string
expected string
}{
{"", Sunday, ""},
{"0000-00-00 00:00:00", Sunday, ""},
{"", Monday, ""},
{"0000-00-00 00:00:00", Monday, ""},
{"2021-06-13", Sunday, "2021-06-13 00:00:00"},
{"2021-06-14", Sunday, "2021-06-13 00:00:00"},
{"2021-06-18", Sunday, "2021-06-13 00:00:00"},
{"2021-06-13", Monday, "2021-06-07 00:00:00"},
{"2021-06-14", Monday, "2021-06-14 00:00:00"},
{"2021-06-18", Monday, "2021-06-14 00:00:00"},
{"2021-06-13", Tuesday, "2021-06-08 00:00:00"},
{"2021-06-14", Tuesday, "2021-06-08 00:00:00"},
{"2021-06-18", Tuesday, "2021-06-15 00:00:00"},
{"2021-06-13", Wednesday, "2021-06-09 00:00:00"},
{"2021-06-14", Wednesday, "2021-06-09 00:00:00"},
{"2021-06-18", Wednesday, "2021-06-16 00:00:00"},
{"2021-06-13", Thursday, "2021-06-10 00:00:00"},
{"2021-06-14", Thursday, "2021-06-10 00:00:00"},
{"2021-06-18", Thursday, "2021-06-17 00:00:00"},
{"2021-06-13", Friday, "2021-06-11 00:00:00"},
{"2021-06-14", Friday, "2021-06-11 00:00:00"},
{"2021-06-18", Friday, "2021-06-18 00:00:00"},
{"2021-06-13", Saturday, "2021-06-12 00:00:00"},
{"2021-06-14", Saturday, "2021-06-12 00:00:00"},
{"2021-06-18", Saturday, "2021-06-12 00:00:00"},
}
for index, test := range tests {
c := Parse(test.input).SetWeekStartsAt(test.week).StartOfWeek()
assert.Nil(c.Error)
assert.Equal(test.expected, c.ToDateTimeString(), "Current test index is "+strconv.Itoa(index))
}
}
func TestCarbon_SetDay(t *testing.T) {
assert := assert.New(t)
@ -586,6 +592,7 @@ func TestError_Setter(t *testing.T) {
input, timezone, locale, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond := "2020-08-50 13:14:15", "xxx", "xxx", 2020, 8, 50, 13, 14, 15, 999, 999999, 999999999
c := Parse(input)
assert.NotNil(t, Parse("xxx").SetWeekStartsAt(Sunday).Error, "It should catch an exception in SetWeekStartsAt()")
assert.NotNil(t, c.SetTimezone(timezone).Error, "It should catch an exception in SetTimezone()")
loc, _ := time.LoadLocation("xxx")

4
tag.go
View File

@ -140,10 +140,10 @@ func LoadTag(v interface{}) error {
if carbon == "" {
carbon = "layout:" + defaultLayout
}
if strings.Contains(carbon, "type:") {
if strings.HasPrefix(carbon, "type:") {
carbon = tagTypes[carbon[5:]]
}
if !strings.Contains(carbon, "layout:") && !strings.Contains(carbon, "format:") {
if !strings.HasPrefix(carbon, "layout:") && !strings.HasPrefix(carbon, "format:") {
return invalidTagError(fieldType.Name)
}
tz := fieldType.Tag.Get("tz")

View File

@ -2,7 +2,7 @@ package carbon
import "testing"
func BenchmarkTag_SetTag(b *testing.B) {
func BenchmarkCarbon_SetTag(b *testing.B) {
now := Now()
for n := 0; n < b.N; n++ {
now.SetTag(&tag{
@ -14,7 +14,7 @@ func BenchmarkTag_SetTag(b *testing.B) {
func BenchmarkCarbon_LoadTag(b *testing.B) {
type Student struct {
Birthday Carbon `json:"birthday" carbon:"date"`
Birthday Carbon `json:"birthday" carbon:"type:date"`
}
student := Student{
Birthday: Now(),

View File

@ -15,7 +15,7 @@ func TestCarbon_parseTag(t *testing.T) {
assert.Equal(t, Local, tz)
}
func TestTag_SetTag(t *testing.T) {
func TestCarbon_SetTag(t *testing.T) {
c := NewCarbon().SetTag(&tag{
tz: PRC,
})