123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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 GetTimestamp(change string) string {
- t, _ := time.Parse(time.RFC3339, change)
- timeUint := t.In(time.Local).Format("2006-01-02 15:04:05")
- return timeUint
- }
- 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 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)
- return now.Unix() - sr
- }
- //获取日期
- 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" //转化所需模板
- //fmt.Println("inDate = ", inDate)
- 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("timestamp", now.Unix())
- return 900 - (now.Unix() - sr)
- }
- //输入日期减去当前日期
- func InDateSubNow(inDate string) int64 {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- timeLayout := "2006-01-02 15:04:05" //转化所需模板
- //fmt.Println("inDate = ", inDate)
- 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 DateSubDate(startDate string, endDate string) (float64, error) {
- timeLayout := "2006-01-02" //转化所需模板
- start, err := time.ParseInLocation(timeLayout, startDate, time.Local)
- if err != nil {
- return 0, err
- }
- end, err := time.ParseInLocation(timeLayout, endDate, time.Local)
- if err != nil {
- return 0, err
- }
- return start.Sub(end).Hours(), nil
- }
- func TimeToStr(tm int64) string {
- timeLayout := "2006-01-02 15:04:05"
- // time.Unix的第二个参数传递0或10结果一样,因为都不大于1e9
- timeStr := time.Unix(tm, 0).Format(timeLayout)
- return timeStr
- }
- //加天数
- func DateAdd(startDate string, num time.Duration) (string, error) {
- timeLayout := "2006-01-02" //转化所需模板
- start, err := time.ParseInLocation(timeLayout, startDate, time.Local)
- if err != nil {
- return "", err
- }
- return start.Add(num).Format("2006-01-02"), nil
- }
- func GetCurrntTime() time.Time {
- NowTimeZone := time.FixedZone("CST", 8*3600)
- return time.Now().In(NowTimeZone)
- }
- func GetNowTimeStamp() int64 {
- return time.Now().Unix()
- }
- func GetTimeStamp(timeIn string) (int64, error) {
- tm2, err := time.Parse("2006-01-02 15:04:05", timeIn)
- if err != nil {
- return 0, err
- }
- return tm2.Unix() - 8*3600, nil
- }
- 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
- }
|