string.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package tools
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "io/ioutil"
  7. "strconv"
  8. "time"
  9. )
  10. func StringToInt64(e string) (int64, error) {
  11. return strconv.ParseInt(e, 10, 64)
  12. }
  13. func StringToInt(e string) (int, error) {
  14. return strconv.Atoi(e)
  15. }
  16. func GetCurrntTimeStr() string {
  17. NowTimeZone := time.FixedZone("CST", 8*3600)
  18. //time.Local, _ = time.LoadLocation("Asia/Chongqing")
  19. //timelocal := time.LoadLocation("Asia/Chongqing")
  20. //time.Local = timelocal
  21. //time.LoadLocation("Asia/Chongqing")
  22. //return time.Now().Local().Format("2006-01-02 15:04:05")
  23. return time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
  24. }
  25. func GetCurrntTime() time.Time {
  26. NowTimeZone := time.FixedZone("CST", 8*3600)
  27. return time.Now().In(NowTimeZone)
  28. }
  29. //获取日期
  30. func GetCurrntDateStr() string {
  31. NowTimeZone := time.FixedZone("CST", 8*3600)
  32. return time.Now().In(NowTimeZone).Format("2006-01-02")
  33. }
  34. func GetPayRemainingTime(inDate string) int64 {
  35. NowTimeZone := time.FixedZone("CST", 8*3600)
  36. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  37. stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
  38. sr := stamp.Unix()
  39. timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
  40. now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
  41. return 900 - (now.Unix() - sr)
  42. }
  43. func SubTimeSecond(inDate string) int64 {
  44. NowTimeZone := time.FixedZone("CST", 8*3600)
  45. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  46. stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
  47. sr := stamp.Unix()
  48. timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
  49. now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
  50. fmt.Println("sr = ", sr)
  51. fmt.Println("now", now.Unix())
  52. return now.Unix() - sr
  53. }
  54. //输入日期减去当前日期
  55. func InDateSubNow(inDate string) int64 {
  56. NowTimeZone := time.FixedZone("CST", 8*3600)
  57. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  58. stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
  59. sr := stamp.Unix()
  60. timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
  61. now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
  62. return sr - now.Unix()
  63. }
  64. //func GetCurrntTime() time.Time {
  65. // return time.Now()
  66. //}
  67. func StructToJsonStr(e interface{}) (string, error) {
  68. if b, err := json.Marshal(e); err == nil {
  69. return string(b), err
  70. } else {
  71. return "", err
  72. }
  73. }
  74. func GetBodyString(c *gin.Context) (string, error) {
  75. body, err := ioutil.ReadAll(c.Request.Body)
  76. if err != nil {
  77. fmt.Printf("read body err, %v\n", err)
  78. return string(body), nil
  79. } else {
  80. return "", err
  81. }
  82. }
  83. func JsonStrToMap(e string) (map[string]interface{}, error) {
  84. var dict map[string]interface{}
  85. if err := json.Unmarshal([]byte(e), &dict); err == nil {
  86. return dict, err
  87. } else {
  88. return nil, err
  89. }
  90. }
  91. func StructToMap(data interface{}) (map[string]interface{}, error) {
  92. dataBytes, err := json.Marshal(data)
  93. if err != nil {
  94. return nil, err
  95. }
  96. mapData := make(map[string]interface{})
  97. err = json.Unmarshal(dataBytes, &mapData)
  98. if err != nil {
  99. return nil, err
  100. }
  101. return mapData, nil
  102. }