diff --git a/.gitignore b/.gitignore index 82c4ca1..7e5bcfe 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ # Go workspace file go.work -.idea \ No newline at end of file +.idea +/go.sum diff --git a/go.mod b/go.mod index f1ec139..73f0d14 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,8 @@ require ( github.com/gookit/color v1.5.4 github.com/rs/xid v1.5.0 github.com/spf13/viper v1.16.0 + go.uber.org/zap v1.26.0 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( @@ -21,10 +23,8 @@ require ( github.com/subosito/gotenv v1.4.2 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect go.uber.org/multierr v1.10.0 // indirect - go.uber.org/zap v1.26.0 // indirect golang.org/x/sys v0.10.0 // indirect golang.org/x/text v0.9.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect - gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/logger/logger.go b/logger/logger.go index 3d85fe3..f310b95 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -37,7 +37,7 @@ func LevelOf(level Level) zapcore.Level { } func GetLogger(key string) *zap.Logger { - rootPath := configure.GetString("logger."+key+".root", "./logs/"+key+"/") + rootPath := configure.GetString("logger."+key+".root", "./.logs/"+key+"/") logLevel := configure.GetString("logger."+key+".level", "debug") fileName := rootPath + configure.GetString("logger."+key+".file", key+".jsonl") maxSize := configure.GetInt("logger."+key+".size.max", 128) diff --git a/sys/color.go b/sys/color.go index 62e3937..6eaa8db 100644 --- a/sys/color.go +++ b/sys/color.go @@ -6,6 +6,24 @@ import ( "strings" ) +type colorWriter struct { +} + +func (c *colorWriter) Write(p []byte) (n int, err error) { + Info(string(p)) + return len(p), nil +} + +type noneWriter struct { +} + +func (c *noneWriter) Write(p []byte) (n int, err error) { + return len(p), nil +} + +var ColorWriter = &colorWriter{} +var NoneWriter = &noneWriter{} + func Info(arg ...any) { color.Style{color.FgBlue}.Println(doFormat(arg...)) } diff --git a/times/dt.go b/times/dt.go new file mode 100644 index 0000000..cf0d1e6 --- /dev/null +++ b/times/dt.go @@ -0,0 +1,76 @@ +package times + +import "time" + +func GetTomorrow(t time.Time) time.Time { + return t.AddDate(0, 0, 1) +} + +func GetYesterday(t time.Time) time.Time { + return t.AddDate(0, 0, -1) +} + +func GetDtRange(t time.Time) (time.Time, time.Time) { + start := time.Date( + t.Year(), t.Month(), t.Day(), + 0, 0, 0, 0, + t.Location(), + ) + end := time.Date( + t.Year(), t.Month(), t.Day(), + 23, 59, 59, int(time.Second-time.Nanosecond), + t.Location(), + ) + return start, end +} + +func GetWeekRange(t time.Time) (time.Time, time.Time) { + firstDay := t.AddDate(0, 0, -int(t.Weekday())) + lastDay := firstDay.AddDate(0, 0, 6) + start := time.Date( + firstDay.Year(), firstDay.Month(), firstDay.Day(), + 0, 0, 0, 0, t.Location(), + ) + end := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), + 23, 59, 59, int(time.Second-time.Nanosecond), t.Location(), + ) + return start, end +} + +func GetMonthRange(t time.Time) (time.Time, time.Time) { + firstDay := time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location()) + nextMonth := firstDay.AddDate(0, 1, 0) + lastDay := nextMonth.AddDate(0, 0, -1) + start := time.Date( + firstDay.Year(), firstDay.Month(), firstDay.Day(), + 0, 0, 0, 0, t.Location(), + ) + end := time.Date( + lastDay.Year(), lastDay.Month(), lastDay.Day(), + 23, 59, 59, int(time.Second-time.Nanosecond), t.Location(), + ) + return start, end +} + +func GetQuarterRange(t time.Time) (time.Time, time.Time) { + quarterStartMonth := time.Month(((t.Month()-1)/3)*3 + 1) + firstDay := time.Date(t.Year(), quarterStartMonth, 1, 0, 0, 0, 0, t.Location()) + nextQuarter := firstDay.AddDate(0, 3, 0) + lastDay := nextQuarter.AddDate(0, 0, -1) + start := time.Date(firstDay.Year(), firstDay.Month(), firstDay.Day(), + 0, 0, 0, 0, t.Location()) + end := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), + 23, 59, 59, int(time.Second-time.Nanosecond), t.Location()) + return start, end +} + +func GetYearRange(t time.Time) (time.Time, time.Time) { + firstDay := time.Date(t.Year(), time.January, 1, 0, 0, 0, 0, t.Location()) + nextYear := firstDay.AddDate(1, 0, 0) + lastDay := nextYear.AddDate(0, 0, -1) + start := time.Date(firstDay.Year(), firstDay.Month(), firstDay.Day(), + 0, 0, 0, 0, t.Location()) + end := time.Date(lastDay.Year(), lastDay.Month(), lastDay.Day(), + 23, 59, 59, int(time.Second-time.Nanosecond), t.Location()) + return start, end +} diff --git a/times/strs.go b/times/strs.go new file mode 100644 index 0000000..ce0c859 --- /dev/null +++ b/times/strs.go @@ -0,0 +1,11 @@ +package times + +import "time" + +func ToDate(t time.Time) string { + return t.Format("2006-01-02") +} + +func ToTimestamp(t time.Time) string { + return t.Format("2006-01-02 15:04:05") +}