common.go 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. "os/exec"
  21. "path"
  22. "regexp"
  23. "runtime"
  24. "strconv"
  25. "strings"
  26. "time"
  27. "unicode"
  28. )
  29. func GetRandString(size int) string {
  30. 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", "!", "@", "#", "$", "%", "^", "&", "*"}
  31. randomSb := ""
  32. digitSize := len(allLetterDigit)
  33. // 随机数种子
  34. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  35. for i := 0; i < size; i++ {
  36. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  37. }
  38. return randomSb
  39. }
  40. func GetRandStringNoSpecialChar(size int) string {
  41. 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"}
  42. randomSb := ""
  43. digitSize := len(allLetterDigit)
  44. // 随机数种子
  45. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  46. for i := 0; i < size; i++ {
  47. randomSb += allLetterDigit[rnd.Intn(digitSize)]
  48. }
  49. return randomSb
  50. }
  51. func StringsToJSON(str string) string {
  52. rs := []rune(str)
  53. jsons := ""
  54. for _, r := range rs {
  55. rint := int(r)
  56. if rint < 128 {
  57. jsons += string(r)
  58. } else {
  59. jsons += "\\u" + strconv.FormatInt(int64(rint), 16) // json
  60. }
  61. }
  62. return jsons
  63. }
  64. // 序列化
  65. func ToString(v interface{}) string {
  66. data, _ := json.Marshal(v)
  67. return string(data)
  68. }
  69. // md5加密
  70. func MD5(data string) string {
  71. m := md5.Sum([]byte(data))
  72. return hex.EncodeToString(m[:])
  73. }
  74. // 获取数字随机字符
  75. func GetRandDigit(n int) string {
  76. // 随机数种子
  77. rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
  78. return fmt.Sprintf("%0"+strconv.Itoa(n)+"d", rnd.Intn(int(math.Pow10(n))))
  79. }
  80. func GetToday(format string) string {
  81. today := time.Now().Format(format)
  82. return today
  83. }
  84. // 获取今天剩余秒数
  85. func GetTodayLastSecond() time.Duration {
  86. today := GetToday(FormatDate) + " 23:59:59"
  87. end, _ := time.ParseInLocation(FormatDateTime, today, time.Local)
  88. return time.Duration(end.Unix()-time.Now().Local().Unix()) * time.Second
  89. }
  90. // 处理出生日期函数
  91. func GetBrithDate(idcard string) string {
  92. l := len(idcard)
  93. var s string
  94. if l == 15 {
  95. s = "19" + idcard[6:8] + "-" + idcard[8:10] + "-" + idcard[10:12]
  96. return s
  97. }
  98. if l == 18 {
  99. s = idcard[6:10] + "-" + idcard[10:12] + "-" + idcard[12:14]
  100. return s
  101. }
  102. return GetToday(FormatDate)
  103. }
  104. // 处理性别
  105. func WhichSexByIdcard(idcard string) string {
  106. var sexs = [2]string{"女", "男"}
  107. length := len(idcard)
  108. if length == 18 {
  109. sex, _ := strconv.Atoi(string(idcard[16]))
  110. return sexs[sex%2]
  111. } else if length == 15 {
  112. sex, _ := strconv.Atoi(string(idcard[14]))
  113. return sexs[sex%2]
  114. }
  115. return "男"
  116. }
  117. // 截取小数点后几位
  118. func SubFloatToString(f float64, m int) string {
  119. n := strconv.FormatFloat(f, 'f', -1, 64)
  120. if n == "" {
  121. return ""
  122. }
  123. if m >= len(n) {
  124. return n
  125. }
  126. newn := strings.Split(n, ".")
  127. if m == 0 {
  128. return newn[0]
  129. }
  130. if len(newn) < 2 || m >= len(newn[1]) {
  131. return n
  132. }
  133. return newn[0] + "." + newn[1][:m]
  134. }
  135. // 截取小数点后几位
  136. func SubFloatToFloat(f float64, m int) float64 {
  137. newn := SubFloatToString(f, m)
  138. newf, _ := strconv.ParseFloat(newn, 64)
  139. return newf
  140. }
  141. // 截取小数点后几位
  142. func SubFloatToFloatStr(f float64, m int) string {
  143. newn := SubFloatToString(f, m)
  144. return newn
  145. }
  146. // 获取相差时间-年
  147. func GetYearDiffer(start_time, end_time string) int {
  148. t1, _ := time.ParseInLocation("2006-01-02", start_time, time.Local)
  149. t2, _ := time.ParseInLocation("2006-01-02", end_time, time.Local)
  150. age := t2.Year() - t1.Year()
  151. if t2.Month() < t1.Month() || (t2.Month() == t1.Month() && t2.Day() < t1.Day()) {
  152. age--
  153. }
  154. return age
  155. }
  156. // 获取相差时间-秒
  157. func GetSecondDifferByTime(start_time, end_time time.Time) int64 {
  158. diff := end_time.Unix() - start_time.Unix()
  159. return diff
  160. }
  161. func FixFloat(f float64, m int) float64 {
  162. newn := SubFloatToString(f+0.00000001, m)
  163. newf, _ := strconv.ParseFloat(newn, 64)
  164. return newf
  165. }
  166. // 将字符串数组转化为逗号分割的字符串形式 ["str1","str2","str3"] >>> "str1,str2,str3"
  167. func StrListToString(strList []string) (str string) {
  168. if len(strList) > 0 {
  169. for k, v := range strList {
  170. if k == 0 {
  171. str = v
  172. } else {
  173. str = str + "," + v
  174. }
  175. }
  176. return
  177. }
  178. return ""
  179. }
  180. // Token
  181. func GetToken() string {
  182. randStr := GetRandString(64)
  183. token := MD5(randStr + Md5Key)
  184. tokenLen := 64 - len(token)
  185. return strings.ToUpper(token + GetRandString(tokenLen))
  186. }
  187. // 数据没有记录
  188. func ErrNoRow() string {
  189. //return "<QuerySeter> no row found"
  190. return "record not found"
  191. }
  192. // 判断文件是否存在
  193. func FileIsExist(filePath string) bool {
  194. _, err := os.Stat(filePath)
  195. return err == nil || os.IsExist(err)
  196. }
  197. // 获取图片扩展名
  198. func GetImgExt(file string) (ext string, err error) {
  199. var headerByte []byte
  200. headerByte = make([]byte, 8)
  201. fd, err := os.Open(file)
  202. if err != nil {
  203. return "", err
  204. }
  205. defer fd.Close()
  206. _, err = fd.Read(headerByte)
  207. if err != nil {
  208. return "", err
  209. }
  210. xStr := fmt.Sprintf("%x", headerByte)
  211. switch {
  212. case xStr == "89504e470d0a1a0a":
  213. ext = ".png"
  214. case xStr == "0000010001002020":
  215. ext = ".ico"
  216. case xStr == "0000020001002020":
  217. ext = ".cur"
  218. case xStr[:12] == "474946383961" || xStr[:12] == "474946383761":
  219. ext = ".gif"
  220. case xStr[:10] == "0000020000" || xStr[:10] == "0000100000":
  221. ext = ".tga"
  222. case xStr[:8] == "464f524d":
  223. ext = ".iff"
  224. case xStr[:8] == "52494646":
  225. ext = ".ani"
  226. case xStr[:4] == "4d4d" || xStr[:4] == "4949":
  227. ext = ".tiff"
  228. case xStr[:4] == "424d":
  229. ext = ".bmp"
  230. case xStr[:4] == "ffd8":
  231. ext = ".jpg"
  232. case xStr[:2] == "0a":
  233. ext = ".pcx"
  234. default:
  235. ext = ""
  236. }
  237. return ext, nil
  238. }
  239. // 保存图片
  240. func SaveImage(path string, img image.Image) (err error) {
  241. //需要保持的文件
  242. imgfile, err := os.Create(path)
  243. defer imgfile.Close()
  244. // 以PNG格式保存文件
  245. err = png.Encode(imgfile, img)
  246. return err
  247. }
  248. // 下载图片
  249. func DownloadImage(imgUrl string) (filePath string, err error) {
  250. imgPath := "./static/imgs/"
  251. fileName := path.Base(imgUrl)
  252. res, err := http.Get(imgUrl)
  253. if err != nil {
  254. fmt.Println("A error occurred!")
  255. return
  256. }
  257. defer res.Body.Close()
  258. // 获得get请求响应的reader对象
  259. reader := bufio.NewReaderSize(res.Body, 32*1024)
  260. filePath = imgPath + fileName
  261. file, err := os.Create(filePath)
  262. if err != nil {
  263. return
  264. }
  265. // 获得文件的writer对象
  266. writer := bufio.NewWriter(file)
  267. written, _ := io.Copy(writer, reader)
  268. fmt.Printf("Total length: %d \n", written)
  269. return
  270. }
  271. // 保存base64数据为文件
  272. func SaveBase64ToFile(content, path string) error {
  273. data, err := base64.StdEncoding.DecodeString(content)
  274. if err != nil {
  275. return err
  276. }
  277. f, err := os.Create(path)
  278. defer f.Close()
  279. if err != nil {
  280. return err
  281. }
  282. f.Write(data)
  283. return nil
  284. }
  285. func SaveBase64ToFileBySeek(content, path string) (err error) {
  286. data, err := base64.StdEncoding.DecodeString(content)
  287. exist, err := PathExists(path)
  288. if err != nil {
  289. return
  290. }
  291. if !exist {
  292. f, err := os.Create(path)
  293. if err != nil {
  294. return err
  295. }
  296. n, _ := f.Seek(0, 2)
  297. // 从末尾的偏移量开始写入内容
  298. _, err = f.WriteAt([]byte(data), n)
  299. defer f.Close()
  300. } else {
  301. f, err := os.OpenFile(path, os.O_WRONLY, 0644)
  302. if err != nil {
  303. return err
  304. }
  305. n, _ := f.Seek(0, 2)
  306. // 从末尾的偏移量开始写入内容
  307. _, err = f.WriteAt([]byte(data), n)
  308. defer f.Close()
  309. }
  310. return nil
  311. }
  312. func PathExists(path string) (bool, error) {
  313. _, err := os.Stat(path)
  314. if err == nil {
  315. return true, nil
  316. }
  317. if os.IsNotExist(err) {
  318. return false, nil
  319. }
  320. return false, err
  321. }
  322. func StartIndex(page, pagesize int) int {
  323. if page > 1 {
  324. return (page - 1) * pagesize
  325. }
  326. return 0
  327. }
  328. func PageCount(count, pagesize int) int {
  329. if count%pagesize > 0 {
  330. return count/pagesize + 1
  331. } else {
  332. return count / pagesize
  333. }
  334. }
  335. func TrimHtml(src string) string {
  336. //将HTML标签全转换成小写
  337. re, _ := regexp.Compile("\\<[\\S\\s]+?\\>")
  338. src = re.ReplaceAllStringFunc(src, strings.ToLower)
  339. re, _ = regexp.Compile("\\<img[\\S\\s]+?\\>")
  340. src = re.ReplaceAllString(src, "[图片]")
  341. re, _ = regexp.Compile("class[\\S\\s]+?>")
  342. src = re.ReplaceAllString(src, "")
  343. re, _ = regexp.Compile("\\<[\\S\\s]+?\\>")
  344. src = re.ReplaceAllString(src, "")
  345. return strings.TrimSpace(src)
  346. }
  347. //1556164246 -> 2019-04-25 03:50:46 +0000
  348. //timestamp
  349. func TimeToTimestamp() {
  350. fmt.Println(time.Unix(1556164246, 0).Format("2006-01-02 15:04:05"))
  351. }
  352. func ToUnicode(text string) string {
  353. textQuoted := strconv.QuoteToASCII(text)
  354. textUnquoted := textQuoted[1 : len(textQuoted)-1]
  355. return textUnquoted
  356. }
  357. func VersionToInt(version string) int {
  358. version = strings.Replace(version, ".", "", -1)
  359. n, _ := strconv.Atoi(version)
  360. return n
  361. }
  362. func IsCheckInList(list []int, s int) bool {
  363. for _, v := range list {
  364. if v == s {
  365. return true
  366. }
  367. }
  368. return false
  369. }
  370. func round(num float64) int {
  371. return int(num + math.Copysign(0.5, num))
  372. }
  373. func toFixed(num float64, precision int) float64 {
  374. output := math.Pow(10, float64(precision))
  375. return float64(round(num*output)) / output
  376. }
  377. // GetWilsonScore returns Wilson Score
  378. func GetWilsonScore(p, n float64) float64 {
  379. if p == 0 && n == 0 {
  380. return 0
  381. }
  382. 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)
  383. }
  384. // 将中文数字转化成数字,比如 第三百四十五章,返回第345章 不支持一亿及以上
  385. func ChangeWordsToNum(str string) (numStr string) {
  386. words := ([]rune)(str)
  387. num := 0
  388. n := 0
  389. for i := 0; i < len(words); i++ {
  390. word := string(words[i : i+1])
  391. switch word {
  392. case "万":
  393. if n == 0 {
  394. n = 1
  395. }
  396. n = n * 10000
  397. num = num*10000 + n
  398. n = 0
  399. case "千":
  400. if n == 0 {
  401. n = 1
  402. }
  403. n = n * 1000
  404. num += n
  405. n = 0
  406. case "百":
  407. if n == 0 {
  408. n = 1
  409. }
  410. n = n * 100
  411. num += n
  412. n = 0
  413. case "十":
  414. if n == 0 {
  415. n = 1
  416. }
  417. n = n * 10
  418. num += n
  419. n = 0
  420. case "一":
  421. n += 1
  422. case "二":
  423. n += 2
  424. case "三":
  425. n += 3
  426. case "四":
  427. n += 4
  428. case "五":
  429. n += 5
  430. case "六":
  431. n += 6
  432. case "七":
  433. n += 7
  434. case "八":
  435. n += 8
  436. case "九":
  437. n += 9
  438. case "零":
  439. default:
  440. if n > 0 {
  441. num += n
  442. n = 0
  443. }
  444. if num == 0 {
  445. numStr += word
  446. } else {
  447. numStr += strconv.Itoa(num) + word
  448. num = 0
  449. }
  450. }
  451. }
  452. if n > 0 {
  453. num += n
  454. n = 0
  455. }
  456. if num != 0 {
  457. numStr += strconv.Itoa(num)
  458. }
  459. return
  460. }
  461. func Sha1(data string) string {
  462. sha1 := sha1.New()
  463. sha1.Write([]byte(data))
  464. return hex.EncodeToString(sha1.Sum([]byte("")))
  465. }
  466. func GetVideoPlaySeconds(videoPath string) (playSeconds float64, err error) {
  467. cmd := `ffmpeg -i ` + videoPath + ` 2>&1 | grep 'Duration' | cut -d ' ' -f 4 | sed s/,//`
  468. out, err := exec.Command("bash", "-c", cmd).Output()
  469. if err != nil {
  470. return
  471. }
  472. outTimes := string(out)
  473. fmt.Println("outTimes:", outTimes)
  474. if outTimes != "" {
  475. timeArr := strings.Split(outTimes, ":")
  476. h := timeArr[0]
  477. m := timeArr[1]
  478. s := timeArr[2]
  479. hInt, err := strconv.Atoi(h)
  480. if err != nil {
  481. return playSeconds, err
  482. }
  483. mInt, err := strconv.Atoi(m)
  484. if err != nil {
  485. return playSeconds, err
  486. }
  487. s = strings.Trim(s, " ")
  488. s = strings.Trim(s, "\n")
  489. sInt, err := strconv.ParseFloat(s, 64)
  490. if err != nil {
  491. return playSeconds, err
  492. }
  493. playSeconds = float64(hInt)*3600 + float64(mInt)*60 + float64(sInt)
  494. }
  495. return
  496. }
  497. func GetMaxTradeCode(tradeCode string) (maxTradeCode string, err error) {
  498. tradeCode = strings.Replace(tradeCode, "W", "", -1)
  499. tradeCode = strings.Trim(tradeCode, " ")
  500. tradeCodeInt, err := strconv.Atoi(tradeCode)
  501. if err != nil {
  502. return
  503. }
  504. tradeCodeInt = tradeCodeInt + 1
  505. maxTradeCode = fmt.Sprintf("W%06d", tradeCodeInt)
  506. return
  507. }
  508. // excel日期字段格式化 yyyy-mm-dd
  509. func ConvertToFormatDay(excelDaysString string) string {
  510. // 2006-01-02 距离 1900-01-01的天数
  511. baseDiffDay := 38719 //在网上工具计算的天数需要加2天,什么原因没弄清楚
  512. curDiffDay := excelDaysString
  513. b, _ := strconv.Atoi(curDiffDay)
  514. // 获取excel的日期距离2006-01-02的天数
  515. realDiffDay := b - baseDiffDay
  516. //fmt.Println("realDiffDay:",realDiffDay)
  517. // 距离2006-01-02 秒数
  518. realDiffSecond := realDiffDay * 24 * 3600
  519. //fmt.Println("realDiffSecond:",realDiffSecond)
  520. // 2006-01-02 15:04:05距离1970-01-01 08:00:00的秒数 网上工具可查出
  521. baseOriginSecond := 1136185445
  522. resultTime := time.Unix(int64(baseOriginSecond+realDiffSecond), 0).Format("2006-01-02")
  523. return resultTime
  524. }
  525. func CheckPwd(pwd string) bool {
  526. compile := `([0-9a-z]+){6,12}|(a-z0-9]+){6,12}`
  527. reg := regexp.MustCompile(compile)
  528. flag := reg.MatchString(pwd)
  529. return flag
  530. }
  531. func GetMonthStartAndEnd(myYear string, myMonth string) (startDate, endDate string) {
  532. // 数字月份必须前置补零
  533. if len(myMonth) == 1 {
  534. myMonth = "0" + myMonth
  535. }
  536. yInt, _ := strconv.Atoi(myYear)
  537. timeLayout := "2006-01-02 15:04:05"
  538. loc, _ := time.LoadLocation("Local")
  539. theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
  540. newMonth := theTime.Month()
  541. t1 := time.Date(yInt, newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  542. t2 := time.Date(yInt, newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  543. return t1, t2
  544. }
  545. // 移除字符串中的空格
  546. func TrimStr(str string) (str2 string) {
  547. if str == "" {
  548. return str
  549. }
  550. return strings.Replace(str, " ", "", -1)
  551. }
  552. // 字符串转换为time
  553. func StrTimeToTime(strTime string) time.Time {
  554. timeLayout := "2006-01-02 15:04:05" //转化所需模板
  555. loc, _ := time.LoadLocation("Local") //重要:获取时区
  556. resultTime, _ := time.ParseInLocation(timeLayout, strTime, loc)
  557. return resultTime
  558. }
  559. // 字符串类型时间转周几
  560. func StrDateTimeToWeek(strTime string) string {
  561. var WeekDayMap = map[string]string{
  562. "Monday": "周一",
  563. "Tuesday": "周二",
  564. "Wednesday": "周三",
  565. "Thursday": "周四",
  566. "Friday": "周五",
  567. "Saturday": "周六",
  568. "Sunday": "周日",
  569. }
  570. var ctime = StrTimeToTime(strTime).Format("2006-01-02")
  571. startday, _ := time.ParseInLocation("2006-01-02", ctime, time.Local)
  572. staweek_int := startday.Weekday().String()
  573. return WeekDayMap[staweek_int]
  574. }
  575. // 时间格式转年月日字符串
  576. func TimeToStrYmd(time2 time.Time) string {
  577. var Ymd string
  578. year := time2.Year()
  579. month := time2.Format("1")
  580. day1 := time.Now().Day()
  581. Ymd = strconv.Itoa(year) + "年" + month + "月" + strconv.Itoa(day1) + "日"
  582. return Ymd
  583. }
  584. // 时间格式去掉时分秒
  585. func TimeRemoveHms(strTime string) string {
  586. var Ymd string
  587. var resultTime = StrTimeToTime(strTime)
  588. year := resultTime.Year()
  589. month := resultTime.Format("01")
  590. day1 := resultTime.Day()
  591. Ymd = strconv.Itoa(year) + "." + month + "." + strconv.Itoa(day1)
  592. return Ymd
  593. }
  594. // 时间格式去掉时分秒
  595. func TimeRemoveHms2(strTime string) string {
  596. var Ymd string
  597. var resultTime = StrTimeToTime(strTime)
  598. year := resultTime.Year()
  599. month := resultTime.Format("01")
  600. day1 := resultTime.Day()
  601. Ymd = strconv.Itoa(year) + "-" + month + "-" + strconv.Itoa(day1)
  602. return Ymd
  603. }
  604. // 文章上一次编辑时间
  605. func ArticleLastTime(strTime string) string {
  606. var newTime string
  607. stamp, _ := time.ParseInLocation("2006-01-02 15:04:05", strTime, time.Local)
  608. diffTime := time.Now().Unix() - stamp.Unix()
  609. if diffTime <= 60 {
  610. newTime = "当前"
  611. } else if diffTime < 60*60 {
  612. newTime = strconv.FormatInt(diffTime/60, 10) + "分钟前"
  613. } else if diffTime < 24*60*60 {
  614. newTime = strconv.FormatInt(diffTime/(60*60), 10) + "小时前"
  615. } else if diffTime < 30*24*60*60 {
  616. newTime = strconv.FormatInt(diffTime/(24*60*60), 10) + "天前"
  617. } else if diffTime < 12*30*24*60*60 {
  618. newTime = strconv.FormatInt(diffTime/(30*24*60*60), 10) + "月前"
  619. } else {
  620. newTime = "1年前"
  621. }
  622. return newTime
  623. }
  624. // 人民币小写转大写
  625. func ConvertNumToCny(num float64) (str string, err error) {
  626. strNum := strconv.FormatFloat(num*100, 'f', 0, 64)
  627. sliceUnit := []string{"仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", "元", "角", "分"}
  628. // log.Println(sliceUnit[:len(sliceUnit)-2])
  629. s := sliceUnit[len(sliceUnit)-len(strNum):]
  630. upperDigitUnit := map[string]string{"0": "零", "1": "壹", "2": "贰", "3": "叁", "4": "肆", "5": "伍", "6": "陆", "7": "柒", "8": "捌", "9": "玖"}
  631. for k, v := range strNum[:] {
  632. str = str + upperDigitUnit[string(v)] + s[k]
  633. }
  634. reg, err := regexp.Compile(`零角零分$`)
  635. str = reg.ReplaceAllString(str, "整")
  636. reg, err = regexp.Compile(`零角`)
  637. str = reg.ReplaceAllString(str, "零")
  638. reg, err = regexp.Compile(`零分$`)
  639. str = reg.ReplaceAllString(str, "整")
  640. reg, err = regexp.Compile(`零[仟佰拾]`)
  641. str = reg.ReplaceAllString(str, "零")
  642. reg, err = regexp.Compile(`零{2,}`)
  643. str = reg.ReplaceAllString(str, "零")
  644. reg, err = regexp.Compile(`零亿`)
  645. str = reg.ReplaceAllString(str, "亿")
  646. reg, err = regexp.Compile(`零万`)
  647. str = reg.ReplaceAllString(str, "万")
  648. reg, err = regexp.Compile(`零*元`)
  649. str = reg.ReplaceAllString(str, "元")
  650. reg, err = regexp.Compile(`亿零{0, 3}万`)
  651. str = reg.ReplaceAllString(str, "^元")
  652. reg, err = regexp.Compile(`零元`)
  653. str = reg.ReplaceAllString(str, "零")
  654. return
  655. }
  656. // GetNowWeekMonday 获取本周周一的时间
  657. func GetNowWeekMonday() time.Time {
  658. offset := int(time.Monday - time.Now().Weekday())
  659. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  660. offset = -6
  661. }
  662. mondayTime := time.Now().AddDate(0, 0, offset)
  663. mondayTime = time.Date(mondayTime.Year(), mondayTime.Month(), mondayTime.Day(), 0, 0, 0, 0, mondayTime.Location())
  664. return mondayTime
  665. }
  666. // GetNowWeekLastDay 获取本周最后一天的时间
  667. func GetNowWeekLastDay() time.Time {
  668. offset := int(time.Monday - time.Now().Weekday())
  669. if offset == 1 { //正好是周日,但是按照中国人的理解,周日是一周最后一天,而不是一周开始的第一天
  670. offset = -6
  671. }
  672. firstDayTime := time.Now().AddDate(0, 0, offset)
  673. firstDayTime = time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 0, 0, 0, 0, firstDayTime.Location()).AddDate(0, 0, 6)
  674. lastDayTime := time.Date(firstDayTime.Year(), firstDayTime.Month(), firstDayTime.Day(), 23, 59, 59, 0, firstDayTime.Location())
  675. return lastDayTime
  676. }
  677. // GetNowMonthFirstDay 获取本月第一天的时间
  678. func GetNowMonthFirstDay() time.Time {
  679. nowMonthFirstDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location())
  680. return nowMonthFirstDay
  681. }
  682. // GetNowMonthLastDay 获取本月最后一天的时间
  683. func GetNowMonthLastDay() time.Time {
  684. nowMonthLastDay := time.Date(time.Now().Year(), time.Now().Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
  685. nowMonthLastDay = time.Date(nowMonthLastDay.Year(), nowMonthLastDay.Month(), nowMonthLastDay.Day(), 23, 59, 59, 0, nowMonthLastDay.Location())
  686. return nowMonthLastDay
  687. }
  688. // GetNowQuarterFirstDay 获取本季度第一天的时间
  689. func GetNowQuarterFirstDay() time.Time {
  690. month := int(time.Now().Month())
  691. var nowQuarterFirstDay time.Time
  692. if month >= 1 && month <= 3 {
  693. //1月1号
  694. nowQuarterFirstDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  695. } else if month >= 4 && month <= 6 {
  696. //4月1号
  697. nowQuarterFirstDay = time.Date(time.Now().Year(), 4, 1, 0, 0, 0, 0, time.Now().Location())
  698. } else if month >= 7 && month <= 9 {
  699. nowQuarterFirstDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  700. } else {
  701. nowQuarterFirstDay = time.Date(time.Now().Year(), 10, 1, 0, 0, 0, 0, time.Now().Location())
  702. }
  703. return nowQuarterFirstDay
  704. }
  705. // GetNowQuarterLastDay 获取本季度最后一天的时间
  706. func GetNowQuarterLastDay() time.Time {
  707. month := int(time.Now().Month())
  708. var nowQuarterLastDay time.Time
  709. if month >= 1 && month <= 3 {
  710. //03-31 23:59:59
  711. nowQuarterLastDay = time.Date(time.Now().Year(), 3, 31, 23, 59, 59, 0, time.Now().Location())
  712. } else if month >= 4 && month <= 6 {
  713. //06-30 23:59:59
  714. nowQuarterLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  715. } else if month >= 7 && month <= 9 {
  716. //09-30 23:59:59
  717. nowQuarterLastDay = time.Date(time.Now().Year(), 9, 30, 23, 59, 59, 0, time.Now().Location())
  718. } else {
  719. //12-31 23:59:59
  720. nowQuarterLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  721. }
  722. return nowQuarterLastDay
  723. }
  724. // GetNowHalfYearFirstDay 获取当前半年的第一天的时间
  725. func GetNowHalfYearFirstDay() time.Time {
  726. month := int(time.Now().Month())
  727. var nowHalfYearLastDay time.Time
  728. if month >= 1 && month <= 6 {
  729. //03-31 23:59:59
  730. nowHalfYearLastDay = time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  731. } else {
  732. //12-31 23:59:59
  733. nowHalfYearLastDay = time.Date(time.Now().Year(), 7, 1, 0, 0, 0, 0, time.Now().Location())
  734. }
  735. return nowHalfYearLastDay
  736. }
  737. // GetNowHalfYearLastDay 获取当前半年的最后一天的时间
  738. func GetNowHalfYearLastDay() time.Time {
  739. month := int(time.Now().Month())
  740. var nowHalfYearLastDay time.Time
  741. if month >= 1 && month <= 6 {
  742. //03-31 23:59:59
  743. nowHalfYearLastDay = time.Date(time.Now().Year(), 6, 30, 23, 59, 59, 0, time.Now().Location())
  744. } else {
  745. //12-31 23:59:59
  746. nowHalfYearLastDay = time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  747. }
  748. return nowHalfYearLastDay
  749. }
  750. // GetNowYearFirstDay 获取当前年的最后一天的时间
  751. func GetNowYearFirstDay() time.Time {
  752. //12-31 23:59:59
  753. nowYearFirstDay := time.Date(time.Now().Year(), 1, 1, 0, 0, 0, 0, time.Now().Location())
  754. return nowYearFirstDay
  755. }
  756. // GetNowYearLastDay 获取当前年的最后一天的时间
  757. func GetNowYearLastDay() time.Time {
  758. //12-31 23:59:59
  759. nowYearLastDay := time.Date(time.Now().Year(), 12, 31, 23, 59, 59, 0, time.Now().Location())
  760. return nowYearLastDay
  761. }
  762. // CalculationDate 计算两个日期之间相差n年m月y天
  763. // FormatPrice 格式化展示金额数字(财务金额展示,小数点前,每三位用,隔开) 1,234,567,898.55
  764. func FormatPrice(price float64) (str string) {
  765. str = decimal.NewFromFloat(price).String()
  766. length := len(str)
  767. if length < 4 {
  768. return str
  769. }
  770. arr := strings.Split(str, ".") //用小数点符号分割字符串,为数组接收
  771. length1 := len(arr[0])
  772. if length1 < 4 {
  773. return str
  774. }
  775. count := (length1 - 1) / 3
  776. for i := 0; i < count; i++ {
  777. arr[0] = arr[0][:length1-(i+1)*3] + "," + arr[0][length1-(i+1)*3:]
  778. }
  779. return strings.Join(arr, ".") //将一系列字符串连接为一个字符串,之间用sep来分隔。
  780. }
  781. // getMonthDay 获取某年某月有多少天
  782. func getMonthDay(year, month int) (days int) {
  783. if month != 2 {
  784. if month == 4 || month == 6 || month == 9 || month == 11 {
  785. days = 30
  786. } else {
  787. days = 31
  788. }
  789. } else {
  790. if ((year%4) == 0 && (year%100) != 0) || (year%400) == 0 {
  791. days = 29
  792. } else {
  793. days = 28
  794. }
  795. }
  796. return
  797. }
  798. func SaveToFile(content, path string) error {
  799. f, err := os.Create(path)
  800. defer f.Close()
  801. if err != nil {
  802. return err
  803. }
  804. f.Write([]byte(content))
  805. return nil
  806. }
  807. // HideString 给字段加***(从字符串中间替换,少于需要替换的长度,那么就补全*的长度)
  808. // src 待*字符串
  809. // hideLen 需要加*的长度
  810. func HideString(src string, hideLen int) string {
  811. if src == "" {
  812. return src
  813. }
  814. str := []rune(src)
  815. if hideLen == 0 {
  816. hideLen = 4
  817. }
  818. hideStr := ""
  819. for i := 0; i < hideLen; i++ {
  820. hideStr += "*"
  821. }
  822. strLen := len(str)
  823. // 字符长度是1
  824. if strLen == 1 {
  825. return string(str[:1]) + hideStr
  826. }
  827. //字符长度大于1,但是小于等于需要隐藏的字符长度,那么就隐藏中间,保留前后各一位字符
  828. if strLen <= hideLen+2 {
  829. return string(str[:1]) + hideStr + string(str[strLen-1:])
  830. }
  831. subLen := strLen - hideLen //剩余需要展示的字符长度
  832. decimal.NewFromFloat(2)
  833. frontLenDecimal := decimal.NewFromInt(int64(subLen)).Div(decimal.NewFromInt(2)) //前面需要展示的字符的长度
  834. frontLen := frontLenDecimal.Floor().IntPart()
  835. return string(str[:frontLen]) + hideStr + string(str[frontLen+int64(hideLen):])
  836. }
  837. // 用户参会时间转换
  838. func GetAttendanceDetailSeconds(secondNum int) string {
  839. var timeStr string
  840. if secondNum <= 60 {
  841. if secondNum < 10 {
  842. timeStr = "0" + strconv.Itoa(secondNum) + "''"
  843. } else {
  844. timeStr = strconv.Itoa(secondNum) + "''"
  845. }
  846. } else {
  847. var remainderStr string
  848. remainderNum := secondNum % 60
  849. minuteNum := secondNum / 60
  850. if remainderNum < 10 {
  851. remainderStr = "0" + strconv.Itoa(remainderNum) + "''"
  852. } else {
  853. remainderStr = strconv.Itoa(remainderNum) + "''"
  854. }
  855. if minuteNum < 10 {
  856. timeStr = "0" + strconv.Itoa(minuteNum) + "'" + remainderStr
  857. } else {
  858. timeStr = strconv.Itoa(minuteNum) + "'" + remainderStr
  859. }
  860. }
  861. return timeStr
  862. }
  863. // SubStr 截取字符串(中文)
  864. func SubStr(str string, subLen int) string {
  865. strRune := []rune(str)
  866. bodyRuneLen := len(strRune)
  867. if bodyRuneLen > subLen {
  868. bodyRuneLen = subLen
  869. }
  870. str = string(strRune[:bodyRuneLen])
  871. return str
  872. }
  873. func GetLocalIP() (ip string, err error) {
  874. addrs, err := net.InterfaceAddrs()
  875. if err != nil {
  876. return
  877. }
  878. for _, addr := range addrs {
  879. ipAddr, ok := addr.(*net.IPNet)
  880. if !ok {
  881. continue
  882. }
  883. if ipAddr.IP.IsLoopback() {
  884. continue
  885. }
  886. if !ipAddr.IP.IsGlobalUnicast() {
  887. continue
  888. }
  889. return ipAddr.IP.String(), nil
  890. }
  891. return
  892. }
  893. func PrintLog(params ...string) {
  894. _, file, line, ok := runtime.Caller(1)
  895. fmt.Println(file, line, ok, params)
  896. }
  897. // InArrayByStr php中的in_array(判断String类型的切片中是否存在该string值)
  898. func InArrayByStr(idStrList []string, searchId string) (has bool) {
  899. for _, id := range idStrList {
  900. if id == searchId {
  901. has = true
  902. return
  903. }
  904. }
  905. return
  906. }
  907. // InArrayByInt php中的in_array(判断Int类型的切片中是否存在该Int值)
  908. func InArrayByInt(idStrList []int, searchId int) (has bool) {
  909. for _, id := range idStrList {
  910. if id == searchId {
  911. has = true
  912. return
  913. }
  914. }
  915. return
  916. }
  917. // GetOrmInReplace 获取orm的in查询替换?的方法
  918. func GetOrmInReplace(num int) string {
  919. /*template := make([]string, num)
  920. for i := 0; i < num; i++ {
  921. template[i] = "?"
  922. }
  923. return strings.Join(template, ",")*/
  924. return "?"
  925. }
  926. // GetTimeSubDay 计算两个时间的自然日期差
  927. func GetTimeSubDay(t1, t2 time.Time) int {
  928. var day int
  929. swap := false
  930. if t1.Unix() > t2.Unix() {
  931. t1, t2 = t2, t1
  932. swap = true
  933. }
  934. t1_ := t1.Add(time.Duration(t2.Sub(t1).Milliseconds()%86400000) * time.Millisecond)
  935. day = int(t2.Sub(t1).Hours() / 24)
  936. // 计算在t1+两个时间的余数之后天数是否有变化
  937. if t1_.Day() != t1.Day() {
  938. day += 1
  939. }
  940. if swap {
  941. day = -day
  942. }
  943. return day
  944. }
  945. // GetFrequencyEndDay 根据当前时间和频度,获取该频度下最后一天的日期
  946. func GetFrequencyEndDay(currDate time.Time, frequency string) (endDate time.Time) {
  947. switch frequency {
  948. case "周度":
  949. // 如果当前就是最后一天,那么就直接返回本日期就好了
  950. if currDate.Weekday() == 0 {
  951. endDate = currDate
  952. } else {
  953. endDate = currDate.AddDate(0, 0, 7-int(currDate.Weekday()))
  954. }
  955. case "旬度":
  956. nextDay := currDate.AddDate(0, 0, 1)
  957. if nextDay.Day() == 1 || currDate.Day() == 10 || currDate.Day() == 20 {
  958. //如果是每月10、20、最后一天,那么就直接返回本日期就好了
  959. endDate = currDate
  960. } else {
  961. if currDate.Day() < 10 { // 每月10号
  962. endDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, time.Local)
  963. } else if currDate.Day() < 20 { // 每月10号
  964. endDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, time.Local)
  965. } else {
  966. // 下旬,多种可能,最大天数可能存在8天,9天,10天,11天,
  967. tmpNextMonth := currDate.AddDate(0, 0, 13)
  968. endDate = time.Date(tmpNextMonth.Year(), tmpNextMonth.Month(), 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  969. }
  970. }
  971. case "月度":
  972. nextDay := currDate.AddDate(0, 0, 1)
  973. if nextDay.Day() == 1 {
  974. //如果是每月的最后一天,那么就直接返回本日期就好了
  975. endDate = currDate
  976. } else {
  977. endDate = time.Date(nextDay.Year(), nextDay.Month()+1, 1, 0, 0, 0, 0, time.Local).AddDate(0, 0, -1)
  978. }
  979. case "季度":
  980. nextDay := currDate.AddDate(0, 0, 1)
  981. if (nextDay.Month() == 1 || nextDay.Month() == 4 || nextDay.Month() == 7 || nextDay.Month() == 10) && nextDay.Day() == 1 {
  982. //如果是每季的最后一天,那么就直接返回本日期就好了
  983. endDate = currDate
  984. } else {
  985. if currDate.Month() < 4 { // 1季度
  986. endDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, time.Local)
  987. } else if currDate.Month() < 7 { // 2季度
  988. endDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, time.Local)
  989. } else if currDate.Month() < 10 { // 3季度
  990. endDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, time.Local)
  991. } else {
  992. // 4季度
  993. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  994. }
  995. }
  996. case "年度":
  997. endDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, time.Local)
  998. default:
  999. endDate = currDate
  1000. return
  1001. }
  1002. return
  1003. }
  1004. // CheckFrequency 获取两个频度之间是否相对低高频
  1005. // 大于0,代表左侧是高频(例:左侧:日度,右侧:周度)
  1006. // 等于0,代表同频
  1007. // 小于0,代表右侧是高频(例:左侧:周度,右侧:日度)
  1008. func CheckFrequency(leftFrequency, rightFrequency string) int {
  1009. frequencyMap := map[string]int{
  1010. "年度": 0,
  1011. "半年度": 1,
  1012. "季度": 2,
  1013. "月度": 3,
  1014. "旬度": 4,
  1015. "周度": 5,
  1016. "日度": 6,
  1017. }
  1018. return frequencyMap[leftFrequency] - frequencyMap[rightFrequency]
  1019. }
  1020. // 将下划线命名转为驼峰命名
  1021. func SnakeToCamel(s string) string {
  1022. var result string
  1023. upper := true
  1024. for _, c := range s {
  1025. if c == '_' {
  1026. upper = true
  1027. continue
  1028. }
  1029. if upper {
  1030. result += string(unicode.ToUpper(c))
  1031. upper = false
  1032. } else {
  1033. result += string(c)
  1034. }
  1035. }
  1036. return result
  1037. }
  1038. // 将驼峰命名转为下划线命名
  1039. func CamelToSnake(s string) string {
  1040. var result string
  1041. for i, c := range s {
  1042. if unicode.IsUpper(c) {
  1043. if i > 0 {
  1044. result += "_"
  1045. }
  1046. result += string(unicode.ToLower(c))
  1047. } else {
  1048. result += string(c)
  1049. }
  1050. }
  1051. return result
  1052. }
  1053. func TimeTransferString(format string, t time.Time) string {
  1054. str := t.Format(format)
  1055. if t.IsZero() {
  1056. return ""
  1057. }
  1058. return str
  1059. }
  1060. // GetEdbRefreshStartDate
  1061. // @Description: 获取开始刷新时间
  1062. // @author: Roc
  1063. // @datetime 2024-02-05 11:23:55
  1064. // @param startDate string
  1065. // @return string
  1066. func GetEdbRefreshStartDate(startDate string) string {
  1067. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1068. if startDate == `` || strings.Contains(startDate, "0000-") {
  1069. return "1990-01-01"
  1070. }
  1071. return startDate
  1072. }
  1073. // GetEdbRefreshEndDate
  1074. // @Description: 获取结束刷新时间
  1075. // @author: Roc
  1076. // @datetime 2024-02-05 11:23:55
  1077. // @param startDate string
  1078. // @return string
  1079. func GetEdbRefreshEndDate(endDate string) string {
  1080. // 没有传入日期,或者日期异常的话,那么就是从1990-01-01开始
  1081. if endDate == `` || strings.Contains(endDate, "0000-") {
  1082. return time.Now().Format(FormatDate)
  1083. }
  1084. return endDate
  1085. }
  1086. func DealExcelDate(date string) (newDate time.Time, err error) {
  1087. /*newDate, err = dateparse.ParseAny(date)
  1088. if err != nil {
  1089. return
  1090. }
  1091. newDate = time.Date(newDate.Year(), newDate.Month(), newDate.Day(), 0, 0, 0, 0, time.Local)*/
  1092. if strings.Contains(date, "/") {
  1093. newDate, err = time.ParseInLocation("2006/01/02", date, time.Local)
  1094. return
  1095. } else {
  1096. newDate, err = time.ParseInLocation(FormatDate, date, time.Local)
  1097. return
  1098. }
  1099. return
  1100. }
  1101. // FloatAlmostEqual 判断两个浮点数是否相等
  1102. func FloatAlmostEqual(a, b float64) bool {
  1103. epsilon := 1e-9 // 容差值
  1104. return math.Abs(a-b) <= epsilon
  1105. }
  1106. // VerifyFrequency
  1107. // @Description: 校验频度是否合规
  1108. // @author: Roc
  1109. // @datetime 2024-04-26 13:30:22
  1110. // @param frequency string 待校验的频度
  1111. // @return bool
  1112. func VerifyFrequency(frequency string) bool {
  1113. return InArrayByStr([]string{"年度", "半年度", "季度", "月度", "旬度", "周度", "日度"}, frequency)
  1114. }
  1115. // DateConvMysqlConvMongo
  1116. // @Description: 将mysql中的日期比较符转换成mongo中的日期比较符
  1117. // @author: Roc
  1118. // @datetime 2024-05-08 11:03:26
  1119. // @param dateCon string
  1120. func DateConvMysqlConvMongo(dateCon string) string {
  1121. cond := ""
  1122. switch dateCon {
  1123. case "=":
  1124. cond = "$eq"
  1125. case "<":
  1126. cond = "$lt"
  1127. case "<=":
  1128. cond = "$lte"
  1129. case ">":
  1130. cond = "$gt"
  1131. case ">=":
  1132. cond = "$gte"
  1133. }
  1134. return cond
  1135. }
  1136. // GenerateEdbCodeMap 当前已经生成的指标编码map(暂时不做定时数据清理了,因为数据不大,我们至少每个月会重启一次,所以暂时不做定时数据清理)
  1137. var GenerateEdbCodeMap = map[string]bool{}
  1138. // GenerateEdbCode
  1139. // @Description: 生成指标编码
  1140. // @author: Roc
  1141. // @datetime 2024-06-05 09:49:53
  1142. // @param num int
  1143. // @param pre string 前缀
  1144. // @return edbCode string
  1145. // @return err error
  1146. func GenerateEdbCode(num int, pre string) (edbCode string, err error) {
  1147. if num >= 10 {
  1148. err = errors.New("指标编码生成失败,请重新生成")
  1149. return
  1150. }
  1151. // 4位随机数
  1152. randStr := GetRandDigit(4)
  1153. // 年月日时分秒+4位随机数
  1154. edbCode = `C` + pre + time.Now().Format(FormatShortDateTimeUnSpace) + randStr
  1155. if _, ok := GenerateEdbCodeMap[edbCode]; ok {
  1156. num++
  1157. edbCode, err = GenerateEdbCode(num, pre)
  1158. }
  1159. GenerateEdbCodeMap[edbCode] = true
  1160. return
  1161. }
  1162. // InsertStr2StrIdx 可分隔的字符串中插入指定字符串, 例如: CO1 Comdty插入V => CO1 V Comdty
  1163. func InsertStr2StrIdx(str, sep string, idx int, value string) string {
  1164. str = strings.TrimSpace(str)
  1165. // 默认以空格作为分隔符
  1166. if sep == "" {
  1167. sep = " "
  1168. }
  1169. slice := strings.Split(str, sep)
  1170. if len(slice) < 2 {
  1171. return str
  1172. }
  1173. // 如果idx不在切片的有效范围内,直接返回原字符串
  1174. if idx < 0 || idx > len(slice) {
  1175. return str
  1176. }
  1177. slice = append(slice[:idx], append([]string{value}, slice[idx:]...)...)
  1178. return strings.Join(slice, sep)
  1179. }
  1180. // FormatFloatPlaces 格式化浮点数位数
  1181. func FormatFloatPlaces(val float64, places int32) (newVal float64, err error) {
  1182. if places <= 0 {
  1183. places = 4
  1184. }
  1185. strNewVal := decimal.NewFromFloat(val).Round(places).String()
  1186. di, e := decimal.NewFromString(strNewVal)
  1187. if e != nil {
  1188. err = fmt.Errorf("NewFromString err: %v", e)
  1189. return
  1190. }
  1191. newVal, _ = di.Float64()
  1192. return
  1193. }
  1194. // handleSystemAppointDateT
  1195. // @Description: 处理系统日期相关的指定频率(所在周/旬/月/季/半年/年的最后/最早一天)
  1196. // @author: Roc
  1197. // @datetime2023-10-27 09:31:35
  1198. // @param Frequency string
  1199. // @param Day string
  1200. // @return date string
  1201. // @return err error
  1202. // @return errMsg string
  1203. func HandleSystemAppointDateT(currDate time.Time, appointDay, frequency string) (date string, err error, errMsg string) {
  1204. //currDate := time.Now()
  1205. switch frequency {
  1206. case "本周":
  1207. day := int(currDate.Weekday())
  1208. if day == 0 { // 周日
  1209. day = 7
  1210. }
  1211. num := 0
  1212. switch appointDay {
  1213. case "周一":
  1214. num = 1
  1215. case "周二":
  1216. num = 2
  1217. case "周三":
  1218. num = 3
  1219. case "周四":
  1220. num = 4
  1221. case "周五":
  1222. num = 5
  1223. case "周六":
  1224. num = 6
  1225. case "周日":
  1226. num = 7
  1227. }
  1228. day = num - day
  1229. date = currDate.AddDate(0, 0, day).Format(FormatDate)
  1230. case "本旬":
  1231. day := currDate.Day()
  1232. var tmpDate time.Time
  1233. switch appointDay {
  1234. case "第一天":
  1235. if day <= 10 {
  1236. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1237. } else if day <= 20 {
  1238. tmpDate = time.Date(currDate.Year(), currDate.Month(), 11, 0, 0, 0, 0, currDate.Location())
  1239. } else {
  1240. tmpDate = time.Date(currDate.Year(), currDate.Month(), 21, 0, 0, 0, 0, currDate.Location())
  1241. }
  1242. case "最后一天":
  1243. if day <= 10 {
  1244. tmpDate = time.Date(currDate.Year(), currDate.Month(), 10, 0, 0, 0, 0, currDate.Location())
  1245. } else if day <= 20 {
  1246. tmpDate = time.Date(currDate.Year(), currDate.Month(), 20, 0, 0, 0, 0, currDate.Location())
  1247. } else {
  1248. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1249. }
  1250. }
  1251. date = tmpDate.Format(FormatDate)
  1252. case "本月":
  1253. var tmpDate time.Time
  1254. switch appointDay {
  1255. case "第一天":
  1256. tmpDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, currDate.Location())
  1257. case "最后一天":
  1258. tmpDate = time.Date(currDate.Year(), currDate.Month()+1, 1, 0, 0, 0, 0, currDate.Location()).AddDate(0, 0, -1)
  1259. }
  1260. date = tmpDate.Format(FormatDate)
  1261. case "本季":
  1262. month := currDate.Month()
  1263. var tmpDate time.Time
  1264. switch appointDay {
  1265. case "第一天":
  1266. if month <= 3 {
  1267. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1268. } else if month <= 6 {
  1269. tmpDate = time.Date(currDate.Year(), 4, 1, 0, 0, 0, 0, currDate.Location())
  1270. } else if month <= 9 {
  1271. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1272. } else {
  1273. tmpDate = time.Date(currDate.Year(), 10, 1, 0, 0, 0, 0, currDate.Location())
  1274. }
  1275. case "最后一天":
  1276. if month <= 3 {
  1277. tmpDate = time.Date(currDate.Year(), 3, 31, 0, 0, 0, 0, currDate.Location())
  1278. } else if month <= 6 {
  1279. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1280. } else if month <= 9 {
  1281. tmpDate = time.Date(currDate.Year(), 9, 30, 0, 0, 0, 0, currDate.Location())
  1282. } else {
  1283. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1284. }
  1285. }
  1286. date = tmpDate.Format(FormatDate)
  1287. case "本半年":
  1288. month := currDate.Month()
  1289. var tmpDate time.Time
  1290. switch appointDay {
  1291. case "第一天":
  1292. if month <= 6 {
  1293. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1294. } else {
  1295. tmpDate = time.Date(currDate.Year(), 7, 1, 0, 0, 0, 0, currDate.Location())
  1296. }
  1297. case "最后一天":
  1298. if month <= 6 {
  1299. tmpDate = time.Date(currDate.Year(), 6, 30, 0, 0, 0, 0, currDate.Location())
  1300. } else {
  1301. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1302. }
  1303. }
  1304. date = tmpDate.Format(FormatDate)
  1305. case "本年":
  1306. var tmpDate time.Time
  1307. switch appointDay {
  1308. case "第一天":
  1309. tmpDate = time.Date(currDate.Year(), 1, 1, 0, 0, 0, 0, currDate.Location())
  1310. case "最后一天":
  1311. tmpDate = time.Date(currDate.Year(), 12, 31, 0, 0, 0, 0, currDate.Location())
  1312. }
  1313. date = tmpDate.Format(FormatDate)
  1314. default:
  1315. errMsg = "错误的日期频度:" + frequency
  1316. err = errors.New(errMsg)
  1317. return
  1318. }
  1319. return
  1320. }
  1321. func CompareFloatByOpStrings(op string, a, b float64) bool {
  1322. switch op {
  1323. case "=":
  1324. return a == b
  1325. case ">":
  1326. return a > b
  1327. case ">=":
  1328. return a >= b
  1329. case "<=":
  1330. return a <= b
  1331. case "<":
  1332. return a < b
  1333. }
  1334. return false
  1335. }
  1336. // IsDivideZero
  1337. // @Description: 判断是否分母为0的bug
  1338. // @author: Roc
  1339. // @datetime 2024-08-23 11:21:25
  1340. // @param err error
  1341. // @return bool
  1342. func IsDivideZero(err error) bool {
  1343. if err == nil {
  1344. return false
  1345. }
  1346. //if strings.Contains(err.Error(), "divide by zero") {
  1347. // return true
  1348. //}
  1349. if strings.Contains(err.Error(), "division by zero") {
  1350. return true
  1351. }
  1352. return false
  1353. }
  1354. // GetTradingDays 获取开始时间至结束时间之间的交易日期(日度)
  1355. func GetTradingDays(startDate, endDate time.Time) []time.Time {
  1356. var tradingDays []time.Time
  1357. for curr := startDate; !curr.After(endDate); curr = curr.AddDate(0, 0, 1) {
  1358. if curr.Weekday() >= time.Monday && curr.Weekday() <= time.Friday {
  1359. tradingDays = append(tradingDays, curr)
  1360. }
  1361. }
  1362. return tradingDays
  1363. }
  1364. // GormDateStrToDateTimeStr
  1365. // @Description: gorm日期字符串格式转正常显示的日期时间字符串
  1366. // @param originalString
  1367. // @return formatStr
  1368. func GormDateStrToDateTimeStr(originalString string) (formatStr string) {
  1369. formatStr = originalString
  1370. if !strings.Contains(originalString, "T") {
  1371. return
  1372. }
  1373. // 解析原始字符串
  1374. t, err := time.Parse(FormatDateWallWithLoc, originalString)
  1375. if err != nil {
  1376. fmt.Println("Error parsing time:", err)
  1377. return
  1378. }
  1379. // 重新格式化时间
  1380. formatStr = t.Format(FormatDateTime)
  1381. return
  1382. }
  1383. // GormDateStrToDateStr
  1384. // @Description: gorm日期字符串格式转正常显示的日期字符串
  1385. // @param originalString
  1386. // @return formatStr
  1387. func GormDateStrToDateStr(originalString string) (formatStr string) {
  1388. formatStr = originalString
  1389. if !strings.Contains(originalString, "T") {
  1390. return
  1391. }
  1392. // 解析原始字符串
  1393. t, err := time.Parse(FormatDateWallWithLoc, originalString)
  1394. if err != nil {
  1395. fmt.Println("Error parsing time:", err)
  1396. return
  1397. }
  1398. // 重新格式化时间
  1399. formatStr = t.Format(FormatDate)
  1400. return
  1401. }