123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package tools
- import (
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "io/ioutil"
- "strconv"
- "time"
- )
- func StringToInt64(e string) (int64, error) {
- return strconv.ParseInt(e, 10, 64)
- }
- func StringToInt(e string) (int, error) {
- return strconv.Atoi(e)
- }
- func GetCurrntTimeStr() string {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- //time.Local, _ = time.LoadLocation("Asia/Chongqing")
- //timelocal := time.LoadLocation("Asia/Chongqing")
- //time.Local = timelocal
- //time.LoadLocation("Asia/Chongqing")
- //return time.Now().Local().Format("2006-01-02 15:04:05")
- return time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
- }
- func GetCurrntTime() time.Time {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- return time.Now().In(NowTimeZone)
- }
- //获取日期
- func GetCurrntDateStr() string {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- return time.Now().In(NowTimeZone).Format("2006-01-02")
- }
- func GetPayRemainingTime(inDate string) int64 {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- timeLayout := "2006-01-02 15:04:05" //转化所需模板
- stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
- sr := stamp.Unix()
- timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
- now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
- return 900 - (now.Unix() - sr)
- }
- func SubTimeSecond(inDate string) int64 {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- timeLayout := "2006-01-02 15:04:05" //转化所需模板
- stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
- sr := stamp.Unix()
- timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
- now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
- fmt.Println("sr = ", sr)
- fmt.Println("now", now.Unix())
- return now.Unix() - sr
- }
- //输入日期减去当前日期
- func InDateSubNow(inDate string) int64 {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- timeLayout := "2006-01-02 15:04:05" //转化所需模板
- stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
- sr := stamp.Unix()
- timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
- now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
- return sr - now.Unix()
- }
- //func GetCurrntTime() time.Time {
- // return time.Now()
- //}
- func StructToJsonStr(e interface{}) (string, error) {
- if b, err := json.Marshal(e); err == nil {
- return string(b), err
- } else {
- return "", err
- }
- }
- func GetBodyString(c *gin.Context) (string, error) {
- body, err := ioutil.ReadAll(c.Request.Body)
- if err != nil {
- fmt.Printf("read body err, %v\n", err)
- return string(body), nil
- } else {
- return "", err
- }
- }
- func JsonStrToMap(e string) (map[string]interface{}, error) {
- var dict map[string]interface{}
- if err := json.Unmarshal([]byte(e), &dict); err == nil {
- return dict, err
- } else {
- return nil, err
- }
- }
- func StructToMap(data interface{}) (map[string]interface{}, error) {
- dataBytes, err := json.Marshal(data)
- if err != nil {
- return nil, err
- }
- mapData := make(map[string]interface{})
- err = json.Unmarshal(dataBytes, &mapData)
- if err != nil {
- return nil, err
- }
- return mapData, nil
- }
|