Skip to content

Commit

Permalink
one
Browse files Browse the repository at this point in the history
  • Loading branch information
tianjun committed Oct 17, 2022
1 parent f255fe0 commit 7d82462
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.vscode
# Dependency directories (remove the comment below to include it)
# vendor/
171 changes: 171 additions & 0 deletions ConvertStrings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package gotool

import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"math"
"reflect"
"strconv"
)

// ToString 转换成string
func ToString(value any) string {
result := ""
if value == nil {
return result
}

v := reflect.ValueOf(value)

switch value.(type) {
case float32, float64:
result = strconv.FormatFloat(v.Float(), 'f', -1, 64)
return result
case int, int8, int16, int32, int64:
result = strconv.FormatInt(v.Int(), 10)
return result
case uint, uint8, uint16, uint32, uint64:
result = strconv.FormatUint(v.Uint(), 10)
return result
case string:
result = v.String()
return result
case []byte:
result = string(v.Bytes())
return result
default:
newValue, _ := json.Marshal(value)
result = string(newValue)
return result
}
}

// ToInt64 将值转换为 int64,如果输入不是数字格式,则返回 0 和错误
func ToInt64(value any) (int64, error) {
v := reflect.ValueOf(value)
var result int64
err := fmt.Errorf("ToInt: invalid interface type %T", value)
switch value.(type) {
case int, int8, int16, int32, int64:
result = v.Int()
return result, nil
case uint, uint8, uint16, uint32, uint64:
result = int64(v.Uint())
return result, nil
case float32, float64:
result = int64(v.Float())
return result, nil
case string:
result, err = strconv.ParseInt(v.String(), 0, 64)
if err != nil {
result = 0
}
return result, err
default:
return result, err
}
}

// ToInt 将值转换为 int/int32
func ToInt(s any) int {
val, _ := ToInt64(s)
return int(val)
}

// ToFloat 将值转换为 float64,如果输入不是浮点数,则返回 0.0 和错误
func ToFloat(value any) (float64, error) {
v := reflect.ValueOf(value)
result := 0.0
err := fmt.Errorf("ToInt: unvalid interface type %T", value)
switch value.(type) {
case int, int8, int16, int32, int64:
result = float64(v.Int())
return result, nil
case uint, uint8, uint16, uint32, uint64:
result = float64(v.Uint())
return result, nil
case float32, float64:
result = v.Float()
return result, nil
case string:
result, err = strconv.ParseFloat(v.String(), 64)
if err != nil {
result = 0.0
}
return result, err
default:
return result, err
}
}

// ToJson 将值转换为有效的 json 字符串
func ToJson(value any) (string, error) {
result, err := json.Marshal(value)
if err != nil {
return "", err
}

return string(result), nil
}

// ToBytes 将接口转换为字节
func ToBytes(value any) ([]byte, error) {
v := reflect.ValueOf(value)

switch value.(type) {
case int, int8, int16, int32, int64:
number := v.Int()
buf := bytes.NewBuffer([]byte{})
buf.Reset()
err := binary.Write(buf, binary.BigEndian, number)
return buf.Bytes(), err
case uint, uint8, uint16, uint32, uint64:
number := v.Uint()
buf := bytes.NewBuffer([]byte{})
buf.Reset()
err := binary.Write(buf, binary.BigEndian, number)
return buf.Bytes(), err
case float32:
number := float32(v.Float())
bits := math.Float32bits(number)
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes, bits)
return bytes, nil
case float64:
number := v.Float()
bits := math.Float64bits(number)
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, bits)
return bytes, nil
case bool:
return strconv.AppendBool([]byte{}, v.Bool()), nil
case string:
return []byte(v.String()), nil
case []byte:
return v.Bytes(), nil
default:
newValue, err := json.Marshal(value)
return newValue, err
}
}

// ToBool 将字符串转换为布尔值
func ToBool(s string) (bool, error) {
return strconv.ParseBool(s)
}

// Rounding 四舍五入,ROUND_HALF_UP 模式实现
// 返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零。
func Rounding(val float64, precision int) float64 {
if precision == 0 {
return math.Round(val)
}
p := math.Pow10(precision)
if precision < 0 {
return math.Floor(val*p+0.5) * math.Pow10(-precision)
}

return math.Floor(val*p+0.5) / p
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/hunktj/gotool

go 1.18
82 changes: 82 additions & 0 deletions gotime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package gotool

import (
"time"
)

//获取当前时间
func GetNowTime() string {
nowTime := time.Now().Format("2006-01-02 15:04:05 ")
return nowTime
}

//获取当前时间戳
func GetNowTimeUinx() int64 {
nowTimeUnix := time.Now().Unix()
return nowTimeUnix
}

// StrToUnix 时间->时间戳 layout:="2006-01-02 15:04:05"
func StrToUnix(timeStr, layout string) (int64, error) {
local, err := time.LoadLocation("Asia/Shanghai") //设置时区
if err != nil {
return 0, err
}
tt, err := time.ParseInLocation(layout, timeStr, local)
if err != nil {
return 0, err
}
timeUnix := tt.Unix()
return timeUnix, nil
}

// UnixToStr 时间戳->时间 layout:="2006-01-02 15:04:05"
func UnixToStr(timeUnix int64, layout string) string {
timeStr := time.Unix(timeUnix, 0).Format(layout)
return timeStr
}

// GetTime 根据日期返回当月的第一天和最后一天的时间戳
// date= 2022-6/2022-06 返回第一天和最后一天
// date= 2000-06-08/2000-6-8 返回当天的时间戳
func GetTime(date string) (int, int) {
if date == "" {
date = time.Now().Format("2006-1")
}
stime := 0
etime := 0
if len(date) <= 7 {
tt, _ := time.ParseInLocation("2006-1", date, time.Local)
stime = (int(tt.Unix()) + 8*3600) / (3600 * 24)
etime = (int(tt.AddDate(0, 1, -1).Unix()) + 8*3600) / (3600 * 24)
} else {
tt, _ := time.ParseInLocation("2006-1-2", date, time.Local)
stime = (int(tt.Unix()) + 8*3600) / (3600 * 24)
etime = stime
}
return stime, etime
}

//返回一天的开始时间戳和最后时间戳如:2022-10-12 00:00:00 -- 2022-10-12 23:59:59
func DayStimeAndEtime(times string) (int64, int64) {
loc, _ := time.LoadLocation("Local")
var T time.Time
if len(times) == 10 {
tt, err := time.ParseInLocation("2006-01-02 ", times, loc)
if err != nil {
return 0, 0
}
T = tt
} else {
ti := times[0:11]
tt, err := time.ParseInLocation("2006-01-02 ", ti, loc)
if err != nil {
return 0, 0
}
T = tt
}

sTime := T.Unix()
eTime := sTime + 86399
return sTime, eTime
}
17 changes: 17 additions & 0 deletions test/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

func main() {
// //ils := gotools.ToString(23)
// num := int64(34)
// sdd := gotool.ToInt(num)
// //r := gotools.Rounding(99449.543556, 3)
// tmp := "2006-01-02 15:04:05"
// //tm := gotools.UnixTimeToStr(1661843160, tmp)
// stm, _ := gotool.StrToUnix("2022-08-30 15:09:16", tmp)
// nowtime := gotool.GetNowTime()
// smp, emp := gotool.DayStimeAndEtime(nowtime)

// nowunixtime := gotool.GetNowTimeUinx()
// //startTime, endTime := gotools.GetTime("2022-06")
// fmt.Println(stm, sdd, smp, emp, nowtime, nowunixtime)
}

0 comments on commit 7d82462

Please sign in to comment.