Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"snow_flake.go",
"time.go",
"tools.go",
],
importpath = "go-common/app/admin/main/growup/util",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,65 @@
package util
import (
"errors"
"sync"
"time"
)
const (
// MaxSequence max sequence in one time
MaxSequence = 1000
// WorkerBit worker bit
WorkerBit = 10
// SequenceBit sequence bit
SequenceBit = 10
)
// SnowFlake snow flake
type SnowFlake struct {
sync.Mutex
lastTimestamp int64
sequence int64
workerID int64
}
// NewSnowFlake new
func NewSnowFlake() *SnowFlake {
return &SnowFlake{
workerID: time.Now().UnixNano() % 1000,
}
}
// Generate generate
func (s *SnowFlake) Generate() (int64, error) {
s.Lock()
defer s.Unlock()
now := time.Now().UnixNano() / 1e6
if now == s.lastTimestamp {
s.sequence = (s.sequence + 1) % MaxSequence
if s.sequence == 0 {
now = s.waitNextMill(now)
}
} else {
s.sequence = 0
}
if now < s.lastTimestamp {
return 0, errors.New("inner time error")
}
s.lastTimestamp = now
return s.generate() % 1000000000, nil
}
func (s *SnowFlake) generate() int64 {
return (s.lastTimestamp << (WorkerBit + SequenceBit)) | (s.workerID << SequenceBit) | s.sequence
}
func (s *SnowFlake) waitNextMill(t int64) int64 {
for t == s.lastTimestamp {
time.Sleep(100 * time.Microsecond)
t = time.Now().UnixNano() / 1e6
}
return t
}

View File

@@ -0,0 +1,18 @@
package util
import "time"
// ToDayStart .
func ToDayStart(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
}
// ToDayEnd .
func ToDayEnd(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 999, time.Local)
}
// ToDayNoon .
func ToDayNoon(t time.Time) time.Time {
return time.Date(t.Year(), t.Month(), t.Day(), 12, 0, 0, 0, time.Local)
}

View File

@@ -0,0 +1,56 @@
package util
import (
"math"
"math/big"
)
// Div div
func Div(x, y float64) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Quo(a, b)
d, _ := c.Float64()
return d
}
// Mul mul
func Mul(x, y float64) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Mul(a, b)
d, _ := c.Float64()
return d
}
// DivWithRound div with round
func DivWithRound(x, y float64, places int) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Quo(a, b)
d, _ := c.Float64()
return Round(d, places)
}
// MulWithRound mul with round
func MulWithRound(x, y float64, places int) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Mul(a, b)
d, _ := c.Float64()
return Round(d, places)
}
// Round round
func Round(val float64, places int) float64 {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= 0.5 {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}