string.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 SubTimeSecond(inDate string) int64 {
  26. NowTimeZone := time.FixedZone("CST", 8*3600)
  27. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  28. stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
  29. sr := stamp.Unix()
  30. timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
  31. now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
  32. return now.Unix() - sr
  33. }
  34. //获取日期
  35. func GetCurrntDateStr() string {
  36. NowTimeZone := time.FixedZone("CST", 8*3600)
  37. return time.Now().In(NowTimeZone).Format("2006-01-02")
  38. }
  39. func GetPayRemainingTime(inDate string) int64 {
  40. NowTimeZone := time.FixedZone("CST", 8*3600)
  41. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  42. //fmt.Println("inDate = ", inDate)
  43. stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
  44. sr := stamp.Unix()
  45. timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
  46. now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
  47. fmt.Println("sr = ", sr)
  48. fmt.Println("timestamp", now.Unix())
  49. return 900 - (now.Unix() - sr)
  50. }
  51. //输入日期减去当前日期
  52. func InDateSubNow(inDate string) int64 {
  53. NowTimeZone := time.FixedZone("CST", 8*3600)
  54. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  55. //fmt.Println("inDate = ", inDate)
  56. stamp, _ := time.ParseInLocation(timeLayout, inDate, time.Local)
  57. sr := stamp.Unix()
  58. timestamp := time.Now().In(NowTimeZone).Format("2006-01-02 15:04:05")
  59. now, _ := time.ParseInLocation(timeLayout, timestamp, time.Local)
  60. return sr - now.Unix()
  61. }
  62. //输入日期相减得出天数
  63. func DateSubDate(startDate string, endDate string) (float64, error) {
  64. timeLayout := "2006-01-02" //转化所需模板
  65. start, err := time.ParseInLocation(timeLayout, startDate, time.Local)
  66. if err != nil {
  67. return 0, err
  68. }
  69. end, err := time.ParseInLocation(timeLayout, endDate, time.Local)
  70. if err != nil {
  71. return 0, err
  72. }
  73. return start.Sub(end).Hours(), nil
  74. }
  75. //加天数
  76. func DateAdd(startDate string, num time.Duration) (string, error) {
  77. timeLayout := "2006-01-02" //转化所需模板
  78. start, err := time.ParseInLocation(timeLayout, startDate, time.Local)
  79. if err != nil {
  80. return "", err
  81. }
  82. return start.Add(num).Format("2006-01-02"), nil
  83. }
  84. func GetCurrntTime() time.Time {
  85. NowTimeZone := time.FixedZone("CST", 8*3600)
  86. return time.Now().In(NowTimeZone)
  87. }
  88. func GetNowTimeStamp() int64 {
  89. return time.Now().Unix()
  90. }
  91. func GetTimeStamp(timeIn string) (int64, error) {
  92. tm2, err := time.Parse("2006-01-02 15:04:05", timeIn)
  93. if err != nil {
  94. return 0, err
  95. }
  96. return tm2.Unix(), nil
  97. }
  98. func StructToJsonStr(e interface{}) (string, error) {
  99. if b, err := json.Marshal(e); err == nil {
  100. return string(b), err
  101. } else {
  102. return "", err
  103. }
  104. }
  105. func GetBodyString(c *gin.Context) (string, error) {
  106. body, err := ioutil.ReadAll(c.Request.Body)
  107. if err != nil {
  108. fmt.Printf("read body err, %v\n", err)
  109. return string(body), nil
  110. } else {
  111. return "", err
  112. }
  113. }
  114. func JsonStrToMap(e string) (map[string]interface{}, error) {
  115. var dict map[string]interface{}
  116. if err := json.Unmarshal([]byte(e), &dict); err == nil {
  117. return dict, err
  118. } else {
  119. return nil, err
  120. }
  121. }
  122. func StructToMap(data interface{}) (map[string]interface{}, error) {
  123. dataBytes, err := json.Marshal(data)
  124. if err != nil {
  125. return nil, err
  126. }
  127. mapData := make(map[string]interface{})
  128. err = json.Unmarshal(dataBytes, &mapData)
  129. if err != nil {
  130. return nil, err
  131. }
  132. return mapData, nil
  133. }