Skip to content

Commit

Permalink
add times tools
Browse files Browse the repository at this point in the history
  • Loading branch information
hootuu committed Oct 25, 2023
1 parent 023eb8e commit c7dd48f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
# Go workspace file
go.work

.idea
.idea
/go.sum
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
)
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions sys/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...))
}
Expand Down
76 changes: 76 additions & 0 deletions times/dt.go
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions times/strs.go
Original file line number Diff line number Diff line change
@@ -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")
}

0 comments on commit c7dd48f

Please sign in to comment.