Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansu committed Feb 24, 2021
1 parent 554277c commit c4684a5
Show file tree
Hide file tree
Showing 14 changed files with 675 additions and 333 deletions.
4 changes: 2 additions & 2 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// Application
type Application struct {
cherryInterfaces.INode // current node info
startTime int64 // application start time
startTime cherryTime.CherryTime // application start time
running bool // is running
die chan bool // wait for end application
components []cherryInterfaces.IComponent // all components
Expand Down Expand Up @@ -62,7 +62,7 @@ func (a *Application) All() []cherryInterfaces.IComponent {
}

func (a *Application) StartTime() string {
return cherryTime.UnixTimeToString(a.startTime)
return a.startTime.ToDateTimeFormat()
}

// Startup
Expand Down
2 changes: 1 addition & 1 deletion cherry.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewApp(configPath, profile, nodeId string) *Application {

thisNode := &Application{
INode: node,
startTime: cherryTime.NowSecond(),
startTime: cherryTime.Now(),
running: false,
die: make(chan bool),
}
Expand Down
45 changes: 31 additions & 14 deletions extend/time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package cherryTime

import (
cherryUtils "github.com/cherry-game/cherry/extend/utils"
"strconv"
"time"
)
Expand Down Expand Up @@ -45,27 +46,30 @@ type CherryTime struct {
time.Time
}

func NewTime(tt time.Time) CherryTime {
func NewTime(tt time.Time, setGlobal bool) CherryTime {
ct := CherryTime{
Time: tt,
}
ct.In(globalLocation)
ct.AddSeconds(globalOffsetSecond)

if setGlobal {
ct.In(globalLocation)
ct.AddSeconds(globalOffsetSecond)
}
return ct
}

func Now() CherryTime {
return NewTime(time.Now())
return NewTime(time.Now(), true)
}

func Yesterday() CherryTime {
t := time.Now().AddDate(0, 0, -1)
return NewTime(t)
return NewTime(t, true)
}

func Tomorrow() CherryTime {
t := Now().AddDate(0, 0, 1)
return NewTime(t)
return NewTime(t, true)
}

// CreateFromTimestamp 从时间戳创建 Carbon 实例
Expand All @@ -85,26 +89,39 @@ func CreateFromTimestamp(timestamp int64) CherryTime {
}

t := time.Unix(ts, 0)
return NewTime(t)
return NewTime(t, false)
}

// CreateFromDateTime 从年月日时分秒创建 Carbon 实例
func CreateFromDateTime(year int, month int, day int, hour int, minute int, second int) CherryTime {
now := Now()
now.Time = time.Date(year, time.Month(month), day, hour, minute, second, 0, now.Location())
return now
t := time.Date(year, time.Month(month), day, hour, minute, second, 0, globalLocation)
return NewTime(t, false)
}

// CreateFromDate 从年月日创建 Carbon 实例(默认时区)
func CreateFromDate(year int, month int, day int) CherryTime {
now := Now()
now.Time = time.Date(year, time.Month(month), day, now.Hour(), now.Minute(), now.Second(), 0, now.Location())
return now
t := time.Date(year, time.Month(month), day, now.Hour(), now.Minute(), now.Second(), 0, now.Location())
return NewTime(t, false)
}

// CreateFromTime 从时分秒创建 Carbon 实例(默认时区)
func CreateFromTime(hour int, minute int, second int) CherryTime {
now := Now()
now.Time = time.Date(now.Year(), now.Time.Month(), now.Day(), hour, minute, second, 0, now.Location())
return now
t := time.Date(now.Year(), now.Time.Month(), now.Day(), hour, minute, second, 0, now.Location())
return NewTime(t, false)
}

// parseByDuration 通过持续时间解析
func ParseByDuration(duration string) (time.Duration, error) {
td, err := time.ParseDuration(duration)
if err != nil {
err = cherryUtils.Errorf("invalid duration %d", duration)
}
return td, err
}

// getAbsValue 获取绝对值
func GetAbsValue(value int64) int64 {
return (value ^ value>>31) - value>>31
}
8 changes: 6 additions & 2 deletions extend/time/time_compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ func (c CherryTime) IsWeekend() bool {

// IsYesterday 是否是昨天
func (c CherryTime) IsYesterday() bool {
return c.ToDateFormat() == Now().SubDay().ToDateFormat()
yesterday := Now()
yesterday.SubDay()
return c.ToDateFormat() == yesterday.ToDateFormat()
}

// IsToday 是否是今天
Expand All @@ -149,5 +151,7 @@ func (c CherryTime) IsToday() bool {

// IsTomorrow 是否是明天
func (c CherryTime) IsTomorrow() bool {
return c.ToDateFormat() == Now().AddDay().ToDateFormat()
tomorrow := Now()
tomorrow.AddDay()
return c.ToDateFormat() == tomorrow.ToDateFormat()
}
146 changes: 146 additions & 0 deletions extend/time/time_compare_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package cherryTime

import (
"fmt"
"testing"
)

func TestCherryTime_IsNow(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsNow()))
}

func TestCherryTime_IsFuture(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsFuture()))
}

func TestCherryTime_IsPast(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsPast()))
}

func TestCherryTime_IsLeapYear(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsLeapYear()))
}

func TestCherryTime_IsLongYear(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsLongYear()))
}

func TestCherryTime_IsJanuary(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsJanuary()))
}

func TestCherryTime_IsFebruary(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsFebruary()))
}

func TestCherryTime_IsMarch(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsMarch()))
}

func TestCherryTime_IsApril(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsApril()))
}

func TestCherryTime_IsMay(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsMay()))
}

func TestCherryTime_IsJune(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsJune()))
}

func TestCherryTime_IsJuly(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsJuly()))
}

func TestCherryTime_IsAugust(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsAugust()))
}

func TestCherryTime_IsSeptember(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsSeptember()))
}

func TestCherryTime_IsOctober(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsOctober()))
}

func TestCherryTime_IsDecember(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsDecember()))
}

func TestCherryTime_IsMonday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsMonday()))
}

func TestCherryTime_IsTuesday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsTuesday()))
}

func TestCherryTime_IsWednesday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsWednesday()))
}

func TestCherryTime_IsThursday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsThursday()))
}

func TestCherryTime_IsFriday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsFriday()))
}

func TestCherryTime_IsSaturday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsSaturday()))
}

func TestCherryTime_IsSunday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsSunday()))
}

func TestCherryTime_IsWeekday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsWeekday()))
}

func TestCherryTime_IsWeekend(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsWeekend()))
}

func TestCherryTime_IsYesterday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsYesterday()))
}

func TestCherryTime_IsToday(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsToday()))
}

func TestCherryTime_IsTomorrow(t *testing.T) {
now := Now()
t.Log(fmt.Sprintf("result = %v", now.IsTomorrow()))
}
Loading

0 comments on commit c4684a5

Please sign in to comment.