common.go 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. package utils
  2. import (
  3. "bufio"
  4. "crypto/md5"
  5. "crypto/sha1"
  6. "encoding/base64"
  7. "encoding/hex"
  8. "encoding/json"
  9. "errors"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "image"
  13. "image/png"
  14. "io"
  15. "math"
  16. "math/rand"
  17. "net"
  18. "net/http"
  19. "os"
  20. "path"
  21. "regexp"
  22. "runtime"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "unicode"
  27. )
  28. func GetRandString(size int) string {
  29. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "!", "@", "#", "$", "%", "^", "&", "*"}
  30. randomSb := ""
  31. digitSize := len(allLetterDigit)
  32. // 随机数种子
  33. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  34. for i := 0; i < size; i++ {
  35. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  36. }
  37. return randomSb
  38. }
  39. func GetRandStringNoSpecialChar(size int) string {
  40. allLetterDigit := []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
  41. randomSb := ""
  42. digitSize := len(allLetterDigit)
  43. // 随机数种子
  44. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  45. for i := 0; i < size; i++ {
  46. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  47. }
  48. return randomSb
  49. }
  50. func StringsToJSON(str string) string {
  51. rs := []rune(str)
  52. jsons := ""
  53. for _, r := range rs {
  54. rint := int(r)
  55. if rint < 128 {
  56. jsons += string(r)
  57. } else {
  58. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  59. }
  60. }
  61. return jsons
  62. }
  63. func ToString(v interface{}) string {
  64. data, _ := json.Marshal(v)
  65. return string(data)
  66. }
  67. func MD5(data string) string {
  68. m := md5.Sum([]byte(data))
  69. return hex.EncodeToString(m[:])
  70. }
  71. func GetRandDigit(n int) string {
  72. // 随机数种子
  73. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  74. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  75. }
  76. func GetToday(format string) string {
  77. today := time.Now().Format(format)
  78. return today
  79. }
  80. func GetTodayLastSecond() time.Duration {
  81. today := GetToday(FormatDate) + " 23:59:59"
  82. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  83. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  84. }
  85. func GetBrithDate(idcard string) string {
  86. l := len(idcard)
  87. var s string
  88. if l == 15 {
  89. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  90. return s
  91. }
  92. if l == 18 {
  93. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  94. return s
  95. }
  96. return GetToday(FormatDate)
  97. }
  98. func WhichSexByIdcard(idcard string) string {
  99. var sexs = [2]string{"女", "男"}
  100. length := len(idcard)
  101. if length == 18 {
  102. sex, _ := strconv.Atoi(string(idcard[16]))
  103. return sexs[sex%2]
  104. } else if length == 15 {
  105. sex, _ := strconv.Atoi(string(idcard[14]))
  106. return sexs[sex%2]
  107. }
  108. return "男"
  109. }
  110. func SubFloatToString(f float64, m int) string {
  111. n := strconv.FormatFloat(f, 'f', -1, 64)
  112. if n == "" {
  113. return ""
  114. }
  115. if m >= len(n) {
  116. return n
  117. }
  118. newn := strings.Split(n, ".")
  119. if m == 0 {
  120. return newn[0]
  121. }
  122. if len(newn) < 2 || m >= len(newn[1]) {
  123. return n
  124. }
  125. return newn[0] + "." + newn[1][:m]
  126. }
  127. func SubFloatToFloat(f float64, m int) float64 {
  128. newn := SubFloatToString(f, m)
  129. newf, _ := strconv.ParseFloat(newn, 64)
  130. return newf
  131. }
  132. func SubFloatToFloatStr(f float64, m int) string {
  133. newn := SubFloatToString(f, m)
  134. return newn
  135. }
  136. func GetYearDiffer(start_time, end_time string) int {
  137. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  138. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  139. age := t2.Year() - t1.Year()
  140. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  141. age--
  142. }
  143. return age
  144. }
  145. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  146. diff := end_time.Unix() - start_time.Unix()
  147. return diff
  148. }
  149. func FixFloat(f float64, m int) float64 {
  150. newn := SubFloatToString(f+0.00000001, m)
  151. newf, _ := strconv.ParseFloat(newn, 64)
  152. return newf
  153. }
  154. func StrListToString(strList []string) (str string) {
  155. if len(strList) > 0 {
  156. for k, v := range strList {
  157. if k == 0 {
  158. str = v
  159. } else {
  160. str = str + "," + v
  161. }
  162. }
  163. return
  164. }
  165. return ""
  166. }
  167. func GetToken() string {
  168. randStr := GetRandString(64)
  169. token := MD5(randStr + Md5Key)
  170. tokenLen := 64 - len(token)
  171. return strings.ToUpper(token + GetRandString(tokenLen))
  172. }
  173. func ErrNoRow() string {
  174. return "record not found"
  175. }
  176. func FileIsExist(filePath string) bool {
  177. _, err := os.Stat(filePath)
  178. return err == nil || os.IsExist(err)
  179. }
  180. func GetImgExt(file string) (ext string, err error) {
  181. var headerByte []byte
  182. headerByte = make([]byte, 8)
  183. fd, err := os.Open(file)
  184. if err != nil {
  185. return "", err
  186. }
  187. defer fd.Close()
  188. _, err = fd.Read(headerByte)
  189. if err != nil {
  190. return "", err
  191. }
  192. xStr := fmt.Sprintf("%x", headerByte)
  193. switch {
  194. case xStr == "89504e470d0a1a0a":
  195. ext = ".png"
  196. case xStr == "0000010001002020":
  197. ext = ".ico"
  198. case xStr == "0000020001002020":
  199. ext = ".cur"
  200. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  201. ext = ".gif"
  202. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  203. ext = ".tga"
  204. case xStr[:8] == "464f524d":
  205. ext = ".iff"
  206. case xStr[:8] == "52494646":
  207. ext = ".ani"
  208. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  209. ext = ".tiff"
  210. case xStr[:4] == "424d":
  211. ext = ".bmp"
  212. case xStr[:4] == "ffd8":
  213. ext = ".jpg"
  214. case xStr[:2] == "0a":
  215. ext = ".pcx"
  216. default:
  217. ext = ""
  218. }
  219. return ext, nil
  220. }
  221. func SaveImage(path string, img image.Image) (err error) {
  222. imgfile, err := os.Create(path)
  223. defer imgfile.Close()
  224. err = png.Encode(imgfile, img)
  225. return err
  226. }
  227. func DownloadImage(imgUrl string) (filePath string, err error) {
  228. imgPath := "./static/imgs/"
  229. fileName := path.Base(imgUrl)
  230. res, err := http.Get(imgUrl)
  231. if err != nil {
  232. fmt.Println("A error occurred!")
  233. return
  234. }
  235. defer res.Body.Close()
  236. reader := bufio.NewReaderSize(res.Body, 32*1024)
  237. filePath = imgPath + fileName
  238. file, err := os.Create(filePath)
  239. if err != nil {
  240. return
  241. }
  242. writer := bufio.NewWriter(file)
  243. written, _ := io.Copy(writer, reader)
  244. fmt.Printf("Total length: %d \n", written)
  245. return
  246. }
  247. func SaveBase64ToFile(content, path string) error {
  248. data, err := base64.StdEncoding.DecodeString(content)
  249. if err != nil {
  250. return err
  251. }
  252. f, err := os.Create(path)
  253. defer f.Close()
  254. if err != nil {
  255. return err
  256. }
  257. f.Write(data)
  258. return nil
  259. }
  260. func SaveBase64ToFileBySeek(content, path string) (err error) {
  261. data, err := base64.StdEncoding.DecodeString(content)
  262. exist, err := PathExists(path)
  263. if err != nil {
  264. return
  265. }
  266. if !exist {
  267. f, err := os.Create(path)
  268. if err != nil {
  269. return err
  270. }
  271. n, _ := f.Seek(0, 2)
  272. _, err = f.WriteAt([]byte(data), n)
  273. defer f.Close()
  274. } else {
  275. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  276. if err != nil {
  277. return err
  278. }
  279. n, _ := f.Seek(0, 2)
  280. _, err = f.WriteAt([]byte(data), n)
  281. defer f.Close()
  282. }
  283. return nil
  284. }
  285. func PathExists(path string) (bool, error) {
  286. _, err := os.Stat(path)
  287. if err == nil {
  288. return true, nil
  289. }
  290. if os.IsNotExist(err) {
  291. return false, nil
  292. }
  293. return false, err
  294. }
  295. func StartIndex(page, pagesize int) int {
  296. if page > 1 {
  297. return (page - 1) * pagesize
  298. }
  299. return 0
  300. }
  301. func PageCount(count, pagesize int) int {
  302. if count%pagesize > 0 {
  303. return count/pagesize + 1
  304. } else {
  305. return count / pagesize
  306. }
  307. }
  308. func TrimHtml(src string) string {
  309. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  310. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  311. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  312. src = re.ReplaceAllString(src, "[图片]")
  313. re, _ = regexp.Compile("class[\\S\\s]+?>")
  314. src = re.ReplaceAllString(src, "")
  315. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  316. src = re.ReplaceAllString(src, "")
  317. return strings.TrimSpace(src)
  318. }
  319. func TimeToTimestamp() {
  320. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  321. }
  322. func ToUnicode(text string) string {
  323. textQuoted := strconv.QuoteToASCII(text)
  324. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  325. return textUnquoted
  326. }
  327. func VersionToInt(version string) int {
  328. version = strings.Replace(version, ".", "", -1)
  329. n, _ := strconv.Atoi(version)
  330. return n
  331. }
  332. func IsCheckInList(list []int, s int) bool {
  333. for _, v := range list {
  334. if v == s {
  335. return true
  336. }
  337. }
  338. return false
  339. }
  340. func round(num float64) int {
  341. return int(num + math.Copysign(0.5, num))
  342. }
  343. func toFixed(num float64, precision int) float64 {
  344. output := math.Pow(10, float64(precision))
  345. return float64(round(num*output)) / output
  346. }
  347. func GetWilsonScore(p, n float64) float64 {
  348. if p == 0 && n == 0 {
  349. return 0
  350. }
  351. return toFixed(((p+1.9208)/(p+n)-1.96*math.Sqrt(p*n/(p+n)+0.9604)/(p+n))/(1+3.8416/(p+n)), 2)
  352. }
  353. func ChangeWordsToNum(str string) (numStr string) {
  354. words := ([]rune)(str)
  355. num := 0
  356. n := 0
  357. for i := 0; i < len(words); i++ {
  358. word := string(words[i : i+1])
  359. switch word {
  360. case "万":
  361. if n == 0 {
  362. n = 1
  363. }
  364. n = n * 10000
  365. num = num*10000 + n
  366. n = 0
  367. case "千":
  368. if n == 0 {
  369. n = 1
  370. }
  371. n = n * 1000
  372. num += n
  373. n = 0
  374. case "百":
  375. if n == 0 {
  376. n = 1
  377. }
  378. n = n * 100
  379. num += n
  380. n = 0
  381. case "十":
  382. if n == 0 {
  383. n = 1
  384. }
  385. n = n * 10
  386. num += n
  387. n = 0
  388. case "一":
  389. n += 1
  390. case "二":
  391. n += 2
  392. case "三":
  393. n += 3
  394. case "四":
  395. n += 4
  396. case "五":
  397. n += 5
  398. case "六":
  399. n += 6
  400. case "七":
  401. n += 7
  402. case "八":
  403. n += 8
  404. case "九":
  405. n += 9
  406. case "零":
  407. default:
  408. if n > 0 {
  409. num += n
  410. n = 0
  411. }
  412. if num == 0 {
  413. numStr += word
  414. } else {
  415. numStr += strconv.Itoa(num) + word
  416. num = 0
  417. }
  418. }
  419. }
  420. if n > 0 {
  421. num += n
  422. n = 0
  423. }
  424. if num != 0 {
  425. numStr += strconv.Itoa(num)
  426. }
  427. return
  428. }
  429. func Sha1(data string) string {
  430. sha1 := sha1.New()
  431. sha1.Write([]byte(data))
  432. return hex.EncodeToString(sha1.Sum([]byte("")))
  433. }
  434. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  435. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  436. tradeCode = strings.Trim(tradeCode, " ")
  437. tradeCodeInt, err := strconv.Atoi(tradeCode)
  438. if err != nil {
  439. return
  440. }
  441. tradeCodeInt = tradeCodeInt + 1
  442. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  443. return
  444. }
  445. func ConvertToFormatDay(excelDaysString string) string {
  446. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  447. curDiffDay := excelDaysString
  448. b, _ := strconv.Atoi(curDiffDay)
  449. realDiffDay := b - baseDiffDay
  450. realDiffSecond := realDiffDay * 24 * 3600
  451. baseOriginSecond := 1136185445
  452. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  453. return resultTime
  454. }
  455. func CheckPwd(pwd string) bool {
  456. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  457. reg := regexp.MustCompile(compile)
  458. flag := reg.MatchString(pwd)
  459. return flag
  460. }
  461. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  462. if len(myMonth) == 1 {
  463. myMonth = "0" + myMonth
  464. }
  465. yInt, _ := strconv.Atoi(myYear)
  466. timeLayout := "2006-01-02 15:04:05"
  467. loc, _ := time.LoadLocation("Local")
  468. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  469. newMonth := theTime.Month()
  470. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  471. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  472. return t1, t2
  473. }
  474. func TrimStr(str string) (str2 string) {
  475. if str == "" {
  476. return str
  477. }
  478. return strings.Replace(str, " ", "", -1)
  479. }
  480. func StrTimeToTime(strTime string) time.Time {
  481. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  482. loc, _ := time.LoadLocation("Local") //重要:获取时区
  483. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  484. return resultTime
  485. }
  486. func StrDateTimeToWeek(strTime string) string {
  487. var WeekDayMap = map[string]string{
  488. "Monday": "周一",
  489. "Tuesday": "周二",
  490. "Wednesday": "周三",
  491. "Thursday": "周四",
  492. "Friday": "周五",
  493. "Saturday": "周六",
  494. "Sunday": "周日",
  495. }
  496. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  497. startday, _ := time.ParseInLocation("2006-01-02", ctime, time.Local)
  498. staweek_int := startday.Weekday().String()
  499. return WeekDayMap[staweek_int]
  500. }
  501. func TimeToStrYmd(time2 time.Time) string {
  502. var Ymd string
  503. year := time2.Year()
  504. month := time2.Format("1")
  505. day1 := time.Now().Day()
  506. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  507. return Ymd
  508. }
  509. func TimeRemoveHms(strTime string) string {
  510. var Ymd string
  511. var resultTime = StrTimeToTime(strTime)
  512. year := resultTime.Year()
  513. month := resultTime.Format("01")
  514. day1 := resultTime.Day()
  515. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  516. return Ymd
  517. }
  518. func TimeRemoveHms2(strTime string) string {
  519. var Ymd string
  520. var resultTime = StrTimeToTime(strTime)
  521. year := resultTime.Year()
  522. month := resultTime.Format("01")
  523. day1 := resultTime.Day()
  524. Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
  525. return Ymd
  526. }
  527. func ArticleLastTime(strTime string) string {
  528. var newTime string
  529. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  530. diffTime := time.Now().Unix() - stamp.Unix()
  531. if diffTime <= 60 {
  532. newTime = "当前"
  533. } else if diffTime < 60*60 {
  534. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  535. } else if diffTime < 24*60*60 {
  536. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  537. } else if diffTime < 30*24*60*60 {
  538. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  539. } else if diffTime < 12*30*24*60*60 {
  540. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  541. } else {
  542. newTime = "1年前"
  543. }
  544. return newTime
  545. }
  546. func ConvertNumToCny(num float64) (str string, err error) {
  547. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  548. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  549. s := sliceUnit[len(sliceUnit)-len(strNum):]
  550. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  551. for k, v := range strNum[:] {
  552. str = str + upperDigitUnit[string(v)] + s[k]
  553. }
  554. reg, err := regexp.Compile(`零角零分$`)
  555. str = reg.ReplaceAllString(str, "整")
  556. reg, err = regexp.Compile(`零角`)
  557. str = reg.ReplaceAllString(str, "零")
  558. reg, err = regexp.Compile(`零分$`)
  559. str = reg.ReplaceAllString(str, "整")
  560. reg, err = regexp.Compile(`零[仟佰拾]`)
  561. str = reg.ReplaceAllString(str, "零")
  562. reg, err = regexp.Compile(`零{2,}`)
  563. str = reg.ReplaceAllString(str, "零")
  564. reg, err = regexp.Compile(`零亿`)
  565. str = reg.ReplaceAllString(str, "亿")
  566. reg, err = regexp.Compile(`零万`)
  567. str = reg.ReplaceAllString(str, "万")
  568. reg, err = regexp.Compile(`零*元`)
  569. str = reg.ReplaceAllString(str, "元")
  570. reg, err = regexp.Compile(`亿零{0, 3}万`)
  571. str = reg.ReplaceAllString(str, "^元")
  572. reg, err = regexp.Compile(`零元`)
  573. str = reg.ReplaceAllString(str, "零")
  574. return
  575. }
  576. func GetNowWeekMonday() time.Time {
  577. offset := int(time.Monday - time.Now().Weekday())
  578. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  579. offset = -6
  580. }
  581. mondayTime := time.Now().AddDate(0, 0, offset)
  582. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  583. return mondayTime
  584. }
  585. func GetNowWeekLastDay() time.Time {
  586. offset := int(time.Monday - time.Now().Weekday())
  587. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  588. offset = -6
  589. }
  590. firstDayTime := time.Now().AddDate(0, 0, offset)
  591. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  592. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  593. return lastDayTime
  594. }
  595. func GetNowMonthFirstDay() time.Time {
  596. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  597. return nowMonthFirstDay
  598. }
  599. func GetNowMonthLastDay() time.Time {
  600. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  601. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  602. return nowMonthLastDay
  603. }
  604. func GetNowQuarterFirstDay() time.Time {
  605. month := int(time.Now().Month())
  606. var nowQuarterFirstDay time.Time
  607. if month >= 1 && month <= 3 {
  608. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  609. } else if month >= 4 && month <= 6 {
  610. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  611. } else if month >= 7 && month <= 9 {
  612. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  613. } else {
  614. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  615. }
  616. return nowQuarterFirstDay
  617. }
  618. func GetNowQuarterLastDay() time.Time {
  619. month := int(time.Now().Month())
  620. var nowQuarterLastDay time.Time
  621. if month >= 1 && month <= 3 {
  622. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  623. } else if month >= 4 && month <= 6 {
  624. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  625. } else if month >= 7 && month <= 9 {
  626. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  627. } else {
  628. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  629. }
  630. return nowQuarterLastDay
  631. }
  632. func GetNowHalfYearFirstDay() time.Time {
  633. month := int(time.Now().Month())
  634. var nowHalfYearLastDay time.Time
  635. if month >= 1 && month <= 6 {
  636. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  637. } else {
  638. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  639. }
  640. return nowHalfYearLastDay
  641. }
  642. func GetNowHalfYearLastDay() time.Time {
  643. month := int(time.Now().Month())
  644. var nowHalfYearLastDay time.Time
  645. if month >= 1 && month <= 6 {
  646. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  647. } else {
  648. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  649. }
  650. return nowHalfYearLastDay
  651. }
  652. func GetNowYearFirstDay() time.Time {
  653. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  654. return nowYearFirstDay
  655. }
  656. func GetNowYearLastDay() time.Time {
  657. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  658. return nowYearLastDay
  659. }
  660. func FormatPrice(price float64) (str string) {
  661. str = decimal.NewFromFloat(price).String()
  662. length := len(str)
  663. if length < 4 {
  664. return str
  665. }
  666. arr := strings.Split(str, ".") //用小数点符号分割字符串,为数组接收
  667. length1 := len(arr[0])
  668. if length1 < 4 {
  669. return str
  670. }
  671. count := (length1 - 1) / 3
  672. for i := 0; i < count; i++ {
  673. arr[0] = arr[0][:length1-(i+1)*3] + "," + arr[0][length1-(i+1)*3:]
  674. }
  675. return strings.Join(arr, ".") //将一系列字符串连接为一个字符串,之间用sep来分隔。
  676. }
  677. func getMonthDay(year, month int) (days int) {
  678. if month != 2 {
  679. if month == 4 || month == 6 || month == 9 || month == 11 {
  680. days = 30
  681. } else {
  682. days = 31
  683. }
  684. } else {
  685. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  686. days = 29
  687. } else {
  688. days = 28
  689. }
  690. }
  691. return
  692. }
  693. func SaveToFile(content, path string) error {
  694. f, err := os.Create(path)
  695. defer f.Close()
  696. if err != nil {
  697. return err
  698. }
  699. f.Write([]byte(content))
  700. return nil
  701. }
  702. func HideString(src string, hideLen int) string {
  703. if src == "" {
  704. return src
  705. }
  706. str := []rune(src)
  707. if hideLen == 0 {
  708. hideLen = 4
  709. }
  710. hideStr := ""
  711. for i := 0; i < hideLen; i++ {
  712. hideStr += "*"
  713. }
  714. strLen := len(str)
  715. if strLen == 1 {
  716. return string(str[:1]) + hideStr
  717. }
  718. if strLen <= hideLen+2 {
  719. return string(str[:1]) + hideStr + string(str[strLen-1:])
  720. }
  721. subLen := strLen - hideLen //剩余需要展示的字符长度
  722. decimal.NewFromFloat(2)
  723. frontLenDecimal := decimal.NewFromInt(int64(subLen)).Div(decimal.NewFromInt(2)) //前面需要展示的字符的长度
  724. frontLen := frontLenDecimal.Floor().IntPart()
  725. return string(str[:frontLen]) + hideStr + string(str[frontLen+int64(hideLen):])
  726. }
  727. func GetAttendanceDetailSeconds(secondNum int) string {
  728. var timeStr string
  729. if secondNum <= 60 {
  730. if secondNum < 10 {
  731. timeStr = "0" + strconv.Itoa(secondNum) + "''"
  732. } else {
  733. timeStr = strconv.Itoa(secondNum) + "''"
  734. }
  735. } else {
  736. var remainderStr string
  737. remainderNum := secondNum % 60
  738. minuteNum := secondNum / 60
  739. if remainderNum < 10 {
  740. remainderStr = "0" + strconv.Itoa(remainderNum) + "''"
  741. } else {
  742. remainderStr = strconv.Itoa(remainderNum) + "''"
  743. }
  744. if minuteNum < 10 {
  745. timeStr = "0" + strconv.Itoa(minuteNum) + "'" + remainderStr
  746. } else {
  747. timeStr = strconv.Itoa(minuteNum) + "'" + remainderStr
  748. }
  749. }
  750. return timeStr
  751. }
  752. func SubStr(str string, subLen int) string {
  753. strRune := []rune(str)
  754. bodyRuneLen := len(strRune)
  755. if bodyRuneLen > subLen {
  756. bodyRuneLen = subLen
  757. }
  758. str = string(strRune[:bodyRuneLen])
  759. return str
  760. }
  761. func GetLocalIP() (ip string, err error) {
  762. addrs, err := net.InterfaceAddrs()
  763. if err != nil {
  764. return
  765. }
  766. for _, addr := range addrs {
  767. ipAddr, ok := addr.(*net.IPNet)
  768. if !ok {
  769. continue
  770. }
  771. if ipAddr.IP.IsLoopback() {
  772. continue
  773. }
  774. if !ipAddr.IP.IsGlobalUnicast() {
  775. continue
  776. }
  777. return ipAddr.IP.String(), nil
  778. }
  779. return
  780. }
  781. func PrintLog(params ...string) {
  782. _, file, line, ok := runtime.Caller(1)
  783. fmt.Println(file, line, ok, params)
  784. }
  785. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  786. for _, id := range idStrList {
  787. if id == searchId {
  788. has = true
  789. return
  790. }
  791. }
  792. return
  793. }
  794. func InArrayByInt(idStrList []int, searchId int) (has bool) {
  795. for _, id := range idStrList {
  796. if id == searchId {
  797. has = true
  798. return
  799. }
  800. }
  801. return
  802. }
  803. func GetOrmInReplace(num int) string {
  804. return "?"
  805. }
  806. func GetTimeSubDay(t1, t2 time.Time) int {
  807. var day int
  808. swap := false
  809. if t1.Unix() > t2.Unix() {
  810. t1, t2 = t2, t1
  811. swap = true
  812. }
  813. t1_ := t1.Add(time.Duration(t2.Sub(t1).Milliseconds()%86400000) * time.Millisecond)
  814. day = int(t2.Sub(t1).Hours() / 24)
  815. if t1_.Day() != t1.Day() {
  816. day += 1
  817. }
  818. if swap {
  819. day = -day
  820. }
  821. return day
  822. }
  823. func GetFrequencyEndDay(currDate time.Time, frequency string) (endDate time.Time) {
  824. switch frequency {
  825. case "周度":
  826. if currDate.Weekday() == 0 {
  827. endDate = currDate
  828. } else {
  829. endDate = currDate.AddDate(0, 0, 7-int(currDate.Weekday()))
  830. }
  831. case "旬度":
  832. nextDay := currDate.AddDate(0, 0, 1)
  833. if nextDay.Day() == 1 || currDate.Day() == 10 || currDate.Day() == 20 {
  834. endDate = currDate
  835. } else {
  836. if currDate.Day() < 10 { // 每月10号
  837. endDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, time.Local)
  838. } else if currDate.Day() < 20 { // 每月10号
  839. endDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, time.Local)
  840. } else {
  841. tmpNextMonth := currDate.AddDate(0, 0, 13)
  842. endDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  843. }
  844. }
  845. case "月度":
  846. nextDay := currDate.AddDate(0, 0, 1)
  847. if nextDay.Day() == 1 {
  848. endDate = currDate
  849. } else {
  850. endDate = time.Date(nextDay.Year(), nextDay.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  851. }
  852. case "季度":
  853. nextDay := currDate.AddDate(0, 0, 1)
  854. if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 {
  855. endDate = currDate
  856. } else {
  857. if currDate.Month() < 4 { // 1季度
  858. endDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, time.Local)
  859. } else if currDate.Month() < 7 { // 2季度
  860. endDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, time.Local)
  861. } else if currDate.Month() < 10 { // 3季度
  862. endDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, time.Local)
  863. } else {
  864. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  865. }
  866. }
  867. case "年度":
  868. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  869. default:
  870. endDate = currDate
  871. return
  872. }
  873. return
  874. }
  875. func CheckFrequency(leftFrequency, rightFrequency string) int {
  876. frequencyMap := map[string]int{
  877. "年度": 0,
  878. "半年度": 1,
  879. "季度": 2,
  880. "月度": 3,
  881. "旬度": 4,
  882. "周度": 5,
  883. "日度": 6,
  884. }
  885. return frequencyMap[leftFrequency] - frequencyMap[rightFrequency]
  886. }
  887. func SnakeToCamel(s string) string {
  888. var result string
  889. upper := true
  890. for _, c := range s {
  891. if c == '_' {
  892. upper = true
  893. continue
  894. }
  895. if upper {
  896. result += string(unicode.ToUpper(c))
  897. upper = false
  898. } else {
  899. result += string(c)
  900. }
  901. }
  902. return result
  903. }
  904. func CamelToSnake(s string) string {
  905. var result string
  906. for i, c := range s {
  907. if unicode.IsUpper(c) {
  908. if i > 0 {
  909. result += "_"
  910. }
  911. result += string(unicode.ToLower(c))
  912. } else {
  913. result += string(c)
  914. }
  915. }
  916. return result
  917. }
  918. func TimeTransferString(format string, t time.Time) string {
  919. str := t.Format(format)
  920. if t.IsZero() {
  921. return ""
  922. }
  923. return str
  924. }
  925. func GetEdbRefreshStartDate(startDate string) string {
  926. if startDate == `` || strings.Contains(startDate, "0000-") {
  927. return "1990-01-01"
  928. }
  929. return startDate
  930. }
  931. func GetEdbRefreshEndDate(endDate string) string {
  932. if endDate == `` || strings.Contains(endDate, "0000-") {
  933. return time.Now().Format(FormatDate)
  934. }
  935. return endDate
  936. }
  937. func FloatAlmostEqual(a, b float64) bool {
  938. epsilon := 1e-9 // 容差值
  939. return math.Abs(a-b) <= epsilon
  940. }
  941. func VerifyFrequency(frequency string) bool {
  942. return InArrayByStr([]string{"年度", "半年度", "季度", "月度", "旬度", "周度", "日度"}, frequency)
  943. }
  944. func DateConvMysqlConvMongo(dateCon string) string {
  945. cond := ""
  946. switch dateCon {
  947. case "=":
  948. cond = "$eq"
  949. case "<":
  950. cond = "$lt"
  951. case "<=":
  952. cond = "$lte"
  953. case ">":
  954. cond = "$gt"
  955. case ">=":
  956. cond = "$gte"
  957. }
  958. return cond
  959. }
  960. var GenerateEdbCodeMap = map[string]bool{}
  961. func GenerateEdbCode(num int, pre string) (edbCode string, err error) {
  962. if num >= 10 {
  963. err = errors.New("指标编码生成失败,请重新生成")
  964. return
  965. }
  966. randStr := GetRandDigit(4)
  967. edbCode = `C` + pre + time.Now().Format(FormatShortDateTimeUnSpace) + randStr
  968. if _, ok := GenerateEdbCodeMap[edbCode]; ok {
  969. num++
  970. edbCode, err = GenerateEdbCode(num, pre)
  971. }
  972. GenerateEdbCodeMap[edbCode] = true
  973. return
  974. }
  975. func InsertStr2StrIdx(str, sep string, idx int, value string) string {
  976. str = strings.TrimSpace(str)
  977. if sep == "" {
  978. sep = " "
  979. }
  980. slice := strings.Split(str, sep)
  981. if len(slice) < 2 {
  982. return str
  983. }
  984. if idx < 0 || idx > len(slice) {
  985. return str
  986. }
  987. slice = append(slice[:idx], append([]string{value}, slice[idx:]...)...)
  988. return strings.Join(slice, sep)
  989. }
  990. func FormatFloatPlaces(val float64, places int32) (newVal float64, err error) {
  991. if places <= 0 {
  992. places = 4
  993. }
  994. strNewVal := decimal.NewFromFloat(val).Round(places).String()
  995. di, e := decimal.NewFromString(strNewVal)
  996. if e != nil {
  997. err = fmt.Errorf("NewFromString err: %v", e)
  998. return
  999. }
  1000. newVal, _ = di.Float64()
  1001. return
  1002. }
  1003. func HandleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  1004. switch frequency {
  1005. case "本周":
  1006. day := int(currDate.Weekday())
  1007. if day == 0 { // 周日
  1008. day = 7
  1009. }
  1010. num := 0
  1011. switch appointDay {
  1012. case "周一":
  1013. num = 1
  1014. case "周二":
  1015. num = 2
  1016. case "周三":
  1017. num = 3
  1018. case "周四":
  1019. num = 4
  1020. case "周五":
  1021. num = 5
  1022. case "周六":
  1023. num = 6
  1024. case "周日":
  1025. num = 7
  1026. }
  1027. day = num - day
  1028. date = currDate.AddDate(0, 0, day).Format(FormatDate)
  1029. case "本旬":
  1030. day := currDate.Day()
  1031. var tmpDate time.Time
  1032. switch appointDay {
  1033. case "第一天":
  1034. if day <= 10 {
  1035. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1036. } else if day <= 20 {
  1037. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  1038. } else {
  1039. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  1040. }
  1041. case "最后一天":
  1042. if day <= 10 {
  1043. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  1044. } else if day <= 20 {
  1045. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  1046. } else {
  1047. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1048. }
  1049. }
  1050. date = tmpDate.Format(FormatDate)
  1051. case "本月":
  1052. var tmpDate time.Time
  1053. switch appointDay {
  1054. case "第一天":
  1055. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1056. case "最后一天":
  1057. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1058. }
  1059. date = tmpDate.Format(FormatDate)
  1060. case "本季":
  1061. month := currDate.Month()
  1062. var tmpDate time.Time
  1063. switch appointDay {
  1064. case "第一天":
  1065. if month <= 3 {
  1066. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1067. } else if month <= 6 {
  1068. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  1069. } else if month <= 9 {
  1070. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1071. } else {
  1072. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  1073. }
  1074. case "最后一天":
  1075. if month <= 3 {
  1076. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  1077. } else if month <= 6 {
  1078. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1079. } else if month <= 9 {
  1080. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  1081. } else {
  1082. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1083. }
  1084. }
  1085. date = tmpDate.Format(FormatDate)
  1086. case "本半年":
  1087. month := currDate.Month()
  1088. var tmpDate time.Time
  1089. switch appointDay {
  1090. case "第一天":
  1091. if month <= 6 {
  1092. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1093. } else {
  1094. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1095. }
  1096. case "最后一天":
  1097. if month <= 6 {
  1098. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1099. } else {
  1100. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1101. }
  1102. }
  1103. date = tmpDate.Format(FormatDate)
  1104. case "本年":
  1105. var tmpDate time.Time
  1106. switch appointDay {
  1107. case "第一天":
  1108. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1109. case "最后一天":
  1110. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1111. }
  1112. date = tmpDate.Format(FormatDate)
  1113. default:
  1114. errMsg = "错误的日期频度:" + frequency
  1115. err = errors.New(errMsg)
  1116. return
  1117. }
  1118. return
  1119. }
  1120. func CompareFloatByOpStrings(op string, a, b float64) bool {
  1121. switch op {
  1122. case "=":
  1123. return a == b
  1124. case ">":
  1125. return a > b
  1126. case ">=":
  1127. return a >= b
  1128. case "<=":
  1129. return a <= b
  1130. case "<":
  1131. return a < b
  1132. }
  1133. return false
  1134. }
  1135. func IsDivideZero(err error) bool {
  1136. if err == nil {
  1137. return false
  1138. }
  1139. if strings.Contains(err.Error(), "division by zero") {
  1140. return true
  1141. }
  1142. return false
  1143. }
  1144. func GetTradingDays(startDate, endDate time.Time) []time.Time {
  1145. var tradingDays []time.Time
  1146. for curr := startDate; !curr.After(endDate); curr = curr.AddDate(0, 0, 1) {
  1147. if curr.Weekday() >= time.Monday && curr.Weekday() <= time.Friday {
  1148. tradingDays = append(tradingDays, curr)
  1149. }
  1150. }
  1151. return tradingDays
  1152. }
  1153. func GormDateStrToDateTimeStr(originalString string) (formatStr string) {
  1154. formatStr = originalString
  1155. if !strings.Contains(originalString, "T") {
  1156. return
  1157. }
  1158. t, err := time.Parse(FormatDateWallWithLoc, originalString)
  1159. if err != nil {
  1160. fmt.Println("Error parsing time:", err)
  1161. return
  1162. }
  1163. formatStr = t.Format(FormatDateTime)
  1164. return
  1165. }
  1166. func GormDateStrToDateStr(originalString string) (formatStr string) {
  1167. formatStr = originalString
  1168. if !strings.Contains(originalString, "T") {
  1169. return
  1170. }
  1171. t, err := time.Parse(FormatDateWallWithLoc, originalString)
  1172. if err != nil {
  1173. fmt.Println("Error parsing time:", err)
  1174. return
  1175. }
  1176. formatStr = t.Format(FormatDate)
  1177. return
  1178. }