q5/sysutils.go
aozhiwei 0644461bc7 1
2023-08-12 20:23:16 +08:00

134 lines
2.6 KiB
Go

package q5
import (
"os"
"net"
"fmt"
"net/http"
"time"
//"io/ioutil"
"reflect"
)
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) {
/*respObj := NewMxoObject()
respObj.SetXValue("errcode", NewXInt32(errCode))
respObj.SetXValue("errmsg", NewXString(errMsg))
Response(w, respObj.ToJsonStr())*/
}
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) *XValue {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return NewXString("")
}
v := NewXString(string(body))
return v
}
*/
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) string {
strTime := time.Unix(sec, 0).Format("2006-01-02 15:04:05")
return strTime
}
func FormatUnixDate(sec int64) string {
strTime := time.Unix(sec, 0).Format("2006-01-02")
return strTime
}
func FormatUnixDateEx(sec int64) string {
strTime := time.Unix(sec, 0).Format("20060102")
return strTime
}