string.go 4.4 KB

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