mirror of
https://gitee.com/dromara/carbon.git
synced 2024-11-30 03:07:36 +08:00
commit
18394513e1
47
README.md
47
README.md
@ -189,11 +189,36 @@ c.Now().SubSeconds(3).ToDateTimeString() // 2020-09-08 12:59:57
|
||||
c.Now().SubSecond().ToDateTimeString() // 2020-09-08 12:59:59
|
||||
```
|
||||
|
||||
###### 日期判断
|
||||
###### 时间判断
|
||||
```go
|
||||
// 是否是闰年
|
||||
c.Now().IsLeapYear() // true
|
||||
|
||||
// 是否是一月
|
||||
c.Now().IsJanuary() // false
|
||||
// 是否是二月
|
||||
c.Now().IsFebruary() // true
|
||||
// 是否是三月
|
||||
c.Now().IsMarch() // false
|
||||
// 是否是四月
|
||||
c.Now().IsApril() // false
|
||||
// 是否是五月
|
||||
c.Now().IsMay() // false
|
||||
// 是否是六月
|
||||
c.Now().IsJune() // false
|
||||
// 是否是七月
|
||||
c.Now().IsJuly() // false
|
||||
// 是否是八月
|
||||
c.Now().IsAugust() // false
|
||||
// 是否是九月
|
||||
c.Now().IsSeptember() // true
|
||||
// 是否是十月
|
||||
c.Now().IsOctober() // false
|
||||
// 是否是十一月
|
||||
c.Now().IsNovember() // false
|
||||
// 是否是十二月
|
||||
c.Now().IsDecember() // false
|
||||
|
||||
// 是否是周一
|
||||
c.Now().IsMonday() // false
|
||||
// 是否是周二
|
||||
@ -217,9 +242,27 @@ c.Now().IsLastDay() // false
|
||||
|
||||
**更新日志**
|
||||
|
||||
##### 2020-09-09
|
||||
* 修复readme.md错误描述
|
||||
* 完善单元测试
|
||||
* 新增gorm时间格式化,支持多种输出格式
|
||||
* 新增IsJanuary()方法判断是否是第一月
|
||||
* 新增IsFebruary()方法判断是否是第二月
|
||||
* 新增IsMarch()方法判断是否是第三月
|
||||
* 新增IsApril()方法判断是否是第四月
|
||||
* 新增IsMay()方法判断是否是第五月
|
||||
* 新增IsJune()方法判断是否是第六月
|
||||
* 新增IsJuly()方法判断是否是第七月
|
||||
* 新增IsAugust()方法判断是否是第八月
|
||||
* 新增IsSeptember()方法判断是否是第九月
|
||||
* 新增IsOctober()方法判断是否是第十月
|
||||
* 新增IsNovember()方法判断是否是第十一月
|
||||
* 新增IsDecember()方法判断是否是第十二月
|
||||
|
||||
##### 2020-09-08
|
||||
* 修复已知BUG
|
||||
* 添加单元测试
|
||||
* 新增FirstDay()方法获取第一天
|
||||
* 新增FirstDay()方法获取最后一天
|
||||
|
||||
* 新增IsFirstDay()方法判断是否第一天
|
||||
* 新增IsFirstDay()方法判断是否最后一天
|
||||
|
60
carbon.go
60
carbon.go
@ -337,6 +337,66 @@ func (c *carbon) IsLeapYear() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// IsJanuary 是否是一月
|
||||
func (c *carbon) IsJanuary() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "January"
|
||||
}
|
||||
|
||||
// IsMonday 是否是二月
|
||||
func (c *carbon) IsFebruary() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "February"
|
||||
}
|
||||
|
||||
// IsMarch 是否是三月
|
||||
func (c *carbon) IsMarch() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "March"
|
||||
}
|
||||
|
||||
// IsApril 是否是四月
|
||||
func (c *carbon) IsApril() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "April"
|
||||
}
|
||||
|
||||
// IsMay 是否是五月
|
||||
func (c *carbon) IsMay() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "May"
|
||||
}
|
||||
|
||||
// IsJune 是否是六月
|
||||
func (c *carbon) IsJune() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "June"
|
||||
}
|
||||
|
||||
// IsJuly 是否是七月
|
||||
func (c *carbon) IsJuly() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "July"
|
||||
}
|
||||
|
||||
// IsAugust 是否是八月
|
||||
func (c *carbon) IsAugust() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "August"
|
||||
}
|
||||
|
||||
// IsSeptember 是否是九月
|
||||
func (c *carbon) IsSeptember() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "September"
|
||||
}
|
||||
|
||||
// IsOctober 是否是十月
|
||||
func (c *carbon) IsOctober() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "October"
|
||||
}
|
||||
|
||||
// IsNovember 是否是十一月
|
||||
func (c *carbon) IsNovember() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "November"
|
||||
}
|
||||
|
||||
// IsDecember 是否是十二月
|
||||
func (c *carbon) IsDecember() bool {
|
||||
return c.Time.In(c.loc).Month().String() == "December"
|
||||
}
|
||||
|
||||
// IsMonday 是否是周一
|
||||
func (c *carbon) IsMonday() bool {
|
||||
return c.Time.In(c.loc).Weekday().String() == "Monday"
|
||||
|
12
gorm_model.go
Normal file
12
gorm_model.go
Normal file
@ -0,0 +1,12 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Model struct {
|
||||
Id int64 `gorm:"primary_key;comment:'主键ID'" json:"id"`
|
||||
CreatedAt ToDateTimeString `gorm:"comment:'创建时间';type:timestamp not null;default:current_timestamp;" json:"created_at"`
|
||||
UpdatedAt ToDateTimeString `gorm:"comment:'更新时间';type:timestamp on update current_timestamp;omitempty;default:current_timestamp;" json:"updated_at"`
|
||||
DeletedAt *time.Time `gorm:"comment:'删除时间'" json:"deleted_at" sql:"index"`
|
||||
}
|
33
gorm_to_date.go
Normal file
33
gorm_to_date.go
Normal file
@ -0,0 +1,33 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToDateString struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToDateString) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t ToDateString) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
func (t *ToDateString) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToDateString{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
34
gorm_to_datetime.go
Normal file
34
gorm_to_datetime.go
Normal file
@ -0,0 +1,34 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToDateTimeString struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToDateTimeString) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
|
||||
return []byte(formatted), nil
|
||||
|
||||
}
|
||||
|
||||
func (t ToDateTimeString) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
func (t *ToDateTimeString) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToDateTimeString{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
33
gorm_to_time.go
Normal file
33
gorm_to_time.go
Normal file
@ -0,0 +1,33 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToTimeString struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToTimeString) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%s\"", t.Format("15-04-05"))
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t ToTimeString) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
func (t *ToTimeString) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToTimeString{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
33
gorm_to_timestamp.go
Normal file
33
gorm_to_timestamp.go
Normal file
@ -0,0 +1,33 @@
|
||||
package carbon
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ToTimestamp struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
func (t ToTimestamp) MarshalJSON() ([]byte, error) {
|
||||
formatted := fmt.Sprintf("\"%d\"", t.Unix())
|
||||
return []byte(formatted), nil
|
||||
}
|
||||
|
||||
func (t ToTimestamp) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
if t.Time.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return t.Time, nil
|
||||
}
|
||||
|
||||
func (t *ToTimestamp) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = ToTimestamp{Time: value}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
Loading…
Reference in New Issue
Block a user