Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
1. 增加部分常量
2. 增加时间处理函数
3. 增加mysql初始化的结构体
  • Loading branch information
MonkeyIsMe committed Sep 17, 2022
1 parent 2c58342 commit 95b5383
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 10 deletions.
27 changes: 21 additions & 6 deletions constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ const (

// 返回2xxxx -> MySQL错误
const (
MySQLQueryError = 20000 // MySQL的查询错误
MySQLScanError = 20001 // MySQL在Scan时候的错误
MySQLInsertError = 20002 // MySQL的插入错误
MySQLDeleteError = 20003 // MySQL的删除错误
MySQLUpdateError = 20004 // MySQL的更新错误
MySQLCountError = 20004 // MySQL的计数错误
MySQLQueryError = 20000 // MySQL的查询错误
MySQLScanError = 20001 // MySQL在Scan时候的错误
MySQLInsertError = 20002 // MySQL的插入错误
MySQLDeleteError = 20003 // MySQL的删除错误
MySQLUpdateError = 20004 // MySQL的更新错误
MySQLCountError = 20004 // MySQL的计数错误
MysqlCountAffectRowError = 20005
)

// 返回3xxxx -> 其他类型的错误
Expand Down Expand Up @@ -48,6 +49,8 @@ const (

const (
MySQLDrive = "mysql" // mysql驱动

SuccessString = "Success"
)

type Weekday int
Expand All @@ -61,3 +64,15 @@ const (
Friday
Saturday
)

var WeekDayMap = map[string]string{
"Monday": "周一",
"Tuesday": "周二",
"Wednesday": "周三",
"Thursday": "周四",
"Friday": "周五",
"Saturday": "周六",
"Sunday": "周日",
}

const StandardTime = "2006-01-02 15:04:05" // StandardTime 标准时间
9 changes: 9 additions & 0 deletions constant/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ type ErrMessage struct {
Err error
ErrCode int
}

// MysqlProxy mysql的proxy
type MysqlProxy struct {
Account string
Password string
Host string
Port string
DBName string
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ require (
github.com/onsi/gomega v1.19.0 // indirect
github.com/prometheus/common v0.26.0
github.com/robfig/cron v1.2.0
github.com/satori/go.uuid v1.2.0
go.etcd.io/etcd/client/v3 v3.5.3
golang.org/x/text v0.3.7
gopkg.in/yaml.v2 v2.4.0
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,6 @@ github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6L
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
129 changes: 129 additions & 0 deletions time/time.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package time

import (
"fmt"
"log"
"strconv"
"time"

"github.com/shootz-developer/gtool/constant"
)

const (
Expand Down Expand Up @@ -106,3 +110,128 @@ func DayAddOrSub(t time.Time, num int64) time.Time {
m, _ = time.ParseDuration(s + "h")
return t.Add(m)
}

// dateFormat 日期格式化处理
func dateFormat(date string) string {
newDate := ""
for i, _ := range date {
if date[i] == 'T' {
newDate = fmt.Sprintf("%s ", newDate)
} else if date[i] == 'Z' {
continue
} else {
newDate = fmt.Sprintf("%s%c", newDate, date[i])
}
}

return newDate
}

// DateUtilSingle 时间格式化处理工具,将开始时间和结束时间统一成一个时间
func DateUtilSingle(start, end string) string {
st, _ := time.Parse(constant.StandardTime, dateFormat(start))
ed, _ := time.Parse(constant.StandardTime, dateFormat(end))
time := fmt.Sprintf("%d", st.Year())
if int(st.Month()) < 10 {
time = fmt.Sprintf("%s-0%d", time, int(st.Month()))
} else {
time = fmt.Sprintf("%s-%d", time, int(st.Month()))
}

if st.Day() < 10 {
time = fmt.Sprintf("%s-0%d", time, st.Day())
} else {
time = fmt.Sprintf("%s-%d", time, st.Day())
}

time = fmt.Sprintf("%s(%s)", time, constant.WeekDayMap[st.Weekday().String()])

if st.Hour() < 10 {
time = fmt.Sprintf(" %s 0%d", time, st.Hour())
} else {
time = fmt.Sprintf(" %s %d", time, st.Hour())
}

if st.Minute() < 10 {
time = fmt.Sprintf("%s:0%d", time, st.Minute())
} else {
time = fmt.Sprintf("%s:%d", time, st.Minute())
}

if ed.Hour() < 10 {
time = fmt.Sprintf("%s~0%d", time, ed.Hour())
} else {
time = fmt.Sprintf("%s~%d", time, ed.Hour())
}

if ed.Minute() < 10 {
time = fmt.Sprintf("%s:0%d", time, ed.Minute())
} else {
time = fmt.Sprintf("%s:%d", time, ed.Minute())
}

return time
}

// FormatStartTime 格式化开始时间
func FormatStartTime(startTime string) string {
time, _ := time.Parse(constant.StandardTime, startTime)
formatTime := fmt.Sprintf("%d", time.Year())
if time.Month() < 10 {
formatTime = fmt.Sprintf("%s-0%d", formatTime, time.Month())
} else {
formatTime = fmt.Sprintf("%s%d-", formatTime, time.Month())
}

if time.Day() < 10 {
formatTime = fmt.Sprintf("%s-0%d", formatTime, time.Day())
} else {
formatTime = fmt.Sprintf("%s-%d", formatTime, time.Day())
}

formatTime = fmt.Sprintf("%s(%s) ", formatTime, constant.WeekDayMap[time.Weekday().String()])

return formatTime
}

// FormatEndTime 格式化结束时间
func FormatEndTime(startTime, endTime string) string {
stTime, err := time.Parse(constant.StandardTime, startTime)
if err != nil {
log.Printf("Parse startTime err: [%+v],startTime = %s", err, startTime)
}
edTime, err := time.Parse(constant.StandardTime, endTime)
if err != nil {
log.Printf("Parse endTime err: [%+v],endTime = %s", err, endTime)
}

formatTime := ""
if stTime.Hour() < 10 {
formatTime = fmt.Sprintf("0%d:", stTime.Hour())
} else {
formatTime = fmt.Sprintf("%d:", stTime.Hour())

}

if stTime.Minute() < 10 {
formatTime = fmt.Sprintf("%s0%d~", formatTime, stTime.Minute())
} else {
formatTime = fmt.Sprintf("%s%d~", formatTime, stTime.Minute())
}

if edTime.Hour() < 10 {
formatTime = fmt.Sprintf("%s0%d:", formatTime, edTime.Hour())
} else {
formatTime = fmt.Sprintf("%s%d:", formatTime, edTime.Hour())

}

if edTime.Minute() < 10 {
formatTime = fmt.Sprintf("%s0%d", formatTime, edTime.Minute())
} else {
formatTime = fmt.Sprintf("%s%d", formatTime, edTime.Minute())

}

return formatTime
}

0 comments on commit 95b5383

Please sign in to comment.