q5/sysutils.go
aozhiwei fe65489de2 1
2023-09-13 10:31:49 +08:00

196 lines
3.8 KiB
Go

package q5
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"reflect"
"time"
)
func GetDaySeconds(seconds int64, timeZone int64) int64 {
return ((seconds+timeZone*3600)/3600/24+1)*3600*24 - 3600*timeZone
}
func GetTickCount() int64 {
return time.Now().UnixNano() / 1e6
}
func MkUInt16(b1 byte, b2 byte) uint16 {
return uint16(b1) + (uint16(b2) << 8)
}
func MkUInt32(b1 byte, b2 byte, b3 byte, b4 byte) uint32 {
return uint32(b1) +
(uint32(b2) << 8) +
(uint32(b3) << 16) +
(uint32(b4) << 24)
}
func MkInt64(lo32 int32, hi32 int32) int64 {
return int64(lo32) + (int64(hi32) << 32)
}
func Request(r *http.Request, name string) string {
if r.Form == nil {
r.ParseForm()
}
if vs, ok := r.Form[name]; ok {
if len(vs) > 0 {
return vs[0]
}
}
return ""
}
func Response(w *http.ResponseWriter, data string) {
(*w).Write([]byte(data))
}
func ResponseOk(w *http.ResponseWriter) {
(*w).Write([]byte(`{"errcode":0, "errmsg":""}`))
}
func ResponseErr(w *http.ResponseWriter, errCode int32, errMsg string) {
rspObj := map[string]interface{}{}
rspObj["errcode"] = errCode
rspObj["errmsg"] = errMsg
jsonObj, _ := json.Marshal(rspObj)
Response(w, string(jsonObj))
}
func ResponseInt32Ok(w *http.ResponseWriter, key string, val int32) {
(*w).Write([]byte(fmt.Sprintf(`{"errcode":0, "errmsg":"", "%s":%d}`, key, val)))
}
func GetPostBody(r *http.Request) string {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return ""
}
return string(body)
}
func GetRequestRemoteAddr(r *http.Request) string {
remoteAddr := r.RemoteAddr
if ip := r.Header.Get("X-Real-Ip"); ip != "" {
remoteAddr = ip
} else if ip = r.Header.Get("X-Forwarded-For"); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
if remoteAddr == "::1" {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
func ForceCreateDir(dir string) bool {
os.MkdirAll(dir, os.ModePerm)
return true
}
func IsNumberType(v interface{}) bool {
switch reflect.TypeOf(v).Kind() {
case
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64:
return true
default:
return false
}
}
func FormatUnixDateTime(sec int64, loc *time.Location) string {
strTime := time.Unix(sec, 0).In(loc). Format("2006-01-02 15:04:05")
return strTime
}
func FormatUnixDate(sec int64, loc *time.Location) string {
strTime := time.Unix(sec, 0).In(loc).Format("2006-01-02")
return strTime
}
func FormatUnixDateEx(sec int64, loc *time.Location) string {
strTime := time.Unix(sec, 0).In(loc).Format("20060102")
return strTime
}
func HasBitFlag(val int64, bitNum int32) bool {
if bitNum < 0 || bitNum > 63 {
return false
}
mask := int64(1) << bitNum
return (val & mask) != 0
}
func SetBitFlag(val *int64, bitNum int32) {
if bitNum < 0 || bitNum > 63 {
return
}
*val |= int64(1) << bitNum
}
func UnSetBitFlag(val *int64, bitNum int32) {
if bitNum < 0 || bitNum > 63 {
return
}
*val &= ^(int64(1) << bitNum)
}
func GetLocalIP() string {
interfaces, err := net.Interfaces()
if err != nil {
panic(err)
}
ipList := []string{}
for _, iface := range interfaces {
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue
}
addrs, err := iface.Addrs()
if err != nil {
panic(err)
}
for _, addr := range addrs {
ipnet, ok := addr.(*net.IPNet)
if !ok || ipnet.IP.IsLoopback() {
continue
}
if ipnet.IP.To4() != nil {
ipList = append(ipList, ipnet.IP.String())
}
}
}
if len(ipList) != 1 {
panic("GetLocalIP error")
} else {
return ipList[0]
}
}
func NewSlice[T any](s *[]T ,len int32, cap int32){
*s = make([]T, len, cap)
}
func NewSliceElement[T any](s *[]T) *T {
v := new(T)
*s = append(*s, *v)
return v
}